Thread: Serial Communication [RS232]

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    Unhappy Serial Communication [RS232]

    Hi everyone!

    I need to create a program to receive and send some data using RS232.
    Program should receive 16 bytes of data, sore it and than I'll write switch case, so if some specific data is received, program will go to specific label.

    In C I know a lot of basic things, but I'm asking you to help me with this.

    The program is supposed to run in Windows XP. The most helpful thing would be if someone could post some example code (with comments), where the data is being received [16bytes] and sored.

    Anyone, who can help me is welcome!

    Johnny

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    Quote Originally Posted by johnny_hush View Post
    Hi everyone!

    I need to create a program to receive and send some data using RS232.
    Program should receive 16 bytes of data, sore it and than I'll write switch case, so if some specific data is received, program will go to specific label.

    In C I know a lot of basic things, but I'm asking you to help me with this.

    The program is supposed to run in Windows XP. The most helpful thing would be if someone could post some example code (with comments), where the data is being received [16bytes] and sored.

    Anyone, who can help me is welcome!

    Johnny
    As I remember, it is not easy to touch to low level of hardware in XP with pure C.
    It is protected.
    With pure C you should work on DOS or Win98, it's better.
    In XP, you can use VC++ which has a class for COM object, you simply config port as you need and use it very easily. Details you can find in MSDN.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I think you can just open up COM1 with fopen or something.

    like fopen("COM1", "rb"); or something.
    Last edited by robwhit; 01-15-2008 at 08:02 PM.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Use CreateFile() to open the com port (check returns for error)
    sprintf(temp,"\\\\.\\COM%d",iComPort);//name of port to open in CreateFile()

    Get the port settings with GetCommState()
    Modify and set with SetCommState()

    Get the timeouts with GetCommTimeouts() [controls how long to wait for data]
    Modify and set with SetCommTimeouts()

    Read the data with ReadFile(). Check both the return and compare the amount of data requested with the amount actually read.

    Store in buffer.....
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Do you perhaps have some little tutorial or document?

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Thanks invinciblevn!

    Can I write program for Win98 and turn on Compatibility Mode in program properties (run in Windows 98 mode)?

    Will this solve problem?

    And how to 'communicate' even in win98, give some code, explanations, please...

    johnny

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    Quote Originally Posted by johnny_hush View Post
    Thanks invinciblevn!

    Can I write program for Win98 and turn on Compatibility Mode in program properties (run in Windows 98 mode)?

    Will this solve problem?

    And how to 'communicate' even in win98, give some code, explanations, please...

    johnny
    You have to use interrupt 14 for interfacing.
    I worked with this quite long time along, so that i can not remember detail. Sorry.
    If you can use Visual C++ or Visual Basic, it's quite easy.
    They support every thing, if you can please use them. And I can send you some example codes.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can't write com code without knowing all the details you left out:

    The baud rate, protocol, etc., of the external device!

    Your external device should have some resources for getting this done - usually they include their own com program you can tailor to fit your needs.

    Lacking that, I'd check out the open source code/programming projects forums, and see what you can find that might be suitable. Don't forget the newsgroups archives, as well, and GIYF, for sure. There is a wealth of info on RS232 also, on the net.

    What will work in your com program, depends on what your device has in hardware and it's own software.

  9. #9

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Ummmm....no.

    I code this type of thing a lot and so know it fairly well.

    Have a try, then post your code (and what goes wrong or you can not work out) and I am sure someone will help fix it.

    A lot of it depends on the implementation (ie timeouts, port settings change depending on the device you are communicating with etc).

    Good Luck!
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  11. #11

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Threads merged.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    First of all, I doubt there's a way around the WinXP protection, second, why would you want to make things awkward for yourself, when there is a nice, portable, flexible and complete interface in XP? [That works with Win98 too, should you wish to "go backwards" - which certainly isn't true for the other way around].

    Reading/writing the serial port directly is far from trivial, and if you don't have access to the interrupt handler [which you absolutely haven't] you'll not be able to figure out when data is available on the serial port, or when you can send the next byte out. So you'd end up polling the port, which means that you'd spend ALL of the system CPU time on polling, which isn't the right thing to do.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplex communication thro serial port
    By Priyachu in forum Networking/Device Communication
    Replies: 1
    Last Post: 05-30-2009, 04:24 AM
  2. Serial port Communication
    By vin_pll in forum C++ Programming
    Replies: 23
    Last Post: 01-07-2009, 09:32 AM
  3. Serial pot communication through c program
    By vin_pll in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2009, 12:52 PM
  4. Please help with serial communication problem - Long
    By spdylude in forum Windows Programming
    Replies: 3
    Last Post: 04-06-2005, 09:41 AM
  5. Serial communication packets
    By Roaring_Tiger in forum C Programming
    Replies: 3
    Last Post: 04-26-2003, 08:33 AM