现想从串中读取数据并从里面取出来,现已作到能从串口读出数据(使用的是API函数的方式),但是不知怎样的取其中的数据,数据协议如下:
客户系统 RS232 无线PDA系统客户系统->系统(发信请求通信协议)
Baud rate Parity Word length Stop bits
波特率 奇偶校验位 数据长度 数据停止位
1200 none 8 128H PDA号(6byte右对齐)(用空格补足6byte) 20H 任务内容(不超过100 byte) 54H无线PDA系统->客户系统(收到发信请求反馈信息, 注:当寻呼接口收到后)05H (ACK)注: 28H , 54H , 20H(空格) , 05H 为 ASCII 码 ,其他为字符。数据信息数据读出代码如下:(采用了一个listview作表显示相应的提取数据)//数据接收消息处理函数
procedure tform1.wmcommnotify(var message:tmessage);
var
titem:tlistitem;
commstate:comstat;
dwnumberofbytesread:dword;
errorflag:dword;
inputbuffer:array[0..1024] of char; //静态数组
recvstring:string;
recstr,recno,recinfo,recmes:string;
begin
if not clearcommerror(hrecv,errorflag,@commstate) then
begin
messagebox(0,'clearcommerror !','notice',mb_ok);
purgecomm(hrecv,purge_rxabort or purge_rxclear);
exit;
end;
if (commstate.cbInQue>0) then
begin
fillchar(inputbuffer,commstate.cbInQue,#0);
if (not readfile(hrecv,inputbuffer,commstate.cbInQue,dwnumberofbytesread,@read_os)) then
begin
errorflag:=getlasterror();
if (errorflag<>0)and (errorflag<>error_io_pending) then
begin
receive:=false;
raise exception.Create('读串口数据出错!!');
end
else
begin
waitforsingleobject(hrecv,infinite);
getoverlappedresult(hrecv,read_os,dwnumberofbytesread,false);
end;
end;
if dwnumberofbytesread>0 then
begin
read_os.Offset:=read_os.Offset+dwnumberofbytesread;
inputbuffer[dwnumberofbytesread]:=#0;//其中inputbuffer即是接收的包,就在这里提取内容 //开始提取数据
recstr:=copy(inputbuffer,1,pos(05H,inputbuffer));
recinfo:=copy(recstr,pos(#28H,recstr),pos(#54H,recstr));
recno:=copy(recinfo,pos(#28H,recinfo)+1,pos(#03H,recinfo)-1);
recmes:=copy(recinfo,pos(#03H,recinfo)+1,pos(#54H,recinfo)-7);
titem:=listview1.Items.Add;
titem.caption:=strpas(recno);
titem.SubItems.Add(strpas(recmes));
end;
end;
setevent(post_event);
end;以上的数据的提取是否写的正确,里面有没有什么要注意的,
1、比如是不是可能出现复的提取着一条数据,因为数据在提取之后在内存是否已删除,
客户系统 RS232 无线PDA系统客户系统->系统(发信请求通信协议)
Baud rate Parity Word length Stop bits
波特率 奇偶校验位 数据长度 数据停止位
1200 none 8 128H PDA号(6byte右对齐)(用空格补足6byte) 20H 任务内容(不超过100 byte) 54H无线PDA系统->客户系统(收到发信请求反馈信息, 注:当寻呼接口收到后)05H (ACK)注: 28H , 54H , 20H(空格) , 05H 为 ASCII 码 ,其他为字符。数据信息数据读出代码如下:(采用了一个listview作表显示相应的提取数据)//数据接收消息处理函数
procedure tform1.wmcommnotify(var message:tmessage);
var
titem:tlistitem;
commstate:comstat;
dwnumberofbytesread:dword;
errorflag:dword;
inputbuffer:array[0..1024] of char; //静态数组
recvstring:string;
recstr,recno,recinfo,recmes:string;
begin
if not clearcommerror(hrecv,errorflag,@commstate) then
begin
messagebox(0,'clearcommerror !','notice',mb_ok);
purgecomm(hrecv,purge_rxabort or purge_rxclear);
exit;
end;
if (commstate.cbInQue>0) then
begin
fillchar(inputbuffer,commstate.cbInQue,#0);
if (not readfile(hrecv,inputbuffer,commstate.cbInQue,dwnumberofbytesread,@read_os)) then
begin
errorflag:=getlasterror();
if (errorflag<>0)and (errorflag<>error_io_pending) then
begin
receive:=false;
raise exception.Create('读串口数据出错!!');
end
else
begin
waitforsingleobject(hrecv,infinite);
getoverlappedresult(hrecv,read_os,dwnumberofbytesread,false);
end;
end;
if dwnumberofbytesread>0 then
begin
read_os.Offset:=read_os.Offset+dwnumberofbytesread;
inputbuffer[dwnumberofbytesread]:=#0;//其中inputbuffer即是接收的包,就在这里提取内容 //开始提取数据
recstr:=copy(inputbuffer,1,pos(05H,inputbuffer));
recinfo:=copy(recstr,pos(#28H,recstr),pos(#54H,recstr));
recno:=copy(recinfo,pos(#28H,recinfo)+1,pos(#03H,recinfo)-1);
recmes:=copy(recinfo,pos(#03H,recinfo)+1,pos(#54H,recinfo)-7);
titem:=listview1.Items.Add;
titem.caption:=strpas(recno);
titem.SubItems.Add(strpas(recmes));
end;
end;
setevent(post_event);
end;以上的数据的提取是否写的正确,里面有没有什么要注意的,
1、比如是不是可能出现复的提取着一条数据,因为数据在提取之后在内存是否已删除,
解决方案 »
- 有关加密和字符类型的问题
- select的问题
- 谁能告诉我TDWindow和TDButton是哪类控件中的类,100分相送
- ado 连接access数据库,用程序中设置connectstring的方法出错了!
- 我要写个ActiveX控件,大家过来给我出个注意吧。
- QuickRep报表控件打印预览时,如何去掉标题栏的最小化按钮?
- 散分!答者均有分。
- Delphi 7 Studio Architect
- 我用TTBALE和TBDGRID,现在我也不知道怎样排序显示.我连建立多索引文件都不会.谁帮我呀.
- 如何把通过摄像头取下的照片来保存??
- 问一个对高手不算太难的问题
- 请大家推荐,常用的DELPHI(免费,无版权问题)的常用控件.....谢谢....
procedure Tfrm_icread.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);var tmpArray:array[0..256] of Byte;
ArraySize: DWORD;
Count:DWORD;
tmpStr:string;
i:integer;
pStr:PChar;
begin //-------------接受返回串口返回数据-----------
pStr:=Buffer;
tmpStr:=string(pStr);
Dec(PStr);
tempb:='';
for i:=0 to bufferlength-1 do
begin
inc(PStr);
tmpArray[i]:=Byte(PSTR^);
tempb:=tempb+IntToHEX(Ord(tmpArray[i]),2); end;
end;
procedure tform1.wmcommnotify(var message:tmessage);
var
titem:tlistitem;
commstate:comstat;
dwnumberofbytesread:dword;
errorflag:dword;
inputbuffer:array[0..1024] of char; //静态数组
recvstring:string;
recstr,recno,recinfo,recmes:string;
i,n,m:integer; begin//-------------接受返回串口返回数据----------- //开始提取数据
recinfo:=copy(trim(inputbuffer),pos(#$28,trim(inputbuffer)),pos(#$54,trim(inputbuffer)));
recno:=copy(trim(recinfo),2,pos(#$54,trim(recinfo))-3); i:=0;
recstr:='';
while i<length(recno) do
begin
i:=i+1;
recmes:=inttostr(ord(recno[i]));
recstr:=recstr+recmes;
end;
titem:=listview1.Items.Add;
titem.caption:=datetimetostr(date()+time());
titem.SubItems.Add(recstr);