public static void ModBus_CreatStruct(ref MB ModBus, byte[] Buf) { int i = 0; ModBus.Start = Buf[i++]; ModBus.TargetAddr = Buf[i++]; ModBus.HostAddr = Buf[i++]; ModBus.MsgFlag = Buf[i++]; ModBus.MsgNum = Buf[i++]; ModBus.MsgLength = Buf[i++]; ModBus.MsgLength <<= 8; ModBus.MsgLength |= Buf[i++]; if (ModBus.MsgLength >= 3) { ModBus.DataFlag = Buf[i++]; ModBus.DataLength = Buf[i++]; ModBus.DataLength <<= 8; ModBus.DataLength |= Buf[i++]; ModBus.Data = new byte[ModBus.DataLength]; try { for (int n = 0; n < ModBus.DataLength; n++) { ModBus.Data[n] = Buf[i++]; } } catch { } } ModBus.XorValue = Buf[i++]; ModBus.End = Buf[i++]; }
public static void ModBus_CreatBuf(ref MB ModBus, ref byte[] Buf) { int i = 0; if (ModBus.Data == null) { ModBus.Data = new byte[ModBus.DataLength]; } Buf[i++] = ModBus.Start; Buf[i++] = ModBus.TargetAddr; Buf[i++] = ModBus.HostAddr; Buf[i++] = ModBus.MsgFlag; Buf[i++] = ModBus.MsgNum; Buf[i++] = (byte)(ModBus.MsgLength >> 8); Buf[i++] = (byte)(ModBus.MsgLength & 0xff); if (ModBus.MsgLength >= 3) { Buf[i++] = (byte)(ModBus.DataFlag); Buf[i++] = (byte)(ModBus.DataLength >> 8); Buf[i++] = (byte)(ModBus.DataLength & 0xff); for (int n = 0; n < ModBus.DataLength; n++) { Buf[i++] = ModBus.Data[n]; } } ModBus.XorValue = Tools.Xor(Buf, i); Buf[i++] = ModBus.XorValue; Buf[i++] = EndFlag; }
public static bool CheakXor(MB ModBuf) { byte[] Buf = new byte[100]; ModBus_CreatBuf(ref ModBuf, ref Buf); byte XorValue = Tools.Xor(Buf, ModBuf.MsgLength + 7); if (XorValue == ModBuf.XorValue) { return(true); } else { return(false); } }
private void GetBoxToStruct(ref MB TempMB) { TempMB.Start = ModBusClass.StartFlag; TempMB.TargetAddr = Tools.StringToHex(textBox1.Text)[0]; TempMB.HostAddr = Tools.StringToHex(textBox2.Text)[0]; TempMB.MsgFlag = Tools.StringToHex(textBox3.Text)[0]; TempMB.MsgNum = Tools.StringToHex(textBox4.Text)[0]; TempMB.MsgLength = Tools.StringToHex(textBox5.Text)[0]; TempMB.MsgLength <<= 8; TempMB.MsgLength |= Tools.StringToHex(textBox5.Text)[1]; if (TempMB.MsgLength >= 3) { TempMB.DataFlag = Tools.StringToHex(textBox6.Text)[0]; TempMB.DataLength = Tools.StringToHex(textBox7.Text)[0]; TempMB.DataLength <<= 8; TempMB.DataLength |= Tools.StringToHex(textBox7.Text)[1]; TempMB.Data = Tools.StringToHex(textBox8.Text); } }
private void SetBoxFromStruct(MB TempMB) { textBox1.Text = TempMB.TargetAddr.ToString("X2"); textBox2.Text = TempMB.HostAddr.ToString("X2"); textBox3.Text = TempMB.MsgFlag.ToString("X2"); textBox4.Text = TempMB.MsgNum.ToString("X2"); textBox5.Text = TempMB.MsgLength.ToString("X4"); if (ModBus.MsgLength >= 3) { textBox6.Text = TempMB.DataFlag.ToString("X2"); textBox7.Text = TempMB.DataLength.ToString("X4"); textBox8.Text = Tools.HexToString(TempMB.Data); } else { textBox6.Text = null; textBox7.Text = null; textBox8.Text = null; } textBox9.Text = TempMB.XorValue.ToString("X2"); button3_Click(null, null); button4_Click(null, null); }
private float GetValue(byte OffSet) { MB TempMB = new MB(); byte[] Buf = new byte[100]; ModBus.TargetAddr = ModBusClass.TargetAddr; ModBus.HostAddr = ModBusClass.HostAddr; ModBus.MsgFlag = 0x13; ModBus.MsgNum = 1; ModBus.DataLength = 0; ModBus.DataFlag = OffSet; ModBus.MsgLength = ModBus.DataLength + 3; ModBus.Data = Tools.StringToHex(textBox8.Text); ModBusClass.ModBus_CreatBuf(ref ModBus, ref Buf); SetBoxFromStruct(ModBus); button3_Click(null, null); button4_Click(null, null); ModBusClass.ModBus_CreatStruct(ref TempMB, Tools.StringToHex(textBox11.Text)); float TempF = Tools.ByteToFloat(TempMB.Data, 0, 1); byte[] Fbuf = new byte[4]; return(TempF); }