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.
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 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 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
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...