Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. COM reading and string format

COM reading and string format

Scheduled Pinned Locked Moved C / C++ / MFC
databasecomhelpquestionannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tanarnelinistit
    wrote on last edited by
    #1

    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(); }

    E 1 Reply Last reply
    0
    • T tanarnelinistit

      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(); }

      E Offline
      E Offline
      earl
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups