COM reading and string format
-
I have the folowing code for a function that reads a string from the COM5 port and then adds it to a table in the database. The value that i should read from COM is smth like "FFFFAAAAAAAAAA000C" but i only get "F". What's wrong? void CMina_sView::OnListenCom5() { CSerial serial; LONG lLastError = ERROR_SUCCESS; DWORD dwBytesRead ; char szBuffer[17]; char * point; // Attempt to open the serial port (COM5) lLastError = serial.Open(_T("COM5"),0,0,false); // Setup the serial port (9600,8N1, which is the default setting) lLastError = serial.Setup(CSerial::EBaud9600,CSerial::EData8,CSerial::EParNone,CSerial::EStop1); // Register only for the receive event lLastError = serial.SetMask(CSerial::EEventBreak | CSerial::EEventCTS | CSerial::EEventDSR | CSerial::EEventError | CSerial::EEventRing | CSerial::EEventRLSD | CSerial::EEventRecv); lLastError = serial.SetupReadTimeouts(CSerial::EReadTimeoutNonblocking); // Wait for an event lLastError = serial.WaitEvent(); // Save event const CSerial::EEvent eEvent = serial.GetEventType(); // Handle ring event // Handle data receive event if (eEvent & CSerial::EEventRecv) { do { // Read data from the COM-port lLastError = serial.Read(szBuffer,sizeof(szBuffer)-1,&dwBytesRead); // Finalize the data, so it is a valid string szBuffer[dwBytesRead] = '\0'; } while (dwBytesRead == sizeof(szBuffer)-1); wchar_t* lpszBuffer = (wchar_t*)calloc(dwBytesRead+1, sizeof(WCHAR)); ::MultiByteToWideChar(CP_ACP, 0, szBuffer, -1, lpszBuffer, dwBytesRead+1); m_pSet->AddNew(); m_pSet->m_Nume="asas"; m_pSet->m_Serie=lpszBuffer; CTime time = CTime::GetCurrentTime(); m_pSet->m_Data=time; UpdateData(TRUE); if (m_pSet->CanUpdate()) m_pSet->Update(); m_pSet->Requery(); UpdateData(FALSE); m_pSet->Close(); m_pSet->Open(); Invalidate(); UpdateWindow(); } serial.Close(); }
-
I have the folowing code for a function that reads a string from the COM5 port and then adds it to a table in the database. The value that i should read from COM is smth like "FFFFAAAAAAAAAA000C" but i only get "F". What's wrong? void CMina_sView::OnListenCom5() { CSerial serial; LONG lLastError = ERROR_SUCCESS; DWORD dwBytesRead ; char szBuffer[17]; char * point; // Attempt to open the serial port (COM5) lLastError = serial.Open(_T("COM5"),0,0,false); // Setup the serial port (9600,8N1, which is the default setting) lLastError = serial.Setup(CSerial::EBaud9600,CSerial::EData8,CSerial::EParNone,CSerial::EStop1); // Register only for the receive event lLastError = serial.SetMask(CSerial::EEventBreak | CSerial::EEventCTS | CSerial::EEventDSR | CSerial::EEventError | CSerial::EEventRing | CSerial::EEventRLSD | CSerial::EEventRecv); lLastError = serial.SetupReadTimeouts(CSerial::EReadTimeoutNonblocking); // Wait for an event lLastError = serial.WaitEvent(); // Save event const CSerial::EEvent eEvent = serial.GetEventType(); // Handle ring event // Handle data receive event if (eEvent & CSerial::EEventRecv) { do { // Read data from the COM-port lLastError = serial.Read(szBuffer,sizeof(szBuffer)-1,&dwBytesRead); // Finalize the data, so it is a valid string szBuffer[dwBytesRead] = '\0'; } while (dwBytesRead == sizeof(szBuffer)-1); wchar_t* lpszBuffer = (wchar_t*)calloc(dwBytesRead+1, sizeof(WCHAR)); ::MultiByteToWideChar(CP_ACP, 0, szBuffer, -1, lpszBuffer, dwBytesRead+1); m_pSet->AddNew(); m_pSet->m_Nume="asas"; m_pSet->m_Serie=lpszBuffer; CTime time = CTime::GetCurrentTime(); m_pSet->m_Data=time; UpdateData(TRUE); if (m_pSet->CanUpdate()) m_pSet->Update(); m_pSet->Requery(); UpdateData(FALSE); m_pSet->Close(); m_pSet->Open(); Invalidate(); UpdateWindow(); } serial.Close(); }
First, if you expect people to read that much code, for the love of god format it. Use the pre tag below this box. Second, you already asked this question. David Crow gave you a good answer. Go read it. Third, you should be trying to figure out if the bug is in your reading code or your string conversion code. Hence David's question. earl