Delphi TCP服务端监听端口获取客户端RFID网络读卡器上传的刷卡数据

本文主要是介绍Delphi TCP服务端监听端口获取客户端RFID网络读卡器上传的刷卡数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本示例使用设备介绍:液显WIFI无线网络HTTP协议RFID云读卡器可编程实时可控开关TTS语-淘宝网 (taobao.com) 

unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, ComCtrls, ScktComp, StdCtrls, ScktComp7, ExtCtrls,Clipbrd;typeTForm1 = class(TForm)ServerSocket1: TServerSocket;Button1: TButton;Button2: TButton;Edit1: TEdit;Edit2: TEdit;Label1: TLabel;ListBox1: TListBox;ListBox2: TListBox;Button3: TButton;CheckBox1: TCheckBox;Panel1: TPanel;RadioButton1: TRadioButton;RadioButton2: TRadioButton;RadioButton3: TRadioButton;RadioButton4: TRadioButton;Label6: TLabel;Label2: TLabel;Edit3: TEdit;Button4: TButton;Button5: TButton;Button6: TButton;Button7: TButton;RichEdit10: TRichEdit;UpDown7: TUpDown;ComboBox1: TComboBox;ComboBox3: TComboBox;RichEdit1: TRichEdit;UpDown1: TUpDown;RichEdit2: TRichEdit;UpDown2: TUpDown;Label3: TLabel;Label5: TLabel;Label7: TLabel;RadioButton5: TRadioButton;RadioButton6: TRadioButton;Label8: TLabel;Label9: TLabel;Label10: TLabel;CheckBox2: TCheckBox;Label4: TLabel;Button8: TButton;Button9: TButton;procedure ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket);procedure ServerSocket1ClientDisconnect(Sender: TObject;Socket: TCustomWinSocket);procedure ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);procedure Button1Click(Sender: TObject);procedure FormActivate(Sender: TObject);procedure Button2Click(Sender: TObject);procedure Button3Click(Sender: TObject);procedure Button6Click(Sender: TObject);procedure Button5Click(Sender: TObject);procedure Button4Click(Sender: TObject);procedure FormClose(Sender: TObject; var Action: TCloseAction);procedure Button7Click(Sender: TObject);procedure Button8Click(Sender: TObject);procedure Button9Click(Sender: TObject);procedure CheckBox1MouseUp(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);private{ Private declarations }procedure Responsedata();procedure GetSenddata(respcode:integer);procedure ButtonSend(sendcode:integer);public{ Public declarations }ResponseBuff:Array of Byte;end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;  Socket: TCustomWinSocket);
beginButton3.Click();
end;procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;  Socket: TCustomWinSocket);
beginButton3.Click();
end;procedure TForm1.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
varRemotAddPort,DispStr,HexStr:String;i,GetDataLen:integer;GetBuff:Array of Byte;SendBuff:Array of Byte;respcode:integer;
begintryRemotAddPort:=Socket.RemoteAddress+':'+inttostr(Socket.RemotePort);GetDataLen:= Socket.ReceiveLength;SetLength(GetBuff, GetDataLen);Socket.ReceiveBuf(GetBuff[0],GetDataLen);   //Socket.ReceiveText;DispStr:='';for i:=0 to GetDataLen-1 dobeginDispStr:=DispStr+inttohex(GetBuff[i],2)+' ';end;if ListBox2.Count >100 then ListBox2.Clear();ListBox2.Items.Add('Get Data From  '+RemotAddPort+' : '+DispStr);case GetBuff[0] of$C1,$CF:beginif GetBuff[0]= $C1 thenDispStr:='数据解析:IC读卡器上传卡号,'elseDispStr:='数据解析:IC卡离开读卡器,';DispStr := DispStr+'设备IP['+IntToStr(GetBuff[1]) + '.' + IntToStr(GetBuff[2]) + '.' + IntToStr(GetBuff[3]) + '.' + IntToStr(GetBuff[4])+'],';DispStr := DispStr+'机号['+IntToStr(GetBuff[5] + GetBuff[6]*256)+'],';DispStr := DispStr+'数据包号['+IntToStr(GetBuff[7] + GetBuff[8]*256)+'],';DispStr := DispStr+'卡号长度['+IntToStr(GetBuff[9])+'],';HexStr:='';for i:=10 to 10+GetBuff[9]-1 doHexStr:=HexStr+inttohex(GetBuff[i],2);DispStr := DispStr+'16进制卡号['+HexStr+'],';HexStr:='';for i:=10+GetBuff[9] to GetDataLen-1 doHexStr:=HexStr+inttohex(GetBuff[i],2);DispStr := DispStr+'唯一硬件序号['+HexStr+']';ListBox2.Items.Add(DispStr);ListBox2.Items.Add('');listbox2.ItemIndex :=listbox2.Items.Count-1;if CheckBox1.Checked thenbeginResponsedata() ;Socket.SendBuf(ResponseBuff[0],Length(ResponseBuff));DispStr:='Send Data To  '+RemotAddPort+' : ';for i:=0 to Length(ResponseBuff)-1 doDispStr:=DispStr+inttohex(ResponseBuff[i],2)+' ';ListBox2.Items.Add(DispStr);ListBox2.Items.Add('');listbox2.ItemIndex :=listbox2.Items.Count-1;end;end;$D1,$DF:beginif GetBuff[0]= $D1 thenDispStr:='数据解析:ID读卡器上传卡号,'elseDispStr:='数据解析:ID卡离开读卡器,';DispStr := DispStr+'设备IP['+IntToStr(GetBuff[1]) + '.' + IntToStr(GetBuff[2]) + '.' + IntToStr(GetBuff[3]) + '.' + IntToStr(GetBuff[4])+'],';DispStr := DispStr+'机号['+IntToStr(GetBuff[5] + GetBuff[6]*256)+'],';DispStr := DispStr+'数据包号['+IntToStr(GetBuff[7] + GetBuff[8]*256)+'],';HexStr:='';for i:=9 to 13 doHexStr:=HexStr+inttohex(GetBuff[i],2);DispStr := DispStr+'16进制卡号['+HexStr+'],';HexStr:='';for i:=14 to GetDataLen-1 doHexStr:=HexStr+inttohex(GetBuff[i],2);DispStr := DispStr+'唯一硬件序号['+HexStr+']';ListBox2.Items.Add(DispStr);ListBox2.Items.Add('');listbox2.ItemIndex :=listbox2.Items.Count-1;if CheckBox1.Checked thenbeginResponsedata() ;Socket.SendBuf(ResponseBuff[0],Length(ResponseBuff));DispStr:='Send Data To  '+RemotAddPort+' : ';for i:=0 to Length(ResponseBuff)-1 doDispStr:=DispStr+inttohex(ResponseBuff[i],2)+' ';ListBox2.Items.Add(DispStr);ListBox2.Items.Add('');listbox2.ItemIndex :=listbox2.Items.Count-1;end;end;$F3:beginDispStr:='数据解析:读卡器心跳包,';DispStr := DispStr+'设备IP['+IntToStr(GetBuff[1]) + '.' + IntToStr(GetBuff[2]) + '.' + IntToStr(GetBuff[3]) + '.' + IntToStr(GetBuff[4])+'],';DispStr := DispStr+'机号['+IntToStr(GetBuff[5] + GetBuff[6]*256)+'],';DispStr := DispStr+'数据包号['+IntToStr(GetBuff[7] + GetBuff[8]*256)+'],';DispStr := DispStr+'心跳包标识['+inttohex(GetBuff[9],2)+'],';DispStr := DispStr+'长度['+IntToStr(GetBuff[10])+'],';DispStr := DispStr+'继电器状态['+inttohex(GetBuff[11],2)+'],';DispStr := DispStr+'输入口状态['+inttohex(GetBuff[12],2)+'],';DispStr := DispStr+'随机校验码['+inttohex(GetBuff[13],2)+inttohex(GetBuff[14],2)+inttohex(GetBuff[15],2)+inttohex(GetBuff[16],2)+'],';HexStr:='';HexStr:='';for i:=17 to GetDataLen-1 doHexStr:=HexStr+inttohex(GetBuff[i],2);DispStr := DispStr+'唯一硬件序号['+HexStr+']';ListBox2.Items.Add(DispStr);ListBox2.Items.Add('');listbox2.ItemIndex :=listbox2.Items.Count-1;  end;end;exceptend;
end;procedure TForm1.Responsedata();           //根据选择的回应方式生成回应数据缓冲
beginif RadioButton1.Checked thenGetSenddata(0)elseif RadioButton2.Checked thenGetSenddata(1)elseif RadioButton3.Checked thenGetSenddata(2)elseGetSenddata(3);
end;procedure TForm1.GetSenddata(respcode:integer);         //根据发送方式生成发送数据缓冲
var
delaytime,i,voicelen,displen:integer;
strls,voicestr:string;
begincase respcode of0:beginSetLength(ResponseBuff, 39);ResponseBuff[0]:=$5A;   //命令字:驱动显示文字+蜂鸣器响声ResponseBuff[1]:=$00;   //机号低ResponseBuff[2]:=$00;   //机号高,0000表示任意机号if(CheckBox2.Checked) thenbeginResponseBuff[3]:=ComboBox1.ItemIndex;   //蜂鸣器响声代码if RadioButton6.Checked then  ResponseBuff[3]:=ResponseBuff[3] xor 128; //背光灯状态不改变endelsebeginResponseBuff[3]:=$ff;          //不响声if RadioButton6.Checked then  ResponseBuff[3]:=ResponseBuff[3] xor 127; //背光灯状态不改变end;delaytime:=StrToInt(RichEdit10.Lines[0]);ResponseBuff[4] := delaytime mod 256;    //显示时长strls := Edit1.Text + '                                        ';for i := 1 to 34 doResponseBuff[4+i] := Byte(strls[i]);end;1:beginvoicestr:='[v'+ trim(RichEdit2.Lines[0])+']';   //本次播报TTS语音的音量大小,取值范围v0 到 v16voicestr:= voicestr+trim(edit3.Text);voicelen:=length(voicestr); //语音长度displen:=34;             //满屏显示长度SetLength(ResponseBuff, 11+displen+voicelen+4);ResponseBuff[0]:=$5C;   //命令字:驱动显示文字+蜂鸣器响声+开启继电器+播报TTS语音ResponseBuff[1]:=$00;   //机号低ResponseBuff[2]:=$00;   //机号高,0000表示任意机号if(CheckBox2.Checked) thenbeginResponseBuff[3]:=ComboBox1.ItemIndex;   //蜂鸣器响声代码if RadioButton6.Checked then  ResponseBuff[3]:=ResponseBuff[3] xor 128; //背光灯状态不改变endelsebeginResponseBuff[3]:=$ff;          //不响声if RadioButton6.Checked then  ResponseBuff[3]:=ResponseBuff[3] xor 127; //背光灯状态不改变end;case ComboBox3.ItemIndex of       //开启的继电器号1: ResponseBuff[4]:=$f1;2: ResponseBuff[4]:=$f2;3: ResponseBuff[4]:=$f3;4: ResponseBuff[4]:=$f4;5: ResponseBuff[4]:=$f5;6: ResponseBuff[4]:=$f6;7: ResponseBuff[4]:=$f7;8: ResponseBuff[4]:=$f8;else ResponseBuff[4]:=$f0;end;delaytime:=StrToInt(RichEdit1.Lines[0]);ResponseBuff[5] := delaytime mod 256;ResponseBuff[6] := (delaytime div 256) mod 256;delaytime:=StrToInt(RichEdit10.Lines[0]);ResponseBuff[7] := delaytime mod 256;    //显示时长ResponseBuff[8] :=0;ResponseBuff[9] :=displen;ResponseBuff[10] :=voicelen;strls := Edit1.Text + '                                        ';for i := 1 to displen doResponseBuff[10+i] := Byte(strls[i]);for i := 1 to voicelen doResponseBuff[10+displen+i] := Byte(voicestr[i]);ResponseBuff[10+displen+voicelen+1]:=$55; //防干扰固定后缀ResponseBuff[10+displen+voicelen+2]:=$aa;ResponseBuff[10+displen+voicelen+3]:=$66;ResponseBuff[10+displen+voicelen+4]:=$99;end;2:beginSetLength(ResponseBuff, 4);ResponseBuff[0]:=$96;   //命令字:驱动蜂鸣器响ResponseBuff[1]:=$00;   //机号低ResponseBuff[2]:=$00;   //机号高,0000表示任意机号ResponseBuff[3]:=ComboBox1.ItemIndex;   //蜂鸣器响声代码end;3:beginSetLength(ResponseBuff, 6);ResponseBuff[0]:=$78;   //命令字:驱动开启继电器ResponseBuff[1]:=$00;   //机号低ResponseBuff[2]:=$00;   //机号高,0000表示任意机号case ComboBox3.ItemIndex of       //开启的继电器号1: ResponseBuff[3]:=$f1;2: ResponseBuff[3]:=$f2;3: ResponseBuff[3]:=$f3;4: ResponseBuff[3]:=$f4;5: ResponseBuff[3]:=$f5;6: ResponseBuff[3]:=$f6;7: ResponseBuff[3]:=$f7;8: ResponseBuff[3]:=$f8;else ResponseBuff[3]:=$f0;end;delaytime:=StrToInt(RichEdit1.Lines[0]);ResponseBuff[4] := delaytime mod 256;ResponseBuff[5] := (delaytime div 256) mod 256;end;4:beginSetLength(ResponseBuff, 6);ResponseBuff[0]:=$78;   //命令字:驱动关闭已开启继电器ResponseBuff[1]:=$00;   //机号低ResponseBuff[2]:=$00;   //机号高,0000表示任意机号case ComboBox3.ItemIndex of       //继电器号1: ResponseBuff[3]:=$e1;2: ResponseBuff[3]:=$e2;3: ResponseBuff[3]:=$e3;4: ResponseBuff[3]:=$e4;5: ResponseBuff[3]:=$e5;6: ResponseBuff[3]:=$e6;7: ResponseBuff[3]:=$e7;8: ResponseBuff[3]:=$e8;else ResponseBuff[3]:=$e0;end;delaytime:=StrToInt(RichEdit1.Lines[0]);ResponseBuff[4] := delaytime mod 256;ResponseBuff[5] := (delaytime div 256) mod 256;end;end;
end;procedure TForm1.ButtonSend(sendcode:integer);
var
i:integer;
RemotAddPort,DispStr:string;
beginif ServerSocket1.Active thenbegini:=ListBox1.ItemIndex ;if i>=0 thenbegintryGetSenddata(sendcode);ServerSocket1.Socket.Connections[i].SendBuf(ResponseBuff[0],Length(ResponseBuff));RemotAddPort:= ServerSocket1.Socket.Connections[i].RemoteAddress+':'+inttostr(ServerSocket1.Socket.Connections[i].RemotePort);DispStr:='Send Data To  '+RemotAddPort+' : ';for i:=0 to Length(ResponseBuff)-1 doDispStr:=DispStr+inttohex(ResponseBuff[i],2)+' ';ListBox2.Items.Add(DispStr);ListBox2.Items.Add('');listbox2.ItemIndex :=listbox2.Items.Count-1;exceptend;endelseApplication.MessageBox('请先选择要向其发送指令的在线客户端!', '警告', MB_OK+MB_ICONSTOP);endelseApplication.MessageBox('请先启动TCP服务监听!', '警告', MB_OK+MB_ICONSTOP);
end;procedure TForm1.Button1Click(Sender: TObject);
beginButtonSend(0);
end;procedure TForm1.FormActivate(Sender: TObject);
begin
if ServerSocket1.Active thenbeginButton2.Caption := '停止';end
elsebeginButton2.Click();end;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
if not ServerSocket1.Active thenbegintryServerSocket1.Port := StrToInt(Edit2.Text);ServerSocket1.Active := True;Button2.Caption := '停止';Edit2.Enabled := False;exceptApplication.MessageBox('启动TCP服务监听失败!可能端口已被其他应用占用。', '警告', MB_OK+MB_ICONSTOP);end;endelsebeginServerSocket1.Active := False;Button2.Caption := '启动TCP服务监听';Edit2.Enabled := True;ListBox1.Items.Clear();ListBox2.Items.Clear();end;end;procedure TForm1.Button3Click(Sender: TObject);
vari,links:integer;
beginListBox1.Items.Clear();links:=ServerSocket1.Socket.ActiveConnections;for i:=0 to links-1 dobeginListBox1.Items.Add(inttostr(i)+'|'+ServerSocket1.Socket.Connections[i].RemoteAddress+':'+inttostr(ServerSocket1.Socket.Connections[i].RemotePort));end;
end;procedure TForm1.Button6Click(Sender: TObject);
beginButtonSend(3);
end;procedure TForm1.Button5Click(Sender: TObject);
beginButtonSend(2);
end;procedure TForm1.Button4Click(Sender: TObject);
beginButtonSend(1);
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if ServerSocket1.Active  then ServerSocket1.Active := False;
end;procedure TForm1.Button7Click(Sender: TObject);
beginButtonSend(4);
end;procedure TForm1.Button8Click(Sender: TObject);
varliststr:string;i:integer;
beginif listbox2.Count <1 then exit;liststr:='';for i:=0 to ListBox2.Count-1 dobeginListBox2.ItemIndex:=i;liststr:=liststr+ListBox2.Items.Strings[ListBox2.ItemIndex]+#13#10;end;Clipboard.SetTextBuf(PChar(liststr));Application.MessageBox('TCP通讯报文日志已拷贝!', '提示', MB_OK+MB_ICONASTERISK );
end;procedure TForm1.Button9Click(Sender: TObject);
beginListBox2.Clear();
end;procedure TForm1.CheckBox1MouseUp(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
beginif checkbox1.Checked then Panel1.Visible :=true else Panel1.Visible :=false;
end;end.

 

这篇关于Delphi TCP服务端监听端口获取客户端RFID网络读卡器上传的刷卡数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/390571

相关文章

大模型研发全揭秘:客服工单数据标注的完整攻略

在人工智能(AI)领域,数据标注是模型训练过程中至关重要的一步。无论你是新手还是有经验的从业者,掌握数据标注的技术细节和常见问题的解决方案都能为你的AI项目增添不少价值。在电信运营商的客服系统中,工单数据是客户问题和解决方案的重要记录。通过对这些工单数据进行有效标注,不仅能够帮助提升客服自动化系统的智能化水平,还能优化客户服务流程,提高客户满意度。本文将详细介绍如何在电信运营商客服工单的背景下进行

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

关于数据埋点,你需要了解这些基本知识

产品汪每天都在和数据打交道,你知道数据来自哪里吗? 移动app端内的用户行为数据大多来自埋点,了解一些埋点知识,能和数据分析师、技术侃大山,参与到前期的数据采集,更重要是让最终的埋点数据能为我所用,否则可怜巴巴等上几个月是常有的事。   埋点类型 根据埋点方式,可以区分为: 手动埋点半自动埋点全自动埋点 秉承“任何事物都有两面性”的道理:自动程度高的,能解决通用统计,便于统一化管理,但个性化定

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

异构存储(冷热数据分离)

异构存储主要解决不同的数据,存储在不同类型的硬盘中,达到最佳性能的问题。 异构存储Shell操作 (1)查看当前有哪些存储策略可以用 [lytfly@hadoop102 hadoop-3.1.4]$ hdfs storagepolicies -listPolicies (2)为指定路径(数据存储目录)设置指定的存储策略 hdfs storagepolicies -setStoragePo

Hadoop集群数据均衡之磁盘间数据均衡

生产环境,由于硬盘空间不足,往往需要增加一块硬盘。刚加载的硬盘没有数据时,可以执行磁盘数据均衡命令。(Hadoop3.x新特性) plan后面带的节点的名字必须是已经存在的,并且是需要均衡的节点。 如果节点不存在,会报如下错误: 如果节点只有一个硬盘的话,不会创建均衡计划: (1)生成均衡计划 hdfs diskbalancer -plan hadoop102 (2)执行均衡计划 hd

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

Linux 网络编程 --- 应用层

一、自定义协议和序列化反序列化 代码: 序列化反序列化实现网络版本计算器 二、HTTP协议 1、谈两个简单的预备知识 https://www.baidu.com/ --- 域名 --- 域名解析 --- IP地址 http的端口号为80端口,https的端口号为443 url为统一资源定位符。CSDNhttps://mp.csdn.net/mp_blog/creation/editor

烟火目标检测数据集 7800张 烟火检测 带标注 voc yolo

一个包含7800张带标注图像的数据集,专门用于烟火目标检测,是一个非常有价值的资源,尤其对于那些致力于公共安全、事件管理和烟花表演监控等领域的人士而言。下面是对此数据集的一个详细介绍: 数据集名称:烟火目标检测数据集 数据集规模: 图片数量:7800张类别:主要包含烟火类目标,可能还包括其他相关类别,如烟火发射装置、背景等。格式:图像文件通常为JPEG或PNG格式;标注文件可能为X

ASIO网络调试助手之一:简介

多年前,写过几篇《Boost.Asio C++网络编程》的学习文章,一直没机会实践。最近项目中用到了Asio,于是抽空写了个网络调试助手。 开发环境: Win10 Qt5.12.6 + Asio(standalone) + spdlog 支持协议: UDP + TCP Client + TCP Server 独立的Asio(http://www.think-async.com)只包含了头文件,不依