122 lines
3.4 KiB
C
122 lines
3.4 KiB
C
#include <windows.h>
|
|
#include <stdio.h>
|
|
#include "hdlc/client.h"
|
|
#include "stdint.h"
|
|
#include "stdbool.h"
|
|
#include "protocol/protocol.h"
|
|
|
|
int main() {
|
|
struct Client hdlc;
|
|
HANDLE Port;
|
|
init_hdlc_client(&hdlc, 200);
|
|
|
|
//Открыть COM-порт
|
|
Port = CreateFile("COM3", GENERIC_READ | GENERIC_WRITE,
|
|
0, NULL, OPEN_EXISTING,
|
|
0, NULL);
|
|
|
|
if (Port == INVALID_HANDLE_VALUE) {
|
|
DWORD errorCode = GetLastError();
|
|
printf("Create file err: %lu.\n", errorCode);
|
|
return 1;
|
|
}
|
|
|
|
COMMTIMEOUTS timeouts1 = {0};
|
|
// Установка таймаутов чтения/записи
|
|
timeouts1.ReadIntervalTimeout = 100;
|
|
timeouts1.ReadTotalTimeoutConstant = 100;
|
|
timeouts1.ReadTotalTimeoutMultiplier = 50;
|
|
timeouts1.WriteTotalTimeoutConstant = 50;
|
|
timeouts1.WriteTotalTimeoutMultiplier = 50;
|
|
|
|
if (!SetCommTimeouts(Port, &timeouts1)) {
|
|
printf("err add timeout COM port.\n");
|
|
CloseHandle(Port);
|
|
return 1;
|
|
}
|
|
|
|
bool flag_connect = false;
|
|
while (!flag_connect){
|
|
uint8_t buffer_recive_connect[6];
|
|
DWORD received;
|
|
BOOL success = ReadFile(Port, buffer_recive_connect, sizeof buffer_recive_connect,
|
|
&received, NULL);
|
|
if (!success)
|
|
{
|
|
printf("Failed to read from port");
|
|
return -1;
|
|
}
|
|
|
|
hdlc_decode_recived_raw_data(&hdlc, buffer_recive_connect, sizeof buffer_recive_connect, 0, 0);
|
|
|
|
|
|
if (hdlc.state == READY_STATE){
|
|
struct Client tmp;
|
|
hdlc_connect(&tmp);
|
|
uint8_t buffer_send_connect[10];
|
|
|
|
hdlc_get_raw_frame(&tmp, buffer_send_connect, sizeof buffer_send_connect);
|
|
|
|
//Write frame from port 1
|
|
DWORD written;
|
|
BOOL success_write_send = WriteFile(Port, buffer_send_connect, sizeof buffer_send_connect,
|
|
&written, NULL);
|
|
|
|
if (!success_write_send){
|
|
DWORD errorCode = GetLastError();
|
|
printf("WriteFile err: %lu.\n", errorCode);
|
|
return -1;
|
|
}
|
|
if (written != sizeof buffer_send_connect){
|
|
printf("Failed to write all bytes to port");
|
|
return -1;
|
|
}
|
|
|
|
flag_connect = true;
|
|
}
|
|
}
|
|
|
|
struct message mess;
|
|
char str_v[] = "its working! wow! postavte 3 ";
|
|
mess.numbers[0] = 123.0;
|
|
mess.numbers[1] = 456.0;
|
|
mess.numbers[2] = 7.0;
|
|
mess.len_numbers = 3;
|
|
strcpy(mess.str, str_v);
|
|
mess.len_str = sizeof str_v;
|
|
|
|
|
|
uint8_t data[64];
|
|
size_t len_data;
|
|
|
|
protocol_encode(mess, data, &len_data);
|
|
|
|
int err = hdlc_send_data(&hdlc, data, sizeof(data));
|
|
printf("%d\n", err);
|
|
|
|
uint8_t buffer_for_ex_data[72];
|
|
|
|
hdlc_get_raw_frame(&hdlc, buffer_for_ex_data, sizeof buffer_for_ex_data);
|
|
|
|
DWORD written_data;
|
|
BOOL success_write_data = WriteFile(Port, buffer_for_ex_data, sizeof buffer_for_ex_data,
|
|
&written_data, NULL);
|
|
|
|
if (!success_write_data){
|
|
DWORD errorCode = GetLastError();
|
|
printf("WriteFile err: %lu.\n", errorCode);
|
|
return -1;
|
|
}
|
|
if (written_data != sizeof buffer_for_ex_data){
|
|
printf("Failed to write all bytes to port");
|
|
return -1;
|
|
}
|
|
|
|
while (true){
|
|
}
|
|
|
|
CloseHandle(Port);
|
|
|
|
return 0;
|
|
}
|