所以我有基于覆盆子Pi(Modberry)的嵌入式设备和4个传感器通过一个USB集线器连接。这些传感器一直通过串行端口发送数据,我必须读取和分析这些数据。数据按块划分:每个块7个字节,它们之间有空间。前4个字节是时间,3个字节是传感器测量的值。正常值应该是0+-5左右。波特率是921600。当我只读取一个传感器时,例如“/dev/ttyUSB0”,一切都很好,但是当我开始增加可读传感器的数量时,事情就完全错了。代表时间的4个字节总是以正确的方式传递,并且从不出错,但最后3个字节的时间值为99%,并开始像这样的垃圾邮件:
[2019-04-22 17:48:02.264] Device: /dev/ttyUSB3, Time: 12226408, Value: 1690
[2019-04-22 17:48:02.265] Device: /dev/ttyUSB2, Time: 12217312, Value: 1690
[2019-04-22 17:48:02.265] Device: /dev/ttyUSB2, Time: 12217316, Value: 1690
有趣的是,在重新启动应用程序后,这种情况不会100%发生,而是大约80%。
我还用Python和Java编写了一个应用程序,它没有这个问题。它们都在同一台设备上运行。我试着在不同的线程中运行读取每个传感器,但也没有帮助。
我简化了很多代码,并删除了这一块中的所有错误检查。
void openSerial()
{
const int fileDescriptor = ::open(mParams.path.c_str(), O_RDONLY | O_NOCTTY);
termios SerialPortSettings;
SerialPortSettings.c_cflag &= ~PARENB;
SerialPortSettings.c_cflag &= ~CSTOPB;
SerialPortSettings.c_cflag &= ~CSIZE;
SerialPortSettings.c_cflag |= CS8;
SerialPortSettings.c_cflag &= ~CRTSCTS;
SerialPortSettings.c_cflag |= CREAD | CLOCAL;
SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY);
SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
SerialPortSettings.c_oflag &= ~OPOST;
SerialPortSettings.c_cc[VMIN] = 10;
SerialPortSettings.c_cc[VTIME] = 0;
cfsetispeed(&SerialPortSettings, 921600);
tcsetattr(fileDescriptor, TCSANOW, &SerialPortSettings);
tcflush(fileDescriptor, TCIOFLUSH);
}
static const int BUFFER_SIZE = 256;
void readSerial()
{
char buffer[BUFFER_SIZE + 1] = { 0 };
int count = 0;
count = read(mfd, buffer, BUFFER_SIZE);
std::vector<char> rawBuffer;
for (int i = 0; i < count; i++) {
rawBuffer.push_back(buffer[i]);
}
parse(rawBuffer);
}
void parse(std::vector<char> rawBuffer)
{
auto currentSpace = std::find(rawBuffer.begin(), rawBuffer.end(), ' ');
auto nextSpace = std::find(currentSpace + 1, rawBuffer.end(), ' ');
size_t counter = 0;
while (currentSpace != rawBuffer.end()) {
const int dist = std::distance(currentSpace, nextSpace);
if (dist == 8) {
XD1000Data data;
data.time = parseTime(&(*(currentSpace + TIME_SHIFT)));
data.value = parseValue(&(*(currentSpace + VALUE_SHIFT)));
} else {
printf("packet size error, dist %d, rawBuffer.size %d, counter %d", dist, rawBuffer.size(), counter);
}
counter++;
currentSpace = nextSpace;
nextSpace = std::find(currentSpace + 1, rawBuffer.end(), ' ');
}
rawBuffer.clear();
}
long parseTime(char *buffer)
{
long dest[4];
long time;
parseBuffer<long, 4>(buffer, dest);
time = dest[3] + (dest[2] << 6) + (dest[1] << 12) + (dest[0] << 18);
return time;
}
int parseValue(char *buffer)
{
int dest[3];
int value;
parseBuffer<int, 3>(buffer, dest);
value = dest[2] + (dest[1] << 6) + (dest[0] << 12);
return (short)((value & 0x1000) ? (value | 0xf000) : (value & 0x0fff));
}
template <typename T, size_t size> void parseBuffer(char *buffer, T *dest)
{
for (size_t i = 0; i < size; i++) {
if (isupper(buffer[i]))
dest[i] = buffer[i] - 'A';
else if (islower(buffer[i]))
dest[i] = buffer[i] - 'a' + 26;
else if (isdigit(buffer[i]))
dest[i] = buffer[i] - '0' + 52;
else if (buffer[i] == '+')
dest[i] = 62;
else if (buffer[i] == '/')
dest[i] = 63;
else // error
;
}
}
我百分之百确定传感器没有发送这1690数据,这种行为在任何其他应用程序中都没有观察到,任何数量的传感器,甚至Minicom。这就是它的实际工作方式,而且它的作用非常罕见:
Device:/dev/ttyUSB3 14006188 -5
Device:/dev/ttyUSB3 14006192 -6
Device:/dev/ttyUSB3 14006196 -5
Device:/dev/ttyUSB3 14006200 -6
Device:/dev/ttyUSB3 14006204 -5
Device:/dev/ttyUSB3 14006208 -6
Device:/dev/ttyUSB3 14006212 -5
Device:/dev/ttyUSB0 14006152 -1
Device:/dev/ttyUSB0 14006156 -2
Device:/dev/ttyUSB0 14006160 -1
Device:/dev/ttyUSB0 14006164 0
最佳答案
这个问题是以错误的方式打开串行端口。所以我从这个问题中提取了开放代码,现在一切正常:
How to open, read, and write from serial port in C?
谢谢你们,@JD}u研磨机和@sawdust给了我正确的方向!
关于c++ - 使用C++从Linux串行端口读取会产生困惑的数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55797700/