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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Read a single serial byte into COMM-Port

Read a single serial byte into COMM-Port

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++comdata-structuresquestion
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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Pleas Help me. I've already asked the code-gurus, but I've got only very common hints, unnecessary voluminous or so fragmented listing, it didn't help me really. For me as a bloody beginner it is very hard to filter out those parts from the listings, which fits to my following problem: A self developed external electronic circuit is sending measurement-values (from an external measurement process) as a single serial byte - according to the RS 232 rules - appr. 5 times per millisecond. I would urgently need a short but complete read-in function for my VC++ program (which is already principally running) to take over the measurements. My program already performs the opening, initialising and closing of the comm-port properly. I can set and clear DTR and RTS and so on. When my external circuit detects the CTS signal - sent from the PC - it starts its transmission of the byte in queue to the comm port. The circuit then repeats this one single serial byte permanently as long as CTS remains HIGH. BUT I AM NOT ABLE TO READ IN THAT SERIAL BYTE!! So the function I am so desperately looking for should read in the serial byte which is currently transmitted to the comm port. In the COMM connector I have shortcut the pins 4 and 6 as well as 7 and 8. So DTR and RTS signals are instantly mirrored back to the PC. Therefore no timeouts or other controls are necessary to perform. At an average my program is taking access to the COM port every millisecond to read in a new byte. So you see, the function should work a little faster than all the common communication solutions with reading, writing, error-testings time-outs and so on. If anybody could write me down such a minimized function, it would be great. May be a simple assembler routine would perform the task sufficiently too? Many thanks in advance an best wishes to you.

    M 1 Reply Last reply
    0
    • L Lost User

      Pleas Help me. I've already asked the code-gurus, but I've got only very common hints, unnecessary voluminous or so fragmented listing, it didn't help me really. For me as a bloody beginner it is very hard to filter out those parts from the listings, which fits to my following problem: A self developed external electronic circuit is sending measurement-values (from an external measurement process) as a single serial byte - according to the RS 232 rules - appr. 5 times per millisecond. I would urgently need a short but complete read-in function for my VC++ program (which is already principally running) to take over the measurements. My program already performs the opening, initialising and closing of the comm-port properly. I can set and clear DTR and RTS and so on. When my external circuit detects the CTS signal - sent from the PC - it starts its transmission of the byte in queue to the comm port. The circuit then repeats this one single serial byte permanently as long as CTS remains HIGH. BUT I AM NOT ABLE TO READ IN THAT SERIAL BYTE!! So the function I am so desperately looking for should read in the serial byte which is currently transmitted to the comm port. In the COMM connector I have shortcut the pins 4 and 6 as well as 7 and 8. So DTR and RTS signals are instantly mirrored back to the PC. Therefore no timeouts or other controls are necessary to perform. At an average my program is taking access to the COM port every millisecond to read in a new byte. So you see, the function should work a little faster than all the common communication solutions with reading, writing, error-testings time-outs and so on. If anybody could write me down such a minimized function, it would be great. May be a simple assembler routine would perform the task sufficiently too? Many thanks in advance an best wishes to you.

      M Offline
      M Offline
      Mark Jones
      wrote on last edited by
      #2

      Dieter Are you using overlapped or non-overlapping i/o on your com port? If you are using overlapped i/o I do have some code which may help you out. I use overlapped i/o because I need simultaneous i/o on multiple COM ports. This is not possible when using non-overlapped i/o, as the relevant WinAPI fns block if two threads try to access them simultaneously. I have had immense trouble with serial comms myself. Part of the problem I had was getting code that worked both with winNT and win9x - without using conditional compilation. The code below should work with either. Below is a description of how I deal with serial comms. My code runs in a worker thread which is responsible for comms on one COM port. There are as many worker threads as there are serial devices connected to the PC. The serial devices are typically GSM (cellphone) modems. I have two functions: 1. UINT CSerialPort::ReadAByte( BYTE &bAcquiredByte) 2. UINT CSerialPort::WaitForCharacter(DWORD dwTimeout) (BYTE is defined as unsigned char, DWORD is defined as unsigned long) ReadAByte() passes back through its bAcquiredByte parameter the byte read in. The return values that are possible are: WAIT_OBJECT_0: Byte read in successfully WAIT_TIMEOUT: No Byte read in WAIT_FAILED: An error occurred ReadAByte() calls ::ReadFile() only if there is a byte currently sitting in the incoming byte queue for the COM port. The result of this is that ReadAByte returns immediately with or without a newly acquired byte. Now when ReadAByte returns WAIT_TIMEOUT what it is really saying is there are no bytes currently in the COM port in queue. WaitForCharacter() is called with a parameter passed determining timeout in milliseconds. The return values that are possible are: WAIT_OBJECT_0: One Byte has arrived at the COM port within the specified timeout. WAIT_TIMEOUT: No Byte has arrived at the COM Port within the specified timeout. WAIT_FAILED: An error occurred Decide on a timeout to use with WaitForCharacter, say 10ms. To sum up how you would read in a stream of bytes: 1. Call ReadAbyte - If you get WAIT_OBJECT_0, then a byte has been read in successfully. Call ReadAByte again to get the next byte. If you get WAIT_TIMEOUT back call WaitForCharacter(). 2. If WaitForCharacter() returns WAIT_OBJECT_0, then you're clear to call ReadAByte again (this time there will DEFINITELY be a byte waiting for you). 3. Otherwise, if WaitForCharacter() returns WAIT_TIMEOUT, then no bytes have been received on the

      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