Java com端口读写问题

时间:2017-01-23 08:11:24

标签: java serial-communication

首先让我介绍一下我的程序。我的应用程序纯java并使用comm jar for com port。我已经添加了

  1. win32com.dll - jre/bin
  2. comm.jar - jre/lib/ext
  3. java.comm.properties - jre/liv
  4. 以下是我的程序

    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();
            }
        }
    }
    

    输出

    Port COM3
    Port COM1
    ====================
    COM1 found
    [B@1a52fdf  Successfully Sent!
    
    ====================
    Port LPT1
    Port LPT2
    

    但是我的预期输出是

    COM1 found
    [B@1a52fdf  Successfully Sent!
    abc
    ====================
    Port LPT1
    Port LPT2
    

    在程序中,我不确定是否发送了“abc”,并且在输出中我看不到。请帮助我是否需要添加或更改。提前谢谢!

1 个答案:

答案 0 :(得分:0)

以下语句中的messageString.getBytes()返回一个字节数组而不是字符串本身:

System.out.println(messageString.getBytes() + "  Successfully Sent!");

这就是为什么你得到一个[B@1a52fdf而不是消息本身。

当然,当您发送数据时,您发送一个字节数组,但如果您希望查看以字符串形式发送的内容,可以使用以下内容:

System.out.println(messageString + "  Successfully Sent!");

此外,语句System.out.println("Port " + portId.getName());打印每个端口名称;如果您只希望显示根据您的条件找到的端口的端口名称(如您所需输出中缺少Port COM3Port COM1),则可以省略此语句。