java - Come configuro e comunico con una porta seriale?

original title: "java - How do I configure and communicate with a serial port?"


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.

Devo inviare e ricevere dati tramite connessioni seriali (RS-232 e RS-422). Come posso configurare e comunicare con tale connessione? Come faccio a capire quali sono le impostazioni di configurazione (ad es. Bau ...

Questo è il riepilogo dopo la traduzione, se è necessario visualizzare la traduzione completa, fare clic sull'icona "traduci"

Tutte le risposte
  • Translate

    Build a time machine and go back to 1987? Ho ho.

    Ok, no more snarky comments.

    How do I figure out what the configuration settings (e.g. baud rate) should be...

    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.

    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.

    one of the major Unix shells

    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.

    On to the meat of the question, you can access it programmatically through the POSIX headers. termios.h is the big one.


    but I also have some interest in serial programming using Windows/Hyperterminal.

    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.

  • Translate

    If you want to code in Java I really recommend SerialIOs SerialPort. 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!

    My advice: do not use Sun's serial IO framework! It is from 1998 and full of bugs. You can use rxtx but serialio is better!

  • Translate

    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.

  • Translate

    If it needs to be cross platfrom, I would suggest looking at Boost Asio.

  • Translate

    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.

    Windows allows you access to the serial port via CreateFile. That gives you a handle and from there you can configure access.

  • Translate

    Awhile back I wrote a decent sized application to route connections from a farm of modems through to a TCP/IP network address.

    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.

    Finally I paid for SerialIO's SerialPort. Converting over was literally an exercise in changing imports, and the library has been absolutely 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.

    If you start development using SerialPort, they have a better API and I would use it.

    If you need cross platform support, Java with SerialPort was the best choice I could find.

    Lastly, their licensing is pretty darn reasonable as long as you are not preinstalling software on the equipment for your customer(s).

  • Translate

    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.

  • Translate

    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.

    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.

  • Translate

    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:

    The code you will write will run on all supprited Qt platforms, at least those that have serial ports.

  • Translate

    I have been using purejavacomm: It is an implementation of javax.comm written in pure java + JNA

    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.

    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.