ในขณะนี้เรากำลังพยายามสร้างอินเทอร์เฟซสำหรับการสื่อสารแบบอนุกรมเพื่อให้สามารถสื่อสารกับไมโครโปรเซสเซอร์ได้
จริงๆแล้ว - ทุกอย่างทำงานได้ดี เกือบ! เพื่อให้สามารถสื่อสารกับคอนโทรลเลอร์ของเราได้ เราจำเป็นต้องซิงค์กับคอนโทรลเลอร์นั้น ในการดำเนินการนี้ เราเขียนสตริง: "?0{SY}13!"
และตัวควบคุมควรตอบกลับด้วย "!0{SY}F5?"
เพื่อยอมรับคำขอซิงค์ ในการทำเช่นนี้ เราใช้ฟังก์ชัน writeData
(ซึ่งได้ผล - เรารู้ว่าใช้ echo
) และหลังจากนั้นเราใช้ readData
เพื่ออ่านคำตอบ ปัญหาคือมันจะไม่อ่านอะไรเลยด้วยเหตุผลบางประการ แม้ว่าจะส่งกลับ 1
เพื่อความสำเร็จ แต่ตัวอักษรที่อ่านก็ยังเป็น " "
อย่างต่อเนื่อง (ไม่มีเลย)
ตอนนี้ส่วนที่แปลกมาถึงแล้ว - ถ้าเราใช้โปรแกรมเทอร์มินัลภายนอกเพื่อเริ่มต้นพอร์ต (เช่น สีโป๊ว) แล้วปิดโปรแกรม ทุกอย่างก็ทำงานได้ดี ยอมรับคำขอซิงค์ คำตอบ (และเราสามารถอ่านได้) จากนั้นเราก็สามารถทำทุกอย่างที่เราต้องการได้ แต่ถ้าเราใช้โปรแกรมภายนอกเพื่อเริ่มต้นพอร์ต มันก็จะไม่ทำงาน
ตัวสร้างสำหรับการเริ่มต้นอินเทอร์เฟซมีลักษณะดังนี้:
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;
}