-1

I am trying to send data through COM-port via SerialPort class and receive data through COM-port, but having this problem.........................................................................................................

Thank you.

import jssc.SerialPort;
import jssc.SerialPortException;
import jssc.*;

public class writeValues {


    public static void main(String[] args) {
        SerialPort serialPort = new SerialPort("COM1");
        try {
            //Open port
            String T;
            serialPort.openPort();
            serialPort.setParams(2400,7,2,1, false, true);
            String bytes = serialPort.readString(); //Reads 10 bytes from serial port
            serialPort.addEventListener(new PortReader(), SerialPort.MASK_RXCHAR);
            serialPort.writeString("connected.");
            //serialPort.closePort();
        }
        catch (SerialPortException ex) {
            System.out.println(ex);
        }
    }

    public static class PortReader implements SerialPortEventListener {

        @Override
        public void serialEvent(SerialPortEvent event) {
            if(event.isRXCHAR() && event.getEventValue() > 0) {
                try {
                    String receivedData = SerialPort.readString(event.getEventValue());
                    System.out.println("Received response: " + receivedData);
                }
                catch (SerialPortException ex) {

                    System.out.println("Error in receiving string from COM-port: " + ex);
                }
            }
        }

    }

}
  • 1
    Problem is here: `String receivedData = SerialPort.readString(event.getEventValue());`. You calling `readString()` on the class (not instance). That is not allowed for non-static methods. In `main()` you do the same properly. – PM 77-1 Jun 29 '15 at 23:10

1 Answers1

0

As the SerialPort class is not static - (you called serialPort = new SerialPort() right?) you need to call readstring on that instance 'serialPort' rather than the class SerialPort. What you could do is change to

public class writeValues {

public static SerialPort serialPort;
public static void main(String[] args) {
    serialPort = new SerialPort("COM1");
    try {
        //Open port
        String T;
        serialPort.openPort();
        serialPort.setParams(2400,7,2,1, false, true);
        String bytes = serialPort.readString(); //Reads 10 bytes from serial port
        serialPort.addEventListener(new PortReader(), SerialPort.MASK_RXCHAR);
        serialPort.writeString("connected.");
        //serialPort.closePort();
    }
    catch (SerialPortException ex) {
        System.out.println(ex);
    }
}

public static class PortReader implements SerialPortEventListener {

    @Override
    public void serialEvent(SerialPortEvent event) {
        if(event.isRXCHAR() && event.getEventValue() > 0) {
            try {
                String receivedData = serialPort.readString(event.getEventValue());
                System.out.println("Received response: " + receivedData);
            }
            catch (SerialPortException ex) {

                System.out.println("Error in receiving string from COM-port: " + ex);
            }
        }
    }

}

so now we refer to the instance of serialPort rather than a static class SerialPort. Hope that helps

Mr Pie
  • 327
  • 2
  • 11