问题描述
- 如何将串口接收到的数据返回给主线程并显示在编辑框中?
-
小弟用的是serialPort,MFC,怎么才能把串口接收到的数据返回给主线程显示在编辑框中?求大侠帮助!void CRBAS_ODBCView::OnButtonSurveyStart() { /**************发数据*********************************/ SerialPort^ _serialPort = gcnew SerialPort("COM3", 38400, Parity(0), 8, StopBits(1));//设置串口信息 _serialPort->DataReceived += gcnew SerialDataReceivedEventHandler(DataReceived); array<unsigned char>^ SendBuf = {0x01,0xF0,0X01,0X08,0XF9,0X0D}; if (!_serialPort->IsOpen) _serialPort->Open(); for (int i = 0;i < SendBuf->Length;i++) { _serialPort->Write(SendBuf, i, 1); Sleep(10); } //Draw(); }
void CRBAS_ODBCView::DataReceived(Object^ sender, SerialDataReceivedEventArgs^ e) { SerialPort^ _serialPort = (SerialPort^)sender; Sleep(100); int count = _serialPort->BytesToRead; array<unsigned char>^ ReadBuf = gcnew array<unsigned char>(count); _serialPort->Read(ReadBuf, 0, count); //解析 double m_dInFlow = (ReadBuf[2] * 65536 + ReadBuf[3] * 256 + ReadBuf[4]) / 1000.000; _serialPort->Close(); }
解决方案
解决方案二:
串口接收到数据之后,解析成功之后就给编辑框赋值就行了,可能需要设计两个线程之间的操作
时间: 2024-12-10 09:47:10