Einzelnen Beitrag anzeigen

Photoner

Registriert seit: 6. Dez 2012
Ort: Nürnberg
103 Beiträge
 
Delphi 10.1 Berlin Starter
 
#21

AW: COM Port Daten auslesen und auf bestimmtes Char reagieren

  Alt 9. Feb 2016, 12:01
Readfile liefert in deiner Konstellation falsche Ergebnisse da es

entweder a) mit Overlapped wieder asynchron ist (Rücksprung bevor fertig gelesen)
oder b) mit nil beim Overlapped einfach falsche Ergebnisse liefern kann.

Du musst das Rückgabeergebnis von Readfile prüfen und wieder auf das Event warten:

Delphi-Quellcode:
if not ReadFile(MyHandle, ReceiveBuffer, 1024, nil, @rOverlapped) then
  if GetLastError()<>ERROR_IO_PENDING raise Exception.Create('COM Fehler');
case WaitForSingleObject(rOverlapped.hEvent,ATimeOutInMS) of
 ...
Ich muss nochmals auf die Quelle schlechthin, MSDN, hinweisen. Steht alles da.

ReadFile

"If hFile is opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must point to a valid and unique OVERLAPPED structure, otherwise the function can incorrectly report that the read operation is complete."

"lpNumberOfBytesRead [out, optional]
A pointer to the variable that receives the number of bytes read when using a synchronous hFile parameter. ReadFile sets this value to zero before doing any work or error checking. Use NULL for this parameter if this is an asynchronous operation to avoid potentially erroneous results."

Unter Remarks:
"Synchronization and File Position

If hFile is opened with FILE_FLAG_OVERLAPPED, it is an asynchronous file handle; otherwise it is synchronous. The rules for using the OVERLAPPED structure are slightly different for each, as previously noted.
Note If a file or device is opened for asynchronous I/O, subsequent calls to functions such as ReadFile using that handle generally return immediately, but can also behave synchronously with respect to blocked execution. For more information see http://support.microsoft.com/kb/156932.


Considerations for working with asynchronous file handles:

ReadFile may return before the read operation is complete. In this scenario, ReadFile returns FALSE and the GetLastError function returns ERROR_IO_PENDING, which allows the calling process to continue while the system completes the read operation.
The lpOverlapped parameter must not be NULL and should be used with the following facts in mind:
Although the event specified in the OVERLAPPED structure is set and reset automatically by the system, the offset that is specified in the OVERLAPPED structure is not automatically updated.
ReadFile resets the event to a nonsignaled state when it begins the I/O operation.
The event specified in the OVERLAPPED structure is set to a signaled state when the read operation is complete; until that time, the read operation is considered pending.
Because the read operation starts at the offset that is specified in the OVERLAPPED structure, and ReadFile may return before the system-level read operation is complete (read pending), neither the offset nor any other part of the structure should be modified, freed, or reused by the application until the event is signaled (that is, the read completes).
If end-of-file (EOF) is detected during asynchronous operations, the call to GetOverlappedResult for that operation returns FALSE and GetLastError returns ERROR_HANDLE_EOF.
"
Chris
  Mit Zitat antworten Zitat