How to communicate via com port with jSerialComm using CP210 driver

174 Views Asked by At

I have Silicon Labs CP210 driver to communicate with com port device. The problem is that I am not able to read data from com port device with cp210 driver. The library I have use is jSerialComm in a JavaFX project.

I have written below code to connect, write, and read with com port device, but I am not able read data.

  SerialPort _serialPort;
        SerialPort[] ports;
        ports = SerialPort.getCommPorts();
        _serialPort = SerialPort.getCommPort(ports[0].getSystemPortName());
        System.out.println(ports[0].getSystemPortName());
        _serialPort.setBaudRate(19200);
        _serialPort.setNumDataBits(8);
        _serialPort.setNumStopBits(1);
        _serialPort.setParity(SerialPort.NO_PARITY);
        _serialPort.setDTR();
        _serialPort.setRTS();
        _serialPort.openPort();
        _serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING, 4000, 4000);
//        TimeUnit.SECONDS.sleep(1);
        String hexValue = "CC"; // Example hex value
        byte[] hexBytes = hexStringToByteArray(hexValue);
        _serialPort.writeBytes(hexBytes, 2);
        TimeUnit.SECONDS.sleep(1 / 3);
        String hexValue2 = "AB"; // Example hex value
        byte[] hexBytes2 = hexStringToByteArray(hexValue2);
        _serialPort.writeBytes(hexBytes2, 2);
        TimeUnit.SECONDS.sleep(1 / 3);
        String hexValue3 = "AB"; // Example hex value
        byte[] hexBytes3 = hexStringToByteArray(hexValue3);
        _serialPort.writeBytes(hexBytes3, 2);
        TimeUnit.SECONDS.sleep(1);

        if (_serialPort.bytesAvailable() == -1) {
//                Portname=null;
            _serialPort.closePort();
            return;
        }

        byte[] readBuffer = new byte[_serialPort.bytesAvailable()];
        long read12 = _serialPort.readBytes(readBuffer, readBuffer.length);
        String response = new String(readBuffer).trim();
        System.out.println("res  " + response);
        System.out.println(readBuffer.length);
        String[] response1 = response.split(",");
        welcomeText.setText("Read " + response + " bytes.");
        if (response1[0].equals("BLE_READER")) {
            String[] ab = response.split(",");
            System.out.println("yess logger1  "+ Arrays.toString(ab));
        } else {
            _serialPort.closePort();
//                openporttittle.setText("Open Port : ");
        }

If there any problem in my code ,then please update me.

0

There are 0 best solutions below