需要帮助在java中显示极点显示上的消息

时间:2011-01-05 13:25:02

标签: java serial-port

我正在使用带有以下设置的PL200杆式显示器     *字符类型:美国/欧洲(默认)     *命令模式:EPSON(默认)Baud     *费率:9600,n,8,1(默认?)     * Passthru无(默认)

每次运行我的代码时显示都会消失,我收到异常消息,例如“设备无法识别此命令。”

我想我没有得到正确的命令,任何人都可以提供示例代码写入极点显示器吗?

代码......

try
    {
    CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM1");
        if (portIdentifier.isCurrentlyOwned())
        {
            System.out.println("Port in use!");
        }
        else {
        System.out.println(portIdentifier.getName());

        SerialPort serialPort = (SerialPort) portIdentifier.open("ListPortClass", 300);
        int b = serialPort.getBaudRate();
        System.out.println(Integer.toString(b));
        serialPort.setSerialPortParams(300, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        OutputStream mOutputToPort = serialPort.getOutputStream();
        InputStream mInputFromPort = serialPort.getInputStream();
        String mValue = "AT\r";
        System.out.println("beginning to Write . \r\n");
        mOutputToPort.write(mValue.getBytes());
        System.out.println("AT Command Written to Port. \r\n");
        mOutputToPort.flush();
        System.out.println("Waiting for Reply \r\n");
        Thread.sleep(500);
        byte mBytesIn [] = new byte[20];
        mInputFromPort.read(mBytesIn);
        mInputFromPort.read(mBytesIn);
        String value = new String(mBytesIn);
        System.out.println("Response from Serial Device: "+value);
        mOutputToPort.close();
        mInputFromPort.close();
    }
    catch (Exception ex)
    {
        System.out.println("Exception : " + ex.getMessage());
    }

1 个答案:

答案 0 :(得分:2)

您的波特率可能不正确。该器件可在9600或19200波特上工作,但您已将端口速率设置为300波特。

我希望这样的一行:

serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, 
    SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

我从this resource获取了我的智慧 - 从未使用过此设备。据我了解,命令是:

new byte[]{0x0C}  // clear display
new byte[]{0x1f, 0x24, 0x01, 0x02};  // move cursor to column 1, row 2 (example)
相关问题