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


     

    本示例使用设备: RFID网络WIFI无线TCP/UDP/HTTP可编程二次开发读卡器POE供电语音-淘宝网 (taobao.com)

    1. <%@ WebHandler Language="C#" Class="HttpReader" %>
    2. using System;
    3. using System.Web;
    4. using System.IO;
    5. using Newtonsoft.Json;
    6. public class HttpReader : IHttpHandler
    7. {
    8. public void ProcessRequest (HttpContext context) {
    9. string info = "";
    10. string jihao = "";
    11. string cardtype = "";
    12. string card = "";
    13. string Data = "";
    14. string dn = "";
    15. string Status = "";
    16. Int16 cardtype16 = 0;
    17. int cardtypecode = 0;
    18. int pushortake = 0;
    19. string dispstr = "";
    20. string ChineseVoice = "[v8]"; //[v8]表示本次播报语音音量,取值范围v1 到 v16
    21. context.Response.ContentType = "text/plain";
    22. if (context.Request["info"] != null) { info = context.Request["info"].ToString(); } //接收到的数据包号,需回应该包号
    23. if (context.Request["jihao"] != null) { jihao = context.Request["jihao"].ToString(); } //设备机号(可自编)
    24. if (context.Request["cardtype"] != null) { cardtype = context.Request["cardtype"].ToString(); } //卡类型,卡状态
    25. if (context.Request["card"] != null) { card = context.Request["card"].ToString(); } //接收到的原始16进制卡号,可根据需要自行转换成其他卡号
    26. if (context.Request["Data"] != null) { Data = context.Request["Data"].ToString(); } //扇区内容
    27. if (context.Request["dn"] != null) { dn = context.Request["dn"].ToString(); } //设备硬件序列号,出厂时已固化,全球唯一
    28. if (context.Request["Status"] != null) { Status = context.Request["Status"].ToString(); } //读卡状态,如12表示卡密码认证失败
    29. if (info != "" && jihao != "" && cardtype != "" && card != ""){
    30. cardtype16 = Convert.ToInt16(cardtype, 16);
    31. pushortake = cardtype16 / 128; //pushortake=0 表示读卡,>0表示卡离开感应区
    32. cardtypecode = cardtype16 % 16; //cardtypecode=1 ID卡,2 HID卡,3 T5557卡,4 EM4305卡,5 IC卡,6 二代身份证,7 是15693卡,IClass"
    33. }
    34. else{ //如未获取到有效参数,使用JSON方式解析获取提交的参数
    35. StreamReader sr = new StreamReader(context.Request.GetBufferlessInputStream());
    36. string response = sr.ReadToEnd();
    37. RootObject rb = JsonConvert.DeserializeObject(response);
    38. info = rb.info; //接收到的数据包号,需回应该包号
    39. jihao = rb.jihao; //设备机号(可自编)
    40. cardtype = rb.cardtype; //卡类型,卡状态
    41. card = rb.card; //接收到的原始16进制卡号,可根据需要自行转换成其他卡号
    42. Data = rb.data; //扇区内容
    43. dn = rb.dn; //设备硬件序列号,出厂时已固化,全球唯一
    44. Status = rb.status; //读卡状态,如12表示卡密码认证失败
    45. if (info != "" && jihao != "" && cardtype != "" && card != "")
    46. {
    47. cardtype16 = Convert.ToInt16(cardtype, 16);
    48. pushortake = cardtype16 / 128; //pushortake=0 表示读卡,>0表示卡离开感应区
    49. cardtypecode = cardtype16 % 16; // cardtypecode=1 ID卡,2 HID卡,3 T5557卡,4 EM4305卡,5 IC卡,6 二代身份证,7 是15693卡,IClass"
    50. }
    51. }
    52. if (info != "" && card != "") //通过解析获取到了有效的参数,回应驱动读卡器显示文字、蜂鸣响声或播报语音
    53. {
    54. dispstr = "{" + getChinesecode("卡号") + ":}" + (card + " ").Substring(0, 12) + DateTime.Now.ToString("yy-MM-dd HH:mm:ss"); //显示信息,注意中文汉字一定要转换为设备能显示的编码,其它字母数字符号不需要转换,{}内的信息反白显示
    55. if (pushortake > 0)
    56. {
    57. ChineseVoice = ChineseVoice + getChinesecode("卡号") + "[n1]" + card + getChinesecode("离开感应区!"); //TTS语音,注意中文汉字一定要转换为设备能识别的编码,[n1]表示数字播报方式,其它字母数字符号不需要转换
    58. }
    59. else
    60. {
    61. ChineseVoice = ChineseVoice + getChinesecode("读取卡号") + "[n1]" + card;
    62. }
    63. string RepStr = "Response=1"; //Response=1 固定前缀,我们的设备以此来检索返回信息,表示 驱动设备显示和响声
    64. RepStr = RepStr + "," + info; //提交的信息序号,一定要对应
    65. RepStr = RepStr + "," + dispstr; //读卡器上显示文字
    66. RepStr = RepStr + ",20"; //显示时长20秒
    67. RepStr = RepStr + ",2"; //蜂鸣器发声种类,取值范围0-12
    68. RepStr = RepStr + "," + ChineseVoice; //播报的TTS语音
    69. context.Response.Write(RepStr);
    70. }
    71. }
    72. public class RootObject //json类
    73. {
    74. public string info { get; set; }
    75. public string jihao { get; set; }
    76. public string cardtype { get; set; }
    77. public string card { get; set; }
    78. public string data { get; set; }
    79. public string dn { get; set; }
    80. public string status { get; set; }
    81. }
    82. public static string getChinesecode(string inputstr) //获取中文编码,显示汉字、TTS中文语音都要转换编码
    83. {
    84. int strlen = inputstr.Length;
    85. string hexcode = "";
    86. for (int i = 0; i < strlen; i++)
    87. {
    88. string str = inputstr.Substring(i, 1);
    89. byte[] Chinesecodearry = System.Text.Encoding.GetEncoding(936).GetBytes(str);
    90. int codelen = (byte)Chinesecodearry.Length;
    91. if (codelen == 1)
    92. {
    93. hexcode = hexcode + str;
    94. }
    95. else
    96. {
    97. hexcode = hexcode + "\\x" + Chinesecodearry[0].ToString("X2") + Chinesecodearry[1].ToString("X2");
    98. }
    99. }
    100. return hexcode;
    101. }
    102. public bool IsReusable {
    103. get {
    104. return false;
    105. }
    106. }
    107. }

  • 相关阅读:
    Apache HTTP Server、IIS反向代理设置
    【论文极速读】IMAGEBIND —— 通过图片作为桥梁桥联多模态语义
    什么是HTTP头部(HTTP headers)?
    vscode更新至1.86版本后,ssh远程连接服务器出现异常
    slam定位学习笔记(七)-g2o学习
    【PostgreSQL】用pgAdmin轻松管理PostgreSQL
    强强联合:OpenFeign 整合 Sentinel
    记录一次Powerjob踩的坑(Failed to deserialize message)
    centos8 安装nginx
    go gorm select * 优化
  • 原文地址:https://blog.csdn.net/zhangjin7422/article/details/132916779