//Program to send and read response from a serial to usb converter
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.*;
import java.util.*;
public class PortWriter implements Runnable,SerialPortEventListener
{
  static Enumeration ports;
  static CommPortIdentifier pID;
  static OutputStream outStream;
  static SerialPort serPort;
  static InputStream is;
  static PrintStream os;
  int i = 0;
  byte[] readBuffer = new byte[10];
  int numBytes = 0;
  public PortWriter() throws Exception
  {
    try
      {
        serPort = (SerialPort) pID.open("COM25", 2000);
        System.out.println("\ngetDataBits"+serPort.getDataBits());
        System.out.println("\ngetStopBits"+serPort.getStopBits());
        System.out.println("\ngetParity"+serPort.getParity());
        System.out.println("\ngetBaudRate"+serPort.getBaudRate());
        serPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        outStream = serPort.getOutputStream();
        serPort.addEventListener(this);
        serPort.notifyOnDataAvailable(true);
        is = serPort.getInputStream();
      }
      catch (Exception e)
      {
        System.out.println("PortInUseException : " + e);
      }
    }
    public static void main(String[] args) throws Exception
    {
      ports = CommPortIdentifier.getPortIdentifiers();
      while (ports.hasMoreElements())
      {
        pID = (CommPortIdentifier) ports.nextElement();
        if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL)
        {
          if (pID.getName().equals("COM25"))
          {
            PortWriterThread pWriter = new PortWriterThread();
            System.out.println("USB found");
            try
            {
              String smsMessage = "TEST DATA";
              send(smsMessage);
              if (is != null)
              {
                is.close();
//System.out.println("Closing input stream..");
              }
              if (outStream != null)
              {
                outStream.close();
// System.out.println("Closing output stream..");
              }
              if (serPort != null)
                {
                serPort.close();
//System.out.println("Closing serial Port..");
              }
            }
            catch (Exception e)
            {
              System.out.println("could not write to outputstream:");
                System.out.println(e.toString());
              }
            }
          }
        }
      }
      public static void send(String cmd)
      {
        System.out.println("Sending...");
        try
        {
          outStream.write(cmd.getBytes());
          System.out.println(cmd+" send SUCCESS");
        }
        catch (Exception e)
        {
          System.out.println("Exception in send:- " + e);
        }
      }
/**
* Method declaration
*
*
* @see
*/
      public void run()
      {
        try
        {
          Thread.sleep(1000);
        }
        catch (Exception e)
        {
          System.out.println("Exception in run:-"+e);
        }
      }
/**
* Method declaration
*
*
* @param event
*
* @see
*/
      public void serialEvent(SerialPortEvent event)
      {
        switch (event.getEventType())
        {
          case SerialPortEvent.BI:
          case SerialPortEvent.OE:
          case SerialPortEvent.FE:
          case SerialPortEvent.PE:
          case SerialPortEvent.CD:
          case SerialPortEvent.CTS:
          case SerialPortEvent.DSR:
          case SerialPortEvent.RI:
          case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
          break;
          case SerialPortEvent.DATA_AVAILABLE:
          byte[] readBuffer = new byte[20];
          try
          {
            while (is.available() > 0)
            {
              int numBytes = is.read(readBuffer);
            }
            System.out.print("Reading Data>>>"+new String(readBuffer));
          }
          catch (Exception e)
          {
            System.out.println("Exception in reading from input stream:-" + e);
          }
          break;
        }
      }
    }
No comments:
Post a Comment
Thanks for visiting my Blog....Ur comments n suggestions are most valuable for me...Thanks