【问题标题】:Strange results using jssc on raspberry-pi在树莓派上使用 jssc 的奇怪结果
【发布时间】:2016-08-11 15:04:47
【问题描述】:

我有一个树莓派使用 USB 串行电缆 HL-340 连接到串行端口设备(pl-2303 也经过测试)

设备接收 1-byte 命令并应以 2 个字节 回复(好的,这是一个非常简单的设备和非常易于学习的协议 :))

我的代码:

[...]
        final int[] status = this.serialPort.getLinesStatus ( );
        final StringBuilder sb = new StringBuilder ( "Lines Status:" );
        final String[] name = new String[] { "CTS", "DSR", "RING", "RLSD" };
        for ( int i = 0; i < status.length; i++ )
          sb.append ( ( i > 0 ) ? "," : "" ).append ( name [i] ).append ( ':' ).append ( status [i] );
        System.out.println ( sb.toString ( ) );
        System.out.println ( "flowcontrol:" + this.serialPort.getFlowControlMode ( ) );
        this.serialPort.purgePort ( SerialPort.PURGE_RXCLEAR | SerialPort.PURGE_TXCLEAR );

        synchronized ( this )
        {
          System.out.println ( "1) getInputBufferBytesCount():" + this.serialPort.getInputBufferBytesCount ( ) );
          this.serialPort.addEventListener ( this );
          this.serialPort.setEventsMask ( SerialPort.MASK_RXCHAR );
          this.serialPort.writeBytes ( COMMAND );
          wait ( 3000 ); // wait 3 seconds for reply
          this.serialPort.removeEventListener ( );
          System.out.println ( "2) getInputBufferBytesCount():" + this.serialPort.getInputBufferBytesCount ( ) );
          final int n = this.serialPort.getInputBufferBytesCount ( );
          if ( n > 0 )
          {
            this.serialPort.readBytes ( n ); // purge garbage data?
            System.out.println ( "3) getInputBufferBytesCount():" + this.serialPort.getInputBufferBytesCount ( ) );
          }
        }  
        if ( ( this.data == null ) && ( this.error == null ) )
          this.error = "no response from device";
[...]

以及回调代码:

@Override
public final void serialEvent ( final SerialPortEvent event )
{
  synchronized ( this )
  {
    try
    {
      final int val;
      if ( event.isRXCHAR ( ) )
        if ( (val = event.getEventValue ( )) == 2 ) // the reply is 2 bytes
          this.data = this.serialPort.readBytes ( 2 );
        else
          this.error = "unexpected value:" + val;
      else
        this.error = "unexpected event:" + event;
    }
    catch ( final Throwable x )
    {
      this.error = x.toString ( );
    }
    finally
    {
      notify ( );
    }
  }
}

结果非常奇怪且不可预测 :(我希望设备的回复只有 2 个字节,但串行端口会通知我更多可供读取的数据:

Lines Status:CTS:0,DSR:0,RING:0,RLSD:0
flowcontrol:0
1) getInputBufferBytesCount():0
2) getInputBufferBytesCount():544
3) getInputBufferBytesCount():0
java.lang.Throwable: unexpected value:192

Lines Status:CTS:0,DSR:0,RING:0,RLSD:0
flowcontrol:0
1) getInputBufferBytesCount():0
2) getInputBufferBytesCount():512
3) getInputBufferBytesCount():0
java.lang.Throwable: unexpected value:192

Lines Status:CTS:0,DSR:0,RING:0,RLSD:0
flowcontrol:0
1) getInputBufferBytesCount():0
2) getInputBufferBytesCount():32
3) getInputBufferBytesCount():0
java.lang.Throwable: unexpected value:32

有什么问题?我应该更改哪些端口设置?

【问题讨论】:

    标签: java serial-port raspberry-pi usb jssc


    【解决方案1】:

    假设您正在等待 2 个字节的回复,现在在收到回复后打印缓冲区中的所有字节,以查看这是垃圾数据(特殊的不可打印字符)还是有效数据。这将为您提供更多洞察力和下一步行动以进一步取得进展。

    【讨论】:

      猜你喜欢
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 2017-03-12
      • 2023-03-07
      • 1970-01-01
      • 2014-09-21
      • 1970-01-01
      相关资源
      最近更新 更多