如何向串口发送命令(JAVA + RXTX)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19682734/
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
How to send command to serial port (JAVA + RXTX)
提问by jlz
I have a problem with sending data input to a weight balance. I need to send a ESC P CR LF
command. I use the RXTX lib. I dont know why it does not work.
我在将数据输入发送到重量天平时遇到问题。我需要发送ESC P CR LF
命令。我使用 RXTX 库。我不知道为什么它不起作用。
Here is the code:
这是代码:
public static class SerialWriter implements Runnable {
OutputStream out;
public SerialWriter ( OutputStream out ) {
this.out = out;
}
public void run () {
try {
while (true) {
this.out.write(new byte[]{0x1B, 0x50, 0x0D, 0x0A});
Thread.sleep(1000);
}
} catch ( IOException | InterruptedException e ) {
e.printStackTrace();
}
}
}
I tried with flush
but nothing happened.
我试过了,flush
但什么也没发生。
I attached the full (modified) code below.
我在下面附上了完整的(修改过的)代码。
package model;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FirstSteps {
public FirstSteps() {
super();
}
void connect (String portName) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Error: Port is currently in use");
} else {
System.out.println("Connect 1/2");
CommPort commPort = portIdentifier.open(this.getClass().getName(),6000);
if (commPort instanceof SerialPort) {
System.out.println("Connect 2/2");
SerialPort serialPort = (SerialPort) commPort;
System.out.println("BaudRate: " + serialPort.getBaudRate());
System.out.println("DataBIts: " + serialPort.getDataBits());
System.out.println("StopBits: " + serialPort.getStopBits());
System.out.println("Parity: " + serialPort.getParity());
System.out.println("FlowControl: " + serialPort.getFlowControlMode());
serialPort.setSerialPortParams(4800,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_ODD);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);
System.out.println("BaudRate: " + serialPort.getBaudRate());
System.out.println("DataBIts: " + serialPort.getDataBits());
System.out.println("StopBits: " + serialPort.getStopBits());
System.out.println("Parity: " + serialPort.getParity());
System.out.println("FlowControl: " + serialPort.getFlowControlMode());
InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();
(new Thread(new SerialReader(in))).start();
(new Thread(new SerialWriter(out))).start();
} else {
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
public static class SerialReader implements Runnable {
InputStream in;
public SerialReader(InputStream in) {
this.in = in;
}
public void run() {
byte[] buffer = new byte[1024];
int len = -1;
try {
while ((len = this.in.read(buffer)) > -1) {
//System.out.println("Received a signal.");
System.out.print(new String(buffer,0,len));
}
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
public static class SerialWriter implements Runnable {
OutputStream out;
public SerialWriter(OutputStream out) {
this.out = out;
}
public void run() {
try {
byte[] array = {0x1B, 0x50, 0x0D, 0x0A};
while (true) {
this.out.write(new byte[]{0x1B, 0x50, 0x0D, 0x0A});
this.out.flush();
Thread.sleep(1000);
}
} catch ( IOException | InterruptedException e ) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
(new FirstSteps()).connect("COM7");
} catch ( Exception e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
采纳答案by jlz
Based on the provided code the only suggestion I could give is to try flushing the stream (out.flush()) after putting data in it.
根据提供的代码,我可以给出的唯一建议是在将数据放入流后尝试刷新流 (out.flush())。
Other than that - I'd need to see the code that gets the output stream.
除此之外 - 我需要查看获取输出流的代码。
回答by Hans Pour
Adding this line:
添加这一行:
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);