Thread: c code for transmitting and recieving data from serial port

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    11

    c code for transmitting and recieving data from serial port

    guys please help me out...
    can anyone provide me with a code in C which can transmit and receive data over a RS232 link.
    i was using _bios_serialcom but it refuses to send data and the timeout bit is set to 1
    Plz help me out.
    Make it fast if possible...
    Thnx

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you'll check out code depot sites like Sourceforge.net, planetcode.com, etc., you'll find little projects involving this.

    Much faster than just asking for help, here. We don't really do "help" very well at all. We do "I need help with this syntax or logic, or compiler problem." Something directly related to a specific problem with code that YOU provide and where YOU tell us what YOUR problem with YOUR code, is.

    You know you'll need a X 232 cable for direct cable communication to/from a two serial ports, right? A straight through cable will not be what you want.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    11
    i am using this code given on MS's site
    support.microsoft.com/support/kb/articles/Q39/5/01.asp
    when i try to initialize the port, it sends me the status as 0x6000 and does not transmit data as the time out bit is set and the send status is somewhat like 0x80...
    See if this code works on your machine or if it needs any changes
    i am using Win 2000 and VC++ 1.5 compiler

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Although a wee verbose, this page probably has what you're looking for.
    This is how I've always done it/seen it done: On Windows, use CreateFile() with the filename set to "COM1", "COM2", etc. Use GetCommState() to get the info on the port, change anything that you need to change, and then SetCommState() to apply those changes. From there, ReadFile/WriteFile to do the I/O.

    What exactly are you trying to talk with? Do you know the protocol involved?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > VC++ 1.5 compiler
    Isn't that one of those crusty old 16-bit DOS compilers from sometime in pre-history?

    Which is odd, if you're trying to run it on top of a pure 32-bit OS like win 2000.

    Very old compilers on top of very new operating systems usually "get by" when doing something simple like printing stuff to the screen. But try something tricky like direct hardware access, and you're at the mercy of the protection systems of the host OS, and the faithfulness of the DOS emulator in which your program runs.

    In short, get a proper win32 compiler for your win32 OS.
    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.

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    11
    i am using it for a dos based application....and i am using the example given on
    http://poli.cs.vsb.cz/c/help/bios.htm#LBL10
    there is no compile time error but i am not able to send data due to the problem mentioned earlier
    plz have a look if there is something to be added/deleted in the code.
    i need to operate it at a baud rate of 9600.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps you will find enlightenment here
    http://www.lvr.com/serport.htm
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function recieving data from other function...
    By hubris in forum C++ Programming
    Replies: 2
    Last Post: 05-21-2009, 06:44 AM