private void ReadBtn_Click(object sender, EventArgs e) { #region 通信 XNetComm hComm = new XNetComm(xnet);//构造通信句柄 //读10个寄存器 short[] readRegs = new short[(int)(RegNum.Value)];//100个 try { hComm.Read(PLCRegType.F0, (int)(OffSetNum.Value), readRegs);//从D1000位置开始读10个寄存器,存入readRegs } catch (XNetException ex) { MessageBox.Show(ex.Message);//读取失败的错误信息 return; } MessageBox.Show("成功"); #endregion ShowTextBox.Text = ""; foreach (short a in readRegs) { ShowTextBox.Text += a.ToString("x4"); ShowTextBox.Text += " "; } }
static void Main(string[] args) { #region 与通信服务建立tcp连接 CommTcp commTcp = new CommTcp("127.0.0.1", 2323);//1.连接服务exe try { commTcp.Connect(); } catch (XNetException ex) { Console.WriteLine(ex.Message); return; } #endregion #region 配置通信参数 XNetConnectParams XNetParams = new XNetConnectParams("192.168.6.6", DeviceType.XDPLC); //2.指定设备地址 XNet xnet = new XNet(XNetParams, commTcp); //3.构造信捷协议栈 #endregion #region 通信 XNetComm hComm = new XNetComm(xnet);//4.构造通信句柄 //读10个寄存器 try { short[] readRegs = new short[10]; //10个 hComm.Read(PLCRegType.D, 1000, readRegs); //从D1000位置开始读10个寄存器,存入readRegs } catch (XNetException ex) { Console.WriteLine(ex.Message);//读取失败的错误信息 return; } //写10个寄存器 try { short[] writeRegs = new short[10]; //10个 hComm.Write(PLCRegType.D, 1000, writeRegs); //从D1000位置开始写10个寄存器,把writeRegs写入 } catch (XNetException ex) { Console.WriteLine(ex.Message); return; } #endregion Console.WriteLine("成功"); Console.ReadLine(); }
static void Main(string[] args) { string err; #region 查找设备 XNet xnet = XNet.FindLocalDevice("127.0.0.1", 2323, DeviceType.XDPLC, out err);//对电脑的所有(USB/串口)端口进行查找XDPLC if (xnet == null) { return;//查找失败 } #endregion #region 通信 XNetComm hComm = new XNetComm(xnet);//4.构造通信句柄 //读10个寄存器 short[] readRegs = new short[10];//10个 try { hComm.Read(PLCRegType.D, 0, readRegs);//从D1000位置开始读10个寄存器,存入readRegs } catch (XNetException ex) { Console.WriteLine(ex.Message);//读取失败的错误信息 return; } //写10个寄存器 short[] writeRegs = new short[10];//10个 try { hComm.Write(PLCRegType.D, 1000, writeRegs);//从D1000位置开始写10个寄存器,把writeRegs写入 } catch (XNetException ex) { Console.WriteLine(ex.Message); return; } #endregion Console.WriteLine("成功"); Console.ReadLine(); }
static void Main(string[] args) { #region 与通信服务建立tcp连接 CommTcp commTcp = new CommTcp("127.0.0.1", 2323);//1.连接到服务 X_Net_Windows.exe try { commTcp.Connect(); } catch (XNetException ex) { Console.WriteLine("连接服务失败" + ex.Message); return; } #endregion #region 配置通信参数 //设备ID,安全模式,PLC密码,用户名,密码(目前,安全模式为1,用户名密码都为“”空) XNetConnectRemoteParams xnetRemoteParams = new XNetConnectRemoteParams("020001003b0f00004", 1, "12345678", "", ""); //远程通信的通信端口为240 XNetConnectParams XNetParams = new XNetConnectParams(240, DeviceType.GBOX_4G, null, xnetRemoteParams);//查找4GBox_M而不是PLC XNet xnet = new XNet(XNetParams, commTcp); #endregion #region 建立远程连接 try { xnet.Connect_RemoteDevice(); } catch (XNetException ex) { Console.WriteLine("建立远程连接失败:" + ex.Message); return; } #endregion #region 查找设备 try { xnet.Connect_Find_Device();//查找设备 } catch (XNetException ex) { Console.WriteLine(ex.Message); return; } #endregion #region 通信 XNetComm hComm = new XNetComm(xnet); XNetComm_XC h_XCComm = new XNetComm_XC(hComm);//构造XC的通信句柄 //读10个寄存器 short[] readRegs = new short[10];//10个 try { h_XCComm.Read(XCPLCRegType.D, 1000, readRegs);//从D1000位置开始读10个寄存器,存入readRegs } catch (XNetException ex) { Console.WriteLine(ex.Message);//读取失败的错误信息 return; } //写10个寄存器 short[] writeRegs = new short[10];//10个 try { h_XCComm.Write(XCPLCRegType.D, 2000, writeRegs);//从D2000位置开始写10个寄存器,把writeRegs写入 } catch (XNetException ex) { Console.WriteLine(ex.Message); return; } #endregion Console.WriteLine("成功"); Console.ReadLine(); }
static void Main(string[] args) { #region 与通信服务建立tcp连接 CommTcp commTcp = new CommTcp("127.0.0.1", 2323);//1.连接到服务 X_Net_Windows.exe try { commTcp.Connect(); } catch (XNetException ex) { Console.WriteLine("连接服务失败" + ex.Message); return; } #endregion #region 配置通信参数 //129为以太网口 不填ID为类型查找 不填远程参数 //XNetConnectParams XNetParams = new XNetConnectParams(129, DeviceType.XDPLC, null, null); //类型查找 XNetConnectParams XNetParams = new XNetConnectParams(129, DeviceType.XDPLC, "00300900170a00002", null); //ID查找 XNet xnet = new XNet(XNetParams, commTcp); #endregion #region 查找设备 try { xnet.Connect_Find_Device();//查找设备 } catch (XNetException ex) { Console.WriteLine(ex.Message); return; } #endregion #region 通信 XNetComm hComm = new XNetComm(xnet);//构造通信句柄 //读10个寄存器 short[] readRegs = new short[10];//10个 try { hComm.Read(PLCRegType.D, 1000, readRegs);//从D1000位置开始读10个寄存器,存入readRegs } catch (XNetException ex) { Console.WriteLine(ex.Message);//读取失败的错误信息 return; } //写10个寄存器 short[] writeRegs = new short[10];//10个 try { hComm.Write(PLCRegType.D, 1000, writeRegs);//从D1000位置开始写10个寄存器,把writeRegs写入 } catch (XNetException ex) { Console.WriteLine(ex.Message); return; } #endregion Console.WriteLine("成功"); Console.ReadLine(); }