如何解决如何使用C#读取多个BLE包并写入不同的CSV文件
我已经写了一个 C# 程序来搜索和读取多个 BLE 包。
但是我发现如果连接两个以上的BLE特性,写入的CSV文件的采样率会下降。
例如,原始采样率为 50Hz,但如果我连接到两个或更多 BLE 设备,采样率可能会降低到 16Hz。
下面是我的参考代码。 https://www.programmersought.com/article/51155122138/
我修改的是添加多个BLE搜索和连接。
Body[bodypart] 是参考代码中的页面 bleCore。
public void CreateNewConnect(int bodypart)
{
Body[bodypart] = new IMU(this);
Body[bodypart].MessAgeChanged += BleCore_MessAgeChanged;
Body[bodypart].DeviceWatcherChanged += BleCore_DeviceWatcherChanged;
Body[bodypart].GattDeviceServiceAdded += BleCore_GattDeviceServiceAdded;
Body[bodypart].CharacteristicAdded += BleCore_CharacteristicAdded;
}
函数 SetOpteron 修改如下
public void SetOpteron(GattCharacteristic gattCharacteristic)
{
GATT = gattCharacteristic;
receiving = true;
ParameterizedThreadStart myPar = new ParameterizedThreadStart(DoReceive);
RxThread = new Thread(myPar);
RxThread.IsBackground = true;
RxThread.Start(0);
}
private void DoReceive(object o)
{
if (GATT.CharacteristicProperties == (GattCharacteristicProperties.Notify | GattCharacteristicProperties.Read | GattCharacteristicProperties.Write | GattCharacteristicProperties.Broadcast))
{
CurrentBroadcastCharacteristic = GATT;
CurrentWriteCharacteristic = GATT;
CurrentNotifyCharacteristic = GATT;
CurrentNotifyCharacteristic.ProtectionLevel = GattProtectionLevel.Plain;
EnableNotifications(CurrentNotifyCharacteristic);
}
if (GATT.CharacteristicProperties == GattCharacteristicProperties.Notify)
{
CurrentNotifyCharacteristic = GATT;
}
if ((uint)GATT.CharacteristicProperties == 26)
{ }
if (GATT.CharacteristicProperties == (GattCharacteristicProperties.Notify | GattCharacteristicProperties.Read | GattCharacteristicProperties.Broadcast))
{
CurrentBroadcastCharacteristic = GATT;
CurrentNotifyCharacteristic = GATT;
CurrentNotifyCharacteristic.ProtectionLevel = GattProtectionLevel.Plain;
CurrentNotifyCharacteristic.ValueChanged += Characteristic_ValueChanged;
EnableNotifications(CurrentNotifyCharacteristic);
}
ConnectDevice();
}
我处理 BLE 数据的内容如下
ReceiveIMUEndSync = new Semaphore(0,1);
ReceiveIMUProcess = new BackgroundWorker();
ReceiveIMUProcess.WorkerReportsProgress = true;
ReceiveIMUProcess.WorkerSupportsCancellation = true;
ReceiveIMUProcess.DoWork += new DoWorkEventHandler(IMUParse);
private void IMUParse(object sender,DoWorkEventArgs e)
{
BackgroundWorker ReceiveWorker = sender as BackgroundWorker;
int n = (int)e.Argument;
while (receiving)
{
if (ReceiveWorker.CancellationPending == true)
{
e.Cancel = true;
ReceiveIMUEndSync.Release();
break;
}
else
{
if (RxIMUDataParse())
{
IMU_StateMachine();
ReceiveWorker.ReportProgress(0);
PackageAmount++;
Thread.Sleep(1);
}
}
}
}
IMU_StateMachine 是一个读写 BLE 数据到 CSV 文件的函数
private void IMU_StateMachine()
{
switch (state)
{ ...
...
...
case (int)State.READ_DATA:
{
if (changeStateKey)
{
processed = 0;
MessAgeChanged(MsgType.NotifyTxt,"Start READ DATA");
changeStateKey = false;
}
SetData(AccRaw,GyroRaw,MagRaw,TimeRaw);
processed++;
if (MainForm.recordDataKey)
{
WriteData();
}
break;
}
...
...
...
}
}
如何修改我的代码,谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。