Reputation:
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 has a method Read(buffer,Count) which reads DATA from input buffer.
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.
TComport also has a methord Readstr which reads data from input buffer into a STRING variable.
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.
I am sure it must be easy. Here's hoping.
Upvotes: 2
Views: 9789
Reputation: 136
// 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?
Upvotes: 0
Reputation: 1336
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.
If you have a Buffer defined as an array of 100 bytes you can code
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
Upvotes: 2