예제 #1
0
        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();
        }
예제 #2
0
        private void SetupRemoteLinkBtn_Click(object sender, EventArgs e)
        {
            #region 与通信服务建立tcp连接
            commTcp = new CommTcp("127.0.0.1", 2323);//1.连接到服务 X_Net_Windows.exe
            try
            {
                commTcp.Connect();
            }
            catch (XNetException ex)
            {
                MessageBox.Show("连接服务失败" + ex.Message);
                return;
            }
            #endregion

            #region 配置通信参数
            //设备ID,安全模式,PLC密码,用户名,密码(目前,安全模式为1,用户名密码都为“”空)
            xnetRemoteParams = new XNetConnectRemoteParams(IDBox.Text, 1, PasswordBox.Text, "", "");

            DeviceType deviceType;
            switch (DeviceTypeCB.Text)
            {
            case "PLC_XD":
                deviceType = DeviceType.XDPLC;
                break;

            case "GBOX_4G":
                deviceType = DeviceType.GBOX_4G;
                break;

            case "WBOX":
                deviceType = DeviceType.WBOX;
                break;

            default:
                deviceType = DeviceType.WBOX;
                break;
            }
            //远程通信的通信端口为240,支持各种查找,这里没填ID是类型查找。
            XNetParams = new XNetConnectParams(240, deviceType, null, xnetRemoteParams);

            xnet = new XNet(XNetParams, commTcp);
            #endregion


            #region 建立远程连接
            try
            {
                xnet.Connect_RemoteDevice();
            }
            catch (XNetException ex)
            {
                MessageBox.Show("建立远程连接失败:" + ex.Message);
                return;
            }
            #endregion

            #region 查找设备
            try
            {
                xnet.Connect_Find_Device();//查找设备
            }
            catch (XNetException ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            #endregion
            MessageBox.Show("成功");
        }
예제 #3
0
        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();
        }
예제 #4
0
        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();
        }