void onReadEvent(const char *portName, unsigned int readBufferLen) { if (readBufferLen > 0) { char *data = new char[readBufferLen + 1]; // '\0' if (data) { // read int recLen = p_sp->readData(data, readBufferLen); if (recLen > 0) { data[recLen] = '\0'; std::cout...
int recLen = p_sp->readData(data, readBufferLen); if (recLen > 0) { data[recLen] = '\0'; std::cout << portName << ", Length: " << recLen << ", Str: " << data << std::endl; } delete[] data; data = NULL; } } }; private: CSerialPort *p_sp; }; int main() {...
// add by itas109 void CCommMFCDlg::onReadEvent(const char *portName, unsigned int readBufferLen) { if(readBufferLen > 0) { char data[1024]; int recLen = m_serialPort.readData(data,readBufferLen > 1023 ? 1023 : readBufferLen); if (recLen > 0) { data[recLen] = '\0'; CStr...
{rxData_.resize(msgLen_);while(readBufferLen){uint8_t*data=newuint8_t[1];intrecLen=csp_->readData(data,1);readBufferLen--;if((*data==0xAA)){mlen_=0;}rxData_[mlen_]=*data;mlen_++;delete[]data;data=NULL;}if(mlen_>=msgLen_){// for (auto s : rxData_)// {// LOGF(INFO...
2.2.5 向串口写入数据函数 writeData 用于向串口写入数据。成功时返回写入字节数,失败时返回-1。失败时可调用itas109::CSerialPort::getLastError()获取错误码。2.2.6 从串口读取指定长度数据函数 readData 用于从串口读取指定长度的数据。正常时返回读取字节数,失败时返回-1。失败时可调用itas109::...
m_SerialPort.Read((BYTE*)data + dwlen, dwReceived, osRead,&dwReaded); } catch(CSerialException* pEx) { //等待读动作完毕 if(pEx->m_dwError == ERROR_IO_PENDING) { m_SerialPort.GetOverlappedResult(osRead, dwReaded, TRUE); //WaitForSingleObject(overlapped.hEvent,INFINITE); ...
baud,parity,databits,stopbits); 250. 251. 252. //getahandletotheport253. 254. m_hComm=CreateFile(szPort,//communicationportstring(COMX)255. 256. GENERIC_READ|GENERIC_WRITE,//read/writetypes257. 258. 0,//commdevicesmustbeopenedwithexclusiveaccess259. 260. NULL,//nosecurityattributes261....
除了利用Read()方法来读数据,还可以使用ReadExisting()方法来读取数据。该方法读取当前所能读到的数据,以字符串的形式返回。 7 事件 SerialPort 提供了DataReceived事件。当有数据进入时,该事件被触发。该事件的触发由操作系统决定,当有数据到达时,该事件在辅助线程中被触发。辅助线程的优先级比较低,因此并不能确保...
= NULL) { CloseHandle(m_hComm); m_hComm = NULL; } sprintf(szPort, "COM%d", portnr); sprintf(szBaud, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopbits); m_hComm = CreateFile(szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_...
Removed the CSerialPort::DataWaiting method as it depends on the port being open in overlapped mode. Instead client code can simply call CSerialPort::WaitEvent directly themselves. Removing this method also means that the CSerialPort::m_hEvent handle has not also been removed. The CSerialPort::...