2011-10-24 6 views
1

シリアルポートを介してブロードバンドカードと通信しています。私はCOM5上でGPSデータを読んで、コマンドを実行してCOM3上の応答を読み取って信号強度を取得する必要があります。 COM3に読み書きしようとすると、プログラムは実行されません。特定のCOMポートから読み書きしようとするとプログラムが実行されない

は、私は含む行をコメントアウトする場合:

if(!WriteFile(hSerial2, wzBuff, 7, &dZBytesRead, NULL)) 
    std::cout << "Write error"; 
// 
if(!ReadFile(hSerial2, buff2, 30, &DZBYTES, NULL)) 
    std::cout << "Read Error"; 

プログラムが完璧に動作します。ここで

は完全なコードです:

#include <Windows.h> 
#include <iostream> 
#include <string> 
#include <fstream> 
#include <vector> 
#include <time.h> 

using namespace std; 

vector<int> findLocation(string str, char character); 

int main() 
{ 

    time_t now; 
    time(&now); 



    ofstream myfile; 

    myfile.open("C:\\log.txt"); 

    HANDLE hSerial = CreateFile("COM5",GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); 
    HANDLE hSerial2 = CreateFile("COM3",GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); 

    if(hSerial==INVALID_HANDLE_VALUE) 
     std::cout << "Cannot find serial port"; 

    DCB dcbSerialParams = {0}; 
    dcbSerialParams.DCBlength=sizeof(dcbSerialParams); 

    if (!GetCommState(hSerial, &dcbSerialParams)) 
     std::cout << "COM State Error"; 

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

    if (!SetCommState(hSerial,&dcbSerialParams)) 
     std::cout << "COM State Error"; 


    COMMTIMEOUTS timeouts={0}; 
    timeouts.ReadIntervalTimeout=50; 
    timeouts.ReadTotalTimeoutConstant=50; 
    timeouts.ReadTotalTimeoutMultiplier=10; 
    timeouts.WriteTotalTimeoutConstant=50; 
    timeouts.WriteTotalTimeoutMultiplier=10; 
    if(!SetCommTimeouts(hSerial, &timeouts)) 
     std::cout << "Set timeout errors"; 


while(1) 
{ 



char szBuff[650+1] = {0}; 
char buff2[30+1] = {0}; 

char wzBuff[14] = {"AT+CSQ\r"}; 

DWORD dZBytesRead = 0; 
DWORD dwBytesRead = 0; 
DWORD DZBYTES = 0; 

//When I uncomment these next two calls my program will neither loop nor start 

//if(!WriteFile(hSerial2, wzBuff, 7, &dZBytesRead, NULL)) 
    //std::cout << "Write error"; 
// 
//if(!ReadFile(hSerial2, buff2, 30, &DZBYTES, NULL)) 
//std::cout << "Read Error"; 


if(!ReadFile(hSerial, szBuff, 650, &dwBytesRead, NULL)) 
    std::cout << "Read Error"; 


// 
//if(dwBytesRead >1){ 


string test = string(szBuff); 
//std::cout << dwBytesRead << endl; 
//std::cout << test; 
// 
//cout << szBuff[1] << endl << endl; 

//myfile << test; 
// 
size_t found; 
size_t secondFound; 
//size_t first; 
//size_t second; 
//size_t third; 
//size_t fourth; 
// 


if(dwBytesRead > 50){ 
myfile.open("C:\\log.txt",fstream::in | fstream::out | fstream::app); 


found = test.rfind("$"); 
secondFound = test.find("\n"); 

time_t now; 
time(&now); 

string timenow = string(ctime(&now)); 

string final = test.substr(found,((secondFound - found)-1)); 
vector<int> locations = findLocation(final, ','); 

string lat = final.substr((locations[2]+1),12); 
string lon = final.substr((locations[4]+1),12); 

cout << lat << " " << lon << endl; 
myfile << lat << " " << lon << endl; 
myfile <<timenow << endl; 

//Wanting to get returned value from different COM port 
/*if(DZBYTES>20) 
{ 
    std::string test2 = std::string(buff2).substr(8,3); 
    myfile << test2 << endl; 
}*/ 

myfile << "________________________" << endl; 
myfile.close(); 

} 





//if (dZBytesRead > 10) 
//{ 
//std::string test2 = std::string(buff2).substr(8,3); 
//std::cout << test2 << endl; 
//myfile << test2 << endl; 
//} 

Sleep(400); 
} 

//myfile.close(); 
return 0; 
} 



vector<int> findLocation(string str, char character) 
{ 
    vector<int> characterLocations; 
    for(int i = 0; i < str.size(); i++) 
     if(str[i] == character) 
      characterLocations.push_back(i); 
    return characterLocations; 
} 
+0

時間がかかり、_how_を指定しないとプログラムが失敗します。 – MSalters

+1

私はそれが実行を開始しないと述べたように、 – lodkkx

+2

私たちは魔法を本当に信じていない傾向があります。コンパイルすると思いますか?そして、 'int main()'にブレークポイントを置くと、最初から停止しますか?結局のところ、これら2つの命令はまだ実行されておらず、Windowsは時間旅行をサポートしていません。だから、 'main'が始まった後のある瞬間に、何か悪いことが起こります。何? – MSalters

答えて

2

あなたは、2つのCOMポートを開くことが、唯一のそれらのいずれかのシリアルポートパラメータ(ボーレート、パリティ)とタイムアウトを設定しています。

その他の設定は不明です。あなたがそれから正常に読むことができないという驚きはありません。タイムアウトがなければ、試してみるだけです。

関連する問題