在linux下编写串口通讯程序,采用select监听串口的可读事件,一旦可读,调用read。但是我们会发现,read一次得到的数据通常不是完整的一个数据帧。
比如完整数据帧为
但是实际上需要read多次才能完全读到。
程序实际运行情况:
两次读完:
四次读完:
为了解决不能接收完整数据帧的问题,借鉴了网友的例子,并进行了一些改动:
现在的效果:
下面是程序代码:
#include "smartlight.h"
int portfd = -1;
void print_frame(const char *desc,uint8_t *buf,int size);
void getCompleteFrame(uint8_t *inBuf,int inCnt,uint8_t *outBuf,int *destCnt,int *readStatus);
void *monitor_serial_readable(void *arg);
int</