windows 使用 Dejan TComport Delphi 组件从串口读取二进制数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2458219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:07:57  来源:igfitidea点击:

Reading binary data from serial port using Dejan TComport Delphi component

windowsdelphi

提问by

Apologies for this question but I am a bit of a noob with Delphi. I am using Dejan TComport component to get data from a serial port. A box of equipment connected to the port sends about 100 bytes of binary data to the serial port. What I want to do is extract the bytes as numerical values into an array so that I can perform calculations on them.

为这个问题道歉,但我对 Delphi 有点菜鸟。我正在使用 Dejan TComport 组件从串行端口获取数据。连接到端口的一盒设备向串行端口发送大约 100 字节的二进制数据。我想要做的是将字节作为数值提取到数组中,以便我可以对它们进行计算。

TComport has a method Read(buffer,Count) which reads DATA from input buffer.

TComport 有一个方法 Read(buffer,Count) 从输入缓冲区读取数据。

function Read(var Buffer; Count: Integer): Integer;

The help says the Buffer variable must be large enough to hold Count bytes but does not provide any example of how to use this function. I can see that the Count variable holds the number of bytes received but I can't find a way to access the bytes in Buffer.

帮助说明 Buffer 变量必须足够大以容纳 Count 字节,但没有提供如何使用此函数的任何示例。我可以看到 Count 变量保存了接收到的字节数,但我找不到访问 Buffer 中字节的方法。

TComport also has a methord Readstr which reads data from input buffer into a STRING variable.

TComport 还有一个方法 Readstr,它从输入缓冲区读取数据到一个 STRING 变量中。

function ReadStr(var Str: String; Count: Integer): Integer;

Again the Count variable shows the number of bytes received and I can use Memo1.Text:=str to display some information but obviously Memo1 has problems displaying the control characters. I have tried various ways to try and extract the byte data from Str but so far without success.

Count 变量再次显示接收到的字节数,我可以使用 Memo1.Text:=str 来显示一些信息,但显然 Memo1 在显示控制字符时有问题。我尝试了各种方法来尝试从 Str 中提取字节数据,但到目前为止都没有成功。

I am sure it must be easy. Here's hoping.

我相信这一定很容易。这是希望。

采纳答案by Daniel Luyo

In this function

在这个函数中

function Read(var Buffer; Count: Integer): Integer;

The Count parameter is the number of bytes you expect to read. While the function return value is actually read bytes.

Count 参数是您希望读取的字节数。而函数返回值实际上是读取字节。

If you have a Buffer defined as an array of 100 bytes you can code

如果你有一个 Buffer 定义为一个 100 字节的数组,你可以编码

x := Read(Buffer, 100);

and if the input is only 70 bytes then x will be 70. That way you can read while x > 0

如果输入只有 70 个字节,那么 x 将为 70。这样你就可以在 x > 0 时读取

回答by becsystems

// I use a timer to read a weight coming in on the Serial Port
// but the routing could also be triggered by OnRXChar (received data event)
// or OnRXBufferFull event.

var
  WeightString: String; //global

procedure TfmUDF.tmScaleTimer(Sender: TObject);
var
  Data: AnsiString;
begin
  ReadStr(Data,Count); //the individual bytes can be read Data[n].....
  WeightData:=WeightData+Data; //just keeps adding the incoming data
end;

Does this help?

这有帮助吗?

回答by RRUZ

@johnma, I recommend that you use the TurboPower Async Library, it is very efficient, has many examples and is well documented.

@johnma,我建议您使用TurboPower Async Library,它非常高效,有很多示例并且有据可查。

check these links

检查这些链接