Пример #1
0
        private bool RequestData(IO_DEVICE device, RealData realData, out string error, ModbusFragmentStore fragmentstore)
        {
            error = "";
            try
            {
                if (udpClient != null)
                {
                    //分段读取数据,如果是读取整个寄存器的话,一次只能最多读取123个,
                    //如果是读取线圈的话最大只能读取1999个,因此要分段进行数据的读取
                    List <byte> allbytes = new List <byte>();
                    try
                    {
                        for (int i = 0; i < fragmentstore.Fragments.Count; i++)
                        {
                            ModbusFragment fragment = fragmentstore.Fragments[i];
                            switch (fragment.Code)
                            {
                            case "01":    // 01和05是一个码 可写可读
                            {
                                //返回的线圈状态,由于线圈是按位操作,转换也是按位转换
                                Task <bool[]> result = master.ReadCoilsAsync(byte.Parse(device.IO_DEVICE_ADDRESS), fragment.StartRegister, fragment.RegisterNum);

                                byte[] bytes = ModbusConvert.BoolToByte(result.Result);
                                fragment.StartIndex = allbytes.Count;
                                fragment.Length     = bytes.Length;
                                allbytes.AddRange(bytes);
                            }
                            break;

                            case "02":    //只读属性
                            {
                                //返回的线圈状态
                                Task <bool[]> result = master.ReadInputsAsync(byte.Parse(device.IO_DEVICE_ADDRESS), fragment.StartRegister, fragment.RegisterNum);
                                byte[]        bytes  = ModbusConvert.BoolToByte(result.Result);
                                fragment.StartIndex = allbytes.Count;
                                fragment.Length     = bytes.Length;
                                allbytes.AddRange(bytes);
                            }
                            break;

                            case "03":    //HR保持寄存器,可写可读
                            {
                                //返回的数据全部是ushort 需要将ushort 转换为byte在进行传递
                                Task <ushort[]> result = master.ReadHoldingRegistersAsync(byte.Parse(device.IO_DEVICE_ADDRESS), fragment.StartRegister, fragment.RegisterNum);
                                byte[]          bytes  = ModbusConvert.Ushorts2Bytes(result.Result);
                                fragment.StartIndex = allbytes.Count;
                                fragment.Length     = bytes.Length;
                                allbytes.AddRange(bytes);
                            }
                            break;

                            case "04":    //只读属性
                            {
                                //返回的数据全部是ushort 需要将ushort 转换为byte在进行传递
                                Task <ushort[]> result = master.ReadInputRegistersAsync(byte.Parse(device.IO_DEVICE_ADDRESS), fragment.StartRegister, fragment.RegisterNum);
                                byte[]          bytes  = ModbusConvert.Ushorts2Bytes(result.Result);
                                fragment.StartIndex = allbytes.Count;
                                fragment.Length     = bytes.Length;
                                allbytes.AddRange(bytes);
                            }
                            break;
                            }
                        }
                    }
                    catch
                    {
                        //读取异常处理
                        this.DeviceStatus(this.IOServer, this.IOCommunication, device, null, "0");//tag为1表示上线,如果为0表示下线
                    }
                    //将数据返回到采集客户端
                    if (allbytes.Count > 0)
                    {
                        device.IO_DEVICE_STATUS = 1;
                        ReceiveData(this.IOServer, this.IOCommunication, device, allbytes.ToArray(), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), fragmentstore);
                        //设置设备状态
                        this.DeviceStatus(this.IOServer, this.IOCommunication, device, null, "1");//tag为1表示上线,如果为0表示下线
                    }
                    else
                    {
                        device.IO_DEVICE_STATUS = 0;
                        //设置设备状态
                        this.DeviceStatus(this.IOServer, this.IOCommunication, device, null, "0");//tag为1表示上线,如果为0表示下线
                    }
                }
                return(true);
            }
            catch
            {
                return(false);
            }
        }