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

<p>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?</p>

<p>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.</p>

[{'answer_id': 3054, 'author': 'Louis Brandy', 'author_id': 2089740, 'author_profile': 'https://Stackoverflow.com/users/2089740', 'pm_score': 6, 'selected': True, 'text': '<p>Build a time machine and go back to 1987? Ho ho.</p>\n\n<p>Ok, no more snarky comments.</p>\n\n<blockquote>\n  <p>How do I figure out what the configuration settings (e.g. baud rate) should be...</p>\n</blockquote>\n\n<p>Read the datasheet? Ok, ok. Seriously, last one. If you don\'t know the baud rate of the device you are trying to communicate with, you have two choices. Start guessing, or possibly bust out an o-scope. If you need a good starting point, let me suggest 9600-8-N-1. My suspicion is you can get there with brute force relatively quickly. There\'s a third option of having an old-school ninja who can tell just by the LOOK of the garbled characters at some standard baud rate what actual baud rate is. An impressive party trick to be sure.</p>\n\n<p>Hopefully though you have access to this information. In unix/linux, you can get ahold of minicom to play with the serial port directly. This should make it fairly quick to get the configuration figured out.</p>\n\n<blockquote>\n  <p>one of the major Unix shells</p>\n</blockquote>\n\n<p>In Unix the serial port(s) is/are file-mapped into the /dev/ subdir. ttyS0, for example. If you setup the correct baud rate and whatnot using minicom, you can even cat stuff to that file to send stuff out there.</p>\n\n<p>On to the meat of the question, you can access it programmatically through the POSIX headers. termios.h is the big one. </p>\n\n<p><strike>See: <a href="http://www.easysw.com/~mike/serial/serial.html#3_1" rel="noreferrer">http://www.easysw.com/~mike/serial/serial.html#3_1</a></strike>\n<strong>(NOT AVAILABLE ANYMORE)</strong></p>\n\n<blockquote>\n  <p>but I also have some interest in serial programming using Windows/Hyperterminal.</p>\n</blockquote>\n\n<p>Hyperterminal and minicom are basically the same program. As for how Windows let\'s you get access to the serial port, I\'ll leave that question for someone else. I haven\'t done that in Windows since the Win95 days.</p>\n'}, {'answer_id': 3060, 'author': 'Patrick', 'author_id': 429, 'author_profile': 'https://Stackoverflow.com/users/429', 'pm_score': 2, 'selected': False, 'text': '<p>From the other side, if you want to do it using C#, which will run on both Windows and Linux--with some <a href="http://www.mono-project.com/HowToSystemIOPorts" rel="nofollow noreferrer">limitations</a> (EDIT: which may be out of date. I have no way to test it.). Just create a <a href="http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx" rel="nofollow noreferrer">SerialPort</a> 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.</p>\n\n<p>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. </p>\n'}, {'answer_id': 3090, 'author': 'graham.reeds', 'author_id': 342, 'author_profile': 'https://Stackoverflow.com/users/342', 'pm_score': 2, 'selected': False, 'text': '<p>At work we use teraterm and realterm for checking serial data is correctly formatted. Also we have a hardware splitter with a switch so we can monitor traffic to our application via a cable back to another port.</p>\n\n<p>Windows allows you access to the serial port via CreateFile. That gives you a handle and from there you can configure access.</p>\n'}, {'answer_id': 9725, 'author': 'smh', 'author_id': 1077, 'author_profile': 'https://Stackoverflow.com/users/1077', 'pm_score': 3, 'selected': False, 'text': '<p>For C/C++ on Windows you have (at least) two choices:</p>\n\n<ol>\n<li>Use the SerialPort class provided by .NET.</li>\n<li>Use the Win32 API. There is an <a href="http://msdn.microsoft.com/en-us/library/ms810467.aspx" rel="noreferrer">extensive MSDN article</a> dating back to 1995, and many free libraries and examples on the web to get you started.</li>\n</ol>\n\n<p>The .NET option will be much easier.</p>\n'}, {'answer_id': 24078, 'author': 'Marcel', 'author_id': 2554, 'author_profile': 'https://Stackoverflow.com/users/2554', 'pm_score': 4, 'selected': False, 'text': '<p>If you want to code in Java I really recommend SerialIOs <a href="http://serialio.com/products/serialport/serialport.php" rel="noreferrer">SerialPort</a>. It is very easy to use and saves you days of work. I\'ve never found an open source library as good as SerialIO, REALLY!</p>\n\n<p>My advice: do not use Sun\'s serial IO framework! It is from 1998 and full of bugs. You can use <a href="http://www.rxtx.org/" rel="noreferrer">rxtx</a> but serialio is better!</p>\n'}, {'answer_id': 41526, 'author': 'Ferruccio', 'author_id': 4086, 'author_profile': 'https://Stackoverflow.com/users/4086', 'pm_score': 3, 'selected': False, 'text': '<p>If it needs to be cross platfrom, I would suggest looking at Boost <a href="http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio.html" rel="noreferrer">Asio</a>.</p>\n'}, {'answer_id': 65870, 'author': 'Lawrence Dol', 'author_id': 8946, 'author_profile': 'https://Stackoverflow.com/users/8946', 'pm_score': 2, 'selected': False, 'text': "<p>Awhile back I wrote a decent sized application to route connections from a farm of modems through to a TCP/IP network address.</p>\n\n<p>Initially I looked for an unencumbered (free) Serial IO library.  I tried Sun's, IBM's and RxTx.  They were fine for developing the application, and in initial testing, but in production they each proved unstable.</p>\n\n<p>Finally I paid for SerialIO's SerialPort.  Converting over was literally an exercise in changing imports, and the library has been <strong><em>absolutely</em></strong> rock solid - I cannot recommend it enough.  My application has been running in the field 24/7 for a couple of years now, with not a single problem encountered by multiple customers.</p>\n\n<p>If you start development using SerialPort, they have a better API and I would use it.</p>\n\n<p>If you need cross platform support, Java with SerialPort was the best choice I could find.</p>\n\n<p>Lastly, their licensing is pretty darn reasonable as long as you are not preinstalling software on the equipment for your customer(s).</p>\n"}, {'answer_id': 118609, 'author': 'Maciej Hehl', 'author_id': 19939, 'author_profile': 'https://Stackoverflow.com/users/19939', 'pm_score': 1, 'selected': False, 'text': "<p>Depending on the device You are trying to communicate with, there may be more parameters than the baud rate, number of data bits, type of parity checking and number of stop bits to consider. If I recall correctly, modems use nine lines of the RS-232C interface. Some devices like, for example cash registers, may use hardware handshaking on RTS/CTS lines or on DTR/STR lines.  </p>\n\n<p>In general it's good to know how the interface works. You can't communicate if the baud rate doesn't match, but wrong setting of other parameters might kind of work. For example You can easily send data to the device expecting 1 stop bit with 2 stop bits set. Problems start when You try to receive data in such case. You can also use appropriately set parity bit as one of stop bits, etc.</p>\n"}, {'answer_id': 15475192, 'author': 'Mzzl', 'author_id': 1336158, 'author_profile': 'https://Stackoverflow.com/users/1336158', 'pm_score': 0, 'selected': False, 'text': '<p>I have been using purejavacomm:\n<a href="http://www.sparetimelabs.com/purejavacomm/index.html" rel="nofollow">It is an implementation of javax.comm written in pure java + JNA</a></p>\n\n<p>Unlike rxtx, you don\'t need to install a dll. It is written in pure Java + JNA, which solved the problem of portability between Windows and Linux for me. It should be easy to port to other OS-es that JNA supports, such as Solaris and FreeBSD, but I haven\'t tried it. </p>\n\n<p>You might expect a pure java library to lag behind a native implementation such as rxtx in performance, but with modern CPU\'s, the bottleneck is very likely to be the bitrate of your serial port, not CPU cycles. Also, it\'s much easier to debug than a mixed Java/Native library or pure compiled native code. </p>\n'}, {'answer_id': 25700546, 'author': 'Marco', 'author_id': 2619479, 'author_profile': 'https://Stackoverflow.com/users/2619479', 'pm_score': 1, 'selected': False, 'text': '<p>If you are not forced to use a particular compiler I suggest to use Qt and in the new 5.3 version you will find a class dedicated to serial ports:</p>\n\n<p><a href="http://qt-project.org/doc/qt-5/qserialport.html" rel="nofollow">http://qt-project.org/doc/qt-5/qserialport.html</a></p>\n\n<p>The code you will write will run on all supprited Qt platforms, at least those that have serial ports. </p>\n'}]
['https://Stackoverflow.com/questions/3049', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/462/']