Embedded Freaks..

August 13, 2008

Reading from serial port (using rxtx)

Filed under: java — Tags: , , — kunilkuda @ 9:29 am

Once you’ve got the serial InputStream, reading would be easy. Here’s an example of code to read the serial port using InputStream:

 * Buffer to hold the reading
private byte[] readBuffer = new byte[400];

private void readSerial() {
    try {
        int availableBytes = inStream.available();
        if (availableBytes > 0) {
            // Read the serial port
            inStream.read(readBuffer, 0, availableBytes);

            // Print it out
                    new String(readBuffer, 0, availableBytes));
    } catch (IOException e) {

But the real problem comes from the place where you should place the code.

August 12, 2008

Serial port event in rxtx

Filed under: java — Tags: , , — kunilkuda @ 6:26 am

So, what do you expect from serial port event ? Most probably is the notification whether you’ve got new data in the buffer or not. But for some people (like me) who deals with real modem (I use GSM modem for server communication), we need RTS/CTS notification as well. Otherwise, we will flood the modem with data, since most PCs are faster than modem.

I’ve tested some of rxtx serial port events in Linux, and here’s some report that you might be interested.

August 11, 2008

Writing to serial port (using Rxtx)

Filed under: java — Tags: , , — kunilkuda @ 7:57 am

Writing to serial port is easy once you’ve got the serial port’s outputstream. Just do something like this:

String serialMessage = "AT\r\n";
OutputStream outstream = serialPort.getOutputStream();

Refer to my previous post for opening and getting the output stream for the serial port.

Additional Notes

From wikibooks Serial Java, comes a good advice, do not use Reader/Writer for AT command, since Reader/Writer will translate the AT command into the Unicode (your code might work as it is, or might not work if you use different Unicode than latin/ISO-8859-1 as default Unicode in the system).

« Newer PostsOlder Posts »

Create a free website or blog at WordPress.com.