Tcp/ip()485通讯

Modbus板卡通讯

客户端动态检测DO

 public class TcpClient
    {
   
        public Form1 Client_DO;
        public TcpClient(Form1 parent)
        {
   
            Client_DO = parent;
        }
        IPAddress ip1 = IPAddress.Parse("192.168.1.75");
        int connPort1 = 502;
        System.Timers.Timer Draw_Timer;
        public  Socket ClientServer;
        public  Thread Thread_DO;
        public IPAddress IP1
        {
   
            get {
    return ip1; }
            set {
    ip1 = value; }
        }

        public int ConnPort1
        {
   
            get {
    return connPort1; }
            set {
    connPort1 = value; }
        }
        public  void ClientServerinit()
    {
   
            Thread_DO = new Thread(Refresh_State_DO);
            Thread_DO.IsBackground = true;
            Thread_DO.Start();
            Draw_Timer = new System.Timers.Timer(900);
            Draw_Timer.Elapsed += new System.Timers.ElapsedEventHandler(clear);
            Draw_Timer.AutoReset = true;
            Draw_Timer.Enabled = true;
            Thread.Sleep(500);
            IPAddress ip1 = IPAddress.Parse("192.168.1.75");
            int connPort1 = 502;
            ClientServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            ClientServer.Connect(new IPEndPoint(ip1, connPort1)); 
        Console.WriteLine("连接服务器成功");
    }
        public  string ToHexString(int length, byte[] bytes)
        {
   
            string hexString = string.Empty;
            if (bytes != null)
            {
   
                StringBuilder strB = new StringBuilder();
                for (int i = 0; i < length; i++)
                {
   
                    for (int j = 0; j < 1; j++)
                    {
   
                        strB.Append(bytes[i].ToString("X2") + " ");
                    }
                }
                hexString = strB.ToString();
            }
            return hexString;
        }
        private int[] DO_State_Box = new int[16];
        byte[] CheckData_DO = new byte[12] {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x01, 0x00, 0x00, 0x00, 0x10 };

        public void clear(object source, System.Timers.ElapsedEventArgs e)
        {
   
            Client_DO.richTextBox1.Invoke(new Action(() 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值