Java 写-读-写串行端口
我正在尝试做一个应用程序,使用PC的串行COM端口与微控制器通信 在我的程序中,我首先向微控制器发送一个字符串(“Connect1”)并等待来自它的Ack响应(“Ack1_PIC”) 到目前为止,一切都在进行中 然后我尝试向微控制器发送另一个字符串(“Connect2”),但微控制器没有接收到它,我不知道为什么:Java 写-读-写串行端口,java,serial-port,microcontroller,serial-communication,Java,Serial Port,Microcontroller,Serial Communication,我正在尝试做一个应用程序,使用PC的串行COM端口与微控制器通信 在我的程序中,我首先向微控制器发送一个字符串(“Connect1”)并等待来自它的Ack响应(“Ack1_PIC”) 到目前为止,一切都在进行中 然后我尝试向微控制器发送另一个字符串(“Connect2”),但微控制器没有接收到它,我不知道为什么: portList = gnu.io.CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreEl
portList = gnu.io.CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (gnu.io.CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == gnu.io.CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
write("Connect2"); // -> 2nd Write operation
}
}
System.out.println("Write2");
}
// close port
serialPort.close();
写入方法:
public void write (String messageString)
{
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {
// System.out.println("Setting parameters");
serialPort.setSerialPortParams(115200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
} catch (UnsupportedCommOperationException e) {}
//System.out.println("Ok");
try {
// System.out.println("Writing to serial port");
outputStream.write(messageString.getBytes());
//outputStream.close();
} catch (IOException e) {}
}
简单阅读课:
public class SimpleRead implements Runnable, SerialPortEventListener
{
public InputStream inputStream;
public int size = 50;
public int numBytes = 0;
public byte readBuffer [] = new byte [size];
public SimpleRead()
{
}
public SimpleRead(CommPortIdentifier portId, Enumeration portList, SerialPort serialPort, Thread readThread,int baud,int databits,int stopbits,int parity) {
// Open Port
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {System.out.println(e);}
// open stream
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {System.out.println(e);}
// add event listeners
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {System.out.println(e);}
serialPort.notifyOnDataAvailable(true);
// set serial parameters
try {
serialPort.setSerialPortParams(baud,databits,stopbits,parity);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
} catch (UnsupportedCommOperationException e) {System.out.println(e);}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {System.out.println(e);}
}
// event listener method
public void serialEvent(SerialPortEvent event) {
for(int i = 0; i < size;i++)
readBuffer[i] = 0;
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
try {
numBytes = 0;
while (inputStream.available() > 0) {
// Copy from serial to byte array "readBuffer"
numBytes = inputStream.read(readBuffer);
}
// Close InputStream
//inputStream.close();
//System.out.println(new String(readBuffer));
} catch (IOException e) {System.out.println(e);}
break;
}
}
}
公共类SimpleRead实现可运行的SerialPortEventListener
{
公共输入流输入流;
公共整数大小=50;
公共整数单位=0;
公共字节读取缓冲区[]=新字节[大小];
公共SimpleRead()
{
}
公共SimpleRead(CommPortIdentifier portId、枚举端口列表、SerialPort SerialPort、线程readThread、int波特、int数据位、int停止位、int奇偶校验){
//开放端口
试一试{
serialPort=(serialPort)portId.open(“simpleradapp”,2000);
}catch(portinuseeexception e){System.out.println(e);}
//明流
试一试{
inputStream=serialPort.getInputStream();
}catch(IOException e){System.out.println(e);}
//添加事件侦听器
试一试{
serialPort.addEventListener(此);
}catch(ToomanyListenerException e){System.out.println(e);}
serialPort.notifyOnDataAvailable(true);
//设置串行参数
试一试{
serialPort.setSerialPortParams(波特率、数据位、停止位、奇偶校验);
serialPort.setFlowControlMode(serialPort.FLOWCONTROL\u无);
}catch(不支持的操作异常){System.out.println(e);}
readThread=新线程(此线程);
readThread.start();
}
公开募捐{
试一试{
睡眠(20000);
}catch(InterruptedException e){System.out.println(e);}
}
//事件侦听器方法
public void serialEvent(SerialPortEvent事件){
对于(int i=0;i0){
//从串行复制到字节数组“readBuffer”
numBytes=inputStream.read(readBuffer);
}
//关闭输入流
//inputStream.close();
//System.out.println(新字符串(readBuffer));
}catch(IOException e){System.out.println(e);}
打破
}
}
}
我意识到的另一件事是,如果我连续写两次,我将正确地接收两个字符串
有什么建议吗?不要每次写信都打开端口。在端口初始化方法中打开它一次,然后在退出程序时用下拉方法关闭它。你能发现更多的错误吗?无论如何,谢谢。@user3253391在100毫秒或200毫秒的书写之间设置一些延迟。我有一个类似的问题(不是所有的读/写操作),我不知道原因,但我把睡眠放在它们之间,我的问题就解决了。也许我错过了什么,所以这可能不是一个好方法,但对我来说很有效。
public class SimpleRead implements Runnable, SerialPortEventListener
{
public InputStream inputStream;
public int size = 50;
public int numBytes = 0;
public byte readBuffer [] = new byte [size];
public SimpleRead()
{
}
public SimpleRead(CommPortIdentifier portId, Enumeration portList, SerialPort serialPort, Thread readThread,int baud,int databits,int stopbits,int parity) {
// Open Port
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {System.out.println(e);}
// open stream
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {System.out.println(e);}
// add event listeners
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {System.out.println(e);}
serialPort.notifyOnDataAvailable(true);
// set serial parameters
try {
serialPort.setSerialPortParams(baud,databits,stopbits,parity);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
} catch (UnsupportedCommOperationException e) {System.out.println(e);}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {System.out.println(e);}
}
// event listener method
public void serialEvent(SerialPortEvent event) {
for(int i = 0; i < size;i++)
readBuffer[i] = 0;
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
try {
numBytes = 0;
while (inputStream.available() > 0) {
// Copy from serial to byte array "readBuffer"
numBytes = inputStream.read(readBuffer);
}
// Close InputStream
//inputStream.close();
//System.out.println(new String(readBuffer));
} catch (IOException e) {System.out.println(e);}
break;
}
}
}