【发布时间】:2015-02-20 14:18:05
【问题描述】:
大家好,我有以下问题。 我有一个扫描仪,我正在处理命令并接收一些行。 到目前为止,我尝试使用完美运行的超级终端。但现在我的程序中需要这些行,所以我设置了 Java Comm API 和 RXTX(只是因为我不能让它与 Comm API 一起工作)。
我已经在论坛上阅读了很多内容,但我无法将其用于工作。
我的意思是只有 3 个部分。 首先,我设置了 Port、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);}
有人可以帮助我吗? 非常感谢您的努力
【问题讨论】:
-
“但它只是卡住”不是错误描述,请查看stackoverflow.com/help/mcve,尤其是stackoverflow.com/help/how-to-ask
-
我没有收到任何错误,而且我希望控制台为空白的行也没有
-
您的扫描仪是否使用任何类型的握手? (XON/XOFF,硬件,...)您应该能够在 HyperTerm 中确定这一点。此外,您是否尝试过将串口上的 Tx 循环回 Rx(使用回形针),并且您的代码应该收到自己的命令作为响应。
-
是的,它需要 xon/xoff
标签: java inputstream outputstream java-communication-api