为什么我的Windows COM端口上的串行读取限制为8192字节?

时间:2019-01-22 17:09:48

标签: c windows serial-port usb

我正在尝试从Windows中的COM端口读取20100个字节。数据被截断为8192字节。是什么赋予了?当我使用TeraTerm时,没有截断。我的同步呼叫是:

<ul>
    <li>
      <a href="" data-drupal-link-system-path="<front>">Menu Link 1</a>
    </li>
    <li>
    <a href="" data-drupal-link-system-path="<front>">Menu Link 2</a>                                   <ul>
    <li>
    <a href="" data-drupal-link-system-path="<front>">Submenu-sidebar 1</a>
    </li>
    <li>
        <a href="" data-drupal-link-system-path="<front>">Submenu-sidebar 2</a>
    </li>
    <li>
        <a href="" data-drupal-link-system-path="<front>">Submenu-sidebar 3</a>
    </li>
    </ul>

    </li>
    <li>
        <a href="" data-drupal-link-system-path="<front>">Menu Link 3</a>
    </li>
    <li>
        <a href="" data-drupal-link-system-path="<front>">Menu Link 4</a>
    </li>
    <li>
        <a href="" data-drupal-link-system-path="<front>">Menu Link 5</a>
    </li>
</ul>

ul {
            border: none;
            -webkit-box-shadow: 1px 0px 9px 0 rgba(0,0,0,0.75);
            -moz-box-shadow: 1px 0px 9px 0 rgba(0,0,0,0.75);
            box-shadow: 1px 0px 9px 0 rgba(0,0,0,0.75);
            list-style: none;
            padding: 0;
            margin: 0;
            width: rem(280px);
            li {
                /* Insert SVGs in anchor tags */
                &:nth-child(1) a:before {
                    @extend %svg-sidebar-decorations;
                    background-image: url("../images/picto-info-sant.svg");
                }
                &:nth-child(2) a:before {
                    @extend %svg-sidebar-decorations;
                    background-image: url("../images/picto-info-social.svg");
                }
                &:nth-child(3) a:before {
                    @extend %svg-sidebar-decorations;
                    background-image: url("../images/picto-signalement.svg");
                }
                &:nth-child(4) a:before {
                    @extend %svg-sidebar-decorations;
                    background-image: url("../images/picto-urgence.svg");
                }
                &:nth-child(5) a:before {
                    @extend %svg-sidebar-decorations;
                    background-image: url("../images/picto-antipoison.svg");
                }

%svg-sidebar-decorations {
    content: '';
    display: inline-block;
    width: rem(14px);
    height: rem(16px);
    margin-top: rem(3);
    margin-right: rem(10px);
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center;
}

和(非重叠只读的代码段)

CreateFile(dev, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);

在WriteFile之后立即发生。我迭代调用此代码,直到获得所有数据为止-除了我只有8192个字节。

2 个答案:

答案 0 :(得分:1)

您是否已在SetupComm函数中将8192明确指定为dwInQueue参数,或者设备驱动程序的默认值为8192?

如何指定SetupComm所需的缓冲区大小?

SetupComm function

  

初始化指定通信设备的通信参数。
  语法C ++

BOOL SetupComm(  
  HANDLE hFile,  
  DWORD  dwInQueue,  
  DWORD  dwOutQueue  
);
  

参数
  hFile
  通信设备的句柄。 CreateFile函数返回此句柄。

     

dwInQueue
  设备内部输入缓冲区的建议大小(以字节为单位)。

     

dwOutQueue
  设备内部输出缓冲区的建议大小(以字节为单位)。

对于.NET SerialPort,默认值为4096,最多可以指定2147483647。 SerialPort.ReadBufferSize Property

  

获取或设置SerialPort输入缓冲区的大小。
  C#

[System.ComponentModel.Browsable(true)]  
public int ReadBufferSize { get; set; }  
  

属性值
  Int32
  缓冲区大小,以字节为单位。预设值为4096;最大值是正整数,即2147483647。

答案 1 :(得分:0)

这不仅仅是评论,还是答案。根据公认的答案,增加缓冲区大小将在99.9%的时间内解决您的问题。

但是串行接口(如套接字)只是字节流。因此,人们总是必须面对两个相对的问题:

  • 部分消息
  • 多封邮件

更糟糕的是,您会收到完整的消息,然后是截断的消息。

在Unix和Windows中,处理此问题的标准方法是使用select。例如,请参见https://beej.us/guide/bgnet/

生成的代码并不长,但是您需要知道您想做什么。


编辑:在Windows上,select仅适用于套接字。也许Batch-File: Receive Data from the Serial-Port and write it into txt-File会有所帮助吗?