コード例 #1
0
        public override void ConnectToDeviceAsync(ResultDel del)
        {
            IPEndPoint endPoint = new IPEndPoint(ServerIP, ServerPort);
            Socket     socket   = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                //尝试连接器件,通过TCP/IP模拟
                socket.Connect(endPoint);
            }
            catch
            {
                socket.Close();
                del.Invoke(false, "连接测试台失败,请检查线路是否连接正确!");
                return;
            }

            TCPSocket   = socket;
            IsConnected = true;
            R_Thread    = new Thread(new ThreadStart(RecieveDatas));
            R_Thread.Start();

            //发送指令,要求器件发送自身信息回来
            SendMeesageAsync("连接并且获取信息", CommandEnum.CONNECT, del);
        }
コード例 #2
0
 /// <summary>
 /// 增加获取数据时候的代理
 /// </summary>
 /// <param name="del"></param>
 public void Add_GetMeeageDel(ResultDel del)
 {
     GetMessageEvent -= del;
     GetMessageEvent += del;
 }
コード例 #3
0
        public async void SendMeesageAsync(String content, CommandEnum command, ResultDel del)
        {
            byte[] datas   = Encoding.UTF8.GetBytes(content);
            byte[] message = new byte[datas.Length + 12];
            byte   verify  = 0x00;


            //填装数据头
            for (int counter = 0; counter < 4; counter++)
            {
                message[counter] = 0xff;
            }
            //填装命令
            message[4] = (byte)command;
            verify    ^= message[4];

            //填装数据长度
            message[5] = (byte)(datas.Length / 256);
            message[6] = (byte)(datas.Length % 256);
            verify    ^= message[5];
            verify    ^= message[6];

            //填装数据
            for (int counter = 7; counter < message.Length - 5; counter++)
            {
                message[counter] = datas[counter - 7];
                verify          ^= message[counter];
            }

            message[message.Length - 5] = verify;
            //填装数据尾部
            for (int counter = message.Length - 1; counter >= message.Length - 4; counter--)
            {
                message[counter] = 0x00;
            }

            //开始异步操作
            await Task.Run(new Action(() =>
            {
                SendMDel = del;
                SendContent = content;
                SendCommand = command;
                ResendTimes = 0;

                lock (this)
                {
                    //如果发送成功
                    if (SendMessage_Real(message))
                    {
                        //那么接受数据时会处理此处的发送代理
                    }
                    //发送数据失败
                    else
                    {
                        if (del != null)
                        {
                            del.Invoke(false, "SNED_FAILED", CommandEnum.NONE);
                        }
                    }
                }
            }));
        }
コード例 #4
0
 /// <summary>
 /// 连接到设备函数
 /// </summary>
 /// <returns>null成功,有信息则是错误信息</returns>
 public abstract void ConnectToDeviceAsync(ResultDel del);