Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.reapptech.creapptos;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import jssc.SerialPort;
- import jssc.SerialPortEvent;
- import jssc.SerialPortEventListener;
- import jssc.SerialPortException;
- /**
- * Hello world!
- *
- */
- public class App
- {
- // The serial port.
- private static SerialPort serialPort;
- /**
- * @param args
- */
- public static void main( String[] args )
- {
- // getting serial ports list into the array
- // String[] portNames = SerialPortList.getPortNames();
- //
- // if(portNames.length == 0) {
- // System.out.println("There are no serial ports : (Use an emulator such as VSPE, to "
- // + "create a virtual serial port.");
- // System.out.println("Press Enter to exit...");
- //
- // try {
- // System.in.read();
- // }catch(IOException ioe) {
- // ioe.printStackTrace();
- // }
- // return;
- // }
- //
- // for(int i = 0; i < portNames.length; i++) {
- // System.out.println(portNames[i]);
- // }
- // Now read and write.
- SerialPort serialPort = new SerialPort("COM15");
- try {
- serialPort.openPort();
- serialPort.setParams( SerialPort.BAUDRATE_115200,
- SerialPort.DATABITS_8,
- SerialPort.STOPBITS_1,
- SerialPort.PARITY_NONE);
- serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
- serialPort.addEventListener(new PortReader(), SerialPort.MASK_RXCHAR);
- Path path = Paths.get("C:\\Users\\shango\\Desktop\\data_20191125.csv");
- try {
- byte[] bArray = Files.readAllBytes(path);
- // reading content from byte array
- for (int i = 0; i < bArray.length; i++){
- serialPort.writeByte(bArray[i]);
- }
- System.out.println("Message sent.");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- } catch (SerialPortException se) {
- System.out.println("There are errors writing to this port: " + se);
- }finally {
- try {
- serialPort.closePort();
- } catch (SerialPortException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- private static class PortReader implements SerialPortEventListener{
- public void serialEvent(SerialPortEvent event) {
- if(event.isRXCHAR() && event.getEventValue() > 0) {
- try {
- String recData = serialPort.readString(event.getEventValue());
- System.out.println("Received response: " + recData);
- }catch(SerialPortException e) {
- System.out.println("Error in receiving string from COM-port: " + e);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement