读命令 [英] Comport readstr
问题描述
我正在尝试从串行线RS232上的主机读取一个简单的字符串。
接收-传输的参数对于两个都是相同的:
I'm trying to reading a simple string from host on Serial line RS232. The parameters of receive - transmission are the same for both:
Baudrate: 9600;
Data: 8bit;
Parity: None;
Stop: 1bit;
Flow Control: None;
我尝试发送以下字符串:
I've tried to send this string:
!A243B324C213D300#
!A243B324C213D300#
我使用'!'和'#'作为标题,并完成操作以确保完全接收到该字符串。
问题是我可以在第15个字符(3)之前收到字符串。
I use '!' and '#' as header and finish to be sure the string will be received at all. The problem is that I can receive the string until the 15th character (3).
我知道这一点,因为如果我使用以下代码:
I know this because if I use this code:
procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
begin
ComPort1.ReadStr(Str, Count);
memo1.Lines.Add(str);
end;
我会看到
!A243B324C213D3
!A243B324C213D3
00#
我试图延长会议的缓冲区,但没有结果。
I've tried to extend the buffer of the comport, without results.
那为什么我一次也收不到完整的琴弦?
So why I can't receive the complete string in one shot?
我在这里找到了这个库,并且到现在为止还可以正常使用:
I found this library here and it works fine until now:
http://sourceforge.net/projects/comport/
推荐答案
带有TComPort组件的是另一个组件:TComDataPacket。只需将其连接到TComPort,设置StartString和StopString属性,然后使用事件onPacket即可获取完整的数据。
With TComPort component comes another one: TComDataPacket. Simply connect this to TComPort, setup StartString and StopString properties and use event onPacket to get complete data.
示例:
...
ComDataPacket1.Comport := Comport1;
ComDataPacket1.StartString := '!';
ComDataPacket1.StopString := '#';
ComDataPacket1.onPacket := ComDataPacket1Packet;
...
//this is real code from one of my applications where I use it for barcode reader service
procedure TDM.ComDataPacket1Packet(Sender: TObject; const Str: string);
begin
BarCodeReader.Barcode := Str;
end;
这篇关于读命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!