Advertisement
Guest User

JAVA SE Serial Port Send DATA

a guest
Nov 14th, 2019
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. package com.reapptech.creapptos;
  2.  
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Path;
  6. import java.nio.file.Paths;
  7.  
  8. import jssc.SerialPort;
  9. import jssc.SerialPortEvent;
  10. import jssc.SerialPortEventListener;
  11. import jssc.SerialPortException;
  12.  
  13. /**
  14.  * Hello world!
  15.  *
  16.  */
  17. public class App
  18. {
  19.     // The serial port.
  20.     private static SerialPort serialPort;  
  21.    
  22.     /**
  23.      * @param args
  24.      */
  25.     public static void main( String[] args )
  26.     {
  27.         // getting serial ports list into the array
  28. //        String[] portNames = SerialPortList.getPortNames();
  29. //        
  30. //        if(portNames.length == 0) {
  31. //          System.out.println("There are no serial ports : (Use an emulator such as VSPE, to "
  32. //                  + "create a virtual serial port.");
  33. //          System.out.println("Press Enter to exit...");
  34. //         
  35. //          try {
  36. //              System.in.read();
  37. //          }catch(IOException ioe) {
  38. //              ioe.printStackTrace();
  39. //          }
  40. //          return;
  41. //        }
  42. //        
  43. //        for(int i = 0; i < portNames.length; i++) {
  44. //          System.out.println(portNames[i]);
  45. //        }
  46.        
  47.         // Now read and write.
  48.         SerialPort serialPort = new SerialPort("COM15");
  49.        
  50.         try {
  51.            
  52.             serialPort.openPort();
  53.            
  54.             serialPort.setParams( SerialPort.BAUDRATE_115200,
  55.                                   SerialPort.DATABITS_8,
  56.                                   SerialPort.STOPBITS_1,
  57.                                   SerialPort.PARITY_NONE);
  58.            
  59.             serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
  60.            
  61.            
  62.             serialPort.addEventListener(new PortReader(), SerialPort.MASK_RXCHAR);
  63.            
  64.             Path path = Paths.get("C:\\Users\\shango\\Desktop\\data_20191125.csv");
  65.            
  66.              try {
  67.                     byte[] bArray = Files.readAllBytes(path);
  68.                     // reading content from byte array
  69.                     for (int i = 0; i < bArray.length; i++){
  70.                           serialPort.writeByte(bArray[i]);
  71.                     }
  72.                    
  73.                     System.out.println("Message sent.");
  74.                 } catch (IOException e) {
  75.                     // TODO Auto-generated catch block
  76.                     e.printStackTrace();
  77.                 }  
  78.         } catch (SerialPortException se) {
  79.             System.out.println("There are errors writing to this port: " + se);
  80.         }finally {
  81.             try {
  82.                 serialPort.closePort();
  83.             } catch (SerialPortException e) {
  84.                 // TODO Auto-generated catch block
  85.                 e.printStackTrace();
  86.             }
  87.         }
  88.     }
  89.    
  90.     private static class PortReader implements SerialPortEventListener{
  91.        
  92.         public void serialEvent(SerialPortEvent event) {
  93.             if(event.isRXCHAR() && event.getEventValue() > 0) {
  94.                 try {
  95.                     String recData = serialPort.readString(event.getEventValue());
  96.                     System.out.println("Received response: " + recData);
  97.                    
  98.                 }catch(SerialPortException e) {
  99.                     System.out.println("Error in receiving string from COM-port: " + e);
  100.                 }
  101.             }
  102.            
  103.         }
  104.        
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement