本文主要是介绍Java TCP服务端多线程接收RFID网络读卡器上传数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本示例使用设备介绍:WIFI/TCP/UDP/HTTP协议RFID液显网络读卡器可二次开发语音播报POE-淘宝网 (taobao.com)
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;public class TCPServer_Java {//监听端口private static final int PORT = 39169;public static void main(String[] args) throws IOException {ServerSocket serverSocket = null;Socket socket = null;try {//建立服务器的Socket,并设定一个监听的端口PORTserverSocket = new ServerSocket(PORT);//由于需要进行循环监听,因此获取消息的操作应放在一个while大循环中while(true){try {//建立跟客户端的连接socket = serverSocket.accept();ServerThread thread = new ServerThread(socket);thread.start();} catch (Exception e) {System.out.println("建立与客户端的连接出现异常");e.printStackTrace();}//ServerThread thread = new ServerThread(socket);//thread.start();}} catch (Exception e) {System.out.println("端口被占用");e.printStackTrace();}finally {serverSocket.close();}}
}//服务端线程类
//继承Thread类的话,必须重写run方法,在run方法中定义需要执行的任务。
class ServerThread extends Thread {private Socket socket ;InputStream inputStream;OutputStream outputStream;public ServerThread(Socket socket){this.socket=socket;}public void run(){try {while (true){//接收客户端的消息,解析信息并回应读卡器//System.out.println(socket);byte[] bytes =new byte[1024];inputStream =socket.getInputStream();int GetDataLen=0;while ((GetDataLen=inputStream.read(bytes))!=-1){ //通过这个方法读取全部数据 及 长度,break;}if(GetDataLen>0) {String bytestr = "";String DataStr = "";String DispStr = "";String CardNo16 = "";String SerialNum = "";long cardno10;long cardnum;for (int p = 0; p < GetDataLen; p++) {bytestr = "00" + Integer.toHexString(bytes[p] & 0xff);DataStr = DataStr + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";}System.out.println("Received from " + socket.getRemoteSocketAddress() + " : " + DataStr);switch (bytes[0]) {case (byte)0xc1:case (byte)0xcf:if(bytes[0]==(byte)0xc1){DispStr = "数据解析:接收到读取IC卡号信息,";}else{DispStr = "数据解析:接收到IC卡离开读卡器,";}DispStr = DispStr + "功能码:" + Integer.toHexString(bytes[0] & 0xff);DispStr = DispStr + ",设备IP:" + Integer.toString(bytes[1] & 0xff) + "." + Integer.toString(bytes[2] & 0xff) + "." + Integer.toString(bytes[3] & 0xff) + "." + Integer.toString(bytes[4] & 0xff);DispStr = DispStr + ",机号:" + Integer.toString((bytes[5] & 0xff) + ((bytes[6] & 0xff) * 256));DispStr = DispStr + ",数据包序号:" + Integer.toString((bytes[7] & 0xff) + ((bytes[8] & 0xff) * 256));int Cardlen = bytes[9];DispStr = DispStr + ",卡号长度:" + Integer.toString(Cardlen);CardNo16 = "";for (int p = 10; p < 10 + Cardlen; p++) {bytestr = "00" + Integer.toHexString(bytes[p] & 0xff);CardNo16 = CardNo16 + bytestr.substring(bytestr.length() - 2, bytestr.length());}DispStr = DispStr + ",16进制卡号:" + CardNo16;cardnum = bytes[10] & 0xff;cardnum = cardnum + (bytes[11] & 0xff) * 256;cardnum = cardnum + (bytes[12] & 0xff) * 65536;cardnum = cardnum + (bytes[13] & 0xff) * 16777216;cardno10 = 0;for (int j = 28; j >= 0; j -= 4) {cardno10 = cardno10 << 4 | (cardnum >>> j & 0xF);}DispStr = DispStr + ",转10进制卡号:" + String.format("%010d", cardno10);SerialNum = "";for (int p = 10 + Cardlen; p < GetDataLen; p++) {bytestr = "00" + Integer.toHexString(bytes[p] & 0xff);SerialNum = SerialNum + bytestr.substring(bytestr.length() - 2, bytestr.length());}DispStr = DispStr + ",唯一硬件序号:" + SerialNum;System.out.println(DispStr + "\n");//向客户端发送消息byte[] RespByte=GetResponseData(2); //生成不同的回应数据包outputStream = socket.getOutputStream();outputStream.write(RespByte);bytestr = "";DataStr = "";for (int p = 0; p < RespByte.length; p++) {bytestr = "00" + Integer.toHexString(RespByte[p] & 0xff);DataStr = DataStr + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";}System.out.println("Send Data To "+socket.getRemoteSocketAddress()+" : "+DataStr+"\n");break;case (byte)0xd1:case (byte)0xdf:if(bytes[0]==(byte)0xd1){DispStr="数据解析:接收到读取ID卡号信息,";}else{DispStr="数据解析:接收到ID卡离开读卡器,";}DispStr=DispStr+"功能码:"+ Integer.toHexString(bytes[0] & 0xff);DispStr=DispStr+",设备IP:"+ Integer.toString(bytes[1] & 0xff) + "." + Integer.toString(bytes[2] & 0xff) + "." + Integer.toString(bytes[3] & 0xff) + "." + Integer.toString(bytes[4] & 0xff);DispStr=DispStr+",机号:"+ Integer.toString((bytes[5] & 0xff) + ((bytes[6] & 0xff) * 256));DispStr=DispStr+",数据包序号:"+ Integer.toString((bytes[7] & 0xff) + ((bytes[8] & 0xff) * 256));CardNo16="";for (int p = 9; p < 14; p++) {bytestr = "00" + Integer.toHexString(bytes[p] & 0xff);CardNo16 = CardNo16 + bytestr.substring(bytestr.length() - 2, bytestr.length());}DispStr=DispStr+",16进制卡号:"+CardNo16;cardnum = bytes[9] & 0xff;cardnum = cardnum + (bytes[10] & 0xff) * 256;cardnum = cardnum + (bytes[11] & 0xff) * 65536;cardnum = cardnum + (bytes[12] & 0xff) * 16777216;cardno10 = 0;for (int j = 28; j >= 0; j -= 4) {cardno10 = cardno10 << 4 | (cardnum >>> j & 0xF);}DispStr=DispStr+",转10进制卡号:"+ String.format("%010d", cardno10);SerialNum="";for (int p = 14; p < GetDataLen; p++) {bytestr = "00" + Integer.toHexString(bytes[p] & 0xff);SerialNum = SerialNum + bytestr.substring(bytestr.length() - 2, bytestr.length());}DispStr=DispStr+",唯一硬件序号:"+ SerialNum;System.out.println(DispStr+"\n");//向客户端发送消息RespByte=GetResponseData(2); //生成不同的回应数据包outputStream = socket.getOutputStream();outputStream.write(RespByte);bytestr = "";DataStr = "";for (int p = 0; p < RespByte.length; p++) {bytestr = "00" + Integer.toHexString(RespByte[p] & 0xff);DataStr = DataStr + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";}System.out.println("Send Data To "+socket.getRemoteSocketAddress()+" : "+DataStr+"\n");break;case (byte)0xf3:DispStr="数据解析:接收到设备心跳包,";DispStr=DispStr+"功能码:"+Integer.toHexString(bytes[0] & 0xff);DispStr=DispStr+",设备IP:"+Integer.toString(bytes[1] & 0xff)+"."+Integer.toString(bytes[2] & 0xff)+"."+Integer.toString(bytes[3] & 0xff)+"."+Integer.toString(bytes[4] & 0xff);DispStr=DispStr+",机器号:"+Integer.toString((bytes[5] & 0xff)+((bytes[6] & 0xff) *256));DispStr=DispStr+",包序号:"+Integer.toString((bytes[7] & 0xff)+((bytes[8] & 0xff) *256));DispStr=DispStr+",心跳码:"+Integer.toHexString(bytes[9] & 0xff);DispStr=DispStr+",长 度:"+Integer.toHexString(bytes[10] & 0xff);DispStr=DispStr+",继电器:"+Integer.toHexString(bytes[11] & 0xff);DispStr=DispStr+",按键值:"+Integer.toHexString(bytes[12] & 0xff);for(int p=13;p<17;p++){bytestr="00"+Integer.toHexString(bytes[p] & 0xff);SerialNum=SerialNum+ bytestr.substring(bytestr.length() -2,bytestr.length());}DispStr=DispStr+",随机码:"+SerialNum;SerialNum="";for(int p=17;p<GetDataLen;p++){bytestr="00"+Integer.toHexString(bytes[p] & 0xff);SerialNum=SerialNum+ bytestr.substring(bytestr.length() -2,bytestr.length());}DispStr=DispStr+",设备序列号:"+SerialNum;System.out.println(DispStr+"\n");break;}}}} catch (Exception e) {System.out.println("客户端主动断开连接了");e.printStackTrace();}//操作结束,关闭sockettry{socket.close();}catch(IOException e){System.out.println("关闭连接出现异常");e.printStackTrace();}}//根据Respcode返回不同的回应数据包static byte[] GetResponseData(int Respcode) throws Exception {byte[] RespByte= new byte[300];String DispStr="本次刷卡成功,感谢您的使用,再见! "; //满屏显示34个字符,不足后面加空格byte[] DispBuf;switch(Respcode){case 0: //驱动读卡器蜂鸣响声RespByte[0] = (byte) 0x96; //蜂鸣响声的指令码RespByte[1] = (byte) 0x00; //机号低位RespByte[2] = (byte) 0x00; //机号高,0000表示任意机号RespByte[3] = (byte) 0x01; //蜂鸣响声代码,取值范围0-12RespByte = Arrays.copyOf(RespByte, 4);break;case 1: //驱动读卡器显示文字+蜂鸣响声DispBuf= DispStr.getBytes("gb2312"); //显示文字转字节数组RespByte[0]=(byte)0x5a; //指令码RespByte[1]=(byte)0x00; //机号低RespByte[2]=(byte)0x00; //机号高,如果高低位都为0表示任意机号RespByte[3]=(byte)0x02; //蜂鸣声代码RespByte[4]=(byte)0x14; //显示时长for(int i=0;i<34;i++){ //显示文字RespByte[i+5]=DispBuf[i];}RespByte = Arrays.copyOf(RespByte, 39);break;case 2: //驱动读卡器显示文字+蜂鸣响声+继电器开关+TTS语音DispBuf= DispStr.getBytes("gb2312"); //显示文字转字节数组String SpeakStr=""; //将要播报的TTS语音生成字节数组,最大不能超过126个字节SpeakStr="[v1]"; //定义语音大小,取值范围v0 到 v16,可加在任何地方SpeakStr=SpeakStr+"欢迎您使用我们的网络读卡器,谢谢!"; //要播报的中文语音byte[] SpeakBuf= SpeakStr.getBytes("gb2312");int SpeakLen=SpeakBuf.length; //语音长度int SendLen=11+34+SpeakLen+4; //总计发送数据长度RespByte[0]=(byte)0x5C; //指令码RespByte[1]=(byte)0x00; //机号低RespByte[2]=(byte)0x00; //机号高,如果高低位都为0表示任意机号RespByte[3]=(byte)0x01; //蜂鸣声代码RespByte[4]=(byte)0xF0; //继电器代码 F0表示全部继电器 F1表示1号继电器 F2表示2号继电器RespByte[5]=(byte)0x20; //继电器开启时长 低位RespByte[6]=(byte)0x00; //继电器开启时长 高位RespByte[7]=(byte)0x14; //显示保留时间,单位为秒,为0xFF时表示永久显示RespByte[8]=(byte)0x00; //在显示屏中的哪个位置开始RespByte[9]=(byte)0x22; //显示字符串长度 为全屏显示为 34 个字符RespByte[10]=(byte)SpeakLen; //语音长度for(int i=0;i<34;i++){ //显示的文字信息RespByte[i+11]=DispBuf[i];}for(int i=0;i<SpeakLen;i++){ //TTS语音播报信息RespByte[i+45]=SpeakBuf[i];}RespByte[10+34+SpeakLen+1]=(byte)0x55; //固定的抗干扰后缀RespByte[10+34+SpeakLen+2]=(byte)0xaa;RespByte[10+34+SpeakLen+3]=(byte)0x66;RespByte[10+34+SpeakLen+4]=(byte)0x99;RespByte = Arrays.copyOf(RespByte, 10+34+SpeakLen+5);break;case 3: //驱动读卡器开启继电器开关RespByte[0]=(byte)0x78; //指令码RespByte[1]=(byte)0x00; //机号低RespByte[2]=(byte)0x00; //机号高,如果高低位都为0表示任意机号RespByte[3]=(byte)0xF0; //继电器代码 F0表示全部继电器、F1表示1号继电器 、F2表示2号继电器......RespByte[4]=(byte)0x2C; //继电器开启延时低位RespByte[5]=(byte)0x01; //继电器开启延时高位,FFFF表示继电器一直开启RespByte = Arrays.copyOf(RespByte, 6);break;default:}return RespByte;}
}
这篇关于Java TCP服务端多线程接收RFID网络读卡器上传数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!