Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import jssc.*;
- public class SerialTest
- {
- private static SerialPort serialPort;
- private static final String PORTNAME = "COM3";
- private static final int BAUDRATE = 115200;
- private static final int DATABITS = 8;
- private static final int STOPBITS = 1;
- private static final int PARITY = 0;
- public static void main(String[] args)
- {
- instantiateSerialPort();
- openSerialPort();
- setupSerialPortParameters();
- try
- {
- serialPort.writeString("1");
- // System.out.println("\n\"Hello World!!!\" successfully written to port: " + serialPort.writeBytes("Hello World!!!".getBytes()));
- }
- catch (SerialPortException ex)
- {
- System.out.println(ex);
- }
- closeSerialPort();
- }
- private static void instantiateSerialPort()
- {
- System.out.println("Instantiating Serial Port \"" + PORTNAME + "\"");
- serialPort = new SerialPort(PORTNAME);
- }
- private static void setupSerialPortParameters()
- {
- try
- {
- System.out.println();
- System.out.println("Setting Serial Port Parameters:");
- System.out.println("Baudrate: " + BAUDRATE + ".");
- System.out.println("Data-Bits: " + DATABITS + ".");
- System.out.println("Stop-Bits" + STOPBITS + ".");
- System.out.println("Parity: " + PARITY + ".");
- serialPort.setParams(BAUDRATE, DATABITS, STOPBITS, PARITY);
- }
- catch (SerialPortException ex)
- {
- System.out.println(ex);
- }
- }
- private static void openSerialPort()
- {
- try
- {
- System.out.println();
- System.out.println("Trying to open serial port");
- serialPort.openPort();
- System.out.println("Serial port opened.");
- }
- catch (SerialPortException ex)
- {
- System.out.println(ex);
- }
- }
- private static void closeSerialPort()
- {
- try
- {
- System.out.println();
- System.out.println("Trying to close serial port.");
- serialPort.closePort();
- System.out.println("Serial port closed.");
- }
- catch (SerialPortException ex)
- {
- System.out.println(ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement