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. Serial communication with Win32 API

Serial communication with Win32 API

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionalgorithmsjson
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.
  • R Offline
    R Offline
    Rico Salfer
    wrote on last edited by
    #1

    Hello, I'm a software developer and have following problem with Win32 API and serial communication : In my protocol is a read thread to receive bytes. The function ReadFile() reads one Byte from serial interface (COM1 or COM2). I use an overlapped structure to receive data. If there is an error the function WaitForSingleObject() is called. Usually the receive is correct. Sometimes the function ReadFile() don't read a Byte although the InQueue has a byte. The function WaitForSingleObject() is called. This function returns with WAIT_TIMEOUT, the current Byte is lost. If I call in this case PurgeComm(), the lost Byte is in the buffer which was in ReadFile() the input buffer. But this algorithm don't works correct every time. How can I read the Byte in the InQueue if this byte can't read from a Win32 API function ? How can I avoid this problem ? Good buy Rico Salfer

    V 1 Reply Last reply
    0
    • R Rico Salfer

      Hello, I'm a software developer and have following problem with Win32 API and serial communication : In my protocol is a read thread to receive bytes. The function ReadFile() reads one Byte from serial interface (COM1 or COM2). I use an overlapped structure to receive data. If there is an error the function WaitForSingleObject() is called. Usually the receive is correct. Sometimes the function ReadFile() don't read a Byte although the InQueue has a byte. The function WaitForSingleObject() is called. This function returns with WAIT_TIMEOUT, the current Byte is lost. If I call in this case PurgeComm(), the lost Byte is in the buffer which was in ReadFile() the input buffer. But this algorithm don't works correct every time. How can I read the Byte in the InQueue if this byte can't read from a Win32 API function ? How can I avoid this problem ? Good buy Rico Salfer

      V Offline
      V Offline
      Vernon Sauder
      wrote on last edited by
      #2

      I also use the ReadFile for serial comm and I found that the problem I was having was that I was using the same overlapped object for both Put and Get. If I did a Put while a Get was waiting for data, the stuff would get trashed. I made sure that only one was doing overlapped operation and the other was just doing a WaitCommEvent. I also found that the number of bytes read that ReadFile returns was not correct. I just figure out the length on my own with a strlen(). Vern ================== The original message was: Hello,

      I'm a software developer and have following problem
      with Win32 API and serial communication :

      In my protocol is a read thread to receive bytes.
      The function ReadFile() reads one Byte from serial
      interface (COM1 or COM2). I use an overlapped
      structure to receive data. If there is an error
      the function WaitForSingleObject() is called. Usually
      the receive is correct. Sometimes the function
      ReadFile() don't read a Byte although the InQueue
      has a byte. The function WaitForSingleObject() is called.
      This function returns with WAIT_TIMEOUT, the current
      Byte is lost.
      If I call in this case PurgeComm(), the lost Byte is in
      the buffer which was in ReadFile() the input buffer.
      But this algorithm don't works correct every time.

      How can I read the Byte in the InQueue if this byte
      can't read from a Win32 API function ?
      How can I avoid this problem ?

      Good buy
      Rico Salfer

      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