首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在raspberry-pi上使用jssc的奇怪结果

在raspberry-pi上使用jssc的奇怪结果
EN

Stack Overflow用户
提问于 2016-08-11 15:04:47
回答 1查看 400关注 0票数 0

我有一个覆盆子pi连接到一个串口设备使用usb-串行电缆HL-340 (pl-2303也测试)

该设备采用1字节命令,并应使用2字节(好,这是非常简单的设备和非常容易学习的协议:)。

我的代码:

代码语言:javascript
复制
[...]
        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";
[...]

以及回调代码:

代码语言:javascript
复制
@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个字节作为设备的答复,但是串口会通知我更多可用的数据可供阅读:

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

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

EN

回答 1

Stack Overflow用户

发布于 2016-10-07 07:00:50

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38900011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档