Delphi 调用的DLL串口函数
int TMS_DownLoad ( HANDLE  hd_no ,Char CommType,struct down_info downfile,char *ret_info)其中hd_no 是在DELPHI中打开并初始化好的串口句柄,但是现在问题是:DELPHI 可以调用DLL,并且成功传入参数,但是DLL无法接收到数据,DLL接收函数是:bool bResult=ReadFile(hd_comm,&inBuffer,nWantRead,&nRealRead,&wrOverlapped);
bResult=GetOverlappedResult(hd_comm,&wrOverlapped,&nRealRead,TRUE);Delphi打开串口方式是:
   CommHandle := CreateFile(PChar(ComboBox1.Text),GENERIC_WRITE or GENERIC_READ,
            0,nil,OPEN_EXISTING,FILE_FLAG_OVERLAPPED or FILE_ATTRIBUTE_NORMAL,0);急等

解决方案 »

  1.   

    CommHandle := CreateFile(PChar(ComboBox1.Text),GENERIC_WRITE or GENERIC_READ,
                0,nil,OPEN_EXISTING,FILE_FLAG_OVERLAPPED or FILE_ATTRIBUTE_NORMAL,0);你先看看 你这个handle的返回值是什么 是否为 INVALID_HANDLE_VALUE...而且你的flag参数 有用到 OVERLAPPED.. FILE_FLAG_OVERLAPPED 
     Instructs the system to initialize the object, so that operations that take a significant amount of time to process return ERROR_IO_PENDING. When the operation is finished, the specified event is set to the signaled state. 
     When you specify FILE_FLAG_OVERLAPPED, the file read and write functions must specify an OVERLAPPED structure. That is, when FILE_FLAG_OVERLAPPED is specified, an application must perform overlapped reading and writing. 
     When FILE_FLAG_OVERLAPPED is specified, the system does not maintain the file pointer. The file position must be passed as part of the lpOverlapped parameter (pointing to an OVERLAPPED structure) to the file read and write functions. 
     This flag also enables more than one operation to be performed simultaneously with the handle (a simultaneous read and write operation, for example). 
      

  2.   

    CommHandle := CreateFile(PChar(ComboBox1.Text),GENERIC_WRITE or GENERIC_READ,
                0,nil,OPEN_EXISTING,FILE_FLAG_OVERLAPPED or FILE_ATTRIBUTE_NORMAL,0);这个handle的返回值是什么一个有效值,
    DLL中的接收函数:
    bool bResult=ReadFile(hd_comm,&inBuffer,nWantRead,&nRealRead,&wrOverlapped);
    bResult=GetOverlappedResult(hd_comm,&wrOverlapped,&nRealRead,TRUE);
      

  3.   

    bool bResult=ReadFile(hd_comm,&inBuffer,nWantRead,&nRealRead,&wrOverlapped);
    // &inBuffer 你的inBuffer是什么?? 如果是数组的话 不需要加&,因为数组名本身就是数组首址
      如果是指针就更不用了bResult=GetOverlappedResult(hd_comm,&wrOverlapped,&nRealRead,TRUE);
      

  4.   

    很有可能接收不到数据,因为你采用的是异步接收的模式,使用了windows的重叠模型,正常情况你的readfile函数应该返回一个IO_PENDING的错误,然后应该使用同步函数WaitCommEvent和WaitForSingleObject等待有数据的事件触发,最后再去调用readfile函数即可,我有一个vc的异步串口操作的代码,你可以借鉴一下,告诉我你的email
      

  5.   

    补充一下,在获得有数据的事件通知后,应该调用ClearCommError中的InQueue(类似)参数的返回值,获悉本次停留在串口缓冲区中数据的长度