Menu

Some hints on Serial Port programming

2003-05-12
2012-09-26
  • Nobody/Anonymous

    There would be somebody here like me using C++ to communicate with the device, using serial port. While unlike VC++, as a freeware, DevC++ does not provide an ActiveX control to realize the job. So, it is necessary to write everything yourself, using the fundamental Windows32 API functions. Sorry but I am also a beginner in this manner. (Beginning using DevC++ to work on serial port. I have used LabView to build a similar program and that’s fairly easy). I hope to share some of my “superficial” experience also to those beginners.
    First, I would suggest a very good web that provides several methods to help facilitate serial port programming: http://www.codeproject.com/system/serial.asp Many thanks to the author Mr.Ramon de Klein.
    I am not very familiar with “thread” and used the simplest class as CSerail.
    To use these classes is not a very difficult thing. In fact, a very tricky phase during the programming is to define a protocol between your host program and the device. I am using the Zilog Encore evaluation board, the micro controller “talk” with DS1722 temperature sensor using SPI bus and send the temperature to the host by RS232.
    How the host computer has to “read” the temperature value sent by the microcontroller. But how does it know when the data arrives? Win32 API doesn't provide a mechanism for serial communication. In the example of Mr.Ramom de Klein there is a class called “CSerialWnd” for use in this case while I think it is also possible to use only the basic CSerial by performing software polling.
    I define the frequency of the microcontroller’s sending data to host as 500 ms. That is, it sends 2 byte of data every 500ms, one(higher 8 bits) on the top of another(lower 8 bits). In host, I set the value for the Windows timer to be 250ms (or some under 500ms). When the timer message occurs, it performs the following (I can not use flow chat here so text instead):

    1.    Check if there is(are) byte(s) in the receiving buffer of host computer. It can be done using read API (read one character, not the string).
    2.    If no byte(s), return directly; else system wait for (1+8+1)*(1/Baudrate) s (to wait for next byte’8 bit, 1 stop bit, no parity’ in buffer) and do read API (also character) again, then return. Now we successfully receive the 2 bytes and ready to use.

    The above is an idea of using polling method in device talking by RS232. Hopefully this will do help to people like me.

    Qu Chen

     
    • Yeoh HS

      Yeoh HS - 2003-05-12

      Hi Qu Chen,

      Thank you very much for this information.

      Best Regards,
      Yeoh
      --
      http://www.yeohhs.com

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.