资讯详情

泥人网络继电器对接

泥网继电器对接

  • 说明
    • ip配置
    • 对接代码

说明

ip配置

工具 在这里插入图片描述打开: 设置设备 工作模式是重点,必须是server

对接代码

 /// <summary>     /// 泥网继电器     /// </summary>     public class NRNetRelayBLL     {         Models.TerminalM haredWareCommonM;         Socket client;          public NRNetRelayBLL(Models.TerminalM terminalM)         {             haredWareCommonM = terminalM;         }         #region 打开输出口         //stach begin 1 2 3 4 5 6 ...         /// <summary>         /// 打开输出口         /// </summary>         /// <param name="num"></param>         /// <returns></returns>         public bool HandleStach(int num)         {             try             {                 ////创建客户端                 client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);                 ///IP地址                 IPAddress ip = IPAddress.Parse(haredWareCommonM.ip);                 ////口号                 IPEndPoint endPoint = new IPEndPoint(ip, (int)haredWareCommonM.port);                 /////与服务器建立远程连接                 client.Connect(endPoint);                 /////线程问题                 //Thread thread = new Thread(ReciveMsg);                 //thread.IsBackground = true;                 //thread.Start(client);                 ///将字符串指令转换为byte数组                 byte[] buf = System.Text.Encoding.Default.GetBytes("AT STACH"   num   "=1,1\r\n");                 //发送AT指令                 client.Send(buf);                  byte[] recbuf = new byte[10];//真正使用时,接收缓冲区需要适当调整                                              //等待硬件响应命令,接收到的数据是byte数组                                              //会等到有数据返回                 int recLen = client.Receive(recbuf);                 client.Close();                 //将byte将数组转换为字符串                 string str = System.Text.Encoding.Default.GetString(recbuf);                 str = str.Replace("\r\n", "").Trim().ToLower();                 if (!str.Contains("ok"))                 {                     Common.Log4.LogManage.WriteErr(str);                     Common.DelegateHelper.InfoMessageEvent?.Invoke(str);                     return false;                 }                 return true;             }             catch (Exception ex)             {                 Common.Log4.LogManage.WriteErr(ex.ToString());                 Common.DelegateHelper.InfoMessageEvent?.Invoke(ex.Message);                 return false;             }          }          #endregion          #region 获取相应输入的结果         /// <summary>         /// 获取相应输入的结果         /// </summary>         /// <param name="num"></param>         /// <returns></returns>         public bool SearchStateIn(int num = 0)         {             try             {                 ////创建客户端                 client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);                 ///IP地址                 IPAddress ip = IPAddress.Parse(haredWareCommonM.ip);                 ////口号                 IPEndPoint endPoint = new IPEndPoint(ip, (int)haredWareCommonM.port);                 /////与服务器建立远程连接                 client.Connect(endPoint);                 /////线程问题                 //Thread thread = new Thread(ReciveMsg);                 //thread.IsBackground = true;                 //thread.Start(client);                 ///将字符串指令转换为byte数组                 byte[] buf = System.Text.Encoding.Default.GetBytes("AT OCCH"   num   "=?\r\n");                 //发送AT指令                 client.Send(buf);                  byte[] recbuf = new byte[10];//真正使用时,接收缓冲区需要适当调整                                              //等待硬件响应命令,接收到的数据为byte数组                                              //会等到有数据返回                 int recLen = client.Receive(recbuf);                 client.Close();                 //将byte将数组转换为字符串                string str = System.Text.Encoding.Default.GetString(recbuf);
                str = str.Replace("\r\n", "").Trim().ToLower();
                if (!str.Contains("ok"))
                {
                    Common.Log4.LogManage.WriteErr(str);
                    Common.DelegateHelper.InfoMessageEvent?.Invoke(str);
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr(ex.ToString());
                Common.DelegateHelper.InfoMessageEvent?.Invoke(ex.Message);
                return false;
            }

        } 
        #endregion
    }

调用

public partial class 泥人网络继电器 : Form
    {
        HardWareBLL.NRNetRelayBLL nRNetRelayBLL;

        //泥人网络继电器 i202
        public 泥人网络继电器()
        {
            InitializeComponent();
        }
        Socket client;
        private void button1_Click(object sender, EventArgs e)
        {
#if false
            ///创建客户端
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            ///IP地址
            IPAddress ip = IPAddress.Parse(textBoxIp.Text.Trim());
            ///端口号
            IPEndPoint endPoint = new IPEndPoint(ip, int.Parse(textBoxPort.Text.Trim()));
            ///建立与服务器的远程连接
            client.Connect(endPoint);
            ///线程问题
            //Thread thread = new Thread(ReciveMsg);
            //thread.IsBackground = true;
            //thread.Start(client);
            //将字符串指令转换为byte数组
            byte[] buf = System.Text.Encoding.Default.GetBytes("AT+STACH1=1,1\r\n");
            //发送AT指令
            client.Send(buf);

            byte[] recbuf = new byte[10];//真正使用时,接收缓冲区需要适当的调整
            //等待硬件响应命令,接收到的数据为byte数组
            //会等待到有数据返回为止
            int recLen = client.Receive(recbuf);
            client.Close();
            //将byte数组转为字符串
            string str = System.Text.Encoding.Default.GetString(recbuf);
            str = str.Replace("\r\n", "");
            MessageBox.Show(str); 
#endif
            byte num = Convert.ToByte(textBoxOut.Text.Trim());
            Common.ShowBlackBox.WriteLine("1 num is " + num);
            bool res = nRNetRelayBLL.HandleStach(num);
            if (!res)
            {
                Common.ShowBlackBox.WriteLine("3 send message fail");
                return;
            }
            Common.ShowBlackBox.WriteLine("1 send message success");
        }

      
        //init
        private void button3_Click(object sender, EventArgs e)
        {
            Models.TerminalM terminalM = new Models.TerminalM();
            terminalM.ip = textBoxIp.Text.Trim();
            terminalM.port = Convert.ToInt32(textBoxPort.Text.Trim());
            nRNetRelayBLL = new HardWareBLL.NRNetRelayBLL(terminalM);
            Common.ShowBlackBox.WriteLine("1 init success");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int num = 0;
            if (!string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                num = Convert.ToInt32(textBox1.Text.Trim());
            }
            Common.ShowBlackBox.WriteLine("1 nRNetRelayBLL SearchStateIn "+ nRNetRelayBLL.SearchStateIn(num));
            Common.ShowBlackBox.WriteLine("1 nRNetRelayBLL success");
        }
    }

标签: 继电器882n

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台