实时在线云消费机、考勤门禁控制器、网络读卡器服务端C# Socket源码

 实时在线云消费机、考勤门禁控制器、网络读卡器服务端C# Socket源码_第1张图片

消费机UDP通讯协议介绍:

消费机介绍:

         账户金额等所有持卡人信息存在电脑服务器上,在线刷卡消费时设备将IC卡的物理卡号和消费金额传到服务器,服务器脑处理保存后将消费结果返发到消费机上显示,内置高灵敏扫码模组,扫码信息即时上传到服务器,脱网时支持白名单记账消费。

实时门禁控制板: 

       考勤门禁控制板可外接刷卡读头、扫描枪、人脸识别器,将读取到的ID号即时发送到服务器,由服务器的软件判断后再返回开门信号或反馈信息。脱网时由控制板内的白名单控制刷卡是否可通行。

实时在线云消费机、考勤门禁控制器、网络读卡器服务端C# Socket源码_第2张图片

门禁板详细介绍:实时TCP网络双门双向门禁控制板大型小区网络开关可编程二次开发-淘宝网 (taobao.com) 

服务器端C#源程序 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Management;

namespace UdpPostReaderDemoCSharp
{
    public partial class Form1 : Form
    {
        Form3 f3;
        Form5 f5;
        Form6 f6;

        int PortNumber;
        public string localIp;
        string machinnos;

        Boolean  ready = false ;  //线程运行标志
        Socket ListenerSock;      //Socket
        Thread ListenerThre;      //端口侦听线程
        IPEndPoint RemoteIPoint;  //这里指向发送数据包的目的Ip及端口,EndPoint是IPEndPoint的基类,IPEndPoint可以强转为EndPoint。EndPoint ep = (EndPoint)iep,主要目的是IPEndPoint可以自己写入IP地址和端口

        string BeepCode;
        string VoiceCode;

        public string sendcode;   //批量下传注册号的指针、缓冲、重发标志
        public bool SendCount;
        public int SendAgain;
        public int SendPoin;
        public int SendPoinMax;
        public string[] SendBufStr=new string[7950];

        delegate void Update1(string text1,string text2);   //线程内更新UI委拖

        [DllImport("Iphlpapi.dll")]
        private static extern int SendARP(Int32 dest, Int32 host, ref   Int64 mac, ref   Int32 length);
        [DllImport("Ws2_32.dll")]
        private static extern Int32 inet_addr(string ip);

        public Form1()
        {
            InitializeComponent();
        }

        public static Int64 GetMACByIP(string ip)   //通过IP获取MAC
        {
            Int32 ldest = inet_addr(ip);   //目的地的ip     
            try
            {
                Int64 macinfo = new Int64();
                Int32 len = 6;
                int res = SendARP(ldest, 0, ref   macinfo, ref   len);
                return macinfo;
            }
            catch (Exception err)
            {
                Console.WriteLine("Error:{0}", err.Message);
            }
            return 0;
        }

        public static bool checkmac(string macstr)   //检查MAC是否合法
        {
            try
            {
                string[] dataArray = macstr.Split(new char[2] { '-', '-' });
                for (int i = 0; i < 6; i++)
                {
                    byte macb = Convert.ToByte(Convert.ToInt32(dataArray[i], 16));
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool checkDateTime(string indt)  //检查日期时间字符串是否合法
        {
            try
            {
                DateTime userqsj = Convert.ToDateTime(indt);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static string getcuptwaystr(string inputip)   // 获取网关
        {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");//获得本机ip、掩码、网关
            ManagementObjectCollection nics = mc.GetInstances();
            foreach (ManagementObject nic in nics)
            {
                if (Convert.ToBoolean(nic["ipEnabled"]) == true)
                {
                    if ((nic["IPAddress"] as String[])[0] == inputip)
                    {
                        string ComputerMaskStr = (nic["IPSubnet"] as String[])[0];
                        string ComputerGatewayStr = (nic["DefaultIPGateway"] as String[])[0];
                        return ComputerGatewayStr;
                    }
                }
            }
            return "";
        }
        public static string GetSTCMAC(string inputstr)  //STC系列MAC获取方式
        {
            string tempmac="00000000"+Convert.ToInt64(inputstr).ToString("X2");
            string stcmac = "16-88";
            for (int i = 1; i <= 4; i++)
            {
                string str1 = tempmac.Remove(0, tempmac.Length - 2);
                tempmac = tempmac.Substring(0, tempmac.Length - 2); 
                stcmac = stcmac + "-" + str1;
            }
            return stcmac;
        }
        public static string GetCHMAC(string inputstr)   //CH系列MAC获取方式
        {
            string stcmac = "";
            if (inputstr.Length >= 16)
            {
                for (int i = 0; i <= 5; i++)
                {
                    stcmac = stcmac + inputstr.Substring(4 + i * 2, 2);
                    if (i < 5) { stcmac = stcmac + "-"; }
                }
            }
            return stcmac;
        }
        public static string DEC_To_Bin(byte indata)      //10进制转8位二进制
        {
            string DTB = "00000000" + Convert.ToString(indata, 2);
            DTB = DTB.Substring(DTB.Length - 8);
            return DTB;
        }

        public static byte BintoDec(string indata)
        {

            byte outbyte = (byte)Convert.ToInt32(indata, 2);
            return outbyte;

        }
        private void EditUi(string text1, string Msg)     //解析显示接收到的数据包,并对设备进行回应
        {
            if (ListBox1.Items.Count > 50) { ListBox1.Items.Clear();}
            ListBox1.Items.Add(text1 + Msg);
            ListBox1.SelectedIndex = ListBox1.Items.Count - 1;

            string DevRecFramesStr;
            string DevBufferIpAddrStr;
            string DevBufferMachinStr;
            string DevBufferCardidStr;
            string DevBufferDatetimeStr;
            string DevBufferUseMoneryStr;
            string DevBufferUseTimeStr;
            string DevBufferMaskStr;
            string DevBufferGatewayStr;
            string DevBufferRemoteAddrStr;
            string DevBufferPort;
            string DevBufferNumberDecStr;
            string DevBufferGatewayStrMAC;
            string DevBufferRemoteAddrMACStr;
            string DevBufferMACSearchStr;
            string DevBufferSerialNumStr;
            string DevBufferStaryStr;
            string DevBufferRecordAddStr;
            string DevBufferAllNoStr;
            string DevBufferInputKeyStr;
            string DevBufferCommandStr;
            string DevBufferFunctionStr1;
            string DevBufferFunctionStr2;
            string DevBufferQrcodeInfStr;
            string DevBufferReaderStr;
            string DevBufferDoorNoStr;
            string DevBufferDelayStr;
            string DevBufferPasswPropStr;
            string DevBufferPassWStr;
            string DevBufferStateStr;
            string DevBufferRandomCode;
            string DevBufferScanStr;

            string MACSTC;
            string MACCH;
            string iswifi;
            string wifidhcp;
            string Sendinf;
            int poi;

            ListViewItem plist;
 
            string[] dataArray = Msg.Split(new char[2] { ',', ',' });
            
            switch (dataArray[0])
            {
                case "100":  //发送搜索指令后,在线设备的返回信息
                    if (dataArray.Length <=8)    //2018年前的设备
                    {
                        DevBufferIpAddrStr = dataArray[1];
                        DevBufferMaskStr = dataArray[2];
                        DevBufferGatewayStr = dataArray[3];
                        DevBufferRemoteAddrStr = dataArray[4];
                        DevBufferPort = dataArray[5];
                        DevBufferNumberDecStr = dataArray[6];

                        poi = machinnos.IndexOf(DevBufferNumberDecStr);
                        if (poi < 0)
                        {
                            MACSTC = GetSTCMAC(DevBufferNumberDecStr);
                            machinnos = machinnos + DevBufferNumberDecStr;
                            plist = ListView1.Items.Add(DevBufferIpAddrStr);
                            plist.SubItems.Add(DevBufferMaskStr);
                            plist.SubItems.Add(DevBufferGatewayStr);
                            plist.SubItems.Add("");
                            plist.SubItems.Add(DevBufferRemoteAddrStr);
                            plist.SubItems.Add("");
                            plist.SubItems.Add("");
                            plist.SubItems.Add(DevBufferPort);
                            plist.SubItems.Add(DevBufferNumberDecStr);
                            plist.SubItems.Add("否");
                            plist.SubItems.Add(MACSTC);
                        }
                    }

                    if (dataArray.Length  >= 9)    //2018年以后的设备
                    {
                        DevBufferIpAddrStr = dataArray[1];
                        DevBufferMaskStr= dataArray[2];
                        DevBufferGatewayStr = dataArray[3];
                        DevBufferGatewayStrMAC = dataArray[4];
                        DevBufferRemoteAddrStr = dataArray[5];
                        DevBufferRemoteAddrMACStr = dataArray[6];
                        DevBufferMACSearchStr = dataArray[7];
                        DevBufferPort = dataArray[8];
                        DevBufferNumberDecStr = dataArray[9];

                        MACSTC = GetSTCMAC(DevBufferNumberDecStr);

                        if ((DEC_To_Bin(Convert.ToByte(DevBufferMACSearchStr))).Substring(0, 1) == "1") { iswifi = "WiFi";} else{ iswifi = "有线"; }
                        if ((DEC_To_Bin(Convert.ToByte(DevBufferMACSearchStr))).Substring(5, 1) == "1") { wifidhcp = "DHCP"; }else{ wifidhcp = "静态ip";}

                        if (dataArray.Length >= 10)
                        {
                            DevBufferSerialNumStr = dataArray[10];
                            MACCH = GetCHMAC(DevBufferSerialNumStr);
                        }
                        else{
                            DevBufferSerialNumStr = "";
                            MACCH = "";
                        }

                        poi = machinnos.IndexOf(DevBufferNumberDecStr);
                        if( poi < 0){
                            machinnos = machinnos + DevBufferNumberDecStr;
                            plist = ListView1.Items.Add(DevBufferIpAddrStr);
                            plist.SubItems.Add(DevBufferMaskStr);
                            plist.SubItems.Add(DevBufferGatewayStr);
                            plist.SubItems.Add(DevBufferGatewayStrMAC);
                            plist.SubItems.Add(DevBufferRemoteAddrStr);
                            plist.SubItems.Add(DevBufferRemoteAddrMACStr);
                            plist.SubItems.Add(DevBufferMACSearchStr);
                            plist.SubItems.Add(DevBufferPort);
                            plist.SubItems.Add(DevBufferNumberDecStr);
                            plist.SubItems.Add("是");
                            plist.SubItems.Add(MACSTC);
                            plist.SubItems.Add(DevBufferSerialNumStr);
                            plist.SubItems.Add(iswifi);
                            plist.SubItems.Add(wifidhcp);
                            plist.SubItems.Add(MACCH);
                        }
                    }
                    break;

                case "101":             //接收到 1、终端开机时向电脑发送的开机信息,         2、电脑发送002查询设备时间所返回的信息
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferDatetimeStr = dataArray[5];       //日期时间*/
                    if (dataArray.Length  > 6)
                    {               //2018年以后的设备有唯一硬件序号*/
                            DevBufferSerialNumStr = dataArray[6];
                    }
                    Response("001," + DevRecFramesStr);       //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次

                    string SystemTimeStr = DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss");
                    if( DevBufferDatetimeStr.Substring(0, 16) != SystemTimeStr.Substring(0, 16) )  //判断设备时间是否正确,不正确就更正时间
                    {
                        Response("003," + DevBufferMachinStr+","+SystemTimeStr);//更新设备时钟
                    }
                    TextBox3.Text = DevBufferDatetimeStr;
                    break;

                case "102":
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferCardidStr = dataArray[5];         //十位物理卡号
                    if (dataArray.Length > 6)
                    {                            //2018年以后的设备有唯一硬件序号*/
                            DevBufferSerialNumStr = dataArray[6];
                    }
                    Response("001," + DevRecFramesStr);       //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次

                    //此处加入业务对数据库的查、增、删、减操作

                    Response("009," + DevBufferMachinStr + "," + "{卡号:}" + DevBufferCardidStr + "\\n" + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString()  + "," + BeepCode + "," + VoiceCode);  //回应显示文字、蜂鸣响声、语音。
                    break;

                case "103":                                    //按消费金额后刷卡 向电脑发送此信息
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferCardidStr = dataArray[5];         //十位物理卡号
                    DevBufferUseMoneryStr =  dataArray[6];     //消费额
                    DevBufferUseTimeStr = dataArray[7] ;       //消费时间
                    if (dataArray.Length > 8)
                    {                                          //2018年以后的设备有唯一硬件序号*/
                        DevBufferSerialNumStr = dataArray[8];
                    }
                    Response("001," + DevRecFramesStr);       //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次

                    //此处加入业务对数据库的查、增、删、减操作

                    //008指令返回本次消费成功并播消费金额,006指令返回其他提示信息,正式系统开发时要有重发机制*
                    switch(VoiceCode)
                    {
                        case "1":           //提示消费成功并显示消费金额
                             Sendinf = "008," + DevBufferMachinStr + "," + DevBufferCardidStr + "," + DevBufferUseMoneryStr + "," + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString()  + "," + BeepCode + "," + VoiceCode;
                             break;
                        default:            //其它提示
                             Sendinf = "006," + DevBufferMachinStr + "," + DevBufferCardidStr + "," + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString () + "," + BeepCode + "," + VoiceCode;
                             break;
                    }
                    Response(Sendinf);
                    break;

                case "104":                                    //计次模式下刷卡 向电脑发送此信息
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferCardidStr = dataArray[5];         //十位物理卡号
                    DevBufferUseTimeStr = dataArray[6];       //消费时间
                    if (dataArray.Length > 7)
                    {                                          //2018年以后的设备有唯一硬件序号*/
                        DevBufferSerialNumStr = dataArray[7];
                    }
                    Response("001," + DevRecFramesStr);       //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次

                    //此处加入业务对数据库的查、增、删、减操作

                    //008指令返回本次消费成功并播消费金额,006指令返回其他提示信息,正式系统开发时要有重发机制*
                    switch (VoiceCode)
                    {
                        case "1":           //提示消费成功并显示消费金额
                            Sendinf = "008," + DevBufferMachinStr + "," + DevBufferCardidStr + ",0," + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString() + "," + BeepCode + "," + VoiceCode;
                            break;
                        default:            //其它提示
                            Sendinf = "006," + DevBufferMachinStr + "," + DevBufferCardidStr + "," + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString() + "," + BeepCode + "," + VoiceCode;
                            break;
                    }
                    Response(Sendinf);
                    break;

                case "105":                                    //接收到 脱网记账消费记录,消费机开启记账功能,有脱机记账消费记录网络恢复正常自动向电脑发送此信息
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferCardidStr = dataArray[5];         //十位物理卡号
                    DevBufferUseMoneryStr = dataArray[6];      //消费额
                    DevBufferUseTimeStr = dataArray[7];        //消费时间
                    DevBufferStaryStr = dataArray[8];          //消费方式  0扣费 1计次*/
                    DevBufferRecordAddStr = dataArray[9];      //记录地址标识*/
                    DevBufferAllNoStr = dataArray[10];          //总计录数*/
                    if (dataArray.Length > 11)
                    {                                          //2018年以后的设备有唯一硬件序号*/
                        DevBufferSerialNumStr = dataArray[11];
                    }
                    Response("001," + DevRecFramesStr);       //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次

                    if (!checkDateTime(DevBufferUseTimeStr)) { DevBufferUseTimeStr = "9999-99-99 99:99:99"; } //如果接收到非法的消费时间,将消费时间改为"9999-99-99 99:99:99"才能清除。

                    //此处加入业务对数据库的查、增、删、减操作

                    if (CheckBox3.Checked)  //清除设备内当前这条已上传的记录,驱动设备继续上传剩下的记录
                    {
                        Sendinf = "011," + DevBufferMachinStr + "," + DevBufferCardidStr + "," + DevBufferUseMoneryStr + "," + DevBufferUseTimeStr + "," + DevBufferRecordAddStr;
                        Response(Sendinf);
                    }
                    break;

                case "106":                                    //接收到设备的按键信息
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferInputKeyStr = dataArray[5];       //十位物理卡号
                    if (dataArray.Length > 6)
                    {                                           //2018年以后的设备有唯一硬件序号*/
                        DevBufferSerialNumStr = dataArray[6];
                    }
                    Response("001," + DevRecFramesStr);         //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次
                    //此处加入业务对数据库的查、增、删、减操作
                    break;

                case "108":                                    //接收到设备在取餐模式内的的刷卡信息
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferCardidStr = dataArray[5];         //十位物理卡号
                    if (dataArray.Length > 6)
                    {                                          //2018年以后的设备有唯一硬件序号*/
                        DevBufferSerialNumStr = dataArray[6];
                    }
                    Response("001," + DevRecFramesStr);       //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次

                    //此处加入业务对数据库的查、增、删、减操作

                    //008指令返回本次消费成功并播消费金额,006指令返回其他提示信息,正式系统开发时要有重发机制*
                    switch (VoiceCode)
                    {
                        case "1":           //提示消费成功并显示消费金额
                            Sendinf = "008," + DevBufferMachinStr + "," + DevBufferCardidStr + ",0," + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString() + "," + BeepCode + "," + VoiceCode;
                            break;
                        default:            //其它提示
                            Sendinf = "006," + DevBufferMachinStr + "," + DevBufferCardidStr + "," + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString() + "," + BeepCode + "," + VoiceCode;
                            break;
                    }
                    Response(Sendinf);
                    break;

                case "109":                                    //设备收到电脑发送的显示声音指令 后 反发的确认指令,可根据这个返回来判断设备有没有收到电脑发出的指令,如没有收到电脑可再次发送
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferCommandStr = dataArray[5];        //指令号
                    //此处根据接收到的设备信息,表明设备已接收到发送的信息,取消该设备的重发机制
                    break;

                case "115":                                    //传送注册卡号到顺序区的反馈 
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferFunctionStr1 = dataArray[5];      //反馈标识

                    Timer2.Enabled = false;  //超时提示清除
                    if (sendcode == "015")   //只传一帧数据
                    {
                        if (DevBufferFunctionStr1 == "0" || DevBufferFunctionStr1 == "1")
                        {
                            f3.TextBox2.Text = "卡号为:" + f3.TextBox1.Text + "的注册卡传送到IP地址为:" + DevBufferIpAddrStr + " 设备的顺序区成功!";
                        }
                        else { f3.TextBox2.Text = "卡号为:" + f3.TextBox1.Text + "的注册卡传送到IP地址为:" + DevBufferIpAddrStr + " 设备的顺序区失败!"; }
                    }
                    else
                    {
                        if (DevBufferFunctionStr1 == "0" || DevBufferFunctionStr1 == "1" || DevBufferFunctionStr1 == "2" || DevBufferFunctionStr1 == "3")
                        {
                            SendPoin++;               //发送指针加1
                            SendAgain = 0;            //重发次数清0
                            f3.Pb1.Value++;
                        }
                        else
                        {
                            SendAgain++;              //重发次数+1
                            if (SendAgain == 3) { SendCount = false; } //重发次数超过3次,退出
                        }
                        if (SendPoin <= SendPoinMax && SendCount)
                        {
                            Timer2.Enabled = true;
                            Sendinf = SendBufStr[SendPoin];
                            Response(Sendinf);    //继续发送
                        }
                        else
                        {
                            f3.Button8.Text = "将注册卡号传送到设备的顺序区";
                            f3.Button9.Enabled = true;
                            if (SendPoin >= SendPoinMax)
                            {
                                f3.TextBox2.Text = "注册卡号已成功地传送到IP地址为:" + DevBufferIpAddrStr + " 设备的顺序区!";
                                f3.Refresh();
                            }
                            else
                            {
                                f3.TextBox2.Text = "传送注册卡号到IP地址为:" + DevBufferIpAddrStr + " 设备时中途退出!请重新传送!";
                            }
                        }
                    }
                    break;

                case "116":                                    //传送注册卡号到随机区的反馈 
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferFunctionStr1 = dataArray[5];      //反馈标识

                    Timer2.Enabled = false;  //超时提示清除
                    if (sendcode == "016")
                    {
                        if (DevBufferFunctionStr1 == "0" || DevBufferFunctionStr1 == "1")
                        {
                            f3.TextBox2.Text = "卡号为:" + f3.TextBox1.Text + "的注册卡传送到IP地址为:" + DevBufferIpAddrStr + " 设备的随机区成功!";
                        }
                        else { f3.TextBox2.Text = "卡号为:" + f3.TextBox1.Text + "的注册卡传送到IP地址为:" + DevBufferIpAddrStr + " 设备的随机区失败!"; }
                    }
                    else
                    {
                        if (DevBufferFunctionStr1 == "0" || DevBufferFunctionStr1 == "1" || DevBufferFunctionStr1 == "2" || DevBufferFunctionStr1 == "3")
                        {
                            SendPoin++;           //发送指针加1
                            SendAgain = 0;        //重发次数清0
                            f3.Pb1.Value++;
                        }
                        else
                        {
                            SendAgain++;         //重发次数+1
                            if (SendAgain == 3) { SendCount = false; }   //重发次数超过3次,退出
                        }
                        if (SendPoin <= SendPoinMax && SendCount)
                        {
                            Timer2.Enabled = true;
                            Sendinf = SendBufStr[SendPoin];
                            Response(Sendinf);
                        }
                        else
                        {
                            f3.Button9.Text = "将注册卡号传送到设备的随机区";
                            f3.Button8.Enabled = true;
                            if (SendPoin >= SendPoinMax)
                            {
                                f3.TextBox2.Text = "注册卡号已成功地传送到IP地址为:" + DevBufferIpAddrStr + " 设备的随机区!";
                            }
                            else
                            {
                                f3.TextBox2.Text = "传送注册卡号到IP地址为:" + DevBufferIpAddrStr + " 设备时中途退出!请重新传送!";
                            }
                        }
                    }
                    break;

                case "117":                                    //删除设备内指定注册卡号的反馈 
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferFunctionStr1 = dataArray[5];      //反馈标识

                    if (DevBufferFunctionStr1 == "0")
                    {
                        f3.TextBox2.Text = "删除IP地址为:" + DevBufferIpAddrStr + " 设备内卡号为:" + f3.TextBox1.Text + " 的注册卡成功!";
                    }
                    else { f3.TextBox2.Text = "删除IP地址为:" + DevBufferIpAddrStr + " 设备内卡号为:" + f3.TextBox1.Text + " 的注册卡失败!"; }
                    break;

                case "118":                                    //清除顺序区注册号的反馈 
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferFunctionStr1 = dataArray[5];      //反馈标识

                    if (DevBufferFunctionStr1 == "0")
                    {
                        f3.TextBox2.Text = "清除IP地址为:" + DevBufferIpAddrStr + " 设备内顺序区的注册卡成功!";
                    }
                    else { f3.TextBox2.Text = "清除IP地址为:" + DevBufferIpAddrStr + " 设备内顺序区的注册卡失败!"; }
                    break;

                case "119":                                    //清除随机区注册号的反馈 
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferFunctionStr1 = dataArray[5];      //反馈标识

                    if (DevBufferFunctionStr1 == "0")
                    {
                        f3.TextBox2.Text = "清除IP地址为:" + DevBufferIpAddrStr + " 设备内随机区的注册卡成功!";
                    }
                    else { f3.TextBox2.Text = "清除IP地址为:" + DevBufferIpAddrStr + " 设备内随机区的注册卡失败!"; }
                    break;

                case "120":                                    //查询指定的注册卡号是否存在的反馈
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferFunctionStr1 = dataArray[5];      //反馈标识

                    if (DevBufferFunctionStr1 == "0")
                    {
                        f3.TextBox2.Text = "IP地址为:" + DevBufferIpAddrStr + " 设备内存在卡号为:" + f3.TextBox1.Text + " 的注册卡!";
                    }
                    else { f3.TextBox2.Text = "IP地址为:" + DevBufferIpAddrStr + " 设备内不存在卡号为:" + f3.TextBox1.Text + " 的注册卡!"; }
                    break;

                case "121":                                    //读设备内注册卡总数的反馈
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferFunctionStr1 = dataArray[5];      //反馈标识1
                    DevBufferFunctionStr2 = dataArray[6];      //反馈标识2

                    f3.TextBox2.Text = "IP地址为:" + DevBufferIpAddrStr + " 设备,顺序区内的注册卡数为:" + DevBufferFunctionStr1 + ",随机区内的注册卡数为:" + DevBufferFunctionStr2;
                    break;

                case "125":                                    //接收到直接扫码信息
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferCommandStr= dataArray[5];         //二维码格式标识
                    DevBufferQrcodeInfStr = "";
                    DevBufferSerialNumStr = "";
                    if (dataArray.Length > 6)
                    {                                          //防止二维码信息中有分隔符,
                        int i = 6;
                        while (i < dataArray.Length-1)
                        {
                            DevBufferQrcodeInfStr = DevBufferQrcodeInfStr + dataArray[i];
                            i++;
                        }
                        DevBufferSerialNumStr = dataArray[dataArray.Length - 1];
                    }
                    Response("001," + DevRecFramesStr);       //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次

                    Sendinf = "009," + DevBufferMachinStr + ",扫码信息:" + DevBufferQrcodeInfStr + "\\n,"+ NumericUpDown2.Value.ToString()+",1,0";
                    Response(Sendinf);
                    break;

                case "126":                                    //返回无按键实时考勤门禁机  写参数指令反馈
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferFunctionStr1 = dataArray[5];      //返回值

                    if (sendcode == "026008")
                    {
                        switch (DevBufferFunctionStr1)
                        {
                            case "0":
                                f5.ListBox1.Items.Add("IP地址为:" + DevBufferIpAddrStr + "的设备,WIFI热点参数传送成功!30秒后可查询WIFI连接状态。");
                                break;
                            case "1":
                                f5.ListBox1.Items.Add("IP地址为:" + DevBufferIpAddrStr + "的设备,广播式发送成功!");
                                break;
                            case "2":
                                f5.ListBox1.Items.Add("IP地址为:" + DevBufferIpAddrStr + "的设备,无线通讯失败!");
                                break;
                            case "4":
                                f5.ListBox1.Items.Add("IP地址为:" + DevBufferIpAddrStr + "的设备,参数设置指令的数据错误!");
                                break;
                            case "5":
                                f5.ListBox1.Items.Add("IP地址为:" + DevBufferIpAddrStr + "的设备,必须按主板上的RST键3秒,将密码清空后才能再更改通讯密码!");
                                break;
                        }
                    }
                    break;

                case "127":                                    //返回无按键实时考勤门禁机  读参数指令反馈
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferFunctionStr1 = dataArray[5];      //返回值
                    switch (DevBufferFunctionStr1)
                    {
                        case "0":
                            for (int i = 6; i < dataArray.Length; i++)
                            {
                                infparsing(dataArray[i]);
                            }
                            break;
                        case "1":
                            f5.ListBox1.Items.Add("不能以广播式发送!");
                            break;
                        case "2":
                            f5.ListBox1.Items.Add("无线通讯失败!");
                            break;
                        default:
                            if (DevBufferFunctionStr1.Substring(0, 2) == "8=")
                            {
                                DevBufferFunctionStr1 = DevBufferFunctionStr1.Substring(2, DevBufferFunctionStr1.Length  - 2);
                                string[] wifiinf = DevBufferFunctionStr1.Split(new char[2] { '&', '&' });
                                for(int i=0;i 9)
                    {                                          //防止二维码信息中有分隔符,
                        int i = 9;
                        while (i < dataArray.Length - 1)
                        {
                            DevBufferQrcodeInfStr = DevBufferQrcodeInfStr + dataArray[i];
                            i++;
                        }
                        DevBufferSerialNumStr = dataArray[dataArray.Length - 1];
                    }
                    Response("001," + DevRecFramesStr);       //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次

                    //此处加入业务对数据库的查、增、删、减操作

                    Sendinf = "008," + DevBufferMachinStr + "," + DevBufferCardidStr + "," + DevBufferUseMoneryStr + ",正在进行扫码支付..{请不要按动键盘!\\n}," + NumericUpDown2.Value.ToString() + ",0,1";
                    Response(Sendinf);
                    break;

                case "150":                                    //接收到实时考勤门禁机刷卡数据
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferCardidStr = dataArray[5];         //十位物理卡号
                    DevBufferReaderStr= dataArray[6];          //读头号
                    DevBufferDoorNoStr = dataArray[7];         //门或继电器号*/
                    DevBufferDelayStr = dataArray[8];          //门磁未关秒数,0表示已关*/
                    DevBufferPasswPropStr = dataArray[9];      //密码属性(0密码无效,1刷卡后按密码,2补签密码,3开门密码,4防迫胁密码)*/
                    DevBufferPassWStr = dataArray[10];         //密码(最多8位数字)*/
                    DevBufferUseTimeStr = dataArray[11];       //刷卡时间                        
                    if(dataArray.Length > 12){
                            DevBufferSerialNumStr = dataArray[12];  //2018年以后的设备有唯一硬件序号
                    }
                    Response("001," + DevRecFramesStr);       //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次

                    //此处加入业务对数据库的查、增、删、减操作
                    //用050指令返回开门成功,051指令返回开门失败,正式系统开发时要有重发机制

                    switch(  VoiceCode)
                    {
                        case "0":
                        case "1":
                        case "2":
                        case "3":
                        case "4":
                        case "5":
                        case "6":
                        case "7":
                        case "8":
                        case "9":
                        case "10":
                        case "11":
                        case "12":
                        case "13":
                        case "14":
                        case "15":  //提示警告不开门
                            Sendinf = "051," + DevBufferMachinStr + "," + DevBufferCardidStr + "," + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString() + "," + BeepCode + "," + VoiceCode;   
                            break;
                        default :   //显示并开门
                            Sendinf = "050," + DevBufferMachinStr + "," + DevBufferCardidStr + "," + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString() + "," + BeepCode + "," + VoiceCode + "," + DevBufferReaderStr + "," + DevBufferDoorNoStr + "," + NumericUpDown1.Value.ToString() + "," + NumericUpDown2.Value.ToString(); 
                            break;
                    }
                    Response(Sendinf);
                    break;

                case "151":                                    //接收到实时考勤门禁机按钮开门申请信息
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferInputKeyStr = dataArray[5];       //按钮号
                    DevBufferReaderStr = dataArray[6];         //对应的门或继电器号
                    DevBufferDelayStr = dataArray[7];          //门磁未关秒数(0门已关)
                    DevBufferUseTimeStr = dataArray[8];        //按键时间                        
                    if (dataArray.Length > 9)
                    {
                        DevBufferSerialNumStr = dataArray[9];  //2018年以后的设备有唯一硬件序号
                    }
                    Response("001," + DevRecFramesStr);       //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次

                    //此处加入业务对数据库的查、增、删、减操作
                    //用050指令返回开门成功,051指令返回开门失败,正式系统开发时要有重发机制

                    switch (VoiceCode)
                    {
                        case "0":
                        case "1":
                        case "2":
                        case "3":
                        case "4":
                        case "5":
                        case "6":
                        case "7":
                        case "8":
                        case "9":
                        case "10":
                        case "11":
                        case "12":
                        case "13":
                        case "14":
                        case "15":  //提示警告不开门
                            Sendinf = "051," + DevBufferMachinStr + "," + DevBufferInputKeyStr + "," + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString() + "," + BeepCode + "," + VoiceCode;
                            break;
                        default:   //显示并开门
                            Sendinf = "050," + DevBufferMachinStr + "," + DevBufferInputKeyStr + "," + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString() + "," + BeepCode + "," + VoiceCode + "," + DevBufferReaderStr + "," +  NumericUpDown1.Value.ToString() + "," + NumericUpDown2.Value.ToString();
                            break;
                    }
                    Response(Sendinf);
                    break;

                case "152":                                    //接收到实时考勤门禁机门磁报警信息
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferInputKeyStr = dataArray[5];       //门磁号
                    DevBufferReaderStr = dataArray[6];         //对应的门或继电器号
                    DevBufferDelayStr = dataArray[7];          //门磁未关秒数(0门已关)
                    DevBufferUseTimeStr = dataArray[8];        //设备时间                        
                    if (dataArray.Length > 9)
                    {
                        DevBufferSerialNumStr = dataArray[9];  //2018年以后的设备有唯一硬件序号
                    }
                    Response("001," + DevRecFramesStr);       //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次
                    break;

                case "153":                                    //接收到实时考勤门禁机查询设备状态的053指令反馈
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferInputKeyStr = dataArray[5];       //门及继电器号
                    DevBufferReaderStr = dataArray[6];         //继电器状态
                    DevBufferDelayStr = dataArray[7];          //门磁未关秒数(0门已关)
                    DevBufferStateStr = dataArray[8];          //门磁报警状态                        
                    if (dataArray.Length > 9)
                    {
                        DevBufferSerialNumStr = dataArray[9];  //2018年以后的设备有唯一硬件序号
                    }
                    Response("001," + DevRecFramesStr);       //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次

                    f6.TextBox3.Text = "继电器号:" + DevBufferInputKeyStr + ",继电器状态:" + DevBufferReaderStr + ",门磁未关秒数:" + DevBufferDelayStr + ",门磁报警状态:" + DevBufferStateStr;
                    break;

                case "154":                                    //接收到实时考勤门禁机卡离开感应区
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferCardidStr = dataArray[5];         //十位物理卡号
                    DevBufferSerialNumStr = dataArray[6];      //设备有唯一硬件序号

                    break;

                case "155":                                    //接收到实时考勤门禁机脱网记录
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferCardidStr = dataArray[5];         //十位物理卡号
                    DevBufferReaderStr = dataArray[6];         //读头号
                    DevBufferDoorNoStr = dataArray[7];         //门或继电器号*/
                    DevBufferPasswPropStr = dataArray[8];      //密码属性(0密码无效,1刷卡后按密码,2补签密码,3开门密码,4防迫胁密码)*/
                    DevBufferUseTimeStr = dataArray[9];        //刷卡时间    
                    DevBufferStaryStr = dataArray[10];         //消费方式  0扣费 1计次*/
                    DevBufferRecordAddStr = dataArray[11];     //记录地址标识*/
                    DevBufferAllNoStr = dataArray[12];         //总计录数*/
                    DevBufferSerialNumStr = "";
                    if (dataArray.Length > 12)
                    {
                        DevBufferSerialNumStr = dataArray[13];  //2018年以后的设备有唯一硬件序号
                    }
                    Response("001," + DevRecFramesStr);         //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次

                    if (!checkDateTime(DevBufferUseTimeStr)) { DevBufferUseTimeStr = "9999-99-99 99:99:99"; } //如果接收到非法的消费时间,将消费时间改为"9999-99-99 99:99:99"才能清除。

                    //此处加入这里对脱网记录进行处理,对数据库的查、增、删、减操作

                    if (CheckBox3.Checked)  //清除设备内当前这条已上传的记录,驱动设备继续上传剩下的记录
                    {
                        Sendinf = "055," + DevBufferMachinStr + "," + DevBufferCardidStr + ","  + DevBufferUseTimeStr + "," + DevBufferRecordAddStr;
                        Response(Sendinf);
                    }
                    break;

                case "158":                                    //接收到实时考勤门禁机远程开门058指令的反馈
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferStaryStr = dataArray[5];          //0开门成功,1开门密码认证失败,2开门密码认证失败超过6次,需等待一段时间后再能再次接收058指令(防暴力破解),3 不允许此时间段开门
                    DevBufferRandomCode = dataArray[6];        //动态随机码(8位十六进制)

                    switch (DevBufferStaryStr)
                    {
                        case "0":
                            f6.TextBox3.Text = "IP:" + DevBufferIpAddrStr + " 开门成功";
                            f6.TextBox2.Text = DevBufferRandomCode;
                            break;
                        case "1":
                            f6.TextBox3.Text = "IP:" + DevBufferIpAddrStr + " 开门密码认证失败!";
                            f6.TextBox2.Text = DevBufferRandomCode;
                            break;
                        case "2":
                            f6.TextBox3.Text = "IP:" + DevBufferIpAddrStr + " 开门密码认证失败超过6次,需等待一段时间后再能再次接收058指令(防暴力破解)";
                            f6.TextBox2.Text = DevBufferRandomCode;
                            break;
                        case "3":
                            f6.TextBox3.Text = "IP:" + DevBufferIpAddrStr + " 不允许此时间段开门";
                            f6.TextBox2.Text = DevBufferRandomCode;
                            break;
                    }
                    break;

                case "159":                                    //接收到实时考勤门禁机心跳信息
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferQrcodeInfStr = dataArray[5];      //信息类型
                    DevBufferFunctionStr1 = dataArray[6];      //继电器状态(bit0为第1门bit1为第2门)
                    DevBufferFunctionStr2 = dataArray[7];      //消防输入状态
                    DevBufferRandomCode = dataArray[8];        //&动态随机码(8位十六进制)
                    DevBufferDatetimeStr = dataArray[9];       //日期时间
                    DevBufferSerialNumStr = dataArray[10];     //设备硬件号

                    try
                    {
                        f6.TextBox2.Text = DevBufferRandomCode;
                    }
                    catch { }
                    break;

                case "170":                                    //接收门禁外接双USB扫描枪模块
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferIpAddrStr = dataArray[2];         //终端IP
                    DevBufferRemoteAddrStr = dataArray[3];     //远程电脑指机IP
                    DevBufferMachinStr = dataArray[4];         //机号
                    DevBufferScanStr = dataArray[5];           //扫码信息
                    DevBufferReaderStr= dataArray[6];          //对应门的扫描枪号
                    DevBufferDoorNoStr = dataArray[7];         //门或继电器号*/
                    DevBufferDelayStr = dataArray[8];          //门磁未关秒数,0表示已关*/
                    DevBufferUseTimeStr = dataArray[9];        //扫码时间                        
                    DevBufferSerialNumStr = dataArray[10];     //设备有唯一硬件序号

                    Response("001," + DevRecFramesStr);         //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次

                    switch(  VoiceCode)
                    {
                        case "0":
                        case "1":
                        case "2":
                        case "3":
                        case "4":
                        case "5":
                        case "6":
                        case "7":
                        case "8":
                        case "9":
                        case "10":
                        case "11":
                        case "12":
                        case "13":
                        case "14":
                        case "15":  //提示警告不开门
                            Sendinf = "051," + DevBufferMachinStr + "," + DevBufferReaderStr + "," + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString() + "," + BeepCode + "," + VoiceCode;   
                            break;
                        default :   //显示并开门
                            Sendinf = "050," + DevBufferMachinStr + ",0," + TextBox5.Text.Trim() + "," + NumericUpDown2.Value.ToString() + "," + BeepCode + "," + VoiceCode + "," + DevBufferReaderStr + "," + DevBufferDoorNoStr + "," + NumericUpDown1.Value.ToString() + "," + NumericUpDown2.Value.ToString(); 
                            break;
                    }
                    Response(Sendinf);
                    break;

                case "198":                                    //接收到消费取消指令
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferRecordAddStr = dataArray[2];      //消费流水号
                    Response("001," + DevRecFramesStr);        //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次
                    break;

                case "199":                                    //消费金额发起指令
                    DevRecFramesStr = dataArray[1];            //包序列号
                    DevBufferRecordAddStr = dataArray[2];      //消费流水号
                    DevBufferMachinStr = dataArray[3];         //机号
                    DevBufferUseMoneryStr = dataArray[4];      //消费额

                    Response("001," + DevRecFramesStr);        //向设备回应001+包序列号,表示已收到信息,否则设备会连续发三次

                    //这里执行对消费机发送消费结果的显示及声音

                    //消费结果成功0,失败1                                       余额,卡号
                    Sendinf = "099," + DevRecFramesStr + "," + DevBufferRecordAddStr + ",0," + DevBufferMachinStr + "," + DevBufferUseMoneryStr + ",888.8,1234567890";
                    Response(Sendinf);
                    break;

            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            ComboBox2.SelectedIndex = 0;
            ComboBox3.SelectedIndex = 1;
            ComboBox4.SelectedIndex = 16;
            ComboBox5.SelectedIndex = 1;

            ready = false;
            GetIp();
            StartListener();
        }

        private void GetIp()
        {
            ArrayList AllIp = new ArrayList();
            string name = Dns.GetHostName();
            IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
            foreach (IPAddress ipa in ipadrlist)
            {
                if (ipa.AddressFamily == AddressFamily.InterNetwork)
                    localIp = ipa.ToString();
                if (checkip(localIp))
                {
                    AllIp.Add(localIp);
                    ComboBox1.Items.Add(localIp);
                }
            }
            if (ComboBox1.Items.Count > 0)
            {
                ComboBox1.SelectedIndex = 0;
                localIp = ComboBox1.Text; 
            }
            else {
                MessageBox.Show("未能查找到本台电脑安装的网卡,暂不能启动本软件。", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                System.Environment.Exit(0);              //这是最彻底的退出方式,不管什么线程都被强制退出,把程序结束的很干净。
            }
        }

        public static bool checkip(string ipstr)   //判断IP是否合法
        {
            IPAddress ip;
            if (IPAddress.TryParse(ipstr, out ip))
            { return true; }
            else { return false; }
        }

        private void StartListener()
        {
            IPEndPoint LocalPoint;
            PortNumber = Convert.ToInt32(TextBox6.Text) ;
            while (!ready)
            {
                try
                {
                    LocalPoint = new IPEndPoint(IPAddress.Parse(localIp), PortNumber);
                    ListenerSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    ListenerSock.Bind(LocalPoint);
                    
                    ListenerThre = new Thread(new ThreadStart(ThrListener));
                    ListenerThre.Start();
                    ready = true;
                }
                catch 
                {
                    ListenerSock.Close();
                    ready = false;
                    MessageBox.Show("有其它应用程序占用了 " + PortNumber.ToString() + " ,请检查并关闭此应用后再打开本程序", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    System.Environment.Exit(0);                      //这是最彻底的退出方式,不管什么线程都被强制退出,把程序结束的很干净。
                }
            }
        }

        public  void ThrListener()
        {
            while (ready)
            {
                try
                {
                    EndPoint RemotePoint = new IPEndPoint(System.Net.IPAddress.Any, 0);
                    byte[] bytes = new byte[1024];
                    int NumGet = ListenerSock.ReceiveFrom(bytes, ref RemotePoint);

                    string Msg = Encoding.GetEncoding(936).GetString(bytes, 0, NumGet);
                    //string Msg = Encoding.UTF8.GetString(bytes, 0, NumGet);             //读wifi参数时,要用utf8编码

                    string dispstr = DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") + (" FromIP:" + Convert.ToString(RemotePoint) + "          ").Substring(0, 30) + "Data:";
                    RemoteIPoint =(IPEndPoint)RemotePoint;                      //获取数据包来源IP及端口,原路回应
                    this.BeginInvoke(new Update1(EditUi), dispstr, Msg);        //显示接收到的数据包,并根据情况回应设备            
                }
                catch(Exception ex )
                {
                    this.BeginInvoke(new Update1(EditUi), DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss")+" ",ex.Message  );
                }
            }
        }

你可能感兴趣的:(实时消费机,18002295132,QQ:954486673,C#Socket通讯,C#UDP通讯,C#实时门禁控制源码,C#实时消费机源码,C#网络读卡器源码)