Alex Rivera | Logout

How do I configure and communicate with a serial port?

Asked 2008-08-06T03:19:19.470
49

I need to send and receive data over serial connections (RS-232 and RS-422).

How do I set up and communicate with such a connection? How do I figure out what the configuration settings (e.g. baud rate) should be and how do I set them?

In particular, I am looking to do this in Java, C/C++, or one of the major Unix shells but I also have some interest in serial programming using Windows/Hyperterminal.

Edit
Report

2 Answers

10

For C/C++ on Windows you have (at least) two choices:

  1. Use the SerialPort class provided by .NET.
  2. Use the Win32 API. There is an extensive MSDN article dating back to 1995, and many free libraries and examples on the web to get you started.

The .NET option will be much easier.

answered 2008-08-13T12:44:34.937
3

From the other side, if you want to do it using C#, which will run on both Windows and Linux--with some limitations (EDIT: which may be out of date. I have no way to test it.). Just create a SerialPort object, set its baudrate, port and any other odd settings, call open on it, and write out your byte[]s. After all the setup, the SerialPort object acts very similar to any networked stream, so it should be easy enough to figure out.

And as ibrandy states, you need to know all these settings, like baud rate, before you even start attempting to communicate to any serial device.

answered 2008-08-06T03:35:50.093

Your Answer