⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ucomm.pas

📁 ah 209型通用读卡器驱动程序源码
💻 PAS
字号:
unit Ucomm;

interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,DataType,SyncObjs;

var
   recbuf : array[1..MAXBUF] of byte;
   sendbuf : array[1..MAXBUF] of byte;
   CommonBuf : array[1..MAXBUF] of byte;  //公共接收数据缓存  头两字节表示已有数据长度
   RecvSendSection : TCriticalSection; //临界区
   ValveInfor : array[1..MAX_LONG] of ValveState; //所有阀门信息
function HostCall(id : byte) : Rec_Send_Message;//查询POS机工作状态
function HostCallBack(id : byte;state : PChar) : Rec_Send_Message;//查询POS机工作状态应答
function OpenValve(id : byte;fid : byte) : Rec_Send_Message;//开阀命令
function OpenValveBack(id : byte;fid : byte) : Rec_Send_Message;//开阀命令应答
function CloseValve(id : byte;fid : byte) : Rec_Send_Message;//关机命令
function CloseValveBack(id : byte;fid : byte) : Rec_Send_Message;//关机命令应答
function CommunicationCheck(id : byte) : Rec_Send_Message;//通讯检查命令
function CommunicationCheckBack(id : byte) : Rec_Send_Message;//通讯检查命令应答
function SendCard(id : byte) : Rec_Send_Message; //发送卡号命令
function SendCardBack(id : byte;ct : byte;cn : integer) : Rec_Send_Message;//发送卡号命令应答
function NoSendCardBack(id : byte;stb : byte;st : byte) : Rec_Send_Message;//发送卡号命令无卡号应答
function RecvCard(id : byte) : Rec_Send_Message; //收到卡号通报
function RecvCardBack(id : byte) : Rec_Send_Message; //收到卡号通报应答
implementation
uses
  CRCMAC_unit,CommandWord, packet_unit;
//查询POS机工作状态
function HostCall(id : byte) : Rec_Send_Message;
var
  crc : Word;
  ph : TPacketHead;
  tempbuf : array[1..3] of byte;
  pb : Rec_Send_Message;
begin
   ZeroMemory(@sendbuf[1],MAXBUF);
   ZeroMemory(@tempbuf[1],3);
   //pb.RSMP := Pointer(LocalAlloc( LPTR, 5 ));
   ph.HeadWord := $AA55;
   ph.PackLen := PACKET_HEAD_LEN + 4;
   CopyMemory(@tempbuf[1],@ph.HeadWord,2);
   tempbuf[3] := ph.PackLen;
   ph.HeadCrc := CRC16(@tempbuf[1],3);
   CopyMemory(@sendbuf[1],@tempbuf[1],3);
   CopyMemory(@sendbuf[4],@ph.HeadCrc,2);
   sendbuf[PACKET_HEAD_LEN+1] := id;
   sendbuf[PACKET_HEAD_LEN+2] := HOST_CALL;
   crc := CRC16(@sendbuf[PACKET_HEAD_LEN+1],2);
   CopyMemory(@sendbuf[PACKET_HEAD_LEN+3],@crc,2);
   //Move((@sendbuf[1])^,pb.RSMP^,4);
   pb.RSMP := @sendbuf[1];
   pb.RSML := ph.PackLen;
   result :=  pb;
end;
//查询POS机工作状态应答
function HostCallBack(id : byte;state : PChar) : Rec_Send_Message;
var
  crc : Word;
  ph : TPacketHead;
  tempbuf : array[1..3] of byte;
  pb : Rec_Send_Message;
begin
   ZeroMemory(@sendbuf[1],MAXBUF);
   ZeroMemory(@tempbuf[1],3);
   //pb.RSMP := Pointer(LocalAlloc( LPTR, 13));
   ph.HeadWord := $AA55;
   ph.PackLen := PACKET_HEAD_LEN + 20;
   CopyMemory(@tempbuf[1],@ph.HeadWord,2);
   tempbuf[3] := ph.PackLen;
   ph.HeadCrc := CRC16(@tempbuf[1],3);
   CopyMemory(@sendbuf[1],@tempbuf[1],3);
   CopyMemory(@sendbuf[4],@ph.HeadCrc,2);
   sendbuf[PACKET_HEAD_LEN+1] := id;
   sendbuf[PACKET_HEAD_LEN+2] := HOST_CALL;
   CopyMemory(@sendbuf[PACKET_HEAD_LEN+3],state,16);
   crc := CRC16(@sendbuf[PACKET_HEAD_LEN+1],18);
   CopyMemory(@sendbuf[PACKET_HEAD_LEN+19],@crc,2);
   //Move((@sendbuf[1])^,pb.RSMP^,12);
   pb.RSMP := @sendbuf[1];
   pb.RSML := ph.PackLen;
   result :=  pb;
end;
//开阀命令
function OpenValve(id : byte;fid : byte) : Rec_Send_Message;
var
  crc : Word;
  ph : TPacketHead;
  tempbuf : array[1..3] of byte;
  pb : Rec_Send_Message;
begin
   ZeroMemory(@sendbuf[1],MAXBUF);
   ZeroMemory(@tempbuf[1],3);
   //pb.RSMP := Pointer(LocalAlloc( LPTR, 6 ));
   ph.HeadWord := $AA55;
   ph.PackLen := PACKET_HEAD_LEN + 5;
   CopyMemory(@tempbuf[1],@ph.HeadWord,2);
   tempbuf[3] := ph.PackLen;
   ph.HeadCrc := CRC16(@tempbuf[1],3);
   CopyMemory(@sendbuf[1],@tempbuf[1],3);
   CopyMemory(@sendbuf[4],@ph.HeadCrc,2);
   sendbuf[PACKET_HEAD_LEN+1] := id;
   sendbuf[PACKET_HEAD_LEN+2] := OPEN_VALVE;
   sendbuf[PACKET_HEAD_LEN+3] := fid;
   crc := CRC16(@sendbuf[PACKET_HEAD_LEN+1],3);
   CopyMemory(@sendbuf[PACKET_HEAD_LEN+4],@crc,2);
   //Move((@sendbuf[1])^,pb.RSMP^,5);
   pb.RSMP := @sendbuf[1];
   pb.RSML := ph.PackLen;
   result :=  pb;
end;
//开阀命令应答
function OpenValveBack(id : byte;fid : byte) : Rec_Send_Message;
var
  crc : Word;
  ph : TPacketHead;
  tempbuf : array[1..3] of byte;
  pb : Rec_Send_Message;
begin
   ZeroMemory(@sendbuf[1],MAXBUF);
   ZeroMemory(@tempbuf[1],3);
   //pb.RSMP := Pointer(LocalAlloc( LPTR, 6 ));
   ph.HeadWord := $AA55;
   ph.PackLen := PACKET_HEAD_LEN + 5;
   CopyMemory(@tempbuf[1],@ph.HeadWord,2);
   tempbuf[3] := ph.PackLen;
   ph.HeadCrc := CRC16(@tempbuf[1],3);
   CopyMemory(@sendbuf[1],@tempbuf[1],3);
   CopyMemory(@sendbuf[4],@ph.HeadCrc,2);
   sendbuf[PACKET_HEAD_LEN+1] := id;
   sendbuf[PACKET_HEAD_LEN+2] := OPEN_VALVE;
   sendbuf[PACKET_HEAD_LEN+3] := fid;
   crc := CRC16(@sendbuf[PACKET_HEAD_LEN+1],3);
   CopyMemory(@sendbuf[PACKET_HEAD_LEN+4],@crc,2);
   //Move((@sendbuf[1])^,pb.RSMP^,5);
   pb.RSMP := @sendbuf[1];
   pb.RSML := ph.PackLen;
   result :=  pb;
end;
//关机命令
function CloseValve(id : byte;fid : byte) : Rec_Send_Message;
var
  crc : Word;
  ph : TPacketHead;
  tempbuf : array[1..3] of byte;
  pb : Rec_Send_Message;
begin
   ZeroMemory(@sendbuf[1],MAXBUF);
   ZeroMemory(@tempbuf[1],3);
   //pb.RSMP := Pointer(LocalAlloc( LPTR, 6 ));
   ph.HeadWord := $AA55;
   ph.PackLen := PACKET_HEAD_LEN + 5;
   CopyMemory(@tempbuf[1],@ph.HeadWord,2);
   tempbuf[3] := ph.PackLen;
   ph.HeadCrc := CRC16(@tempbuf[1],3);
   CopyMemory(@sendbuf[1],@tempbuf[1],3);
   CopyMemory(@sendbuf[4],@ph.HeadCrc,2);
   sendbuf[PACKET_HEAD_LEN+1] := id;
   sendbuf[PACKET_HEAD_LEN+2] := CLOSE_VALVE;
   sendbuf[PACKET_HEAD_LEN+3] := fid;
   crc := CRC16(@sendbuf[PACKET_HEAD_LEN+1],3);
   CopyMemory(@sendbuf[PACKET_HEAD_LEN+4],@crc,2);
   //Move((@sendbuf[1])^,pb.RSMP^,5);
   pb.RSMP := @sendbuf[1];
   pb.RSML := ph.PackLen;
   result :=  pb;
end;
//关机命令应答
function CloseValveBack(id : byte;fid : byte) : Rec_Send_Message;
var
  crc : Word;
  ph : TPacketHead;
  tempbuf : array[1..3] of byte;
  pb : Rec_Send_Message;
begin
   ZeroMemory(@sendbuf[1],MAXBUF);
   ZeroMemory(@tempbuf[1],3);
   //pb.RSMP := Pointer(LocalAlloc( LPTR, 6 ));
   ph.HeadWord := $AA55;
   ph.PackLen := PACKET_HEAD_LEN + 5;
   CopyMemory(@tempbuf[1],@ph.HeadWord,2);
   tempbuf[3] := ph.PackLen;
   ph.HeadCrc := CRC16(@tempbuf[1],3);
   CopyMemory(@sendbuf[1],@tempbuf[1],3);
   CopyMemory(@sendbuf[4],@ph.HeadCrc,2);
   sendbuf[PACKET_HEAD_LEN+1] := id;
   sendbuf[PACKET_HEAD_LEN+2] := CLOSE_VALVE;
   sendbuf[PACKET_HEAD_LEN+3] := fid;
   crc := CRC16(@sendbuf[PACKET_HEAD_LEN+1],3);
   CopyMemory(@sendbuf[PACKET_HEAD_LEN+4],@crc,2);
   //Move((@sendbuf[1])^,pb.RSMP^,5);
   pb.RSMP := @sendbuf[1];
   pb.RSML := ph.PackLen;
   result :=  pb;
end;
//通讯检查命令
function CommunicationCheck(id : byte) : Rec_Send_Message;
var
  crc : Word;
  ph : TPacketHead;
  tempbuf : array[1..3] of byte;
  pb : Rec_Send_Message;
begin
   ZeroMemory(@sendbuf[1],MAXBUF);
   ZeroMemory(@tempbuf[1],3);
   //pb.RSMP := Pointer(LocalAlloc( LPTR, 5 ));
   ph.HeadWord := $AA55;
   ph.PackLen := PACKET_HEAD_LEN + 4;
   CopyMemory(@tempbuf[1],@ph.HeadWord,2);
   tempbuf[3] := ph.PackLen;
   ph.HeadCrc := CRC16(@tempbuf[1],3);
   CopyMemory(@sendbuf[1],@tempbuf[1],3);
   CopyMemory(@sendbuf[4],@ph.HeadCrc,2);
   sendbuf[PACKET_HEAD_LEN+1] := id;
   sendbuf[PACKET_HEAD_LEN+2] := COMMUNICATION_CHECK;
   crc := CRC16(@sendbuf[PACKET_HEAD_LEN+1],2);
   CopyMemory(@sendbuf[PACKET_HEAD_LEN+3],@crc,2);
   //Move((@sendbuf[1])^,pb.RSMP^,4);
   pb.RSMP := @sendbuf[1];
   pb.RSML := ph.PackLen;
   result :=  pb;
end;
//通讯检查命令应答
function CommunicationCheckBack(id : byte) : Rec_Send_Message;
var
  crc : Word;
  ph : TPacketHead;
  tempbuf : array[1..3] of byte;
  pb : Rec_Send_Message;
begin
   ZeroMemory(@sendbuf[1],MAXBUF);
   ZeroMemory(@tempbuf[1],3);
   //pb.RSMP := Pointer(LocalAlloc( LPTR, 5 ));
   ph.HeadWord := $AA55;
   ph.PackLen := PACKET_HEAD_LEN + 4;
   CopyMemory(@tempbuf[1],@ph.HeadWord,2);
   tempbuf[3] := ph.PackLen;
   ph.HeadCrc := CRC16(@tempbuf[1],3);
   CopyMemory(@sendbuf[1],@tempbuf[1],3);
   CopyMemory(@sendbuf[4],@ph.HeadCrc,2);
   sendbuf[PACKET_HEAD_LEN+1] := id;
   sendbuf[PACKET_HEAD_LEN+2] := COMMUNICATION_CHECK;
   crc := CRC16(@sendbuf[PACKET_HEAD_LEN+1],2);
   CopyMemory(@sendbuf[PACKET_HEAD_LEN+3],@crc,2);
   //Move((@sendbuf[1])^,pb.RSMP^,4);
   pb.RSMP := @sendbuf[1];
   pb.RSML := ph.PackLen;
   result :=  pb;
end;
//发送卡号命令
function SendCard(id : byte) : Rec_Send_Message;
var
  crc : Word;
  ph : TPacketHead;
  tempbuf : array[1..3] of byte;
  pb : Rec_Send_Message;
begin
   ZeroMemory(@sendbuf[1],MAXBUF);
   ZeroMemory(@tempbuf[1],3);
   //pb.RSMP := Pointer(LocalAlloc( LPTR, 5 ));
   ph.HeadWord := $AA55;
   ph.PackLen := PACKET_HEAD_LEN + 4;
   CopyMemory(@tempbuf[1],@ph.HeadWord,2);
   tempbuf[3] := ph.PackLen;
   ph.HeadCrc := CRC16(@tempbuf[1],3);
   CopyMemory(@sendbuf[1],@tempbuf[1],3);
   CopyMemory(@sendbuf[4],@ph.HeadCrc,2);
   sendbuf[PACKET_HEAD_LEN+1] := id;
   sendbuf[PACKET_HEAD_LEN+2] := SEND_CARD;
   crc := CRC16(@sendbuf[PACKET_HEAD_LEN+1],2);
   CopyMemory(@sendbuf[PACKET_HEAD_LEN+3],@crc,2);
   //Move((@sendbuf[1])^,pb.RSMP^,4);
   pb.RSMP := @sendbuf[1];
   pb.RSML := ph.PackLen;
   result :=  pb;
end;
//发送卡号命令应答
{
卡种:00H   无卡
      11H   光电卡
      22H   惠众卡
      33H   射频卡
卡号长度 4 字节,高位在前
}
function SendCardBack(id : byte;ct : byte;cn : integer) : Rec_Send_Message;
var
  crc : Word;
  ph : TPacketHead;
  tempbuf : array[1..3] of byte;
  pb : Rec_Send_Message;
begin
   ZeroMemory(@sendbuf[1],MAXBUF);
   ZeroMemory(@tempbuf[1],3);
   //pb.RSMP := Pointer(LocalAlloc( LPTR, 5 ));
   ph.HeadWord := $AA55;
   ph.PackLen := PACKET_HEAD_LEN + 9;
   CopyMemory(@tempbuf[1],@ph.HeadWord,2);
   tempbuf[3] := ph.PackLen;
   ph.HeadCrc := CRC16(@tempbuf[1],3);
   CopyMemory(@sendbuf[1],@tempbuf[1],3);
   CopyMemory(@sendbuf[4],@ph.HeadCrc,2);
   sendbuf[PACKET_HEAD_LEN+1] := id;
   sendbuf[PACKET_HEAD_LEN+2] := SEND_CARD;
   sendbuf[PACKET_HEAD_LEN+3] := ct;
   CopyMemory(@sendbuf[PACKET_HEAD_LEN+4],@cn,4);
   crc := CRC16(@sendbuf[PACKET_HEAD_LEN+1],7);
   CopyMemory(@sendbuf[PACKET_HEAD_LEN+8],@crc,2);
   //Move((@sendbuf[1])^,pb.RSMP^,4);
   pb.RSMP := @sendbuf[1];
   pb.RSML := ph.PackLen;
   result :=  pb;
end;
//发送卡号命令无卡号应答
function NoSendCardBack(id : byte;stb : byte;st : byte) : Rec_Send_Message;
var
  crc : Word;
  ph : TPacketHead;
  tempbuf : array[1..3] of byte;
  pb : Rec_Send_Message;
begin
   ZeroMemory(@sendbuf[1],MAXBUF);
   ZeroMemory(@tempbuf[1],3);
   //pb.RSMP := Pointer(LocalAlloc( LPTR, 5 ));
   ph.HeadWord := $AA55;
   ph.PackLen := PACKET_HEAD_LEN + 6;
   CopyMemory(@tempbuf[1],@ph.HeadWord,2);
   tempbuf[3] := ph.PackLen;
   ph.HeadCrc := CRC16(@tempbuf[1],3);
   CopyMemory(@sendbuf[1],@tempbuf[1],3);
   CopyMemory(@sendbuf[4],@ph.HeadCrc,2);
   sendbuf[PACKET_HEAD_LEN+1] := id;
   sendbuf[PACKET_HEAD_LEN+2] := SEND_CARD;
   sendbuf[PACKET_HEAD_LEN+3] := stb;
   sendbuf[PACKET_HEAD_LEN+4] := st;
   crc := CRC16(@sendbuf[PACKET_HEAD_LEN+1],4);
   CopyMemory(@sendbuf[PACKET_HEAD_LEN+5],@crc,2);
   //Move((@sendbuf[1])^,pb.RSMP^,4);
   pb.RSMP := @sendbuf[1];
   pb.RSML := ph.PackLen;
   result :=  pb;
end;
//收到卡号通报
function RecvCard(id : byte) : Rec_Send_Message;
var
  crc : Word;
  ph : TPacketHead;
  tempbuf : array[1..3] of byte;
  pb : Rec_Send_Message;
begin
   ZeroMemory(@sendbuf[1],MAXBUF);
   ZeroMemory(@tempbuf[1],3);
   //pb.RSMP := Pointer(LocalAlloc( LPTR, 5 ));
   ph.HeadWord := $AA55;
   ph.PackLen := PACKET_HEAD_LEN + 4;
   CopyMemory(@tempbuf[1],@ph.HeadWord,2);
   tempbuf[3] := ph.PackLen;
   ph.HeadCrc := CRC16(@tempbuf[1],3);
   CopyMemory(@sendbuf[1],@tempbuf[1],3);
   CopyMemory(@sendbuf[4],@ph.HeadCrc,2);
   sendbuf[PACKET_HEAD_LEN+1] := id;
   sendbuf[PACKET_HEAD_LEN+2] := RECV_CARD;
   crc := CRC16(@sendbuf[PACKET_HEAD_LEN+1],2);
   CopyMemory(@sendbuf[PACKET_HEAD_LEN+3],@crc,2);
   //Move((@sendbuf[1])^,pb.RSMP^,4);
   pb.RSMP := @sendbuf[1];
   pb.RSML := ph.PackLen;
   result :=  pb;
end;
//收到卡号通报应答
function RecvCardBack(id : byte) : Rec_Send_Message;
var
  crc : Word;
  ph : TPacketHead;
  tempbuf : array[1..3] of byte;
  pb : Rec_Send_Message;
begin
   ZeroMemory(@sendbuf[1],MAXBUF);
   ZeroMemory(@tempbuf[1],3);
   //pb.RSMP := Pointer(LocalAlloc( LPTR, 5 ));
   ph.HeadWord := $AA55;
   ph.PackLen := PACKET_HEAD_LEN + 4;
   CopyMemory(@tempbuf[1],@ph.HeadWord,2);
   tempbuf[3] := ph.PackLen;
   ph.HeadCrc := CRC16(@tempbuf[1],3);
   CopyMemory(@sendbuf[1],@tempbuf[1],3);
   CopyMemory(@sendbuf[4],@ph.HeadCrc,2);
   sendbuf[PACKET_HEAD_LEN+1] := id;
   sendbuf[PACKET_HEAD_LEN+2] := RECV_CARD;
   crc := CRC16(@sendbuf[PACKET_HEAD_LEN+1],2);
   CopyMemory(@sendbuf[PACKET_HEAD_LEN+3],@crc,2);
   //Move((@sendbuf[1])^,pb.RSMP^,4);
   pb.RSMP := @sendbuf[1];
   pb.RSML := ph.PackLen;
   result :=  pb;
end;
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -