tags:

views:

430

answers:

4

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.

+2  A: 

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

check these links

RRUZ
There is IMHO nothing wrong with tcomport.
Marco van de Voort
+3  A: 

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

Daniel Luyo
If data is read in a loop the `Buffer` parameter should not be the complete array, but the next element of the array to be written to.
mghie
the buffer should be a "workimg buffer" append its content to the final variable
Daniel Luyo
You should flesh out your answer so it becomes clearer what you mean.
mghie
A: 
// 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?

becsystems
A: 

Hi there - sorry to re-open this thread, but I might be having a similar problem...

I'm trying to replicate an old Windows program running in XP that reads data from an old valve machine via RS232.

I am using unchanged ComExample.dpr, with rxOnChar, but only first 2 chars show on memo i.e. ",," which (I believe) is the first packet.

Has anyone any idea as to why?

e.g. ComSpy shows data as follows: ",, ÿ B 101SV30 0898 mˆ‚PPš" etc. etc.
(but all I get is these first 2 characters which ComSpy identifies as first packet)

Very many thanks in advance

Paul.

P.S. Additional Comspy info is as follows:

In/out queue size 1024/512
Purge the serial port: RXABORT, RXCLEAR, TXABORT, TXCLEAR
Set timeouts: ReadInterval=-1, ReadTotalTimeoutMultiplier=0, ReadTotalTimeoutConstant=0, WriteTotalTimeoutMultiplier=0, WriteTotalTimeoutConstant=5000 Baud rate 9600 (I've set this)
RTS off
DTR on
Data bits=8, Stop bits=1, Parity=Odd (and I've set these)
Set chars: Eof=0x1A, Error=0x3F, Break=0x3F, Event=0x00, Xon=0x11, Xoff=0x13
Handflow: ControlHandShake=(DTR_CONTROL), FlowReplace=(ERROR_CHAR), XonLimit=256, XoffLimit=256