[英]Java com port read and write issue
Firstly let me introduce my program. 首先让我介绍一下我的程序。 My application pure java and use comm jar for com port.
我的应用程序纯java并使用comm jar for com port。 I already added
我已经添加了
win32com.dll
- jre/bin
win32com.dll
- jre/bin
comm.jar
- jre/lib/ext
comm.jar
- jre/lib/ext
java.comm.properties
- jre/liv
java.comm.properties
- jre/liv
The following is my program 以下是我的计划
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
public class PortReaderWriter
{
static Enumeration<?> ports;
static CommPortIdentifier portId;
static InputStream inputStream;
static OutputStream outputStream;
static SerialPort serialPort;
static String messageString = "abc";
public static void main(String[] args) throws Exception
{
try
{
ports = CommPortIdentifier.getPortIdentifiers();
while (ports.hasMoreElements())
{
portId = (CommPortIdentifier) ports.nextElement();
System.out.println("Port " + portId.getName());
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
if (portId.getName().equals("COM1"))
{
System.out.println("====================");
System.out.println("COM1 found");
serialPort = (SerialPort) portId.open("PortReaderWriter", 2000);
serialPort
.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
outputStream = serialPort.getOutputStream();
outputStream.write(messageString.getBytes());
System.out.println(messageString.getBytes() + " Successfully Sent!");
inputStream = serialPort.getInputStream();
byte[] readBuffer = new byte[2048];
while (inputStream.available() > 0)
{
int numBytes = inputStream.read(readBuffer);
System.out.println("numBytes " + numBytes);
}
System.out.print(new String(readBuffer));
outputStream = serialPort.getOutputStream();
outputStream.write(messageString.getBytes());
System.out.println(messageString.getBytes() + " Successfully Sent!");
System.out.println("====================");
}
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
The output is 输出是
Port COM3
Port COM1
====================
COM1 found
[B@1a52fdf Successfully Sent!
====================
Port LPT1
Port LPT2
But My expected output is 但我的预期产量是
COM1 found
[B@1a52fdf Successfully Sent!
abc
====================
Port LPT1
Port LPT2
In the program, I am not sure "abc" is sent or not and in the output I can not see. 在程序中,我不确定是否发送“abc”,并且在输出中我看不到。 Please help me is there any required to add or change.
请帮助我是否需要添加或更改。 Thanks in advance!
提前致谢!
The messageString.getBytes()
in the following statement returns a byte array instead of the string itself : 以下语句中的
messageString.getBytes()
返回一个字节数组而不是字符串本身:
System.out.println(messageString.getBytes() + " Successfully Sent!");
and that is why you get a [B@1a52fdf
instead of the message itself. 这就是为什么你得到一个
[B@1a52fdf
而不是消息本身。
Of course, when you send the data you send a byte array, but if you wish to see what was sent in a String form, you could use the below : 当然,当您发送数据时,您发送一个字节数组,但如果您希望查看以String形式发送的内容,可以使用以下内容:
System.out.println(messageString + " Successfully Sent!");
Additionally, the statement System.out.println("Port " + portId.getName());
另外,语句
System.out.println("Port " + portId.getName());
prints every Port name; 打印每个端口名称; if you only wish to show the port name for ports that have been found as per your criteria (as the
Port COM3
and Port COM1
ar missing from your desired output), you could omit this statement. 如果您只希望显示根据您的条件找到的端口的端口名称(因为您所需的输出中缺少
Port COM3
和Port COM1
),则可以省略此语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.