使用Dejan TComport Delphi组件从串行端口读取二进制数据 [英] Reading binary data from serial port using Dejan TComport Delphi component

查看:339
本文介绍了使用Dejan TComport Delphi组件从串行端口读取二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

TComport有一个Read(buffer,Count)方法,

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

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

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

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.

TComport还有一个方法Readstr

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

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

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

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.

我确信它一定很容易。希望如此

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

推荐答案

此功能

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

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

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

如果您将Buffer定义为100个字节的数组,则可以进行编码

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

x := Read(Buffer, 100);

如果输入仅为70个字节,则x为70。
可以在x> 0

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

这篇关于使用Dejan TComport Delphi组件从串行端口读取二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆