Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TComPort again

747 views
Skip to first unread message

Sanyin

unread,
Feb 26, 2008, 9:30:28 AM2/26/08
to
Using TComPort component, I send command to device :


ComPort.write(command[0], length(c));


Waiting for response:

tev := [evRxChar];
ComPort.WaitForEvent(tev, stopevent.handle, TimeOut);
if not (evRxChar in tev) then exit;

Read from port:

if ComPort.InputCount <> 100 then exit;
setlength(s, 1);
ComPort.Read(s[0], 1);

Now, I am expecting some bytes from commport, but with this code above
(waiting) after WaitForEvent, InputCount is low (17-18).
Solution is to put sleep after waiting.
So, when you wait for evRxChar, it doesnt mean that you'll have all data,
right?
I suppose I must call WaitForEvent until InputCount is 100?
Thanks.


Ricardo Cardona R

unread,
Feb 26, 2008, 9:55:01 AM2/26/08
to
Sanyin,

Sanyin escribió:


> Now, I am expecting some bytes from commport

Synaser is more convenient when you need send and wait data because not use events.

The following is a small example how establish a connection by modem:

ser:=TBlockSerial.Create;
try
ser.Connect('COM3');
ser.config(460800,8,'N',0,false,true);
ser.ATCommand('AT');
if (ser.LastError <> 0) or (not ser.ATResult) then
Exit;
ser.ATConnect('ATDT+420971200111');
if (ser.LastError <> 0) or (not ser.ATResult) then
Exit;
// you are now connected to a modem at +420971200111
// you can transmit or receive data now
finally
ser.free;
end;

or use:

function RecvTerminated(Timeout: Integer; const Terminator: string): string; virtual;

This method waits until a terminated data string is received. This string is terminated by the Terminator string. The resulting string is returned without this termination string! If no data is received within the Timeout (in milliseconds) period, LastError is set to ErrTimeout.

Ricardo Cardona.


Sanyin

unread,
Feb 29, 2008, 2:47:41 AM2/29/08
to

"Ricardo Cardona R" <ricardona_fix_@no_this___.gmail.com> wrote in message
news:47c42732$1...@newsgroups.borland.com...
Sanyin,

Sanyin escribió:
> Now, I am expecting some bytes from commport

WHen I send some data, that I expect device to send back some data, but how
to do that?
For example, sending byte $5 should make device to respond with byte $6, but
InputCount property gives me 7 bytes?!?
(using RecvBufferEx to read)


Sanyin

unread,
Feb 29, 2008, 2:50:44 AM2/29/08
to

"Ricardo Cardona R" <ricardona_fix_@no_this___.gmail.com> wrote in message
news:47c42732$1...@newsgroups.borland.com...
Sanyin,

Sanyin escribió:
> Now, I am expecting some bytes from commport

WHen I send some data, that I expect device to send back some data, but how

Dave Hackett

unread,
Mar 5, 2008, 3:14:02 PM3/5/08
to
Yes, you have the right idea. You keep collecting input from the COM port
until you have what you want.

Example
var
ResponseStr : string;
procedure TForm1.ComPortRxChar(Sender: TObject; Count: Integer);
var
Str: String;
begin
ComPort.ReadStr(Str, Count);
ResponseStr := ResponseStr + Str;
if length(ResponseStr) = 100 then
//call some method to parse amd handle ResponseStr
//reset the ResponseStr to blank
end;

This is more efficient then blocking while waiting for input. You only call
the handler when you have something to do.

BTW, ComPortRxChar is only called when Windows has something to give you.
IOW, something has come in through the serial port. TComPort doesn't
control what comes in nor does it do any translation. What you see is what
you got. If it's not what you expected, throw it away - but you might want
to determine "why is this being sent to me over the COM port?".

DaveH

"Sanyin" <prevo...@hotmail.com> wrote in message
news:47c4...@newsgroups.borland.com...

0 new messages