所以我试图使用JSSC库向我的设备发送命令,但还没有成功。这是我的代码,从几个在线示例构建:
static SerialPort tPort = new SerialPort("/dev/ttyUSB0");
public static void main(String[] args) {
String[] portNames = SerialPortList.getPortNames();
for (String tPortName : portNames) {
System.out.println(tPortName);
}
try {
tPort.openPort();
tPort.setParams(
SerialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
tPort.setFlowControlMode(SerialPort.FLOWCONTROL_XONXOFF_IN|SerialPort.FLOWCONTROL_XONXOFF_OUT);
int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR; //Prepare mask
tPort.writeBytes("AT".getBytes()); //Write data to port
tPort.setEventsMask(mask); //Set mask
tPort.addEventListener(new SerialPortReader()); //Add SerialPortEventListener
} catch (SerialPortException ex) {
ex.printStackTrace(System.out);
}
}
static class SerialPortReader implements SerialPortEventListener {
public void serialEvent(SerialPortEvent event) {
if(event.isRXCHAR()){//If data is available
System.out.println("available!");
if(event.getEventValue() == 10){//Check bytes count in the input buffer
//Read data, if 10 bytes available
try {
byte buffer[] = tPort.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");
}
}
}
}
当我运行它时,我得到的唯一输出是
CTS - OFF
DSR - OFF
没有明显的设备响应。但是当我使用minicom使用这个配置时
lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk
x A - Serial Device : /dev/ttyUSB0 x
x B - Lockfile Location : /var/lock x
x C - Callin Program : x
x D - Callout Program : x
x E - Bps/Par/Bits : 9600 8N1 x
x F - Hardware Flow Control : No x
x G - Software Flow Control : No x
x x
x Change which setting? x
mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj
设备返回一些东西。
我做错了什么?
编辑:我尝试使用超时方法读取,并且该方法继续抛出超时。
答案 0 :(得分:0)
至少应该通过addEventListener
writeBytes.
发送电话号码.setFlowControl(SerialPort.FLOWCONTROL_NONE)
如果您想要与minicom
完全相同的设置,还应设置$output
。
答案 1 :(得分:0)
显然我错过了规范,为了被认为是消息,输入字符串必须以CR结尾。添加CR字符后,设备已返回该内容。愚蠢的错误(doh)。