質問

I am working on a project that involves an embedded device. I am expected to send a command to this device. This command is a string: "LAMP1_ON\r\n";

I am using RXTX with java and sending the data via serial port. But when i send the String command, the device receives "AMP_N" and some other scattered string.

I have no idea why it is so, and how i can fix it.

My code is below:

public class SerialWriter implements Runnable {

    OutputStream out;
    String str;
    public SerialWriter(OutputStream out, String str) {
        this.out = out;
        this.str = str;
    }

    public void run() {
        try {

            byte[] array = this.str.getBytes();

                this.out.write(array);
                this.out.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I expect to send the string to the port via the write method. It works but it doesn't send the exact String that is contained in this.str instead it sends "AMP_N" and "AMP" and scattered Strings

役に立ちましたか?

解決

I have seen the problem. I didn't set the FLOWCONTROL property correctly. Thank you. This code works fine

serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top