#include <stdio.h> | |
#include <string.h> | |
#include <strings.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <fcntl.h> | |
#include <signal.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <pthread.h> | |
#include <termios.h> | |
#include <time.h> | |
#include <sys/ioctl.h> | |
#include <dlfcn.h> | |
#include <stdint.h> | |
#include <stdbool.h> | |
#define MY_IOCTL_CMD3 _IOW('d', 1, char*) // 写入字符串 | |
typedef enum{ | |
GSW_HAL_BAUDRATE_1200=1200, | |
GSW_HAL_BAUDRATE_1800=1800, | |
GSW_HAL_BAUDRATE_4800=4800, | |
GSW_HAL_BAUDRATE_9600=9600, | |
GSW_HAL_BAUDRATE_19200=19200, | |
GSW_HAL_BAUDRATE_38400=38400, | |
GSW_HAL_BAUDRATE_57600=57600, | |
GSW_HAL_BAUDRATE_115200=115200, | |
GSW_HAL_BAUDRATE_230400=230400, | |
GSW_HAL_BAUDRATE_460800=460800, | |
GSW_HAL_BAUDRATE_500000=500000, | |
GSW_HAL_BAUDRATE_576000=576000, | |
GSW_HAL_BAUDRATE_921600=921600 | |
}gsw_hal_uart_baudrate; | |
int32_t (*gsw_uart_open_ex)(int8_t *port, gsw_hal_uart_baudrate, uint32_t, int8_t, uint32_t); | |
int32_t (*gsw_uart_flush)(int32_t); | |
int32_t (*gsw_uart_close)(int32_t); | |
int32_t (*gsw_uart_write)(int32_t, const uint8_t *buffer, uint32_t); | |
int32_t (*gsw_uart_read)(int32_t, uint8_t *buffer, uint32_t, int32_t); | |
int32_t (*gsw_uart_ioctl)(int32_t, uint32_t, void *pvalue); | |
void *dlHandle_uart; | |
char *lynqLib_uart = "/lib/libgsw_lib.so"; | |
int8_t *port; | |
gsw_hal_uart_baudrate baudrate; | |
uint32_t bits = -1; | |
int8_t parity = 3; | |
uint32_t stop = -1; | |
int fd = -1; | |
int ret = -1; | |
void prama(char *buffer) | |
{ | |
char *words[10]; | |
for (int i = 0; i < 10; i++) | |
{ | |
words[i] = (char *)malloc(20 * sizeof(char)); | |
if (words[i] != NULL) | |
{ | |
strcpy(words[i], ""); | |
} | |
} | |
int i = 0; | |
buffer[strlen(buffer)-1] = '\0'; | |
char *token = strtok(buffer, " "); | |
while (token != NULL && i < 10) | |
{ | |
strcpy(words[i], token); | |
printf("var%d = %s\n", i, words[i]); | |
i++; | |
token = strtok(NULL, " "); | |
} | |
if (words[0]) | |
port = (int8_t *)words[0]; | |
if (words[1]) | |
baudrate = atoi(words[1]); | |
if (words[2]) | |
bits = atoi(words[2]); | |
if (words[3]) | |
parity = (int8_t)(words[3][0]); | |
if (words[4]) | |
stop = atoi(words[4]); | |
for (int i = 1; i < 10; i++) | |
{ | |
free(words[i]); | |
} | |
return; | |
} | |
void *read_function(void *timeout_ms) | |
{ | |
uint8_t buf[128]; | |
while(1) | |
{ | |
memset(buf,0,sizeof(buf)); | |
ret = gsw_uart_read(fd, buf, sizeof(buf) - 1, *(int *)timeout_ms); | |
if(ret > 0) | |
{ | |
if(memcmp(buf, "exit", 4) == 0) | |
{ | |
printf("exit\n"); | |
break; | |
} | |
printf("%s\n",buf); | |
printf("gsw_uart_read success.\n"); | |
} | |
} | |
close(fd); | |
return NULL; | |
} | |
int main(void) | |
{ | |
int opt = 0; | |
int timeout_ms; | |
dlHandle_uart = dlopen(lynqLib_uart, RTLD_NOW); | |
pthread_t read_thread; | |
while(1) | |
{ | |
printf("=========uart main=========\n"); | |
if (scanf("%d", &opt) != 1) | |
printf("input error,please check it"); | |
while(getchar()!='\n'); | |
switch (opt) | |
{ | |
case 1: | |
{ | |
char buffer[126]; | |
if (fgets(buffer, sizeof(buffer), stdin) == NULL) | |
{ | |
printf("input error\n"); | |
break; | |
} | |
prama(buffer); | |
gsw_uart_open_ex=(int32_t(*)(int8_t *port, gsw_hal_uart_baudrate, uint32_t, int8_t, uint32_t))dlsym(dlHandle_uart, "gsw_uart_open_ex"); | |
fd = gsw_uart_open_ex(port, baudrate, bits, parity, stop); | |
if(fd < 0) | |
{ | |
printf("gsw_uart_open_ex FAIL.\n"); | |
break; | |
} | |
printf("gsw_uart_open_ex success.\n"); | |
break; | |
} | |
case 2: | |
{ | |
gsw_uart_flush=(int32_t(*)(int32_t))dlsym(dlHandle_uart, "gsw_uart_flush"); | |
ret = gsw_uart_flush(fd); | |
if(ret < 0) | |
{ | |
printf("gsw_uart_flush FAIL.\n"); | |
break; | |
} | |
printf("gsw_uart_flush success.\n"); | |
break; | |
} | |
case 3: | |
{ | |
gsw_uart_close=(int32_t(*)(int32_t))dlsym(dlHandle_uart, "gsw_uart_close"); | |
ret = gsw_uart_close(fd); | |
if(ret < 0) | |
{ | |
printf("gsw_uart_close FAIL.\n"); | |
break; | |
} | |
free(port); | |
printf("gsw_uart_close success.\n"); | |
return 0; | |
} | |
case 4: | |
{ | |
const uint8_t *buf = (const uint8_t *)"Hello ward"; | |
gsw_uart_write=(int32_t(*)(int32_t, const uint8_t *buffer, uint32_t))dlsym(dlHandle_uart, "gsw_uart_write"); | |
ret = gsw_uart_write(fd, buf, strlen((const char* )buf)); | |
if(ret < 0) | |
{ | |
printf("gsw_uart_write FAIL.\n"); | |
break; | |
} | |
printf("gsw_uart_write success.\n"); | |
break; | |
} | |
case 5: | |
{ | |
printf("Non-blocking read : 0\nBlockage : 1\nTimeout time (milliseconds) : greater than 1\n"); | |
ret = scanf("%d",&timeout_ms); | |
gsw_uart_read=(int32_t(*)(int32_t, uint8_t *buffer, uint32_t, int32_t))dlsym(dlHandle_uart, "gsw_uart_read"); | |
pthread_create(&read_thread, NULL, read_function, (void *)&timeout_ms); | |
pthread_join(read_thread, NULL); | |
break; | |
} | |
case 6: | |
{ | |
char buf[128] = "Hello ward"; | |
gsw_uart_ioctl=(int32_t(*)(int32_t, uint32_t, void *pvalue))dlsym(dlHandle_uart, "gsw_uart_ioctl"); | |
ret = gsw_uart_ioctl(fd, MY_IOCTL_CMD3, buf); | |
if(ret < 0) | |
{ | |
printf("gsw_uart_ioctl FAIL.\n"); | |
break; | |
} | |
printf("gsw_uart_ioctl success.\n"); | |
break; | |
} | |
} | |
} | |
return 0; | |
} |