Mina.NET:汉王人脸通SDK示例代码(一)执行设备端命令

注意:代码仅供参考,需要根据具体的设备做调整
同系列文章:

添加引用:

演示程序界面
MinaTcpClientDemo

源代码
MinaTcpClientForm.cs

/* ----------------------------------------------------------
 * 文件名称:MinaTcpClientForm.cs
 * 
 * 作者:秦建辉
 * 
 * QQ:36748897
 * 
 * 博客:http://www.firstsolver.com/wordpress/
 * 
 * 开发环境:
 *      Visual Studio 2015
 *      .NET Framework 4 Client Profile
 *      
 * 版本历史:
 *      V1.0    2016年01月09日
 *              基于Mina.NET框架的Tcp连接客户端演示
 *
 * 参考资料:
 *      http://git.oschina.net/longshine/Mina.NET
------------------------------------------------------------ */
using Com.FirstSolver.FaceId;
using Mina.Core.Future;
using Mina.Filter.Codec;
using Mina.Transport.Socket;
using System;
using System.Net;
using System.Windows.Forms;

namespace Splash
{
    public partial class MinaTcpClientForm : Form
    {
        /// <summary>
        /// 设备字符集代码页为简体中文
        /// </summary>
        private const int DeviceCodePage = 936;

        public MinaTcpClientForm()
        {
            InitializeComponent();
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBoxAnswer.Clear();
        }

        private void buttonExecute_Click(object sender, EventArgs e)
        {
            // 通信密钥
            string SecretKey = textBoxSecretKey.Text;

            // 连接器
            using (AsyncSocketConnector TcpClient = new AsyncSocketConnector())
            {
                TcpClient.FilterChain.AddLast("codec", new ProtocolCodecFilter(new FaceIdProtocolCodecFactory(DeviceCodePage)));

                // 设置事件处理器
                TcpClient.SessionOpened += (o, ea) =>
                {
                    if (!String.IsNullOrEmpty(SecretKey))
                    {   // 设置通信密钥
                        FaceIdProtocolCodecFactory.SetEncoderKey(ea.Session, SecretKey);
                        FaceIdProtocolCodecFactory.SetDecoderKey(ea.Session, SecretKey);
                    }
                };

                TcpClient.MessageReceived += (o, ea) =>
                {   // 线程安全性
                    textBoxAnswer.BeginInvoke(new Action(() =>
                    {
                        textBoxAnswer.Text = ea.Message.ToString();
                    }));

                    ea.Session.Close(true);
                };

                TcpClient.ExceptionCaught += (o, ea) =>
                {   // 线程安全性
                    textBoxAnswer.BeginInvoke(new Action(() =>
                    {
                        textBoxAnswer.Text = "Exception: " + ea.Exception.Message.ToString();
                    }));

                    ea.Session.Close(true);
                };

                // 设置连接超时等待时间为10秒
                TcpClient.ConnectTimeout = 10;

                // 设置读超时等待时间为60秒
                TcpClient.SessionConfig.ReaderIdleTime = 60;

                // 建立连接
                IConnectFuture ConnFuture = TcpClient.Connect(new IPEndPoint(IPAddress.Parse(textBoxDeviceIP.Text), Int32.Parse(textBoxDevicePort.Text)));
                ConnFuture.Await();

                // 发送查询命令
                ConnFuture.Session.Write(textBoxCommand.Text);
                ConnFuture.Session.CloseFuture.Await();
            }                
        }
    }
}

Comments are closed.