[英]Java JSSC rs232 no events
I'm trying to get some values from rs232 (card reader Roger prt66lt) with a java simple serial connetor (jssc) but it look like i dont get any events started. 我正在尝试使用Java简单串行连接器(jssc)从rs232(读卡器Roger prt66lt)中获取一些值,但是看起来我没有启动任何事件。 I'm using the example code. 我正在使用示例代码。 The card reader is set to send card number when he reads it. 读卡器设置为在读卡时发送卡号。 But i dont get any event started coz i dont see event that System.out. 但是我没有启动任何事件,因为我没有看到System.out事件。 I'm including jssc.jar (from 2.5.0 version) and its connecting to divice. 我包括jssc.jar(从2.5.0版本开始)及其与设备的连接。 Is this version of jssc doesnt need any .dll ? 此版本的jssc不需要任何.dll吗? I think its included in it ? 我认为它包含在其中吗? The similar code to this one but with RXTX library works fine but i need to include attahed dll library. 与此类似但具有RXTX库的代码可以正常工作,但我需要包含附加的dll库。
Card reader send me HEX value: 02, 10 chars DATA, 0D, 0A, 03 读卡器向我发送十六进制值:02,10个字符的数据,0D,0A,03
Code: 码:
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
public class Main {
static SerialPort serialPort;
public static void main(String[] args) {
serialPort = new SerialPort("COM4");
try {
serialPort.openPort();//Open port
serialPort.setParams(9600, 8, 1, 0);//Set params
int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;//Prepare mask
serialPort.setEventsMask(mask);//Set mask
serialPort.addEventListener(new SerialPortReader());//Add SerialPortEventListener
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
static class SerialPortReader implements SerialPortEventListener {
public void serialEvent(SerialPortEvent event) {
System.out.println("Event started");
if(event.isRXCHAR()){//If data is available
if(event.getEventValue() == 10){//Check bytes count in the input buffer
//Read data, if 10 bytes available
try {
byte buffer[] = serialPort.readBytes(10);
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
}
else if(event.isCTS()){//If CTS line has changed state
if(event.getEventValue() == 1){//If line is ON
System.out.println("CTS - ON");
}
else {
System.out.println("CTS - OFF");
}
}
else if(event.isDSR()){///If DSR line has changed state
if(event.getEventValue() == 1){//If line is ON
System.out.println("DSR - ON");
}
else {
System.out.println("DSR - OFF");
}
}
}
}
}
The problem was that the default setting of RTS and DTR line is High so i need to use another setParams method and set the RTS line to false as needed by device. 问题是RTS和DTR线路的默认设置为“高”,因此我需要使用另一个setParams方法,并根据设备需要将RTS线路设置为false。
Solution: 解:
serialPort.setParams(9600, 8, 1, 0, false, true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.