Mr.Yang|Hello 2022-05-14 16:51 采纳率: 100%
浏览 142
已结题

C#串口通讯时读写锁怎么加

本人最近学习C#Modbus通讯,要一直读取从站数据,但是在写的时候会和读冲突,导致线程阻塞,网上搜的要加读写锁,麻烦哪位指导一下
这里的读写锁应该怎么加,才能让读写不冲突
#region 读取输出线圈0x01

    //Rx:01 01 00 00 00 0A BC 0D
    //Tx:01 01 02 25 01 62 AC



    /// <summary>
    /// 读取输出线圈0x01
    /// </summary>
    /// <param name="slaveAddress">从站地址</param>
    /// <param name="startAddress">起始地址</param>
    /// <param name="count">读取数据数量</param>
    /// <returns>布尔数组(若返回结果为null,则读取数据失败,若返回结果不为null,则数据读取成功)</returns>
    public bool[] ReadOutPutCoil(byte slaveAddress, ushort startAddress, ushort count)
    {
       
        //1、拼接报文
        List<byte> SendCommand = new List<byte>();
        SendCommand.Add(slaveAddress);
        SendCommand.Add(0x01);

        SendCommand.Add((byte)(startAddress / 256));
        SendCommand.Add((byte)(startAddress % 256));

        SendCommand.Add((byte)(count / 256));
        SendCommand.Add((byte)(count % 256));

        SendCommand.AddRange(CRC16(SendCommand.ToArray()));

        //2、发送报文

        CurrentCOM.Write(SendCommand.ToArray(), 0, SendCommand.Count);

        //3、接收报文
        Thread.Sleep(100);

        byte[] buffer = new byte[CurrentCOM.BytesToRead];

        CurrentCOM.Read(buffer, 0, buffer.Length);

        //4、验证报文

        if (buffer.Length == (5 + (count <= 8 ? 1 : (count / 8 + 1))))
        {
            if (buffer[0] == slaveAddress && buffer[1] == 0x01 && buffer[2] == (count <= 8 ? 1 : (count / 8 + 1)))
            {
                //5、解析报文

                byte[] result = new byte[count <= 8 ? 1 : (count / 8 + 1)];
                //截取数据
                Array.Copy(buffer, 3, result, 0, count <= 8 ? 1 : (count / 8 + 1));

               
                return ByteArrToBoolArr(result);
            }
        }
        
        return null;
    }

    #endregion

    #region 读取输入线圈0x02

    //Rx:01 02 00 00 00 0A F8 0D
    //Tx:01 02 02 00 00 B9 B8


    /// <summary>
    /// 读取输入线圈0x02
    /// </summary>
    /// <param name="slaveAddress">从站地址</param>
    /// <param name="startAddress">起始地址</param>
    /// <param name="count">读取数据数量</param>
    /// <returns>布尔数组(若返回结果为null,则读取数据失败,若返回结果不为null,则数据读取成功)</returns>
    public bool[] ReadInPutCoil(byte slaveAddress, ushort startAddress, ushort count)
    {
       
        //1、拼接报文
        List<byte> SendCommand = new List<byte>();
        SendCommand.Add(slaveAddress);
        SendCommand.Add(0x02);

        SendCommand.Add((byte)(startAddress / 256));
        SendCommand.Add((byte)(startAddress % 256));

        SendCommand.Add((byte)(count / 256));
        SendCommand.Add((byte)(count % 256));

        SendCommand.AddRange(CRC16(SendCommand.ToArray()));

        //2、发送报文

        CurrentCOM.Write(SendCommand.ToArray(), 0, SendCommand.Count);

        //3、接收报文
        Thread.Sleep(100);

        byte[] buffer = new byte[CurrentCOM.BytesToRead];

        CurrentCOM.Read(buffer, 0, buffer.Length);

        //4、验证报文

        if (buffer.Length == (5 + (count <= 8 ? 1 : (count / 8 + 1))))
        {
            if (buffer[0] == slaveAddress && buffer[1] == 0x02 && buffer[2] == (count <= 8 ? 1 : (count / 8 + 1)))
            {
                //5、解析报文

                byte[] result = new byte[count <= 8 ? 1 : (count / 8 + 1)];

                Array.Copy(buffer, 3, result, 0, count <= 8 ? 1 : (count / 8 + 1));

              
                return ByteArrToBoolArr(result);
               
            }
        }
      
        return null;
        
    }


    #endregion
  • 写回答

1条回答 默认 最新

  • 金士顿 2022-05-14 17:37
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月22日
  • 已采纳回答 5月14日
  • 创建了问题 5月14日

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!