HTTP协议网络读卡器通讯报文
本说明使用的读卡器:https://item.taobao.com/item.htm?spm=a21dvs.23580594.0.0.52de2c1bGBhMtE&ft=t&id=22173428704
一、读卡器设置为GET方式提交刷卡数据
GET /IC-14h.asp?info=37&jihao=0&cardtype=5&card=0000000A&dn=169850547BD05F21 HTTP/1.1
Host: 192.168.1.211:5000
Connection: close
User-Agent: Mozilla/4.0
Accept-Language: en
二、读卡器设置为POST方式提交刷卡数据
POST /IC-14h.asp HTTP/1.1
Host: 192.168.1.211:5000
Connection: close
User-Agent: Mozilla/4.0
Accept-Language: en
Content-Type: application/x-www-form-urlencoded
Content-Length: 60
info=37&jihao=0&cardtype=5&card=0000000A&dn=169850547BD05F21
三、读卡器设置为JSON方式提交刷卡数据
POST /IC-14h.asp HTTP/1.1
Host: 192.168.1.211:5000
Connection: close
User-Agent: Mozilla/4.0
Accept-Language: en
Content-Type: application/json
Content-Length: 82
{"info":"24","jihao":"0","cardtype":"5","card":"0000000A","dn":"169850547BD05F21"}
四、服务器端以JSON方式回应驱动读卡器显示文字、播报语音
{"Response":"json","infotype":"1","info":"28","disp":"{卡号:}1234567890AB25-04-30 11:30:50","dispdelay":"20","beeptype":"1","voicetype":"读取卡号[n1]1234567890ABCDEF","k1delay":"20","k2delay":"20"}
五、服务器端以字符串方式回应驱动读卡器显示文字、播报语音
Response=1,32,{卡号:}1234567890AB25-04-30 11:30:50,20,2,读取卡号[n1]1234567890ABCDEF,20,30
六、服务端ASHX网页获取刷卡数据及回应示例
<%@ 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 scantype = "";string heartbeattype = "";string card = "";string data = "";string input = "";string output = "";string rand = "";string dn = "";string status = ""; Int16 cardtype16 = 0;int cardtypecode = 0;int pushortake = 0;string dispstr = "";string ChineseVoice = "[v8]"; //[v8]表示本次播报语音音量,取值范围v1 到 v16context.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["scantype"] != null) { scantype = context.Request["scantype"].ToString(); } //扫码标识位if (context.Request["heartbeattype"] != null) { heartbeattype = context.Request["heartbeattype"].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["input"] != null) { input = context.Request["input"].ToString(); } //输入标志if (context.Request["output"] != null) { output = context.Request["output"].ToString(); } //输出标志if (context.Request["rand"] != null) { rand = context.Request["rand"].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 == "" && dn == "") //如果未接收到有效的Request信息,再尝试用Json解析是否有Request信息{StreamReader sr = new StreamReader(context.Request.GetBufferlessInputStream());string response = sr.ReadToEnd();info = getjsonval(response, "info"); //接收到的数据包号,需回应该包号jihao = getjsonval(response, "jihao"); //设备机号(可自编)cardtype = getjsonval(response, "cardtype"); //刷卡标志,卡类型scantype = getjsonval(response, "scantype"); //扫码标志heartbeattype = getjsonval(response, "heartbeattype"); //心跳包标志 card = getjsonval(response, "card"); //接收到的原始16进制卡号,可根据需要自行转换成其他卡号data = getjsonval(response, "data"); //扇区内容 或 扫码信息 input = getjsonval(response, "input"); //输入标识output = getjsonval(response, "output"); //输出标识 rand = getjsonval(response, "rand"); //设备随机数dn = getjsonval(response, "dn"); //设备硬件序列号,出厂时已固化,全球唯一status = getjsonval(response, "status"); //读卡状态,如12表示卡密码认证失败 }if (heartbeattype == "1" && dn != "" && info != "") //接收到的Request信息是 心跳包{string RepStr = "Response=1"; //Response=1 固定前缀,我们的设备以此来检索返回信息,RepStr = RepStr + "," + info; //提交的信息序号,一定要对应RepStr = RepStr + "," + dn + getChinesecode("接收到心跳信息! "); //读卡器上显示文字,显示中文要转换编码RepStr = RepStr + ",20"; //显示时长20秒RepStr = RepStr + ",1"; //蜂鸣器发声种类,取值范围0-12RepStr = RepStr + ","; //播报的TTS语音//string RepStr = "Response=1," + info + ",,0,0,"; //正式项目可以用不显示文字、不响声、不播报语音的指令来回应心跳,也可以不回应,此处加入显示、响声只是用来检测读卡器功能context.Response.Write(RepStr); }else{if (info != "" && card != "" && cardtype != "") //接收到有效的刷卡Request信息,回应驱动读卡器显示文字、蜂鸣响声或播报语音{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" 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-12RepStr = RepStr + "," + ChineseVoice; //播报的TTS语音RepStr = RepStr + ",20"; //第1继电器开启延时单位,每1个单位延时25mm,20*25表示500mm,取值为0表示关闭继电器RepStr = RepStr + ",30"; //第2继电器开启延时单位,以,分隔,总计可以控制8个继电器context.Response.Write(RepStr); //将回应信息传送到读卡器驱动读卡器显示文字、蜂鸣响声、播报语音//第二种以JSON的方式回应//string jsonText = "{\"Response\":\"json\",\"infotype\":\"1\""; //固定前缀,我们的设备以此来检索返回信息,表示 驱动设备显示和响声//jsonText = jsonText + ",\"info\":\"" + info + "\""; //提交的信息序号,一定要对应//jsonText = jsonText + ",\"disp\":\"" + dispstr + "\""; //显示文字,注意中文汉字一定要转换为设备能显示的编码,其它字母数字符号不需要转换,{}内的信息反白显示//jsonText = jsonText + ",\"dispdelay\":\"20\""; //显示时长20秒//jsonText = jsonText + ",\"beeptype\":\"1\""; //蜂鸣器发声种类,取值范围0-12//jsonText = jsonText + ",\"voicetype\":\""+ChineseVoice+"\""; //播报的TTS语音//jsonText = jsonText + ",\"k1delay\":\"20\""; //第1继电器开启延时单位,每1个单位延时25mm,20*25表示500mm,取值为0表示关闭继电器//jsonText = jsonText + ",\"k2delay\":\"20\"}"; //第2继电器开启延时单位,以,分隔,总计可以控制8个继电器//context.Response.ContentType = "application/json";//context.Response.Write(jsonText); }else{if (info != "" && scantype != "") //接收到有效的扫码信息{dispstr = "{" + getChinesecode("扫码") + ":}" + data; //显示信息,注意中文汉字一定要转换为设备能显示的编码,其它字母数字符号不需要转换,{}内的信息反白显示 ChineseVoice = getChinesecode("扫码") + data;//第一种回应方式 string RepStr = "Response=1"; //Response=1 固定前缀,我们的设备以此来检索返回信息,表示 驱动设备显示和响声RepStr = RepStr + "," + info; //提交的信息序号,一定要对应RepStr = RepStr + "," + dispstr; //读卡器上显示文字,显示中文要转换编码RepStr = RepStr + ",20"; //显示时长20秒RepStr = RepStr + ",2"; //蜂鸣器发声种类,取值范围0-12RepStr = RepStr + "," + ChineseVoice; //播报的TTS语音RepStr = RepStr + ",20"; //第1继电器开启延时单位,每1个单位延时25mm,20*25表示500mm,取值为0表示关闭继电器RepStr = RepStr + ",30"; //第2继电器开启延时单位,以,分隔,总计可以控制8个继电器context.Response.Write(RepStr); //将回应信息传送到读卡器驱动读卡器显示文字、蜂鸣响声、播报语音 //第二种以JSON的方式回应//string jsonText = "{\"Response\":\"json\",\"infotype\":\"1\""; //固定前缀,我们的设备以此来检索返回信息,表示 驱动设备显示和响声//jsonText = jsonText + ",\"info\":\"" + info + "\""; //提交的信息序号,一定要对应//jsonText = jsonText + ",\"disp\":\"" + dispstr + "\\n\\n\""; //显示文字,注意中文汉字一定要转换为设备能显示的编码,其它字母数字符号不需要转换,{}内的信息反白显示//jsonText = jsonText + ",\"dispdelay\":\"20\""; //显示时长20秒//jsonText = jsonText + ",\"beeptype\":\"1\""; //蜂鸣器发声种类,取值范围0-12//jsonText = jsonText + ",\"voicetype\":\"" + ChineseVoice + "\""; //播报的TTS语音//jsonText = jsonText + ",\"k1delay\":\"20\""; //第1继电器开启延时单位,每1个单位延时25mm,20*25表示500mm,取值为0表示关闭继电器//jsonText = jsonText + ",\"k2delay\":\"20\"}"; //第2继电器开启延时单位,以,分隔,总计可以控制8个继电器//context.Response.ContentType = "application/json";//context.Response.Write(jsonText); }}} }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 static string getjsonval(string totalstr, string namestr) //解析JSON数据{string valustr = "";totalstr = totalstr.Replace("{", "");totalstr = totalstr.Replace("}", "");totalstr = totalstr.Replace("\"", "");string[] dataArray = totalstr.Split(new char[2] { ',', ',' });if (dataArray.GetUpperBound(0) > 0){for (int i = 0; i < dataArray.Length; i++){string[] dataArray2 = dataArray[i].Split(new char[2] { ':', ':' });if (dataArray2[0] == namestr){valustr = dataArray2[1];break;} }}return valustr;} public bool IsReusable {get {return false;}}}