private void button3_Click(object sender, EventArgs e) { byte[] Buf = new byte[100]; GetBoxToStruct(ref ModBus); ModBusClass.ModBus_CreatBuf(ref ModBus, ref Buf); //根据结构体获取buf ModBus.XorValue = Tools.Xor(Buf, ModBus.MsgLength + 7); //计算bufxor校验 ModBusClass.ModBus_CreatBuf(ref ModBus, ref Buf); textBox9.Text = ModBus.XorValue.ToString("X2"); textBox10.Text = Tools.HexToString(Buf); }
private void button12_Click(object sender, EventArgs e) { byte[] Buf = new byte[100]; ModBus.TargetAddr = ModBusClass.TargetAddr; ModBus.HostAddr = ModBusClass.HostAddr; ModBus.MsgFlag = 0x86; ModBus.MsgNum = 1; ModBus.DataFlag = 0x01; ModBus.DataLength = 0; ModBus.MsgLength = ModBus.DataLength + 3; ModBusClass.ModBus_CreatBuf(ref ModBus, ref Buf); SetBoxFromStruct(ModBus); button3_Click(null, null); button4_Click(null, null); }
private void button6_Click(object sender, EventArgs e) { byte[] Buf = new byte[100]; ModBus.TargetAddr = ModBusClass.BroadAddr; ModBus.HostAddr = ModBusClass.HostAddr; ModBus.MsgFlag = (byte)MsgFlag.TimeLock; ModBus.MsgNum = 1; ModBus.DataLength = 8; ModBus.DataFlag = 0x01; ModBus.MsgLength = ModBus.DataLength + 3; ModBus.Data = Tools.DateTimeToBytes(DateTime.Now); ModBusClass.ModBus_CreatBuf(ref ModBus, ref Buf); SetBoxFromStruct(ModBus); button3_Click(null, null); button4_Click(null, null); }
private void button4_Click(object sender, EventArgs e) { // SetBar(0, 100); byte[] RxBuffer = new byte[100]; textBox11.Text = null; GetBoxToStruct(ref ModBus); byte[] Buf = new byte[ModBus.MsgLength + 9]; ModBusClass.ModBus_CreatBuf(ref ModBus, ref Buf); bool R = Usart.SendData(serialPort1, Buf, Buf.Length, ref RxBuffer, 100); if (ModBus.TargetAddr != ModBusClass.BroadAddr & R) { textBox11.Text = Tools.HexToString(RxBuffer); } ModBusClass.ModBus_CreatStruct(ref ModBus, RxBuffer); }
private void SetValue(byte OffSet, float F) { byte[] Buf = new byte[100]; ModBus.TargetAddr = ModBusClass.TargetAddr; ModBus.HostAddr = ModBusClass.HostAddr; ModBus.MsgFlag = 0x05; ModBus.MsgNum = 1; ModBus.DataFlag = OffSet; ModBus.DataLength = 4; ModBus.MsgLength = ModBus.DataLength + 3; ModBus.Data = new byte[4]; Tools.FloatToBytes(F, ref ModBus.Data); ModBusClass.ModBus_CreatBuf(ref ModBus, ref Buf); SetBoxFromStruct(ModBus); 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); }