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. Problem with Serial Port Transmission

Problem with Serial Port Transmission

Scheduled Pinned Locked Moved C / C++ / MFC
beta-testingquestiontestingjsonhelp
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.
  • B Offline
    B Offline
    BeakX
    wrote on last edited by
    #1

    Hello, I am using the bit of code below in a function to send instructions thru the serial port to some equipment and also to read off some feedback from the equipment: WriteFile(hCOM1, txbuff, 8, &bytesRead, NULL); ReadFile(hCOM1, rxbuff, 8, &bytesRead, NULL); Power= (((int)rxbuff[6])%4)*256 + (int)rxbuff[5]; When i am testing this function alone, it seem to work perfectly and obtain nice readings. However, when i integrate it with the rest of the program i find that the function gives wierd readings.I later found that this was due to the ReadFile portion reading the recieved portion at an offset, meaning there was some meaningless data before my actual portion of the feedback. eg I expect 255 255 43 ..... instead i get 05 04 255 255 43... I managed to solve this problem by making use of the PURGE command. However, i would like to know, why does the offset of data occurr ? Is there any better way to solve this problem instead of PURGE ? Thanks

    R 1 Reply Last reply
    0
    • B BeakX

      Hello, I am using the bit of code below in a function to send instructions thru the serial port to some equipment and also to read off some feedback from the equipment: WriteFile(hCOM1, txbuff, 8, &bytesRead, NULL); ReadFile(hCOM1, rxbuff, 8, &bytesRead, NULL); Power= (((int)rxbuff[6])%4)*256 + (int)rxbuff[5]; When i am testing this function alone, it seem to work perfectly and obtain nice readings. However, when i integrate it with the rest of the program i find that the function gives wierd readings.I later found that this was due to the ReadFile portion reading the recieved portion at an offset, meaning there was some meaningless data before my actual portion of the feedback. eg I expect 255 255 43 ..... instead i get 05 04 255 255 43... I managed to solve this problem by making use of the PURGE command. However, i would like to know, why does the offset of data occurr ? Is there any better way to solve this problem instead of PURGE ? Thanks

      R Offline
      R Offline
      Ryan Binns
      wrote on last edited by
      #2

      The serial port has an input buffer of usually around 16 characters. Anything that is received by the serial port but not read by an application will be just sitting there waiting to be read. A good practice is to clear the input buffer (just read until you can't read any more) when you open the serial port, and then make sure you keep up with all the reads after that.

      Ryan

      "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

      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