public void TCPConnect(string ipaddr) { if (_ModbusTCP == null || !_ModbusTCP.Connected) { _ModbusTCP = new ModbusMasterTCP(ipaddr, 502); _ModbusTCP.Connect(); //读版本号 //ushort[] version; //ushort[] samlightversion; //lock (_Locker) //{ // version = _ModbusTCP.ReadInputRegisters(1, 52000, 100); // samlightversion = _ModbusTCP.ReadInputRegisters(1, 52100, 100); //} //string ver = Class1.StringFromUShortArray(version); //string samver = Class1.StringFromUShortArray(samlightversion); //if (!string.IsNullOrEmpty(ver) && !string.IsNullOrEmpty(samver)) //{ // Version = ver; // SamlightVersion = samver; // //线程开始 // if (_T == null || _T.Status == TaskStatus.RanToCompletion) // Start(); //} //else // throw new Exception("不是东一打码机"); TCPConnected = _ModbusTCP.Connected; } }
private void Manip25Form_Load(object sender, EventArgs e) { if (!simulation_BC9020) { try { // Exec the connection // if (!Piaget_CSharp.uPanel.BeckhoffEnLigne) if (!connected) { tcpm.Connect(); connected = true; // Piaget_CSharp.uPanel.BeckhoffEnLigne = true; // sets the TaskControlMarker to 0 (C# Control) tcpm.WriteSingleRegister(unit_id, 0x4003, 0); } } catch { MessageBox.Show("172.16.22.2 not found! Conected?"); MessageBox.Show("Change to simulation!"); simulation_BC9020 = true; } } // InitializeComponent(); if (connected) { toolStripStatusLabel1.Text = "Connecté"; } }
/// <summary> /// 开启生产线按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { robot1.Start(); ModbusMasterTCP mm = new ModbusMasterTCP("192.168.0.2", 502); mm.Connect(); mm.WriteSingleCoil(2, 0, true); try { while (mm.ReadCoils(2, 0, 1).First()) { //Application.DoEvents();//待定,空执行 } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } mm.Disconnect(); mm = new ModbusMasterTCP("192.168.0.3", 502); mm.Connect(); mm.WriteSingleCoil((byte)3, 0, true); /*while (mm.ReadCoils(3, 0, 1).First()) * { * //Application.DoEvents();//待定,空执行 * }*/ mm.Disconnect(); }
/// <summary> /// 连接按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, EventArgs e) { if (button3.Text == "连接") { mm = new ModbusMasterTCP(textBox1.Text, 502); mm.Connect(); button3.Text = "断开"; } else { mm.Disconnect(); button3.Text = "连接"; } }
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; } }