|
|
@ -1,21 +1,18 @@ |
|
|
|
#include <error.h> |
|
|
|
#include <fcntl.h> |
|
|
|
#include <malloc.h> |
|
|
|
#include <pthread.h> |
|
|
|
#include <stdio.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <strings.h> |
|
|
|
#include <string.h> |
|
|
|
#include <pthread.h> |
|
|
|
#include <unistd.h> |
|
|
|
#include <fcntl.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <error.h> |
|
|
|
#include <termios.h> |
|
|
|
#include <malloc.h> |
|
|
|
#include <sys/types.h> |
|
|
|
#include <strings.h> |
|
|
|
#include <sys/stat.h> |
|
|
|
#include <sys/types.h> |
|
|
|
#include <termios.h> |
|
|
|
#include <unistd.h> |
|
|
|
typedef struct termios termios_t; |
|
|
|
|
|
|
|
typedef struct serial_data |
|
|
|
{ |
|
|
|
|
|
|
|
typedef struct serial_data { |
|
|
|
char databuf[100]; // 发送/接受数据 |
|
|
|
int serfd; // 串口文件描述符 |
|
|
|
|
|
|
@ -24,8 +21,7 @@ typedef struct serial_data |
|
|
|
void *sersend(void *arg); |
|
|
|
void *serrecv(void *arg); |
|
|
|
|
|
|
|
int main(int argc, char *argv[]) |
|
|
|
{ |
|
|
|
int main(int argc, char *argv[]) { |
|
|
|
pthread_t pid1, pid2; |
|
|
|
pthread_attr_t *pthread_arr1, *pthread_arr2; |
|
|
|
pthread_arr1 = NULL; |
|
|
@ -36,8 +32,7 @@ int main(int argc, char *argv[]) |
|
|
|
termios_t *ter_s = malloc(sizeof(*ter_s)); |
|
|
|
|
|
|
|
serport1fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY); // 不成为控制终端程序,不受其他程序输出输出影响 |
|
|
|
if (serport1fd < 0) |
|
|
|
{ |
|
|
|
if (serport1fd < 0) { |
|
|
|
printf("%s open faild\r\n", argv[1]); |
|
|
|
return -1; |
|
|
|
} |
|
|
@ -70,8 +65,7 @@ int main(int argc, char *argv[]) |
|
|
|
|
|
|
|
tcflush(serport1fd, TCIFLUSH); // 刷清未处理的输入和/或输出 |
|
|
|
|
|
|
|
if (tcsetattr(serport1fd, TCSANOW, ter_s) != 0) |
|
|
|
{ |
|
|
|
if (tcsetattr(serport1fd, TCSANOW, ter_s) != 0) { |
|
|
|
printf("com set error!\r\n"); |
|
|
|
} |
|
|
|
|
|
|
@ -90,9 +84,7 @@ int main(int argc, char *argv[]) |
|
|
|
pthread_create(&pid2, pthread_arr2, serrecv, (void *)&rec_data); |
|
|
|
|
|
|
|
ssize_t sizec; |
|
|
|
while (1) |
|
|
|
{ |
|
|
|
|
|
|
|
while (1) { |
|
|
|
usleep(100000); |
|
|
|
} |
|
|
|
|
|
|
@ -106,15 +98,11 @@ void *sersend(void *arg) // 串口发送线程函数 |
|
|
|
{ |
|
|
|
ser_Data *snd = (ser_Data *)arg; |
|
|
|
int ret; |
|
|
|
while (1) |
|
|
|
{ |
|
|
|
while (1) { |
|
|
|
ret = write(snd->serfd, snd->databuf, strlen(snd->databuf)); |
|
|
|
if (ret > 0) |
|
|
|
{ |
|
|
|
if (ret > 0) { |
|
|
|
printf("send success, data is %s\r\n", snd->databuf); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
} else { |
|
|
|
printf("send error!\r\n"); |
|
|
|
} |
|
|
|
usleep(300000); |
|
|
@ -129,15 +117,11 @@ void *serrecv(void *arg) // 串口发送线程函数 |
|
|
|
{ |
|
|
|
ser_Data *rec = (ser_Data *)arg; |
|
|
|
int ret; |
|
|
|
while (1) |
|
|
|
{ |
|
|
|
while (1) { |
|
|
|
ret = read(rec->serfd, rec->databuf, 1024); |
|
|
|
if (ret > 0) |
|
|
|
{ |
|
|
|
if (ret > 0) { |
|
|
|
printf("recv success,recv size is %d,data is %s\r\n", ret, rec->databuf); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
} else { |
|
|
|
/* |
|
|
|
什么也不做 |
|
|
|
*/ |
|
|
|