마이크로 프로세서와 통신 할 수 있도록 직렬 통신을위한 인터페이스를 만들려고하고 있습니다.직렬 포트 통신 초기화
실제로 모든 것이 정상적으로 작동합니다. 거의! 컨트롤러와 통신하려면 컨트롤러와 동기화해야합니다. 이렇게하려면 문자열 ("?0{SY}13!"
)을 쓰고 컨트롤러는 "!0{SY}F5?"
으로 응답하여 동기화 요청을 수락해야합니다. 이렇게하려면 writeData
함수 (작동합니다 - 우리는 echo
을 사용합니다)를 사용하고 그 후에 readData
을 사용하여 대답을 읽습니다. 문제는 어떤 이유로 든 아무 것도 읽지 않는다는 것입니다. 성공을 위해 1
을 반환하지만, 읽는 chars는 항상 " "
(아무것도 아님)입니다.
이상한 부분이 있습니다. 외부 터미널 프로그램을 사용하여 포트 (퍼티 등)를 초기화 한 다음 프로그램을 닫으면 모든 것이 정상적으로 작동합니다. 그것은 동기화 요청을 받아 들여 응답하고 (우리는 그것을 읽을 수 있습니다), 우리가 원하는 모든 것을 할 수 있습니다. 그러나 외부 프로그램을 사용하여 포트를 초기화하지 않으면 작동하지 않습니다. 하여 인터페이스를 초기화
생성자는 다음과 같습니다
SerialIF::SerialIF(int baud, int byteSize, int stopBits, char* parity, int debug)
{
string coutport = getPort();
wstring wideport;
debug_ = debug; //Debuglevel
sync = false; //sync starts with false
error = false; //Error false as beginnging
//this is just for converting to the right type
for (int i = 0; i < coutport.length(); i++)
{
wideport += wchar_t(coutport[i]);
}
const wchar_t* port = wideport.c_str();
SerialIF::hserial = CreateFile(port,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (hserial == INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
if (debug_ != LOW)
{
cout << "[-] Port " << coutport << "doesn't exist." << endl;
}
}
if (debug_ != LOW)
{
cout << "[-] Handle error - is there another terminal active?" << endl;
}
error = true;
}
DCB dcbParms = { 0 };
dcbParms.DCBlength = sizeof(dcbParms);
if (!GetCommState(hserial, &dcbParms))
{
if (debug_ != LOW)
{
cout << "[-] Couldn't get status from port " << coutport << endl;
}
error = true;
}
if (!error)
{
setBaud(dcbParms, baud);
setParity(dcbParms, parity);
setByteSize(dcbParms, byteSize);
setStopbits(dcbParms, stopBits);
if (debug_ == HIGH)
{
cout << "[+] Serial port " << coutport << " has been activated. \nBaud-rate: " << baud << "\nParity: "
<< parity << "\nStop bits: " << stopBits << endl;
}
}
else if (debug_ != LOW)
{
cout << "[-] Port not initialized" << endl;
}
}
이 작동합니다 - 왜 안 난 정말 모르겠어요. 그것은 오류를 반환하지, 나는 지난 며칠 동안 많은 오류를 시도해 보았습니다. 타임 아웃을 시도했지만 다른 방법으로 시도했지만 동일한 문제가있었습니다.
왜 포트를 초기화하지 않습니까?
편집 :
동기화하려고 출력 : 인해 평판의 부족으로 사진을 게시 할 수 없습니다.
[+] 직렬 포트 COM1이 활성화되었습니다. 보오율 : 9600 패리티 : NONE 스톱 비트 : 1
[+] ->? 0 {SY} 13! 포트에 기록됩니다. ((그리고 무한 루프 읽기 "")에가는 곳이다)
편집 : 읽기 코드 :
const int bytesToRead = 1; //I byte pr læsning
char buffer[bytesToRead + 1] = { 0 }; //Bufferen til data
DWORD dwBytesRead = 0; //Antal bytes læst
string store; //Store - den vi gemmer den samlede streng i
bool end = false; //Kontrolvariabel til whileloop.
while (end == false)
{
if (ReadFile(hserial, buffer, bytesToRead, &dwBytesRead, NULL))
/*Readfile læser fra interfacet vha. hserial som vi oprettede i constructoren*/
{
if (buffer[0] == '?') //Da protokollen slutter en modtaget streng med "?", sætter vi end til true
{ //Hvis denne læses.
end = true;
}
store += buffer[0];
}
else
{
if (debug_ != LOW)
{
cout << "[-] Read fail" << endl; //Hvis readfile returnerer false, så er der sket en fejl.
}
end = true;
}
}
if (debug_ == HIGH)
{
cout << "[+] Recieved: " << store << endl; //I forbindelse med debug, er det muligt at få udsrkevet det man fik ind.
}
recentIn = store; //RecentIN brugES i andre funktioner
if (verify()) //Som f.eks. her, hvor vi verificerer dataen
{
if (debug_ == HIGH)
{
cout << "[+] Verification success!" << endl;
}
return convertRecData(store);
}
else
{
if (debug_ != LOW)
{
cout << "[-] Verification failed." << endl;
}
vector <string> null; //Returnerer en string uden data i, hvis der er sket en fejl.
return null;
}
포트가 초기화 될 때 출력되는 내용은 무엇입니까? 마찬가지로, "[+] 직렬 포트 ...."로 – Greycon
readData() 코드를 제공 할 수 있습니까? – alexm
@ 그레이 콘 - 원본 게시물을 편집하여 출력물을 포함 시켰습니다. –