May i know how to do serial communication? STEP BY STEP

Discussion in 'Programmer's Corner' started by Xeeshan Qureshi, Jan 17, 2009.

  1. Xeeshan Qureshi

    Thread Starter Member

    Jan 13, 2009
    Could any1 plz help me regarding serial communication on btween two PCs using visual c++?
    the things i need is the code and the visual discription (of how to make connections btw the two PCs)
    plz help me
  2. beenthere

    Retired Moderator

    Apr 20, 2004
  3. davebee

    Well-Known Member

    Oct 22, 2008
    I can't help with C++ code, but I have some Delphi (Pascal) code that sets up comports using Windows system calls, in case it might help.

    I wrote it a long time ago for Delphi 2, but have also run it in Delphi 7 successfully.

    I think the Windows system function calls made by Delphi would be called pretty much the same way by C++, with the same names and arguments, so this might provide enough clues to help you get your own version working.

    There is nothing fancy to this code; it just makes a call to request a port control structure, assigns values to a number of parameters, then lets the user-level code make data reads and writes. The Windows structure includes a data buffer which will buffer some data, but it is up to your calling program to periodically request data from this system buffer and copy the results to your user-level destination. This also includes functions to set or clear other RS232 control bits, like CTS, RTS, etc.

    This has worked fine in many of my projects.

    For the connection between the two PCs, the easiest way is to get a standard serial cable and a null-modem module. You may also need a gender-changer module. I don't know where you are located, but in the U.S., Radio Shack sells both items. Be sure to get cabling that matches the ports on your PCs. Older PCs used 25 pin serial ports; newer ones use 9-pins, and the newest PCs don't even provide serial ports. So if you don't know what you have, be sure to check.

    You'll also need to find the port number that Windows assigns to each of your physical connectors, like COM1, COM2.

    It's not an easy task to get two separate sessions communicating! There are so many conditions to test - states of each side of the communication, timeout parameters, etc., and once connected, sent character strings may be sliced apart and received in separate chunks which your code must reassemble.

    But it's great when it's finally working! So good luck.

  4. Skeebopstop

    Active Member

    Jan 9, 2009
    Visual C# mate. You can drop in serial ports like objects and just start using them.

    C# is bloody lovely, just becomes difficult when you start to exploit all of its' features, something we electronics guys leave up to the software junkies.

    I used to do my serial comm's directly interfacing into windows drivers from C and C# just makes that job so much easier.
  5. leftyretro

    Active Member

    Nov 25, 2008
    First step is to establish the hardware link between the two PCs. For that you can use Windows Hyperterminal because that should be already included with most windows installations.

    Now build a serial communications cable. You will need two female DB9 connectors. You should need only 3 wires connected between the two connectors. Wire pin 5 to pin 5, pin 3 to pin 2 and pin 2 to pin 3 between the two connectors.

    Now run Hyperterminal on both PCs. Be sure to select the same baud rate, data bits, parity and stop bit options on both PCs and make sure each has selected the proper comm port for their perspective system. Now when you type on one PC you should see the characters on the other machine and visa versa.

    This will prove out the serial link between the two machines before writing your own communications program.