开发者

Get the count of bytes waiting on a serial port before reading

开发者 https://www.devze.com 2023-03-30 16:31 出处:网络
I have a custom made device with which I send/receive data trough the serial port. I was wondering if there is any function I can use to get the count of bytes waiting to be read from the serial port?

I have a custom made device with which I send/receive data trough the serial port. I was wondering if there is any function I can use to get the count of bytes waiting to be read from the serial port?

I want only a windows api solution, if there exists one. It seems a trivial task and I don't want to use external components开发者_如何学Python.


ClearCommError should fill in a COMSTAT (TComStat record) having a 'cbInQue' member specifying unread number of bytes received on the port.


I use TComPort. Although you can use WinAPi calls, they are tricky and TComPort takes care of the boring stuff. It is very light and free and you can use the TComport.InputCount function which from the help:

 Returns the number of bytes in input buffer.

function InputCount: Integer;

Description
Call InputCount function to get the number of bytes in input buffer.


Win API ClearCommError should return the numbers of characters waiting in receiver buffer, where cHandle is currently used/opend serial communication port.

function TRS232Comm.InputCount: cardinal;
var
  Errors: Cardinal;
  CommStat: TComStat;
begin
  if not ClearCommError(cHandle, Errors, @CommStat) then
  begin
    PurgeComm(cHandle, PURGE_RXCLEAR);     //Just empty comm buffer on error and return 0
    result := 0;
  end else
    result := CommStat.cbInQue;
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消