我成功地安装并测试了Java。我可以通过串口发送和接收数据。但问题是,在每个字符之前还有19个空格。当我尝试使用超级终端时,多余的空间已经没有了。
例:
+CENG:0,“0021,37,97,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;
}
}
}发布于 2013-08-13 12:53:11
在您的代码中(为了示例目的删除了异常处理):
byte[] readBuffer = new byte[20];
while (inputStream.available() > 0)
{
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));您有一个20字节的缓冲区,并且不管读取了多少数据,您都要从所有20个字节构建一个String。在Java中,String可以包含空值,所以您的字符串最后是一个字符,后面是19个空值。然后,当您打印它时,您的特定控制台将显示空格而不是空值。
有几件事要注意。首先,不太清楚您要用这个循环做什么。不管流上剩下多少字节(例如,如果有400个字节要读取,最多只能读取最后20个字节),您都可以重复读取相同的20字节缓冲区。
其次,为了解决我刚才描述的字符串问题,您只想使用从readBuffer中实际读取的字节,即缓冲区的第一个numBytes字节。
第三,在从原始字节构造String时,应该始终指定字符编码。对于您的数据,us-ascii似乎是合适的。
例如,类似这样的事情:
byte[] readBuffer = new byte[20];
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
System.out.print(new String(readBuffer, 0, numBytes, "us-ascii"));
}https://stackoverflow.com/questions/18201079
复制相似问题