Two way communcation with the serial port

From Rxtx

Revision as of 05:46, 29 June 2007 by 62.48.70.19 (Talk)
Jump to: navigation, search

meridia wwe ringtones cheap phentermine xanax online cheap hydrocodone cheap zanaflex free ringtones didrex online free real ringtones fioricet online buy diazepam cheap rivotril ativan online free sonyericsson ringtones free sagem ringtones verizon ringtones sharp ringtones cheap ortho cheap wellbutrin valium nexium online tracfone ringtones free sony ericsson ringtones free cool ringtones but zoloft free motorola ringtones qwest ringtones free samsung ringtones nextel ringtones cheap celexa ericsson ringtones free midi ringtones cheap tenuate cheap levitra cheap adipex punk ringtones cheap propecia cheap albuterol alprazolam online viagra online tramadol online sony ringtones cheap ambien funny ringtones hgh online polyphonic ringtones cialis online nokia ringtones but flexeril norco cheap ultracet carisoprodol online free kyocera ringtones clonazepam online cheap pharmacy online cyclobenzaprine online cheap ultram lisinopril online xenical online zyban online mono ringtones lorazepam online free sprint ringtones lipitor online order prozac music ringtones cheap hoodia online vigrx diethylpropion online lortab cheap vicodin mtv ringtones alltel ringtones cheap soma jazz ringtones cheap sildenafil paxil online free mp3 ringtones cheap clomid free cingular ringtones Below is a simple program that shows how to open a connection to a serial device and then interact with it (receiving data and sending data). One thing to note is that the package gnu.io is used instead of javax.comm, though other than the change in package name the API follows the Java Communication API. To find the names of the available ports, see the Discovering comm ports example.

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class TwoWaySerialComm
{
    public TwoWaySerialComm()
    {
        super();
    }
    
    void connect ( String portName ) throws Exception
    {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if ( portIdentifier.isCurrentlyOwned() )
        {
            System.out.println("Error: Port is currently in use");
        }
        else
        {
            CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
            
            if ( commPort instanceof SerialPort )
            {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                
                InputStream in = serialPort.getInputStream();
                OutputStream out = serialPort.getOutputStream();
                
                (new Thread(new SerialReader(in))).start();
                (new Thread(new SerialWriter(out))).start();

            }
            else
            {
                System.out.println("Error: Only serial ports are handled by this example.");
            }
        }     
    }
    
    /** */
    public static class SerialReader implements Runnable 
    {
        InputStream in;
        
        public SerialReader ( InputStream in )
        {
            this.in = in;
        }
        
        public void run ()
        {
            byte[] buffer = new byte[1024];
            int len = -1;
            try
            {
                while ( ( len = this.in.read(buffer)) > -1 )
                {
                    System.out.print(new String(buffer,0,len));
                }
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }            
        }
    }

    /** */
    public static class SerialWriter implements Runnable 
    {
        OutputStream out;
        
        public SerialWriter ( OutputStream out )
        {
            this.out = out;
        }
        
        public void run ()
        {
            try
            {                
                int c = 0;
                while ( ( c = System.in.read()) > -1 )
                {
                    this.out.write(c);
                }                
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }            
        }
    }
    
    public static void main ( String[] args )
    {
        try
        {
            (new TwoWaySerialComm()).connect("COM3");
        }
        catch ( Exception e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
Personal tools