HANDLE CreateFile(
  LPCTSTR lpFileName, // pointer to name of the file
  DWORD dwDesiredAccess, // access (read-write) mode
  DWORD dwShareMode, // share mode
  LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security attributes
  DWORD dwCreationDistribution, // how to create
  DWORD dwFlagsAndAttributes, // file attributes
  HANDLE hTemplateFile // handle to file with attributes to copy
  );
  lpFileName: 指明串口制备,例:COM1,COM2
  dwDesiredAccess: 指明串口存取方式,例:GENERIC_READ|GENERIC_WRITE
  dwShareMode: 指明串口共享方式
  lpSecurityAttributes: 指明串口的安全属性结构,NULL为缺省安全属性
  dwCreateionDistribution: 必须为OPEN_EXISTIN
  dwFlagAndAttributes: 对串口唯一有意义的是FILE_FLAG_OVERLAPPED
  hTemplateFile: 必须为NULL

解决方案 »

  1.   

    inp()和outp()真的就不能用吗?
      

  2.   

    Return Values
    If the function succeeds, the return value is an open handle to the specified file. If the specified file exists before the function call and dwCreationDisposition is CREATE_ALWAYS or OPEN_ALWAYS, a call to GetLastError returns ERROR_ALREADY_EXISTS (even though the function has succeeded). If the file does not exist before the call, GetLastError returns zero. If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError. 
      

  3.   

    CreateFile()可以打开COM1,COM2,....;要监听COM1,COM2传来的实时信号,需要使用以下API函数:具体使用方法查看MSDN
    BuildCommDCB
    BuildCommDCBAndTimeouts
    ClearCommBreak
    ClearCommError
    CommConfigDialog
    EscapeCommFunction 
    GetCommConfig
    GetCommMask
    GetCommModemStatus 
    GetCommProperties
    GetCommState 
    GetCommTimeouts
    GetDefaultCommConfig
    PurgeComm
    SetCommBreak
    SetCommConfig 
    SetCommMask 
    SetCommState 
    SetCommTimeouts 
    SetDefaultCommConfig 
    SetupComm 
    TransmitCommChar
    WaitCommEvent 
    WriteFile
    ReadFile等
      

  4.   

    给你几个函数看看:
    OpenCom(LPCTSTR ComName)
    {
    DCB tmpdcb;
    COMMTIMEOUTS timeout; hcom=CreateFile(ComName, //打开串口
    GENERIC_READ|GENERIC_WRITE, //以读\写方式打开
    0, //共享属性,必须设为0
    NULL, //设为NULL使用缺省的安全属性
    OPEN_EXISTING, //打开已经存在的串口
    FILE_FLAG_OVERLAPPED, //对串口只能使用此设置
    NULL); //对串口只能使用此设置
    if(hcom==INVALID_HANDLE_VALUE)
    {
    ShowMessage("Open com failed!");
    return false;
    }
    ComOpened=true; //设置TIMEOUT
    GetCommTimeouts(hcom,&timeout);
    timeout.ReadIntervalTimeout=50;
    timeout.ReadTotalTimeoutConstant=500;
    timeout.ReadTotalTimeoutMultiplier=50;
    SetCommTimeouts(hcom,&timeout); GetCommState(hcom,&tmpdcb);
    BuildCommDCB("24,N,8,1",&tmpdcb);
    tmpdcb.fDtrControl=DTR_CONTROL_DISABLE;
    SetCommState(hcom,&tmpdcb); SetupComm(hcom,1024,1024);
    return true;
    }
    //-----------------------------------------------------------------
    SendChar(unsigned char x[],DWORD n)
    {
    DWORD nAct;
    WriteFile(hcom,x,n,&nAct,NULL);
    return true;
    }
    //-----------------------------------------------------------------
    ReceiveChar(unsigned char x[],DWORD n)
    {
    DWORD nRead;
    ReadFile(hcom,x,n,&nRead,NULL);
    return nRead;
    }