我有一个Matlab函数从串行设备执行串行读取(读取),传感器从每秒发送150个42字节的数据包中获取数据,这意味着它实际上每秒向串行端口写入6300字节。
最近我实现了完全相同的代码,以使用c++
从端口读取日期数据包,它是来自端口处理程序的ReadFile。在matlab中,我使用availableBytes
来表示读取缓冲区中的数据,而在c ++中,我从端口处理程序中读取了cbInQue
,在matlab中,它始终接近零,但是在c ++代码中,我写的越来越大,并且更大。
我应该在这里添加内容,而不是我只读取数据,并且在c ++代码中做过多的工作不会给cpu带来其他负担。
Matlab代码正是这样:
s = serial(COMPort,'BaudRate',115200,'DataBits',8);
s.InputBufferSize = 1000*1000*100;
fopen(s);
fwrite(s,hex2dec('C4'));fwrite(s,hex2dec('C1'));fwrite(s,hex2dec('29'));
fwrite(s,hex2dec('CB'));fread(s,8);%flushinput(s)
i = 0; % Counter
j = 0;
lost = 0;
PacketSize = 42;
clc;disp('Started Sampling');
while i < Samples
j = j + 1;
Header = fread(s,1);
if Header == hex2dec('CB')
pack = fread(s, PacketSize);
cks = convert2int16(pack(41:42)) ;
cksc = sum(pack(1:40)) + hex2dec('CB');
if(cks - cksc == 0)
i = i + 1;
a_x(i,1) = convert2float32bin(pack(1:4));
a_y(i,1) = convert2float32bin(pack(5:8));
a_z(i,1) = convert2float32bin(pack(9:12));
w1(i,1) = convert2float32bin(pack(13:16));
w2(i,1) = convert2float32bin(pack(17:20));
w3(i,1) = convert2float32bin(pack(21:24));
m1(i,1) = convert2float32bin(pack(25:28));
m2(i,1) = convert2float32bin(pack(29:32));
m3(i,1) = convert2float32bin(pack(33:36));
tspan(i,1) = convert2int32(pack(37:40))/19660800;
if(mod(i,Rate) == 0)
if( i < CalibrCount)
disp(['Calibratuing ... [', num2str(100*i/CalibrCount),'%]']);
else
disp(['Time: ', num2str((i - CalibrCount)/Rate),'sec/ [', num2str(100*(i - CalibrCount)/(SampleCount - CalibrCount)) ,'%] AwPacks: ', num2str(floor(s.BytesAvailable/PacketSize))]);
end
end
else
lost = lost + 1;
disp('Packet Lost !!!!!!');
disp(['Lost Count: ', num2str(lost), ' LossRatio:[' , num2str(100*lost/i), '%]']);
end
end
和c ++代码是这样的:
int i{0};
while (true)
{
Sensor->read(&header, sizeof(ReadType));
if (header == Header_CB)
{
int add{0};
GX2->read(packet, sizeof(packet));
Bytes2Float(&packet[3] + 4 * add++, &imuData[i].accX);
Bytes2Float(&packet[3] + 4 * add++, &imuData[i].accY);
Bytes2Float(&packet[3] + 4 * add++, &imuData[i].accZ);
Bytes2Float(&packet[3] + 4 * add++, &imuData[i].gyrX);
Bytes2Float(&packet[3] + 4 * add++, &imuData[i].gyrY);
Bytes2Float(&packet[3] + 4 * add++, &imuData[i].gyrZ);
Bytes2Float(&packet[3] + 4 * add++, &imuData[i].magX);
Bytes2Float(&packet[3] + 4 * add++, &imuData[i].magY);
Bytes2Float(&packet[3] + 4 * add++, &imuData[i].magZ);
// Bytes2Float(packet + 3, accX);
// Bytes2Float(packet + 11, accZ);
printf("accX: %+1.4f \taccY: %+1.4f \taccZ: %+1.4f i:%d buff: %d\n", imuData[i].accX,imuData[i].accY,imuData[i].accZ, i, GX2->Get_cbInQue());
//system("@cls||clear");
i++;
}
在Serialport类的Read Mthod中,是这样的:
int SerialPort::read(void *buffer, unsigned int buf_size)
{
DWORD bytesRead;
unsigned int toRead = 0;
ClearCommError(this->handler, &this->errors, &this->status);
if (this->status.cbInQue > 0)
{
if (this->status.cbInQue > buf_size)
{
toRead = buf_size;
}
else
toRead = this->status.cbInQue;
}
memset(buffer, 0, buf_size);
if (ReadFile(this->handler, buffer, toRead, &bytesRead, NULL))
return bytesRead;
return 0;
}
仅需提及:我从ByeToFloat
代码中删除了所有c++
调用,但仍然遇到缓冲区越来越大的问题。
编译:
g++ -g main.cpp include/SerialPort.cpp include/Sensor.cpp -o bin/main.exe -Iinclude