我正在尝试使用我的PC的SERIAL COM PORT进行与微控制器通信的应用程序。
在我的程序中,我首先向微控制器发送一个字符串(" Connect1")并等待Ack响应(" Ack1_PIC")。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
SimpleRead reader = new SimpleRead();
// Get list of COM ports
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")) {
openPort();
write("Connect1"); // -> 1st Write operation
}
}
System.out.println("\nWrite1");
}
serialPort.close();
// Get list of COM ports
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")) {
System.out.println("Read1");
reader = new SimpleRead(portId,portList,serialPort,readThread,115200,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); // / -> 1st Read operation
}
}
// Waiting for the desired String
while(!strCompare(reader.readBuffer,"Ack1_PIC"));
System.out.println(new String(reader.readBuffer));
}
serialPort.close();
到目前为止一切正常。
然后我尝试向微控制器发送另一个字符串(" Connect2"),但微控制器没有收到它,我不知道原因:
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;
}
}
}
我意识到的另一件事是,如果我连续2次写入,我会正确接收两个字符串。
有什么建议吗?