ashx后台获取GET、POST、JSON方式提交的刷卡信息,并回应驱动读卡器显示文字播报语音
  bHlya0zM3Kzo 2023年11月12日 87 0


 

ashx后台获取GET、POST、JSON方式提交的刷卡信息,并回应驱动读卡器显示文字播报语音_json

本示例使用设备: 

<%@ WebHandler Language="C#" Class="HttpReader" %>

using System;
using System.Web;
using System.IO;
using Newtonsoft.Json;

public class HttpReader : IHttpHandler
{    
    public void ProcessRequest (HttpContext context) {        
        string info = "";
        string jihao = "";
        string cardtype = "";
        string card = "";
        string Data = "";
        string dn = "";
        string Status = "";
        Int16 cardtype16 = 0;
        int cardtypecode = 0;
        int pushortake = 0;
        string dispstr = "";
        string ChineseVoice = "[v8]";    //[v8]表示本次播报语音音量,取值范围v1 到 v16

        context.Response.ContentType = "text/plain";
        
        if (context.Request["info"] != null) { info = context.Request["info"].ToString(); }             //接收到的数据包号,需回应该包号
        if (context.Request["jihao"] != null) { jihao = context.Request["jihao"].ToString(); }          //设备机号(可自编)
        if (context.Request["cardtype"] != null) { cardtype = context.Request["cardtype"].ToString(); } //卡类型,卡状态
        if (context.Request["card"] != null) { card = context.Request["card"].ToString(); }             //接收到的原始16进制卡号,可根据需要自行转换成其他卡号
        if (context.Request["Data"] != null) { Data = context.Request["Data"].ToString(); }             //扇区内容
        if (context.Request["dn"] != null) { dn = context.Request["dn"].ToString(); }                   //设备硬件序列号,出厂时已固化,全球唯一
        if (context.Request["Status"] != null) { Status = context.Request["Status"].ToString(); }       //读卡状态,如12表示卡密码认证失败    
        
        if (info != "" && jihao != "" && cardtype != "" && card != ""){
                cardtype16 = Convert.ToInt16(cardtype, 16);
                pushortake = cardtype16 / 128;       //pushortake=0 表示读卡,>0表示卡离开感应区
                cardtypecode = cardtype16 % 16;      //cardtypecode=1 ID卡,2 HID卡,3 T5557卡,4 EM4305卡,5 IC卡,6 二代身份证,7 是15693卡,IClass"                
         }
         else{    //如未获取到有效参数,使用JSON方式解析获取提交的参数
                StreamReader sr = new StreamReader(context.Request.GetBufferlessInputStream());
                string response = sr.ReadToEnd();
            
                RootObject rb = JsonConvert.DeserializeObject<RootObject>(response);
                info = rb.info;            //接收到的数据包号,需回应该包号
                jihao = rb.jihao;          //设备机号(可自编)
                cardtype = rb.cardtype;    //卡类型,卡状态
                card = rb.card;            //接收到的原始16进制卡号,可根据需要自行转换成其他卡号
                Data = rb.data;            //扇区内容
                dn = rb.dn;                //设备硬件序列号,出厂时已固化,全球唯一
                Status = rb.status;        //读卡状态,如12表示卡密码认证失败

                if (info != "" && jihao != "" && cardtype != "" && card != "")
                {
                    cardtype16 = Convert.ToInt16(cardtype, 16);
                    pushortake = cardtype16 / 128;       //pushortake=0 表示读卡,>0表示卡离开感应区
                    cardtypecode = cardtype16 % 16;      // cardtypecode=1 ID卡,2 HID卡,3 T5557卡,4 EM4305卡,5 IC卡,6 二代身份证,7 是15693卡,IClass"                     
                }
        }

        if (info != "" && card != "")    //通过解析获取到了有效的参数,回应驱动读卡器显示文字、蜂鸣响声或播报语音
        {
            dispstr = "{" + getChinesecode("卡号") + ":}" + (card + "      ").Substring(0, 12) + DateTime.Now.ToString("yy-MM-dd HH:mm:ss"); //显示信息,注意中文汉字一定要转换为设备能显示的编码,其它字母数字符号不需要转换,{}内的信息反白显示

            if (pushortake > 0)
            {
                ChineseVoice = ChineseVoice + getChinesecode("卡号") + "[n1]" + card + getChinesecode("离开感应区!");     //TTS语音,注意中文汉字一定要转换为设备能识别的编码,[n1]表示数字播报方式,其它字母数字符号不需要转换              
            }
            else
            {
                ChineseVoice = ChineseVoice + getChinesecode("读取卡号") + "[n1]" + card;
            }

            string RepStr = "Response=1";             //Response=1 固定前缀,我们的设备以此来检索返回信息,表示 驱动设备显示和响声
            RepStr = RepStr + "," + info;             //提交的信息序号,一定要对应
            RepStr = RepStr + "," + dispstr;          //读卡器上显示文字
            RepStr = RepStr + ",20";                  //显示时长20秒
            RepStr = RepStr + ",2";                   //蜂鸣器发声种类,取值范围0-12
            RepStr = RepStr + "," + ChineseVoice;     //播报的TTS语音

            context.Response.Write(RepStr);
        }        
    }

    public class RootObject  //json类
    {
        public string info { get; set; }
        public string jihao { get; set; }
        public string cardtype { get; set; }
        public string card { get; set; }
        public string data { get; set; }
        public string dn { get; set; }
        public string status { get; set; }

    }

    public static string getChinesecode(string inputstr)   //获取中文编码,显示汉字、TTS中文语音都要转换编码
    {
        int strlen = inputstr.Length;
        string hexcode = "";
        for (int i = 0; i < strlen; i++)
        {
            string str = inputstr.Substring(i, 1);
            byte[] Chinesecodearry = System.Text.Encoding.GetEncoding(936).GetBytes(str);
            int codelen = (byte)Chinesecodearry.Length;
            if (codelen == 1)
            {
                hexcode = hexcode + str;
            }
            else
            {
                hexcode = hexcode + "\\x" + Chinesecodearry[0].ToString("X2") + Chinesecodearry[1].ToString("X2");
            }
        }
        return hexcode;
    } 
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月12日 0

暂无评论

推荐阅读
  lljXvtSXxgF2   2023年12月11日   18   0   0 MySQLMySQLJSONJSON
bHlya0zM3Kzo