Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Com port don't work in java [closed]
- public static void main(String[] args) {
- SerialPort serialPort = new SerialPort("COM1");
- try {
- serialPort.openPort();
- serialPort.setParams(9600, 8, 1, 0);
- serialPort.setParams(SerialPort.BAUDRATE_9600,
- SerialPort.DATABITS_8,
- SerialPort.STOPBITS_1,
- SerialPort.PARITY_NONE);
- serialPort.writeBytes("$KE");
- byte[] buffer = serialPort.readBytes(10);
- //Закрываем порт
- serialPort.closePort();
- }
- catch (SerialPortException ex) {
- System.out.println(ex);
- }
- }
- }
- static CommPortIdentifier portId;
- static Enumeration portList;
- InputStream inputStream;
- SerialPort serialPort;
- Thread readThread;
- portList = CommPortIdentifier.getPortIdentifiers();
- while (portList.hasMoreElements()) {
- portId = (CommPortIdentifier) portList.nextElement();
- if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
- if (portId.getName().equals("COM1")) {
- // if (portId.getName().equals("/dev/ttyUSB0")) {
- System.out.println("Connected");
- }
- }
- }
Add Comment
Please, Sign In to add comment