fix com ports

This commit is contained in:
Qukich 2024-02-26 12:07:26 +03:00
parent 11b9374377
commit d223f78392

View File

@ -11,11 +11,6 @@ int main() {
HANDLE Port_2; HANDLE Port_2;
init_hdlc_client(&hdlc, 200); init_hdlc_client(&hdlc, 200);
hdlc_connect(&hdlc);
uint8_t buffer_send_connect[10];
hdlc_get_raw_frame(&hdlc, buffer_send_connect, sizeof buffer_send_connect);
//Открыть COM-порт //Открыть COM-порт
Port_1 = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, Port_1 = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL, OPEN_EXISTING,
@ -32,12 +27,12 @@ int main() {
// Установка таймаутов чтения/записи // Установка таймаутов чтения/записи
timeouts1.ReadIntervalTimeout = 100; timeouts1.ReadIntervalTimeout = 100;
timeouts1.ReadTotalTimeoutConstant = 100; timeouts1.ReadTotalTimeoutConstant = 100;
timeouts1.ReadTotalTimeoutMultiplier = 20; timeouts1.ReadTotalTimeoutMultiplier = 50;
timeouts1.WriteTotalTimeoutConstant = 50; timeouts1.WriteTotalTimeoutConstant = 100;
timeouts1.WriteTotalTimeoutMultiplier = 20; timeouts1.WriteTotalTimeoutMultiplier = 50;
if (!SetCommTimeouts(Port_1, &timeouts1)) { if (!SetCommTimeouts(Port_1, &timeouts1)) {
printf("Ошибка при установке таймаутов COM-порта.\n"); printf("err add timeout COM port.\n");
CloseHandle(Port_1); CloseHandle(Port_1);
return 1; return 1;
} }
@ -59,6 +54,10 @@ int main() {
CloseHandle(Port_1); CloseHandle(Port_1);
return 1; return 1;
} }
hdlc_connect(&hdlc);
uint8_t buffer_send_connect[10];
hdlc_get_raw_frame(&hdlc, buffer_send_connect, sizeof buffer_send_connect);
//Write frame from port 1 //Write frame from port 1
DWORD written; DWORD written;
@ -112,16 +111,16 @@ int main() {
mess.str = "word war"; mess.str = "word war";
mess.len_str = sizeof mess.str; mess.len_str = sizeof mess.str;
uint8_t data[32]; uint8_t data[23];
size_t len_data; size_t len_data;
protocol_encode(mess, data, &len_data); protocol_encode(mess, data, &len_data);
hdlc_send_data(&hdlc, data, sizeof(data));
uint8_t buffer_for_ex_data[29];
hdlc_send_data(&hdlc, data, len_data); hdlc_get_raw_frame(&hdlc, buffer_for_ex_data, sizeof buffer_for_ex_data);
uint8_t buffer_data[72];
hdlc_get_raw_frame(&hdlc, buffer_data, sizeof buffer_data);
DWORD written_data; DWORD written_data;
BOOL success_write_data = WriteFile(Port_1, buffer_send_connect, sizeof buffer_send_connect, BOOL success_write_data = WriteFile(Port_1, buffer_for_ex_data, sizeof buffer_for_ex_data,
&written_data, NULL); &written_data, NULL);
if (!success_write_data){ if (!success_write_data){
@ -129,20 +128,16 @@ int main() {
printf("WriteFile err: %lu.\n", errorCode); printf("WriteFile err: %lu.\n", errorCode);
return -1; return -1;
} }
if (written_data != sizeof buffer_send_connect){ if (written_data != sizeof buffer_for_ex_data){
printf("Failed to write all bytes to port"); printf("Failed to write all bytes to port");
return -1; return -1;
} }
for (int i = 0; i <1100; i++ ){
}
bool flag_data = false; bool flag_data = false;
while(!flag_data){ while(!flag_data){
uint8_t buffer_recive_data[80]; uint8_t buffer_recive_data[29];
DWORD received_data; DWORD received_data;
BOOL success = ReadFile(Port_2, &buffer_recive_data, sizeof buffer_recive_data, BOOL success = ReadFile(Port_2, buffer_recive_data, sizeof buffer_recive_data,
&received_data, NULL); &received_data, NULL);
if (!success) if (!success)
{ {
@ -150,7 +145,7 @@ int main() {
return -1; return -1;
} }
uint8_t data_recive[64]; uint8_t data_recive[23];
size_t len_data_recive; size_t len_data_recive;
int err = hdlc_decode_recived_raw_data(&hdlc, buffer_recive_data, sizeof buffer_recive_data, int err = hdlc_decode_recived_raw_data(&hdlc, buffer_recive_data, sizeof buffer_recive_data,
data_recive, &len_data_recive); data_recive, &len_data_recive);
@ -163,20 +158,15 @@ int main() {
// } // }
return err; return err;
} }
for(int i = 0; i < sizeof data_recive; i++){
printf("%d: %d\n", i, data_recive[i]);
}
struct message resp; struct message resp;
protocol_decode(data_recive, sizeof data_recive, &resp); protocol_decode(data_recive, sizeof data_recive, &resp);
if (resp.len_str > 0){
printf("1: %f\n", resp.numbers[0]); printf("1: %f\n", resp.numbers[0]);
printf("2: %f\n", resp.numbers[1]); printf("2: %f\n", resp.numbers[1]);
printf("3: %f\n", resp.numbers[2]); printf("3: %f\n", resp.numbers[2]);
printf("str: %s\n", resp.str); printf("str: %s\n", resp.str);
flag_data = true; flag_data = true;
} }
}
CloseHandle(Port_1); CloseHandle(Port_1);
CloseHandle(Port_2); CloseHandle(Port_2);