2
我有一個發送數據輸入到重量平衡的問題。我需要發送一個ESC P CR LF
命令。我使用RXTX庫。我不知道爲什麼它不起作用。以下是代碼。如何發送命令到串口(JAVA + RXTX)
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();
}
}
}
我試過flush
,但什麼也沒有發生。 我附上下面的完整(修改)代碼。
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();
}
}
}
感謝,但沒有happend。我將完整的代碼附加到我的帖子中。 – jlz
如果您看到連接方法中的所有控制檯消息 - 確保串行端口參數正確,請嘗試重新插入設備並使用其他端口...我無法想到其他任何東西。 無論哪種方式,您應該添加代碼來關閉流,關閉端口並在完成與端口通信後停止讀取/寫入線程。 RXTX非常麻煩,如果您不能正確管理端口和線程,可能會非常輕鬆地使應用程序發生死鎖。 –
我檢查串行端口監視器中的串行端口連接,並在此程序中,當我發送0x1B和0x50都是正確的天平發送到計算機的權重,但在Java沒有發生:S – jlz