>
> Hello,
>
> I work on RedHat 6.2 platform and I'm to make a  communication on
Serial
> Port using RXTX-1.5-3 package and JavaSun JDK1.3 package.
> I've succeeded to send date from Linux on the serial port /dev/ttyS1
(in
> Windows is COM2).
> This data is received on Windows well.
> But the problem occures when I try to receive data on the serial port
> on Linux.
> Some messages get lost and the receiving process dies after a few
> received message.
> The same Java program works fine on Windows (with Sun's JavaComm2.0
> package on COM2 port ).
> The souce code is attached to this document.
>  Can you please give me any sugestion, ideeas for resolving this
> problem?
> Thanks in advance for your help,
>
> Leonard
>
>
------------------------------------------------------------------------

>
> import javax.comm.*;
> import java.io.*;
> import java.awt.TextArea;
> import java.awt.event.*;
> import java.util.TooManyListenersException;
>
> /** A class that handles the details of a serial connection.
>  */
> public class SerialPortConnection implements SerialPortEventListener,
>                                          CommPortOwnershipListener
> {
>
>     private OutputStream os;
>     private InputStream is;
>
>     private CommPortIdentifier portId;
>     private SerialPort sPort;
>
>         private String portName;
>         private int baudrate;//
>         private int dataBits;// DATABITS_5: 5 bits      DATABITS_6: 6
bits      DATABITS_7: 7 bits      DATABITS_8: 8 bits
>         private int     stopBits;//     STOPBITS_1: 1 stop bit
STOPBITS_2: 2 stop bits STOPBITS_1_5: 1.5 stop bits
>         private int     parity;// PARITY_NONE: no parity PARITY_ODD:
odd parity PARITY_EVEN: even parity        PARITY_MARK: mark parity
PARITY_SPACE: space parity
>         private int flowcontrolIn;//FLOWCONTROL_NONE  Flow control
off. FLOWCONTROL_RTSCTS_IN  RTS/CTS flow control on input.
>         private int flowcontrolOut; //  FLOWCONTROL_RTSCTS_OUT RTS/CTS
flow control on output. FLOWCONTROL_XONXOFF_IN XON/XOFF flow control on
input.
>                                         // FLOWCONTROL_XONXOFF_OUT
XON/XOFF flow control on output.
>
>     private boolean open;
>
>     /**
>     Creates a SerialConnection object and initilizes variables passed
in
>     as params.
>     */
>     public SerialPortConnection()
>     {
>
>           open = false;
>     }
>
>    /** Attempts to open a serial connection and streams using the
parameters.
>      * If it is unsuccesfull at any step it
>      * returns the port to a closed state, throws a
>      * Exception, and returns.
>      * Gives a timeout of 30 seconds on the portOpen to allow other
applications
>      * to reliquish the port if have it open and no longer need it.
>      * @param portName the name of port ("COM1, "/dev/ttyS0")
>      * @param params the parameters of port
>      * @throws Exception if its parameters is wrong or the connection
fail
>      */
>    public void openConnection(String portName,String[] params)throws
Exception
>    {
>
>                 // Obtain a CommPortIdentifier object for the port you
want to open.
>                 this.portName=portName;
>                 try
>                 {
>                     portId =
>
CommPortIdentifier.getPortIdentifier(portName);
>                 }
>                 catch (NoSuchPortException e)
>                 {
>                   throw e;
>                 }
>
>                 // Open the port represented by the CommPortIdentifier
object. Give
>                 // the open call a relatively long timeout of 30
seconds to allow
>                 // a different application to reliquish the port if
the user
>                 // wants to.
>                 try
>                 {
>                     sPort = (SerialPort)portId.open("Serial", 30000);
>                 }
>                 catch (PortInUseException e)
>                 { throw e;
>         }
>
>                 // Set the parameters of the connection. If they won't
set, close the
>                 // port before throwing an exception.
>             try
>                 {
>                     setConnectionParameters(params);
>                 }
>                 catch (Exception e)
>                 {
>                     sPort.close();
>                     throw e;
>                 }
>
>                 // Open the input and output streams for the
connection. If they won't
>                 // open, close the port before throwing an exception.
>                 try
>                 {
>                     os = sPort.getOutputStream();
>                     is = sPort.getInputStream();
>                 } catch (IOException e)
>                 {
>                     sPort.close();
>                     throw e;
>                 }
>                 // Add this object as an event listener for the serial
port.
>                 try
>                 {
>                     sPort.addEventListener(this);
>                 }
>                 catch (TooManyListenersException e)
>                 {
>                     sPort.close();
>                    throw e;
>                 }
>
>                 // Set notifyOnDataAvailable to true to allow event
driven input.
>                 sPort.notifyOnDataAvailable(true);
>
>                 // Set notifyOnBreakInterrup to allow event driven
break handling.
>                 sPort.notifyOnBreakInterrupt(true);
>
>                 // Set receive timeout to allow breaking out of
polling loop during
>                 // input handling.
>                 try
>                 {
>                     sPort.enableReceiveTimeout(30);
>                 }
>                 catch (UnsupportedCommOperationException e)
>                 {
>                 }
>
>                 // Add ownership listener to allow ownership event
handling.
>                 portId.addPortOwnershipListener(this);
>
>                 open = true;
>     }
>
>     /** Sets the connection parameters to the setting in the
parameters object.
>      * If set fails return the parameters object to origional settings
and
>      * throw exception.
>      * @param params the new parameters for port
>      * @throws Exception if the new parameters is wong or can not
speciffied
>      */
>     public void setConnectionParameters(String[] params)throws
Exception
>         {
>             int oldbaudrate=baudrate;
>             int olddataBits=dataBits;
>                 int     oldstopBits=stopBits;
>             int oldparity=parity;
>             int oldflowcontrolIn=flowcontrolIn;
>             int oldflowcontrolOut=flowcontrolOut;
>
>                 //save current parameters
>                 if (params.length==4)
>            {
>                         try
>                         {
>                           baudrate=Integer.parseInt(params[0]);
>                         }
>                         catch(Exception e)
>                         {
>                           throw new Exception("ERROR: incorrect
baudrate parameter !");
>                         }
>                         //DATABITS_5: 5 bits    DATABITS_6: 6
bits      DATABITS_7: 7 bits      DATABITS_8: 8 bits
>                         if(params[1].equals("DATABITS_5"))
dataBits=SerialPort.DATABITS_5;
>                         else if(params[1].equals("DATABITS_6"))
dataBits=SerialPort.DATABITS_6;
>                         else if(params[1].equals("DATABITS_7"))
dataBits=SerialPort.DATABITS_7;
>                         else if(params[1].equals("DATABITS_8"))
dataBits=SerialPort.DATABITS_8;
>                         else
>                         {
>
>                           throw new Exception("ERROR: incorrect
dataBits parameter !");
>                         }
>                 //STOPBITS_1: 1 stop bit        STOPBITS_2: 2 stop
bits STOPBITS_1_5: 1.5 stop bits
>                 if(params[2].equals("STOPBITS_1"))
stopBits=SerialPort.STOPBITS_1;
>                     else if(params[2].equals("STOPBITS_1_5"))
stopBits=SerialPort.STOPBITS_1_5;
>                     else if(params[2].equals("STOPBITS_2"))
stopBits=SerialPort.STOPBITS_2;
>                     else
>                     {
>                       throw new Exception("ERROR: incorrect dataBits
parameter !");
>                     }
>                         //PARITY_NONE: no parity PARITY_ODD: odd
parity PARITY_EVEN: even parity        PARITY_MARK: mark parity
PARITY_SPACE: space parity
>                         if(params[3].equals("PARITY_NONE"))
parity=SerialPort.PARITY_NONE;
>                         else if(params[3].equals("PARITY_ODD"))
parity=SerialPort.PARITY_ODD;
>                         else if(params[3].equals("PARITY_EVEN"))
parity=SerialPort.PARITY_EVEN;
>                         else if(params[3].equals("PARITY_MARK"))
parity=SerialPort.PARITY_MARK;
>                         else if(params[3].equals("PARITY_SPACE"))
parity=SerialPort.PARITY_SPACE;
>                         else
>                     {
>                       throw new Exception("ERROR: incorrect parity
parameter !");
>                     }
>            }else if (params.length==6)
>            {   ////FLOWCONTROL_NONE  Flow control off.
FLOWCONTROL_RTSCTS_IN  RTS/CTS flow control on input.
>             //  FLOWCONTROL_RTSCTS_OUT RTS/CTS flow control on output.
FLOWCONTROL_XONXOFF_IN XON/XOFF flow control on input.
>             // FLOWCONTROL_XONXOFF_OUT  XON/XOFF flow control on
output.
>             if(params[4].equals("FLOWCONTROL_NONE"))
flowcontrolIn=SerialPort.FLOWCONTROL_NONE;
>                         else
if(params[4].equals("FLOWCONTROL_RTSCTS_IN"))
flowcontrolIn=SerialPort.FLOWCONTROL_RTSCTS_IN;
>                         else
if(params[4].equals("FLOWCONTROL_RTSCTS_OUT"))
flowcontrolIn=SerialPort.FLOWCONTROL_RTSCTS_OUT;
>                         else
if(params[4].equals("FLOWCONTROL_XONXOFF_IN"))
flowcontrolIn=SerialPort.FLOWCONTROL_XONXOFF_IN;
>                         else
if(params[4].equals("FLOWCONTROL_XONXOFF_OUT"))
flowcontrolIn=SerialPort.FLOWCONTROL_XONXOFF_OUT;
>                         else
>                         {
>               throw new Exception("ERROR: incorrect dataBits parameter
!");
>                     }
>                         if(params[5].equals("FLOWCONTROL_NONE"))
flowcontrolOut=SerialPort.FLOWCONTROL_NONE;
>                         else
if(params[5].equals("FLOWCONTROL_RTSCTS_IN"))
flowcontrolOut=SerialPort.FLOWCONTROL_RTSCTS_IN;
>                         else
if(params[5].equals("FLOWCONTROL_RTSCTS_OUT"))
flowcontrolOut=SerialPort.FLOWCONTROL_RTSCTS_OUT;
>                         else
if(params[5].equals("FLOWCONTROL_XONXOFF_IN"))
flowcontrolOut=SerialPort.FLOWCONTROL_XONXOFF_IN;
>                         else
if(params[5].equals("FLOWCONTROL_XONXOFF_OUT"))
flowcontrolOut=SerialPort.FLOWCONTROL_XONXOFF_OUT;
>                         else
>                         {
>                       throw new Exception("ERROR: incorrect dataBits
parameter !");
>                     }
>            }
>
>     //serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
>                 // Set connection parameters, if set fails return
parameters object
>                 // to original state.
>                 try
>         {
>             sPort.setSerialPortParams(baudrate,
>                         dataBits,
>                         stopBits,
>                         parity);
>                 }
>                 catch (UnsupportedCommOperationException e)
>                 {
>                         baudrate=oldbaudrate;
>                 dataBits=olddataBits;
>                 stopBits=oldstopBits;
>                 parity=oldparity;
>
>                    throw e;
>                 }
>
>                 // Set flow control.
>                 try
>             {
>                         /*
>                            FLOWCONTROL_NONE
>                             Flow control off.
>                            FLOWCONTROL_RTSCTS_IN
>                             RTS/CTS flow control on input.
>                            FLOWCONTROL_RTSCTS_OUT
>                             RTS/CTS flow control on output.
>                            FLOWCONTROL_XONXOFF_IN
>                             XON/XOFF flow control on input.
>                            FLOWCONTROL_XONXOFF_OUT
>                             XON/XOFF flow control on output.
>                         */
>                    sPort.setFlowControlMode(flowcontrolIn//input
>                                            | flowcontrolOut
);//output*/
>                 }
>                 catch (UnsupportedCommOperationException e)
>                 {
>             System.out.println(e);
>                     throw e;
>                 }
>     }
>
>     /**
>     Close the port and clean up associated elements.
>     */
>     public void closeConnection()
>         {
>                 // If port is alread closed just return.
>                 if (!open) return;
>                 // Check to make sure sPort has reference to avoid a
NPE.
>                 if (sPort != null)
>                 {
>                     try
>                         {
>                         // close the i/o streams.
>                         os.close();
>                         is.close();
>                     }
>                         catch (IOException e)
>                         {
>                            System.err.println(e);
>                     }
>
>                     // Close the port.
>                     sPort.close();
>
>                     // Remove the ownership listener.
>                     portId.removePortOwnershipListener(this);
>                 }
>
>                 open = false;
>     }
>
>     /**
>     Send a one second break signal.
>     */
>     public void sendBreak()
>         {
>                 sPort.sendBreak(1000);
>     }
>
>     /**
>     Reports the open status of the port.
>     return true if port is open, false if port is closed.
>     */
>     public boolean isOpen()
>         {
>           return open;
>     }
>
>     /**
>     Handles SerialPortEvents. During DATA_AVAILABLE the port buffer is
read until
>         it is drained, when no more data is availble and 30ms has
passed the method returns.
>         When a another event occurs the name are written to the
output.
>     */
>
>     public void serialEvent(SerialPortEvent e)
>     {
>         // Create a StringBuffer and int to receive input data.
>         StringBuffer inputBuffer = new StringBuffer();
>         int newData = 0;
>         // Determine type of event.
>         switch (e.getEventType())
>         {
>             // Read data until -1 is returned.
>             case SerialPortEvent.DATA_AVAILABLE:
>                 while (newData != -1)
>                 {
>                     try
>                     {
>                         newData = is.read();
>                         if (newData == -1)   break;
>                         inputBuffer.append((char)newData);
>                     }
>                     catch (IOException ex)
>                     {
>                         System.err.println(ex);
>                         return;
>                     }
>                 }
>
>                 ////////////////  display data
received///////////////////
>                 System.out.println(new String(inputBuffer));
>                 ///////////////////////////////////
>                 break;
>            case SerialPortEvent.BI: System.out.println("BI");
>            case SerialPortEvent.OE: System.out.println("OE");
>            case SerialPortEvent.FE: System.out.println("FE");
>            case SerialPortEvent.PE: System.out.println("PE");
>            case SerialPortEvent.CD: System.out.println("CD");
>            case SerialPortEvent.CTS: System.out.println("CTS");
>            case SerialPortEvent.DSR: System.out.println("DSR");
>            case SerialPortEvent.RI: System.out.println("RI");
>            case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
System.out.println("BI");
>                break;
>
>         }
>
>     }
>
>     /**
>     Handles ownership events. If a PORT_OWNERSHIP_REQUESTED event is
>      No action is taken on other types of ownership events.
>     */
>     public void ownershipChange(int type)
>         {
>                 if (type ==
CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED)
>                 {
>
System.out.println("PORT_OWNERSHIP_REQUESTED");
>                 }
>     }
>
>
>
>         public void sendMessage(String mess)
>         {
>         byte[] messaj=mess.getBytes();
>                 try
>                 {
>                   os.write(messaj);
>                 }
>                 catch(IOException e)
>                 {
>                         e.printStackTrace();
>                 }
>         }
>
>
>         //testare
>         public static void main(String[] args)
>         {
>
>             String[]
params1={"9600","DATABITS_8","STOPBITS_1","PARITY_NONE"};
>                 String[]
params2={"57600","DATABITS_8","STOPBITS_1","PARITY_NONE"};
>                 SerialPortConnection spc=new SerialPortConnection();
>
>         try
>         {
>             spc.openConnection("/dev/ttyS1",params1);
>             spc.sendMessage("kdsjbncsdc");
>             Thread.sleep(1000);
>             spc.sendMessage("kdsjbncsdc2");
>
>         }
>         catch(Exception e)
>         {
>             e.printStackTrace();
>         }
>
>
>         }
> }




----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to