目前我们正在尝试创建一个串行通信接口,以便能够与微处理器进行通信。
事实上 - 一切都很好。几乎!
为了能够与我们的控制器通信,我们需要与其同步。为此,我们编写一个字符串:"?0{SY}13!"
,然后控制器应该回复"!0{SY}F5?"
接受同步请求。
为此,我们使用writeData
函数(有效 - 我们知道通过使用echo
),之后我们使用readData
阅读答案。
问题是,由于某种原因,它不会读取任何内容。虽然它回来了1
为了成功,它读取的字符是不断的" "
(没有什么)。
现在出现了奇怪的部分 - 如果我们使用外部终端程序来初始化端口(如 putty),然后关闭程序,那么一切都会正常。它接受同步请求、应答(我们可以读取它),然后我们就可以做我们想做的一切。但除非我们使用外部程序来初始化端口,否则它不起作用。
初始化接口的构造函数如下所示:
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;
}
}
这应该有效 - 我真的不知道为什么它不应该。它没有返回错误,我在过去几天尝试了很多错误搜索,我尝试了超时,我尝试了其他构建它的方法,但这一切都归结为同一问题。
为什么这不会初始化端口?
EDIT:
尝试同步时的输出:
由于缺乏信誉,无法发布图片。虽然它输出如下:
[+] 串口COM1已激活。
波特率:9600
奇偶校验:无
停止位: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;
}