private void timer1_Tick(object sender, EventArgs e) { // read the DigitalInputsMarker register (System Manager TwinCat -> 0x4000) int bits; if (connected) { bits = tcpm.ReadHoldingRegisters(unit_id, 0x4000, 1).First(); } else if (blink) { bits = 21; blink = false; } else { bits = 42; blink = true; } foreach (Control control in gBEntreesDigitales.Controls) { Label iconLabel = control as Label; if ((bits % 2) == 1) { iconLabel.BackColor = Color.LimeGreen; } else { iconLabel.BackColor = SystemColors.Control; } bits /= 2; } int NS = 1; ushort bitsOut = 0; ushort count = 1; foreach (Control control in groupBox1.Controls) { CheckBox checkBox_x = control as CheckBox; if ((Piaget_CSharp.uPanel.SignauxOut[NS].EtatVF) /*|| (Piaget_CSharp.uPanel.SignauxOut[i].EtatVF) */) { bitsOut += count; checkBox_x.BackColor = Color.LimeGreen; checkBox_x.Checked = true; } else { checkBox_x.BackColor = SystemColors.Control; checkBox_x.Checked = false; } count *= 2; NS += 1; } }
private void button1_Click(object sender, EventArgs e) { // Read and write some registers on RTU n. 5 mm.WriteSingleRegister(byte.Parse(textBox5.Text), 4, ushort.Parse(textBox2.Text)); mm.WriteSingleCoil(byte.Parse(textBox5.Text), 2, checkBox1.Checked); textBox3.Text = mm.ReadHoldingRegisters(byte.Parse(textBox5.Text), 4, 1).First().ToString("D5"); textBox4.Text = mm.ReadCoils(byte.Parse(textBox5.Text), 2, 1).First().ToString(); // Exec the cicle each 2 seconds }
public void Test_Modbus_TCPMuster(string Ip_Address, int Port) { string Address_Unit_ID = numericAddress.Value.ToString(); byte unit_id = Convert.ToByte(Address_Unit_ID); //textBox3.Text += unit_id; try { ModbusMasterTCP TCPmm = new ModbusMasterTCP(Ip_Address, Port); TCPmm.Connect(); try { ushort[] arc = TCPmm.ReadHoldingRegisters(unit_id, 3059, 2); string data_from_register = null; for (ushort i = 0; i < arc.Length; i++) { data_from_register += arc[i].ToString("X4"); } uint num = uint.Parse(data_from_register, System.Globalization.NumberStyles.AllowHexSpecifier); byte[] floatVals = BitConverter.GetBytes(num); float f = BitConverter.ToSingle(floatVals, 0); textBox3.Text += "IP: " + Ip_Address + " Address: " + unit_id + System.Environment.NewLine + "Consumed power in kWts = " + f + System.Environment.NewLine; textBox3.Text += "HEX Format: " + data_from_register + System.Environment.NewLine + System.Environment.NewLine; //byte[] bytes = BitConverter.GetBytes(data_from_register); //if (BitConverter.IsLittleEndian) //{ // bytes = bytes.Reverse().ToArray(); //} //float myFloat = BitConverter.ToSingle(bytes, 0); //textBox3.Text += //textBox3.Text += arc + System.Environment.NewLine; } catch { textBox3.Text += "Error 1: Can not Poll this register range" + System.Environment.NewLine; } } catch { textBox3.Text += "Error 2: Can not find slave" + System.Environment.NewLine; } }
public static void Beckhoff(string api) { byte unit_id = 1; int bitsIn = 0, test; ushort bitsOut = 0, count = 1; ModbusMasterTCP tcpmM25 = new ModbusMasterTCP("172.16.22.2", 502), tcpmRHY = new ModbusMasterTCP("???.???.???.???", 502); switch (uPanel.Platform) { case uPanel.tPlatform.MANIP25: tcpmM25.Connect(); test = tcpmM25.ReadHoldingRegisters(unit_id, 0x4003, 1).First(); switch (api) { case "BC9020_ON": if (!((test == 0) || (test == 2))) { tcpmM25.WriteSingleRegister(unit_id, 0x4003, 0); } // LECTURE bitsIn = tcpmM25.ReadHoldingRegisters(unit_id, 0x4000, 1).First(); for (int i = 1; i < 7; i++) { if ((bitsIn % 2) == 1) { uPanel.SignauxIn[i].EtatVF = true; } else { uPanel.SignauxIn[i].EtatVF = false; } bitsIn /= 2; } // ECRITURE for (int i = 1; i < 7; i++) { if (uPanel.SignauxOut[i].EtatVF) { bitsOut += count; } // else // { // } count *= 2; } tcpmM25.WriteSingleRegister(unit_id, 0x4002, bitsOut); tcpmM25.Disconnect(); uMTasks.Work[6].TaskStatus = uMTasks.tPhase.Faite; break; case "BC9020_OFF": if (test == 0) { // release the valves tcpmM25.WriteSingleRegister(unit_id, 0x4002, 0); // sets the TaskControlMarker to 1 (Beckhoff Control) tcpmM25.WriteSingleRegister(unit_id, 0x4003, 1); } tcpmM25.Disconnect(); uMTasks.Work[6].TaskStatus = uMTasks.tPhase.Suspendue; break; } // Disconnect the IP address break; } }