[英]Can not read data from serial port
我從串行端口讀取時遇到問題,我正在嘗試使用下面的代碼從串行端口讀取和寫入,我運行程序並在控制台中輸入一些數據,然后我可以寫入串行端口。 我正在使用“Free Device Monitoring Studio”應用程序來觀察串行端口的行為。 但是當我運行我的程序時,它不會讀取任何數據。 這是我從這個鏈接得到的程序:
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* This version of the TwoWaySerialComm example makes use of the
* SerialPortEventListener to avoid polling.
*
*/
public class TwoWaySerialComm
{
public TwoWaySerialComm()
{
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
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
if ( commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
OutputStream out = serialPort.getOutputStream();
(new Thread(new SerialWriter(out))).start();
InputStream in = serialPort.getInputStream();
// serialPort.notifyOnDataAvailable(true);
serialPort.addEventListener(new SerialReader(in));
serialPort.notifyOnDataAvailable(true);
}
else
{
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
public static class SerialWriter implements Runnable
{
OutputStream out;
public SerialWriter ( OutputStream out )
{
this.out = out;
}
public void run ()
{
try
{
int c = 0;
while ( ( c = System.in.read()) > -1 )
{
this.out.write((byte)c);
System.out.println((byte)c);
}
}
catch ( IOException e )
{
e.printStackTrace();
System.exit(-1);
}
}
}
/**
* Handles the input coming from the serial port. A new line character
* is treated as the end of a block in this example.
*/
public static class SerialReader implements SerialPortEventListener
{
private InputStream in;
private byte[] buffer = new byte[1024];
public SerialReader ( InputStream in )
{
this.in = in;
}
public void serialEvent(SerialPortEvent arg0) {
int data;
try
{
data = in.read();
int len = 0;
while ( in.available()>0 )
{
if ( data == '\n' ) {
break;
}
buffer[len++] = (byte) data;
}
System.out.print("read"+new String(buffer,0,len));
}
catch ( IOException e )
{
System.out.println(e.getMessage());
System.out.println("error");
e.printStackTrace();
}
}
}
/** */
public static void main ( String[] args )
{
try
{
(new TwoWaySerialComm()).connect("COM5");
}
catch ( Exception e )
{
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print(e.toString());
}
}
}
我在 serialEvent(SerialPortEvent arg0) 方法中的 While 循環中放置了一個斷點,
while ( ( data = in.read()) > -1 )
但它永遠不會到達斷點。 它無法讀取數據,因此它不會使用以下行在控制台中寫入內容:
System.out.print("read"+new String(buffer,0,len));
我需要一些幫助才能知道問題出在哪里。
您不應該在事件處理程序中讀取流的結尾。 這是您應該循環while (in.available() > 0)
的罕見情況之一。
編輯嘆息。
int len = 0;
while (in.available() > 0)
{
data = in.read();
if ( data == -1 || data == '\n' ) {
break;
}
buffer[len++] = (byte) data;
}
我自己找到了答案。 我的錯誤是我通過串口向我的電路板發送了一些無意義的東西,當我發送一些有意義的東西時,它也在從串口讀取。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.