procedure TMXPLCThread.ReadData(var ReadStr:String;len:Longint);
var
  dwReadBufferSize,ReadCount : Longint;
  szReadBuffer : Array[1..4096] of Char;
  strReadBuffer : String;
begin
   ReadStr := '';
   dwReadBufferSize := 1024;
   StrReadBuffer := '';
   if len=-1 then
   begin
      if ReadFile(CommHandle,szReadBuffer,1024,dwReadBufferSize,nil) then   
      //上一句报错,ERROR CODE:[Type of actual and formal var parameters must be identical]
      begin
         ReadStr := StrPas(@szReadBuffer);
         MSGStr := ReadStr;
         Synchronize(UpdMSGMemo);
      end;
           ReadStr := StrPas(@szReadBuffer);
           MSGStr := ReadStr;
           Synchronize(UpdMSGMemo);
           PurgeComm(CommHandle, PURGE_TXABORT + PURGE_TXCLEAR);
     end
   else
      begin
        ReadCount:= 0;
        repeat
            if ReadFile(CommHandle,szReadBuffer,len,dwReadBufferSize,
               nil) then
      //上一句报错,ERROR CODE:[Type of actual and formal var parameters must be identical]
            begin
               Inc(ReadCount,dwReadBufferSize);
               StrReadBuffer := StrReadBuffer+StrPas(@szReadBuffer);
            end;
        until (ReadCount>=len);
        ReadStr := StrReadBuffer;     end;
end;
procedure TMXPLCThread.WriteData(const WriteStr : String;len : Longint);
var
  dwLastError,
  dwNumberOfBytesWritten,
  dwWhereToStartWriting,
  dwNumberOfBytesToWrite : integer;
  tempStr : String;
begin
    dwNumberOfBytesToWrite := len;
    dwNumberOfBytesWritten := 0;
    dwWhereToStartWriting  := 1; 
   repeat
      // Start the overlapped I/O.
      if not WriteFile(CommHandle,
             WriteStr[ dwWhereToStartWriting ],
             dwNumberOfBytesToWrite, dwNumberOfBytesWritten,
            nil) then 
       
      //上一句报错,ERROR CODE:[Type of actual and formal var parameters must be identical]      begin
                // WriteFile failed.  Expected; lets handle it.
                dwLastError := GetLastError;
                // Its possible for this error to occur if the
                // service provider has closed the port.  Time to end.
                if (dwLastError = ERROR_INVALID_HANDLE) then
                begin
                   Exit;
                end;
      end;
      Dec( dwNumberOfBytesToWrite, dwNumberOfBytesWritten );
      Inc( dwWhereToStartWriting, dwNumberOfBytesWritten );
   until (dwNumberOfBytesToWrite <= 0);  // Write the whole thing!   //PurgeComm(CommHandle, PURGE_TXABORT + PURGE_TXCLEAR);
end;
高人指点!

解决方案 »

  1.   

    最后一个参数的说明
    lpOverlappedPoints to an OVERLAPPED structure. This structure is required if hFile was created with FILE_FLAG_OVERLAPPED.If hFile was opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must not be NULL. It must point to a valid OVERLAPPED structure. If hFile was created with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the function can incorrectly report that the read operation is complete. 
    If hFile was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset specified in the OVERLAPPED structure and ReadFile may return before the read operation has been completed. In this case, ReadFile returns FALSE and the GetLastError function returns ERROR_IO_PENDING. This allows the calling process to continue while the read operation finishes. The event specified in the OVERLAPPED structure is set to the signaled state upon completion of the read operation. If hFile was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the read operation starts at the current file position and ReadFile does not return until the operation has been completed.
    If hFile is not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset specified in the OVERLAPPED structure. ReadFile does not return until the read operation has been completed.
      

  2.   

    if ReadFile(CommHandle,  szReadBuffer[0], 1024, dwReadBufferSize, nil) then
          if ReadFile(CommHandle, szReadBuffer[0], len,dwReadBufferSize, nil) then下列参数代入规则:
    var buf;
    or
    const buf;
    ==>
    V: string;  ==> V[1]  or PChar(V)^
    V: PChar;   ==> V^
    V: Pointer  ==> V^
    V: Integer  ==> V
    V: Byte     ==> V
    V: array of Byte ==> V[0] or V[low(V)]
    至于V的长度,有些是SizeOf, 有些用Length, 有些则需要指定(PChar, Pointer)
      

  3.   

    出参数是var V: DataType时,定义变量时,需要是DataType一致, 不然就会出现//上一句报错,ERROR CODE:[Type of actual and formal var parameters must be identical]基本知识,错误出现多了就熟了。
    procedure WriteData(const WriteStr: string);
    var
      dwLastError,
      dwNumberOfBytesWritten, dwWhereToStartWriting, dwNumberOfBytesToWrite: Cardinal;
    begin
      dwNumberOfBytesToWrite := Length(WriteStr);
      dwNumberOfBytesWritten := 0;
      dwWhereToStartWriting  := 1;
      repeat
        if not WriteFile(CommHandle, WriteStr[dwWhereToStartWriting],
          dwNumberOfBytesToWrite, dwNumberOfBytesWritten, nil) then
        begin
          dwLastError := GetLastError;
          if (dwLastError <> 0) then
            raise Exception.CreateFmt('向串口写数据错误,原因: %s(%d)', [SysErrorMessage(dwLastError), dwLastError]);
          Dec( dwNumberOfBytesToWrite, dwNumberOfBytesWritten );
          Inc( dwWhereToStartWriting, dwNumberOfBytesWritten );
        end else
        begin
          dwLastError := GetLastError;
          raise Exception.CreateFmt('向串口写数据错误,原因: %s(%d)', [SysErrorMessage(dwLastError), dwLastError]);
        end;
       until (dwNumberOfBytesToWrite <= 0);  // Write the whole thing!   //PurgeComm(CommHandle, PURGE_TXABORT + PURGE_TXCLEAR);
    end;