//创建命令帧 public void CreatCommand() { foreach (DeviceBll Devbll in DeviceList) { CommandCreat.CreatDeviceCommandList(Port.PortType, Devbll); } }
public void sendCommand(CommDeviceModbuseControlEntity obj) { COMMUNICATERESULT ret = new COMMUNICATERESULT(); byte[] tempbyte = new byte[2]; CommandClass OneSetCommand; List <CommandClass> SetCommandList = new List <CommandClass>(); bool isEnabled = ReceiveTimer.Enabled; if (isEnabled) { ReceiveTimer.Stop(); } try { if ((Port.PortType == Protocol.Modbus_USBPort)) //USB端口Modbus协议特殊处理 { SetCommandList = CommandCreat.CreatModbusSetDivInfoCommandList(Port.PortType, (byte)obj.DivID, obj.functioncode, obj.SetData, obj.SetInfoStartAdr, obj.RegisterLen); lock (LockObject) { ret = PortBll.SetData(SetCommandList); } } else { OneSetCommand = CommandCreat.CreatModbusSetDivInfoCommand(Port.PortType, (byte)obj.DivID, obj.functioncode, obj.SetData, obj.SetInfoStartAdr, obj.RegisterLen); lock (LockObject) { ret = PortBll.SetData(OneSetCommand); } } obj.IsSuccess = ret == COMMUNICATERESULT.OK ? true : false; } finally { if (isEnabled) { ReceiveTimer.Start(); } } }
public void SetDivInfo(Object Object) { CommDeviceModbuseControlEntity tempEntity = new CommDeviceModbuseControlEntity(); COMMUNICATERESULT ret = new COMMUNICATERESULT(); byte[] tempbyte = new byte[2]; CommandClass OneSetCommand; List <CommandClass> SetCommandList = new List <CommandClass>(); if (ReceiveTimer.Enabled) { ReceiveTimer.Stop(); } try { if (Object is CommDeviceModbuseControlEntity) { tempEntity = (CommDeviceModbuseControlEntity)Object; if ((Port.PortType == Protocol.Modbus_USBPort)) //USB端口Modbus协议特殊处理 { SetCommandList = CommandCreat.CreatModbusSetDivInfoCommandList(Port.PortType, (byte)tempEntity.DivID, tempEntity.functioncode, tempEntity.SetData, tempEntity.SetInfoStartAdr, tempEntity.RegisterLen); lock (LockObject) { ret = PortBll.SetData(SetCommandList); } } else { switch (Port.PortType) { case Protocol.Modbus_SerialPort: case Protocol.Modbus_TCPIPPort: OneSetCommand = CommandCreat.CreatModbusSetDivInfoCommand(Port.PortType, (byte)tempEntity.DivID, tempEntity.functioncode, tempEntity.SetData, tempEntity.SetInfoStartAdr, tempEntity.RegisterLen); break; default: OneSetCommand = CommandCreat.CreatModbusSetDivInfoCommand(Port.PortType, (byte)tempEntity.DivID, tempEntity.functioncode, tempEntity.SetData, tempEntity.SetInfoStartAdr, tempEntity.RegisterLen); break; } lock (LockObject) { ret = PortBll.SetData(OneSetCommand); } } if (ret == COMMUNICATERESULT.OK) { tempEntity.IsSuccess = true; } } } finally { ReceiveTimer.Start(); } switch (Port.PortType)//只有在Modbus协议通讯时才返回通讯结果 { case Protocol.Modbus_SerialPort: case Protocol.Modbus_TCPIPPort: case Protocol.Modbus_USBPort: if (SetInfoResultMethod != null) { SetInfoResultMethod.Invoke(tempEntity); } break; } }
//读下位机 历史告警数据 public int GetHistoricRecordParameters(int DeviceListID, byte UnitID, ref short AddressStart, ref short PerRecordLen, ref short TotalLen, DataTable da) { //判断端口是否连接,如果没连接,则直接返回;否则关掉轮询定时器 if (!this.PortBll.ConnectState()) { return(-1); } if (ReceiveTimer.Enabled) { ReceiveTimer.Stop(); } System.Threading.Thread.Sleep(2000); try { //1.读下位机历史记录 存储状态 short RecDataLen = 3 * 2 + 3; byte[] Cmd = CommandCreat.CreatOneModbusReadCommand(Port.PortType, UnitID, (byte)FunctionCode.ReadInputRegisters, (short)0x26, (short)3, ref RecDataLen); byte[] RecData = new byte[RecDataLen]; PortBll.GetBasicDataFromCommand(Cmd, ref RecData, RecDataLen); //2.解析 存储状态 TotalLen = (short)(RecData[0] * 256 + RecData[1]); PerRecordLen = (short)(RecData[2] * 256 + RecData[3]);//默认为5 AddressStart = (short)(RecData[4] * 256 + RecData[5]); //3.判断历史记录是否存在,如果存在就读取,如果不存在继续读实时数据 if (TotalLen == 0) { return(TotalLen); } else { for (int i = 0; i < TotalLen; i++) { //每条基本MODBUS长度 RecDataLen = (short)(PerRecordLen * 2 + 3); //基本MODBUS为13 Cmd = CommandCreat.CreatOneModbusReadCommand(Port.PortType, UnitID, (byte)FunctionCode.ReadInputRegisters, (short)AddressStart, (short)PerRecordLen, ref RecDataLen); byte[] RecData_record = new byte[RecDataLen]; //TCP协议应该为19,串口协议为15 PortBll.GetBasicDataFromCommand(Cmd, ref RecData_record, RecDataLen); //读完之后更改起始地址 AddressStart += 5; //解析报警时间 DateTime dt = MathConvertHelper.BitConverter.BCDtoDate(RecData_record); //解析报警量 int VarialbeID = RecData_record[6]; //=信号ID-1(数字量从0开始) bool Ishappen = RecData_record[7] == 0 ? false : true; //消失或者发生 string SignalName = DeviceList[DeviceListID].DigitalList[VarialbeID].DigitalInfo.SignalName; //解析报警值 byte[] va = new byte[2]; va[0] = RecData_record[8]; va[1] = RecData_record[9]; int VA = MathConvertHelper.BitConverter.ToInt16Reverse(va); //double value = VA * DeviceList[DeviceListID].AnalogList[VarialbeID].AnalogInfo.ScalingFactorA + DeviceList[DeviceListID].AnalogList[VarialbeID].AnalogInfo.ScalingFactorB; //将 模拟量报警 抛出 string alarm_msg; if (Ishappen) { alarm_msg = DeviceList[DeviceListID].DigitalList[VarialbeID].DigitalInfo.SignalName + " " + DeviceList[DeviceListID].DigitalList[VarialbeID].DigitalInfo.Value0_Describe; } else { alarm_msg = DeviceList[DeviceListID].DigitalList[VarialbeID].DigitalInfo.SignalName + " " + DeviceList[DeviceListID].DigitalList[VarialbeID].DigitalInfo.Value1_Describe; }; da.Rows.Add(dt, SignalName, Ishappen, alarm_msg); AlarmThrow(dt, VarialbeID + 1, alarm_msg); } return(TotalLen); } } finally { ReceiveTimer.Start(); } }