public void InitializeAllReadRegisterCommands(List <DataPoint> dataPoints) { if (null == dataPoints) { return; } List <List <DataPoint> > dataPointsGroupedForRead = DataPointGrouper.GroupingDataPointsForReadRegister(dataPoints); foreach (var dataPointsGroup in dataPointsGroupedForRead) { List <List <byte> > requestCommadBytes = RequestCommandByteStreamCreater.CreateRequestCommandByteStreamForReadRegisterBy(dataPointsGroup); foreach (var requestCommadByte in requestCommadBytes) { ReadRegisterCommand readRegisterCommand = new ReadRegisterCommand(); //请求唯一性,用guid标示唯一性 List <byte> guidBytes = Guid.NewGuid().ToByteArray().ToList(); requestCommadByte.AddRange(guidBytes); readRegisterCommand.ReadCommand = requestCommadByte; //把所有的读命令保存在内存中,换取速度 this.allReadRegisterCommands.Enqueue(readRegisterCommand); } } }
private void ReadRegister() { ReadRegisterCommand readRegisterCommand = this.allReadRegisterCommands.Dequeue(); this.modbusRtu.WriteSerialPort(readRegisterCommand.ReadCommand.ToArray()); this.allReadRegisterCommands.Enqueue(readRegisterCommand); }
private void InitializeAllReadRegisterCommands(List <DataPoint> dataPoints) { if (null != dataPoints) { return; } List <List <DataPoint> > dataPointsGroupedForRead = DataPointGrouper.GroupingDataPointsForReadRegister(dataPoints); foreach (var dataPointsGroup in dataPointsGroupedForRead) { List <List <byte> > requestCommadBytes = RequestCommandByteStreamCreater.CreateRequestCommandByteStreamForReadRegisterBy(dataPointsGroup); foreach (var requestCommadByte in requestCommadBytes) { ReadRegisterCommand readRegisterCommand = new ReadRegisterCommand(); readRegisterCommand.ReadCommand = requestCommadByte; //把所有的读命令保存在内存中,换取速度 this.allReadRegisterCommands.Enqueue(readRegisterCommand); } } }
private void ReadRegister() { if (null == this.allReadRegisterCommands || this.allReadRegisterCommands.Count < 1) { return; } if (canSendNextRequestCommandBytes) { this.CurrentReadRegisterCommand = this.allReadRegisterCommands.Dequeue(); if (null != this.CurrentReadRegisterCommand) { this.serialPortNotResponseCount = 0; //TODO:删除,测试用的 //List<byte> textBytes = new List<byte>(); //textBytes.Add((byte)0x02); //textBytes.Add((byte)0x03); //textBytes.Add((byte)0x07); //textBytes.Add((byte)0xD4); //textBytes.Add((byte)0x00); //textBytes.Add((byte)0x01); //textBytes.Add((byte)0x09); //textBytes.Add((byte)0x09); ////GUID //textBytes.Add((byte)0x02); //textBytes.Add((byte)0x02); //textBytes.Add((byte)0x02); //textBytes.Add((byte)0x02); //textBytes.Add((byte)0x02); //textBytes.Add((byte)0x02); //textBytes.Add((byte)0x02); //textBytes.Add((byte)0x02); //textBytes.Add((byte)0x02); //textBytes.Add((byte)0x02); //textBytes.Add((byte)0x02); //textBytes.Add((byte)0x02); //textBytes.Add((byte)0x02); //textBytes.Add((byte)0x02); //textBytes.Add((byte)0x02); //textBytes.Add((byte)0x02); //this.modbusRtu.WriteSerialPort(textBytes.ToArray()); this.modbusRtu.WriteSerialPort(this.CurrentReadRegisterCommand.ReadCommand.ToArray()); this.allReadRegisterCommands.Enqueue(this.CurrentReadRegisterCommand); this.canSendNextRequestCommandBytes = false; } } else { if (SerialPortNotResponseGiveUpValue < ++this.serialPortNotResponseCount) { //如果在modbus没有响应,超过SerialPortNotResponseGiveUpValue指定 //的次数,就可以发生下一个请求。 this.canSendNextRequestCommandBytes = true; } System.Threading.Thread.Sleep(2000); } }