diff --git a/.vscode/settings.json b/.vscode/settings.json index ab5f439..a853f5b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "files.associations": { - "serial.h": "c" + "serial.h": "c", + "steering_gear.h": "c" } } \ No newline at end of file diff --git a/app/a.exe b/app/a.exe index fcc6dcb..5b570f6 100644 Binary files a/app/a.exe and b/app/a.exe differ diff --git a/app/main.c b/app/main.c index 5aef6bb..24505cd 100644 --- a/app/main.c +++ b/app/main.c @@ -3,25 +3,11 @@ #include #include "serial.h" +#include "steering_gear.h" -int serial_com_id; +PORT serial_port; +int serial_com_id; -int serial_test() { - PORT COM1; - char buff[1024] = {0}; - int rcv_len = 0; - - printf("Start open com%d\n", serial_com_id); - COM1 = serial_init(serial_com_id, 115200, 8, 1, 0); - - while (1) { - Serial_SendData(COM1, "hello finny", 12); - memset(buff, 0, 1024); - rcv_len = Serial_ReciveData(COM1, buff, 1024); - printf("rcv:%s\n", buff); - Sleep(1); - } -} int main(int argc, char *argv[]) { /* 参数判断 */ if (argc != 2) { @@ -32,14 +18,17 @@ int main(int argc, char *argv[]) { /* 字符串转int类型 */ serial_com_id = atoi(argv[1]); - if (serial_com_id == 0) {/* */ + if (serial_com_id == 0) { /* */ /* 转换错误 */ printf("The serial port id is incorrect\n"); return -2; } - serial_test(); + serial_port = serial_init(serial_com_id, 115200, 8, 1, 0); + while (1) { + Serial_SendData(serial_port, "hello finny", 12); + Sleep(1000); } return 0; diff --git a/app/serial.c b/app/serial.c index 485c39b..5cf4096 100644 --- a/app/serial.c +++ b/app/serial.c @@ -3,6 +3,34 @@ #include #include +/*** + * 用于串口初始化时设置停止位 + * stopbits只有三种选择 + * 1.停止位为1 + * 2.停止位为1.5 + * 3.停止位为2 + * 如果输入错误停止位则返回-1 + */ +static int switch_get_stopbits(int stopbits) { + int real_stopbits; + switch (stopbits) { + case 1: + real_stopbits = ONESTOPBIT; + break; + case 2: + real_stopbits = ONE5STOPBITS; + break; + case 3: + real_stopbits = TWOSTOPBITS; + break; + + default: + real_stopbits = -1; + break; + } + return real_stopbits; +} + PORT OpenPort(int idx) { HANDLE hComm; TCHAR comname[100]; @@ -166,8 +194,10 @@ PORT serial_init(int idx, int rate, int databits, int stopbits, int parity) { printf("set COM%d databits fail\n", idx); return NULL; } - stopbits = ONESTOPBIT; - printf("stopbits %d\n", stopbits); + stopbits = switch_get_stopbits(stopbits); + if (stopbits == -1) { + printf("stopbits set error\n"); + } ret = SetPortStopBits(com_port, stopbits); if (ret == FALSE) { printf("set COM%d stopbits fail\n", idx); @@ -212,3 +242,21 @@ int Serial_ReciveData(PORT com_port, char *data, int len) { return NoBytesRead; } + +/* 测试函数,demo工程放在main中,但是我不需要,暂时放在这 */ +void serial_test(int serial_com_id) { + PORT COM1; + char buff[1024] = {0}; + int rcv_len = 0; + + printf("Start open com%d\n", serial_com_id); + COM1 = serial_init(serial_com_id, 115200, 8, 1, 0); + + while (1) { + Serial_SendData(COM1, "hello finny", 12); + memset(buff, 0, 1024); + rcv_len = Serial_ReciveData(COM1, buff, 1024); + printf("rcv:%s\n", buff); + Sleep(1); + } +} diff --git a/app/serial.h b/app/serial.h index 0c37ad5..fc639b0 100644 --- a/app/serial.h +++ b/app/serial.h @@ -19,4 +19,7 @@ PORT serial_init(int idx, int rate, int databits, int stopbits, int parity); int Serial_SendData(PORT com_port, const char *data, int len); int Serial_ReciveData(PORT com_port, char *data, int len); +/* 测试函数,demo工程放在main中,但是我不需要,暂时放在这 */ +void serial_test(int serial_com_id); + #endif diff --git a/app/steering_gear.c b/app/steering_gear.c new file mode 100644 index 0000000..e69de29 diff --git a/app/steering_gear.h b/app/steering_gear.h new file mode 100644 index 0000000..e69de29