我成功安装并测试了 Java Comm API。我可以通过串口发送和接收数据。但问题是,每个字符前有 19 个额外的空格。当我尝试使用超级终端时,没有多余的空格。
例如:
+CENG:0,"0021,37,99,404,46,36,0000,05,05,77,255"
是期待。但是Java程序在每个字符之前输出空格
+ E N
代码是:
import java.io.*;
import java.util.*;
import javax.comm.*;
public class SimpleRead implements Runnable, SerialPortEventListener
{
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args) throws Exception
{
portList = CommPortIdentifier.getPortIdentifiers();
System.out.println("SimpleRead Started.");
while (portList.hasMoreElements())
{
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
System.out.println ("Found " + portId.getName());
if (portId.getName().equals("COM5"))
{
OutputStream outputStream;
SerialPort writePort = (SerialPort) portId.open("SimpleWriteApp", 2000);
writePort.setSerialPortParams(2400,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
outputStream = writePort.getOutputStream();
outputStream.write("AT+CENG=2".getBytes());
writePort.close();
SimpleRead reader = new SimpleRead();
}
}
}
}
public SimpleRead()
{
try
{
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
}
catch (PortInUseException e)
{
e.printStackTrace();
}
try
{
inputStream = serialPort.getInputStream();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
serialPort.addEventListener(this);
}
catch (TooManyListenersException e)
{
e.printStackTrace();
}
serialPort.notifyOnDataAvailable(true);
try
{
serialPort.setSerialPortParams(2400, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e)
{
e.printStackTrace();
}
readThread = new Thread(this);
readThread.start();
}
public void run()
{
try
{
Thread.sleep(20000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
public void serialEvent(SerialPortEvent event)
{
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:
byte[] readBuffer = new byte[20];
try
{
while (inputStream.available() > 0)
{
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
}
catch (IOException e)
{
e.printStackTrace();
}
break;
}
}
}