2013-02-26 26 views
1

我想從COM1讀115200,N,8,1(最好是阻塞調用,但我可以補充一點,而且我不需要線程)。尋求串口讀取例如

我能找到的唯一代碼是在堆棧溢出this question(微軟也有some useful info)。

撰文人說,他的代碼工作的,我不懷疑他,但是當我運行代碼,我沒有收到任何字符,即使端口正確打開(如果我有一個終端程序,數據檢查正在發送)。

可有人請張貼URL的一些例子C代碼?謝謝。

FWIW,這裏是我的代碼:

// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= 
E_boolean OpenCom1(void) 
{ 
    COMMTIMEOUTS timeouts; 

    comPorthandle = CreateFile("COM1", // Specify port device: default "COM1" 
    GENERIC_READ | GENERIC_WRITE,  // Specify mode that open device. 
    0,         // the device isn't shared. 
    NULL,        // the object gets a default security. 
    OPEN_EXISTING,      // Specify which action to take on file. 
    0,         // default (not overlapped i/o). 
    NULL);        // default (hTemplate must be NULL for COM devices). 

    if (comPorthandle == INVALID_HANDLE_VALUE) 
     return False; 

    deviceControlBlock.DCBlength = sizeof(deviceControlBlock); 

    if((GetCommState(comPorthandle, &deviceControlBlock) == 0)) 
    { 
     // CodeMe: do what? 
     return False; 
    } 

    deviceControlBlock.BaudRate = CBR_115200; 
    deviceControlBlock.StopBits = ONESTOPBIT; 
    deviceControlBlock.Parity = NOPARITY; 
    deviceControlBlock.ByteSize = DATABITS_8; 
    deviceControlBlock.fRtsControl = 0; 

    if (!SetCommState(comPorthandle, &deviceControlBlock)) 
    { 
     // CodeMe: do what? 
     return False; 
    } 

#if 0 
// alternative to GetCommState() and SetCommState() 
// both versions succeed 
    if (!BuildCommDCB("115200,n,8,1", &deviceControlBlock)) 
    { 
     // CodeMe: do what? 
     return False; 
    } 
#endif 

    // set short timeouts on the comm port. 
    timeouts.ReadIntervalTimeout = 0; 
    timeouts.ReadTotalTimeoutMultiplier = 1; 
    timeouts.ReadTotalTimeoutConstant = 1; 
    timeouts.WriteTotalTimeoutMultiplier = 1; 
    timeouts.WriteTotalTimeoutConstant = 1; 
    if (!SetCommTimeouts(comPorthandle, &timeouts)) 
    { 
     // CodeMe: do what? 
     return False; 
    } 

    return True; 
}//OpenCom1() 

// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= 
void  ReadCharacterFromCom1(INPUT char *theCharacter) 
{ 
    DWORD numBytesRead; 

    numBytesRead = 0; 

    while (numBytesRead == 0) 
    { 
     ReadFile(comPorthandle,   // handle of file to read 
       theCharacter,   // store read data here 
       sizeof(theCharacter), // number of bytes to read 
       &numBytesRead,   // pointer to number of bytes actually read 
       NULL); 
    } 
    return; 
}//ReadCharacterFromCom1() 
+0

只需按照您想要的方式設置端口,然後對其使用常規的「讀取」調用。 – vonbrand 2013-02-26 19:33:46

回答

2

我看到這個代碼一個問題:

 
    sizeof(theCharacter), 

 
    sizeof(char), 

,因爲你想讀一個字節替換此,和sizeof(char *)是4或8.可能還有別的東西,但是你需要顯示更多的代碼。

此外,使用Portmon程序http://technet.microsoft.com/en-us/sysinternals/bb896644.aspx,看數據是否被接收 - 你可以用你的程序一起運行它。

+0

+1一個很好的觀點(以前我犯過一個錯誤)。我會盡快檢查出來,然後回覆你。 – Mawg 2013-02-26 07:36:31

+0

好吧,現在它正在閱讀字符,但它們看起來像非ASCII值,我只發送文本。我明天上午。 – Mawg 2013-02-26 09:16:37

+1

使用PortMon進行驗證。 – 2013-02-26 09:46:00