Guest User

Untitled

a guest
Sep 18th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. Com port don't work in java [closed]
  2. public static void main(String[] args) {
  3.  
  4. SerialPort serialPort = new SerialPort("COM1");
  5. try {
  6.  
  7. serialPort.openPort();
  8. serialPort.setParams(9600, 8, 1, 0);
  9. serialPort.setParams(SerialPort.BAUDRATE_9600,
  10. SerialPort.DATABITS_8,
  11. SerialPort.STOPBITS_1,
  12. SerialPort.PARITY_NONE);
  13.  
  14. serialPort.writeBytes("$KE");
  15.  
  16.  
  17.  
  18. byte[] buffer = serialPort.readBytes(10);
  19. //Закрываем порт
  20. serialPort.closePort();
  21. }
  22. catch (SerialPortException ex) {
  23. System.out.println(ex);
  24. }
  25. }
  26. }
  27.  
  28. static CommPortIdentifier portId;
  29. static Enumeration portList;
  30.  
  31. InputStream inputStream;
  32. SerialPort serialPort;
  33. Thread readThread;
  34.  
  35. portList = CommPortIdentifier.getPortIdentifiers();
  36.  
  37. while (portList.hasMoreElements()) {
  38. portId = (CommPortIdentifier) portList.nextElement();
  39. if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
  40. if (portId.getName().equals("COM1")) {
  41. // if (portId.getName().equals("/dev/ttyUSB0")) {
  42. System.out.println("Connected");
  43.  
  44.  
  45. }
  46. }
  47. }
Add Comment
Please, Sign In to add comment