首页 > 技术知识 > 正文

在平时使用中,串口的使用率是比较高的,数据交互也是比较多,此次以485串口的一段代码为例:

bool PortTwo::start() { CSerialPort::UartAtrr attr; // need read form config file. attr.dev = “/dev/ttymxc2”; attr.dataBits = 8; attr.baudRate = 9600; attr.parity = N; attr.stopBits = 1; attr.flowControl = FLOW_CONTROL_NONE; bool result = serialPort->open(attr); if(result) { isRunning = true; thread = new std::thread(std::bind(&PortTwo::porttwouartTotalCallWorker, this)); } return result; } void PortTwo::stop() { isRunning = false; if(thread) { thread->join(); delete thread; } serialPort->close(); }
<

此处写了初始化的设置和暂停的部分,在开启之后,通过使用线程,对数据进行获取,以及解析,解析由于每个人的协议不一样,就不多加阐述了。

猜你喜欢