2

我已经修改了https://code.google.com/p/java-simple-serial-connector/wiki/jSSC_examples上显示的示例,以显示来自 java 程序的读/写。我可以运行程序,但是我使用 serialPort.writeString("HelloWorld"); 发送的数据;似乎没有在 SerialPortReader 事件类中读取。有人可以指出问题所在吗?

 public class SerialReaderWriter {

static SerialPort serialPort;

public static void main(String[] args) {
    serialPort = new SerialPort("COM1"); 
    try {
        serialPort.openPort();
        serialPort.setParams(9600, 8, 1, 0);
        //Preparing a mask. In a mask, we need to specify the types of events that we want to track.
        //Well, for example, we need to know what came some data, thus in the mask must have the
        //following value: MASK_RXCHAR. If we, for example, still need to know about changes in states 
        //of lines CTS and DSR, the mask has to look like this: SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR
        int mask = SerialPort.MASK_RXCHAR;
        //Set the prepared mask
        serialPort.setEventsMask(mask);
        //Add an interface through which we will receive information about events
        serialPort.addEventListener(new SerialPortReader());

        serialPort.writeString("HelloWorld");
    }
    catch (SerialPortException ex) {
        System.out.println(ex);
    }
}

static class SerialPortReader implements SerialPortEventListener {

    public void serialEvent(SerialPortEvent event) {
        //Object type SerialPortEvent carries information about which event occurred and a value.
        //For example, if the data came a method event.getEventValue() returns us the number of bytes in the input buffer.
        System.out.println(event.getEventType());
        if(event.isRXCHAR()){
            if(event.getEventValue() == 10){
                try {
                    String data= serialPort.readString();
                    System.out.println(data);
                }
                catch (SerialPortException ex) {
                    System.out.println(ex);
                }
            }
        }
        //If the CTS line status has changed, then the method event.getEventValue() returns 1 if the line is ON and 0 if it is OFF.
        else if(event.isCTS()){
            if(event.getEventValue() == 1){
                System.out.println("CTS - ON");
            }
            else {
                System.out.println("CTS - OFF");
            }
        }
        else if(event.isDSR()){
            if(event.getEventValue() == 1){
                System.out.println("DSR - ON");
            }
            else {
                System.out.println("DSR - OFF");
            }
        }
    }
}

}

4

3 回答 3

1

您无法从您写入的同一端口(此处为 COM1)读取数据。我已按照以下步骤使用 JSSC 进行读写。

使用SerialPortMonitor伪造您的串行端口。

  1. 从安装的 SerialPortMonitor 设备从 COM2 发送数据。 在此处输入图像描述

  2. Mode->Spy 将显示您编写的字符串“HelloWorld”并收到字符串“OK”在此处输入图像描述

进行以下修改并检查您的代码:

serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN |
                                 SerialPort.FLOWCONTROL_RTSCTS_OUT);

serialPort.writeBytes("HelloWorld");//Write data to port
PortReader portReader=new PortReader(serialPort)
serialPort.addEventListener(portReader, SerialPort.MASK_RXCHAR);
int[][] eventArray=serialPort.waitEvents()
for (int i = 0; i < eventArray.length; i++) {
    if ((eventArray[i][0] > 0) ) {
    serialPort.eventListener.serialEvent(new SerialPortEvent("COM1", eventArray[i][0], eventArray[i][1])); // wait for the listener event to complete
        }
    }

端口阅读器类:( 您缺少 Override 注释并传入串行端口)

public class PortReader implements SerialPortEventListener{
SerialPort serialPort
public PortReader(){}
public PortReader(SerialPort serialPort){this.serialPort=serialPort}
    @Override
    public void serialEvent(SerialPortEvent event) {
    if(event.isRXCHAR() && event.getEventValue() > 0) {
            try {
                String receivedData = this.serialPort.readString(event.getEventValue());
                System.out.println("Received response: " + receivedData);
                this.serialPort.closePort();//Close serial port
            }
            catch (SerialPortException ex) {
                System.out.println("Error in receiving string from COM-port: " + ex);
                this.serialPort.closePort();//Close serial port
            }
        }
    }

}
于 2016-02-22T06:14:03.323 回答
0

如果您的设备不需要 RTS/CTS 流控制或者您没有完全连接的串行电缆

(仅 RX、TX、GND)

您应该关闭串行通信的数据终端就绪 (dtr) 信号

添加这个

serialPort.setRTS(false); 
serialPort.setDTR(false);

serialPort.addEventListener(new SerialPortReader());
于 2013-08-09T01:33:51.857 回答
0

命令

serialPort.writeString("HelloWorld");

发送字符串

你好世界

来自COM1。您正在实现的 SerialPortReader 类导致 COM1侦听事件类型是 RXCHAR(当 COM1接收到字符时也称为)。

你有串行电缆连接到串行端口吗?

除非您将 COM1 的 RX 和 TX 引脚交叉或有单独的 COM 端口(其 TX 和 RX 分别连接到 COM1 的 RX 和 TX 引脚),否则 SerialPortReader 将永远不会被激活。

于 2013-05-16T19:07:38.133 回答