Lynntox
V2EX  ›  C#

C#串口通信问题 求教

  •  1
     
  •   Lynntox · Sep 7, 2024 · 4404 views
    This topic created in 619 days ago, the information mentioned may be changed or developed.

    我使用 labview 写的是没有问题的,通过串口助手发送产品也有反应

    但是在使用 C#就没办法正常通讯

    首先 我确认了线是没问题的,串口属性是没问题的 9600-8-n-1

    byte[] command = new byte[] { 0xAA, 0x55, 0x12, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEE };

    // 发送数据 serialPort.Write(command, 0, command.Length);

    就很奇怪

    5 replies    2024-11-22 16:02:40 +08:00
    sunmker
        1
    sunmker  
       Sep 11, 2024
    先打开串口,.open
    Lynntox
        2
    Lynntox  
    OP
       Sep 18, 2024
    @sunmker 这个肯定是 open 了,写了两个 command ,第一条是没有问题的,第二条就无响应了
    hez2010
        3
    hez2010  
       Oct 7, 2024
    官方文档有句注释:
    If there are too many bytes in the output buffer and Handshake is set to XOnXOff then the SerialPort object may raise a TimeoutException while it waits for the device to be ready to accept more data.

    检查一下你的 Handshake 设置。
    wangybsyuct
        4
    wangybsyuct  
       Nov 22, 2024
    为什么是两个 command ?一个程序 command 里打开串口,另一个程序 command 是无法再打开这个串口的,独占了,另一个程序也就无法写入数据了。另外一个程序写数据也要间隔多少毫秒再写吓一跳,Write 返回 void ,无法知道是否写成功了。
    Lynntox
        5
    Lynntox  
    OP
       Nov 22, 2024
    @wangybsyuct 我当时应该是想表达 有几条指令 有一条可以偶发触发 但是有一条就没办法 我当时写测试是间隔 50ms 去写一下 读写我分离去搞
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3129 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 12:46 · PVG 20:46 · LAX 05:46 · JFK 08:46
    ♥ Do have faith in what you're doing.