java-simple-serial-connector 读/写服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16303585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java-simple-serial-connector read/write server
提问by Ayub
I have modified the example shown on https://code.google.com/p/java-simple-serial-connector/wiki/jSSC_examplesto show read/write from java program. I can run the program, however the data I send using serialPort.writeString("HelloWorld"); does not seem to be read in the SerialPortReader event class. Could any one please point what the issue is ?
我修改了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");
}
}
}
}
}
}
回答by sandhya murugesan
You can't read data from the same port where you write(COM1 here). I have followed the below steps for reading and writing using JSSC.
您无法从写入的同一端口读取数据(此处为 COM1)。我已按照以下步骤使用 JSSC 进行读写。
Fake your serial port with SerialPortMonitor.
用SerialPortMonitor伪造你的串口。
Send data from COM2 from the SerialPortMonitor device installed.

Mode->Spy would show your written string "HelloWorld" and received String "OK"

Make the below modifications and check your code:
进行以下修改并检查您的代码:
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
}
}
The port reader class:(You were missing the Override annotation and passing in the serial port)
端口读取器类:(您缺少 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
}
}
}
}
回答by onoitsjon
The command
命令
serialPort.writeString("HelloWorld");
sendsthe string
发送字符串
HelloWorld
你好世界
fromCOM1. The SerialPortReader class that you are implementing causes COM1 to listenfor the event type isRXCHAR(AKA when COM1 receivesa char).
从COM1。您正在实现的 SerialPortReader 类使 COM1侦听事件类型isRXCHAR(当 COM1接收到一个字符时也称为)。
Do you have a serial cable connected to the serial port?
您是否有连接到串行端口的串行电缆?
Unless you cross the RX and TX pins of COM1 or have a separate COM port (whose TX and RX is connected to COM1's RX and TX pins respectively), the SerialPortReader will never be activated.
除非您将 COM1 的 RX 和 TX 引脚交叉或有一个单独的 COM 端口(其 TX 和 RX 分别连接到 COM1 的 RX 和 TX 引脚),否则 SerialPortReader 将永远不会被激活。
回答by astral
if your device doesn't require RTS/CTS flow control or you dont have a fully connected serial cable
如果您的设备不需要 RTS/CTS 流量控制或您没有完全连接的串行电缆
( only RX, TX, GND)
(仅RX、TX、GND)
you should switch off the data terminal ready (dtr) signal for the serial communication
您应该关闭串行通信的数据终端就绪 (dtr) 信号
add this
添加这个
serialPort.setRTS(false);
serialPort.setDTR(false);
after
后
serialPort.addEventListener(new SerialPortReader());

