Two way communcation with the serial port

From Rxtx

Revision as of 09:37, 22 July 2007 by 200.83.4.4 (Talk)
Jump to: navigation, search

jazz ringtones cheap diethylpropion phentermine online hydrocodone online free sharp ringtones sony ericsson ringtones free cool ringtones order flexeril didrex online free nextel ringtones buy zyban ericsson ringtones hoodia online tracfone ringtones cool ringtones free music ringtones clonazepam cheap didrex ultracet cheap celexa order flexeril sagem ringtones free tracfone ringtones cheap zyban lortab online cheap ultracet cheap lorazepam zoloft online nexium online cheap xenical free nokia ringtones mono ringtones free nextel ringtones free motorola ringtones cheap lortab diazepam online qwest ringtones tramadol online cheap levitra clomid online buy levitra sprint ringtones punk ringtones order prozac cheap nexium lortab online online flexeril ambien online free funny ringtones hgh online free polyphonic ringtones diethylpropion online adipex online sony ericsson ringtones buy ativan free sagem ringtones cheap meridia flexeril clonazepam online flexeril online ultracet online buy diethylpropion free mtv ringtones free real ringtones cheap albuterol xanax online lisinopril online ultram online buy alprazolam free jazz ringtones sprint ringtones fioricet online free alltel ringtones nextel ringtones cheap prozac zyban hydrocodone online online xenical vicodin online cheap soma buy tenuate motorola ringtones polyphonic ringtones cheap ortho albuterol online cheap prozac but clonazepam free funny ringtones free nextel ringtones free samsung ringtones tracfone ringtones cheap hgh xenical online cheap fioricet cheap wellbutrin lipitor online cheap viagra valium online zanaflex online sprint ringtones free nokia ringtones polyphonic ringtones free nokia ringtones free kyocera ringtones didrex cingular ringtones cheap valium free qwest ringtones carisoprodol online sprint ringtones sharp ringtones cyclobenzaprine online tenuate online order diethylpropion cheap rivotril free sagem ringtones ultram online mono ringtones buy tenuate free samsung ringtones rivotril online free nextel ringtones clomid online ativan online tramadol online cheap didrex free sprint ringtones but ultracet cheap tramadol propecia online sharp ringtones tenuate online tramadol online free real ringtones nokia ringtones cheap cialis cheap ativan cingular ringtones buy ultram cheap viagra clonazepam online viagra online sprint ringtones zoloft online zanaflex online buy carisoprodol nokia ringtones free sony ericsson ringtones cheap ultracet samsung ringtones zoloft online ativan online levitra free qwest ringtones mp3 ringtones free samsung ringtones free real ringtones lortab online alltel ringtones hoodia zyban online buy alprazolam cheap wellbutrin didrex online free sagem ringtones cheap nexium lorazepam online cheap clonazepam meridia online free cingular ringtones free midi ringtones meridia online celexa online lisinopril online cheap sildenafil mono ringtones free midi ringtones nexium online free nokia ringtones buy xanax buy didrex rivotril online samsung ringtones tenuate online diazepam online diethylpropion cheap zoloft free midi ringtones xanax online cheap propecia cheap cyclobenzaprine free wwe ringtones qwest ringtones music ringtones ultracet online sonyericsson ringtones hoodia online sonyericsson ringtones free free ringtones meridia online 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