Advertisement
Guest User

Untitled

a guest
Jul 14th, 2015
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. import jssc.*;
  2.  
  3. public class SerialTest
  4. {
  5.  
  6.     private static SerialPort serialPort;
  7.     private static final String PORTNAME = "COM3";
  8.     private static final int BAUDRATE = 115200;
  9.     private static final int DATABITS = 8;
  10.     private static final int STOPBITS = 1;
  11.     private static final int PARITY = 0;
  12.    
  13.     public static void main(String[] args)
  14.     {
  15.         instantiateSerialPort();
  16.         openSerialPort();  
  17.         setupSerialPortParameters();
  18.        
  19.         try
  20.         {
  21.             serialPort.writeString("1");
  22.             // System.out.println("\n\"Hello World!!!\" successfully written to port: " + serialPort.writeBytes("Hello World!!!".getBytes()));
  23.         }
  24.         catch (SerialPortException ex)
  25.         {
  26.             System.out.println(ex);
  27.         }
  28.        
  29.         closeSerialPort();
  30.     }
  31.  
  32.     private static void instantiateSerialPort()
  33.     {
  34.         System.out.println("Instantiating Serial Port \"" + PORTNAME + "\"");
  35.         serialPort = new SerialPort(PORTNAME);
  36.     }
  37.  
  38.     private static void setupSerialPortParameters()
  39.     {
  40.         try
  41.         {
  42.             System.out.println();
  43.             System.out.println("Setting Serial Port Parameters:");
  44.             System.out.println("Baudrate: " + BAUDRATE + ".");
  45.             System.out.println("Data-Bits: " + DATABITS + ".");
  46.             System.out.println("Stop-Bits" + STOPBITS + ".");
  47.             System.out.println("Parity: " + PARITY + ".");
  48.             serialPort.setParams(BAUDRATE, DATABITS, STOPBITS, PARITY);
  49.         }
  50.         catch (SerialPortException ex)
  51.         {
  52.             System.out.println(ex);
  53.         }
  54.     }
  55.    
  56.     private static void openSerialPort()
  57.     {
  58.         try
  59.         {
  60.             System.out.println();
  61.             System.out.println("Trying to open serial port");
  62.             serialPort.openPort();
  63.             System.out.println("Serial port opened.");
  64.         }
  65.         catch (SerialPortException ex)
  66.         {
  67.             System.out.println(ex);
  68.         }
  69.     }
  70.    
  71.     private static void closeSerialPort()
  72.     {
  73.         try
  74.         {
  75.             System.out.println();
  76.             System.out.println("Trying to close serial port.");
  77.             serialPort.closePort();
  78.             System.out.println("Serial port closed.");
  79.         }
  80.         catch (SerialPortException ex)
  81.         {
  82.             System.out.println(ex);
  83.         }
  84.     }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement