大家好,我有以下问题。我有一个扫描仪,我正在用命令寻址它,并接收一些行。到目前为止,我一直在尝试超级终端,它工作得很好。但现在我需要在我的程序中使用这些行,所以我设置了Java Comm API和RXTX (只是因为我不能将它与Comm API一起使用)。
我已经在论坛上读了很多,但我不能把它带到工作中去。
我的意思是只有3个部分。首先,我设置了端口、InputStream和OutputStream,它们工作得很好。
portList = CommPortIdentifier.getPortIdentifiers();
int i;
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM3")) {
// if (portId.getName().equals("/dev/term/a")) {
try {
serialPort = (SerialPort)
portId.open("SimpleWriteApp", 2000);
} catch (PortInUseException e) {System.out.println(e);}
try {
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
} catch (IOException e) {System.out.println(e);}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_XONXOFF_IN);
} catch (UnsupportedCommOperationException e) {System.out.println(e);}然后我想用outputStream.write(“xYZ”.getByteS())发送一个命令;我想这也应该可以。但是我得不到回复。
响应应该看起来像这个02349234235883挪威克朗,但它只是卡住了。代码如下所示
try {
System.out.println(messageString);
outputStream.write(messageString.getBytes());
BufferedReader portReader = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
String line = portReader.readLine();
System.out.println(line);
if (inputStream != null) inputStream.close();
if (outputStream != null) outputStream.close();
if (serialPort != null) serialPort.close();
} catch (IOException e) {System.out.println(e);} 有人能帮帮我吗?非常感谢您的努力
发布于 2015-02-24 04:49:37
谢谢你的帮助。问题是在添加串行事件和发送命令之间没有足够的时间。在我将线程设置为休眠几秒钟之后,我得到了没有问题的响应。
https://stackoverflow.com/questions/28631108
复制相似问题