2011-05-19 20 views
-1

當我將我的嵌入式設備連接到我的系統時,我正在運行我的程序,它將寫入端口,我的嵌入式連接並將答覆打印到控制檯。vC++中的串行編程的一些問題mfc

當我連接我的設備並運行該程序時,它沒有給出任何輸出。

但是,當連接我的設備和使用膩子首先發送一些命令,然後運行我的程序,它正在.....

可能存在我開始溝通的方式有問題,任何人幫助我解決緊急問題。

我的源代碼是................

#include "stdafx.h" 
#include <iostream> 
//#include <windows.h> 
#include <afx.h> 

int main() 
{ 
    using namespace std; 
    int i=0; 
// cout << "Hello world!" << endl; 



    HANDLE hSerial; 

    hSerial = CreateFile("COM5", 
    GENERIC_READ | GENERIC_WRITE, 
    FILE_SHARE_WRITE | FILE_SHARE_READ, 
    0, 
    OPEN_EXISTING, 
    FILE_ATTRIBUTE_NORMAL, 
    0); 

    if(hSerial==INVALID_HANDLE_VALUE) 
    { 
     if(GetLastError()==ERROR_FILE_NOT_FOUND) 
     { 
//   TRACE("serial port does not exist for reading\n"); 
     //serial port does not exist. Inform user. 
     } 
//   TRACE("some other error,serial port does not exist for reading\n"); 
     //some other error occurred. Inform user. 
    } 

    DCB dcbSerialParams = {0}; 

    dcbSerialParams.DCBlength=sizeof(dcbSerialParams); 

    if (!GetCommState(hSerial, &dcbSerialParams)) 
    { 
//     TRACE("error getting state for reading\n"); 
    //error getting state 
    } 

    dcbSerialParams.BaudRate=9600; 
    dcbSerialParams.ByteSize=8; 
    dcbSerialParams.StopBits=ONESTOPBIT; 
    dcbSerialParams.Parity=NOPARITY; 

    if(!SetCommState(hSerial, &dcbSerialParams)) 
    { 
    //TRACE("error setting state for reading\n"); 
    //error setting serial port state 
    } 
    COMMTIMEOUTS timeouts={0}; 

    timeouts.ReadIntervalTimeout=50; 
    timeouts.ReadTotalTimeoutConstant=50; 
    timeouts.ReadTotalTimeoutMultiplier=10; 

    timeouts.WriteTotalTimeoutConstant=50; 
    timeouts.WriteTotalTimeoutMultiplier=10; 

    if(!SetCommTimeouts(hSerial, &timeouts)) 
    { 
//     TRACE("some error occured for reading\n"); 
     //error occureed. Inform user 
    }  
    int n=100,n1=100; 
    char szBuff[100]; 
    DWORD dwBytesRead = 0; 
    char szBuff1[100]; 
    DWORD dwByteswrote = 0; 
    memset(szBuff1,0,100); 
    memcpy(szBuff1,"LIST\r",5); 
    if(!WriteFile(hSerial, szBuff1,5, &dwByteswrote, NULL)) 
    { 
        cout << "error writing" ; 
    } 
    cout << szBuff1 << endl; 
    cout << dwByteswrote << endl; 
    while(1) 
    { 
     if(!ReadFile(hSerial, szBuff, n1, &dwBytesRead, NULL)) 
     { 
      cout << "error reading"; 
      break; 
     } 
     else 
     { 
      cout << dwBytesRead << endl; 
      szBuff[dwBytesRead]='\0'; 
      if(dwBytesRead>0) 
      { 
       cout << (szBuff); 

       break; 
      } 
     } 
    } 

    cin >> i; 
} 

回答

0

嘗試......你可能會需要有例外(如執行代碼:if響應大於2024)

bool SendModemATCommand(const string &strCommand, int iModemPort, string &strRetValue) 
{ 
    bool bRetValue = false; 

    strRetValue = ""; 
    char cBuffer[2024]; 

    HANDLE hCom = NULL; 
    char cComPort[64]; 
    sprintf_s(cComPort,"\\\\.\\COM%d", iModemPort); 


    hCom = CreateFile(cComPort, 
        GENERIC_READ | GENERIC_WRITE, 
        0, // must be opened with exclusive-access 
        NULL, // no security attributes 
        OPEN_EXISTING, // must use OPEN_EXISTING 
        0, // not overlapped I/O 
        NULL // hTemplate must be NULL for comm devices 
        ); 

    if (hCom != INVALID_HANDLE_VALUE) 
    { 
     COMMTIMEOUTS comTimeOuts; 
     comTimeOuts.ReadIntervalTimeout = MAXDWORD; 
     comTimeOuts.ReadTotalTimeoutMultiplier = MAXDWORD; 
     comTimeOuts.ReadTotalTimeoutConstant = 0;//MAXDWORD; 
     comTimeOuts.WriteTotalTimeoutMultiplier = 0; 
     comTimeOuts.WriteTotalTimeoutConstant = 0; 
     if(SetCommTimeouts(hCom, &comTimeOuts)) 
     { 
      DCB dcb; 
      dcb.DCBlength = sizeof(DCB); 
      if(GetCommState(hCom, &dcb)) 
      { 
       DWORD dwBytesWritten = 0;     
       DWORD dwBytesRead = 0; 
       DWORD dwBytesTotal = 0; 

       if(WriteFile(hCom, strCommand.c_str(), (int)strCommand.size(), &dwBytesWritten, NULL)) 
       { 
        if(dwBytesWritten == strCommand.size()) 
        { 
         dwBytesRead = 0; 
         DWORD tickStart = GetTickCount(); 
         bool bTimeOut = false;      
         while(true) 
         { 
          while(ReadFile(hCom, cBuffer + dwBytesTotal, 1, &dwBytesRead, NULL)) 
          {  
           if(dwBytesRead == 0 && dwBytesTotal != dwBytesWritten) 
            break; 
           dwBytesTotal += dwBytesRead;         
          } 
          if (dwBytesTotal == 0) 
          { 
           // timeout 
           if (GetTickCount() - tickStart > 10000) // 10 Seconds 
           { 
            bTimeOut = true; 
            break;        
           } 
          } 
          else 
           break; 
         }     

         cBuffer[dwBytesTotal] = '\0'; 
         strRetValue = cBuffer; 

         if(bTimeOut) 
          strRetValue = "Timed out:" + strCommand; 
         else 
          bRetValue = true; 
        } 
       } 
      } 
     } 
     CloseHandle(hCom); 
    } 

    return bRetValue; 
} 
0

最有可能的問題是您的初始化。

我記得之前有這種類型的麻煩,Com Timeouts結構特別麻煩。

我建議你從COM5到機器上的另一個端口(如果有的話)或另一臺計算機上得到一條零調制解調器電纜。然後使用一個終端程序打開另一個端口,並看到當您運行程序時可以看到「List」命令。如果不是,那麼它很可能與您初始化打開com端口的方式有關。

此鏈接可能證明是有用的。只需刪除Afx的東西,並特別注意初始化。 http://www.codeproject.com/KB/system/chaiyasit_t.aspx

另一個建議,你只發送List一次。如果設備尚未插入並準備就緒,則不會發生任何事情。也許它應該繼續發送列表命令,直到獲得 響應。

另外,你需要「List \ r \ n」還是隻需要「List \ r」?其他的目的是什麼?

+0

它預計LIST \ r – abhinav 2011-05-19 09:48:46

+0

我的問題是,當我連接我的設備並運行我的程序和工作時,我使用PUTTY打開COM7併發送一個命令列表到它,如果我現在運行我的程序這是工作。 PUTTY正在做的事情,我不是,我需要幫助找到它 – abhinav 2011-05-19 10:01:00

+0

您是否啓用膩子的日誌選項?也可能你應該在調用其他命令之前先寫「ATZ」之類的東西... – 2011-05-19 10:18:56