java-simple-serial-connector读/写服务器

时间:2013-04-30 15:29:50

标签: java serial-port

我修改了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");
            }
        }
    }
}

}

3 个答案:

答案 0 :(得分:1)

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

使用SerialPortMonitor伪造您的串口。

  1. 从安装的SerialPortMonitor设备发送COM2数据。 enter image description here

  2. 模式 - >间谍会显示您的书面字符串“HelloWorld”并收到字符串“OK”enter image description here

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

    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
                }
            }
        }
    
    }
    

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

    using UnityEngine;
    using UnityEngine.EventSystems;
    using System.Collections;
    
    [RequireComponent(typeof(AudioSource))]
    public class Kim : MonoBehaviour {
    
        public GameObject kkhair;
        public AudioSource audio;
    
        void Start() {
            audio = GetComponent<AudioSource>();
        }
    
        public void speakKim() {
            audio.Play();
        }
    }
    

答案 1 :(得分:0)

命令

serialPort.writeString("HelloWorld");

发送字符串

  

的HelloWorld

来自 COM1的

。您正在实现的SerialPortReader类导致COM1 监听以查找事件类型 isRXCHAR (当COM1 接收字符时为AKA)。

您是否有串行电缆连接到串口?

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

答案 2 :(得分:0)

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

(仅限RX,TX,GND)

你应该关掉准备好的数据终端(dtr) 用于串行通信的信号

添加此

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

之后

serialPort.addEventListener(new SerialPortReader());