You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
#include <error.h>
#include <fcntl.h>
#include <malloc.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include "uart.hpp"
using namespace iflytop; class Uart g_uart; int g_rec_len;
void *thread_uart_send(void *arg) { while (1) { if (g_rec_len > 0) { g_uart.send((char *)arg, g_rec_len); g_rec_len = 0; } usleep(1000); } }
void *thread_uart_receive(void *arg) { while (1) { if (g_rec_len == 0) { g_rec_len = g_uart.receive((char *)arg, 128); } usleep(1000); } }
int main(int argc, char *argv[]) { char share_buffer[128];
pthread_t pid1, pid2; pthread_attr_t *pthread_arr1, *pthread_arr2; pthread_arr1 = NULL; pthread_arr2 = NULL;
if (argc != 3) { cout << "input error,please input ttyS and baudrate" << endl; return -1; }
if (g_uart.open(argv[1], argv[2]) < 0) { cout << "open uart error" << endl; return -2; } else { cout << "open uart success" << endl; }
if (pthread_create(&pid1, pthread_arr1, thread_uart_send, (void *)&share_buffer) != 0) { cout << "create uart send thread error" << endl; return -3; } else { cout << "create uart send thread success" << endl; } if (pthread_create(&pid2, pthread_arr2, thread_uart_receive, (void *)&share_buffer) != 0) { cout << "create uart receive thread error" << endl; return -4; } else { cout << "create uart receive thread success" << endl; }
while (1) { usleep(100000); }
pthread_join(pid1, NULL); pthread_join(pid2, NULL); return 0; }
|