-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart-xterm.c
158 lines (120 loc) · 3.32 KB
/
uart-xterm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// modified for supporting multi ports, by Bill.Zhang, in 2022-12-28;
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
#include <pthread.h>
#include "light_uart.h"
#include "queue_obj.h"
#define LIGHT_UART_MULTI_PORT_VERSION "1.0.1"
#define UART_DATA_PROCESS_BUFFER_LENGTH 1024*10
static int rxfd,txfd;
LightUart *pLightUartObj;
char uart_data_process_buffer[UART_DATA_PROCESS_BUFFER_LENGTH];
unsigned int uart_data_process_buffer_i;
pthread_t thread_uart_data_process;
void *uart_data_process(void *args);
QUEUE_OBJ QUEUE_OBJ_APP;
void *capture_keystrokes(void *context)
{
do
{
char c;
// stdin;
read(0, &c, 1);
if (c != '\r')
{
//write(txfd, &c, 1);
uart_data_process_buffer[uart_data_process_buffer_i]=c;
uart_data_process_buffer_i++;
}
if(c == 0x08)
{
if(uart_data_process_buffer_i>0)
{
uart_data_process_buffer_i--;
}
}
if((c == 0x0d)||(c==0x0a))
{
// move data from queue to buffer;
if(uart_data_process_buffer_i>0)
{
for(int uart_data_i=0;uart_data_i<uart_data_process_buffer_i-1;uart_data_i++)
{
QUEUE_OBJ_APP.EnQueue(uart_data_process_buffer[uart_data_i]);
}
QUEUE_OBJ_APP.EnQueue(0x0d);
memset(uart_data_process_buffer,0,UART_DATA_PROCESS_BUFFER_LENGTH);
uart_data_process_buffer_i=0;
}
}
} while (1);
return NULL;
}
void *uart_data_process(void *args)
{
char send_char;
//write(txfd, &c, 1);
for(;;)
{
if(QUEUE_OBJ_APP.QueueLength()>0)
{
send_char=QUEUE_OBJ_APP.DeQueue();
write(txfd,&send_char,1);
fputc(send_char,pLightUartObj->uartlog);
}
else
{
usleep(10000);// 10ms scan once;
}
}
}
int main(int argc, char *argv[])
{
pthread_t tid;
memset(uart_data_process_buffer,0,UART_DATA_PROCESS_BUFFER_LENGTH);
uart_data_process_buffer_i=0;
QUEUE_OBJ_APP.InitQueue();
pLightUartObj=new LightUart();
char *tempVersion=pLightUartObj->GetVersion();
printf("Ver:%s\n",tempVersion);
pLightUartObj->InitAll();
if(argc!=3)
{
perror("parameter error!");
return -1;
}
sprintf(pLightUartObj->uartfifo_buffer,"./uart_fifo/uartfifo_%s",argv[1]);
sprintf(pLightUartObj->uartfifo_tx_buffer,"./uart_fifo/uartfifo-tx_%s",argv[1]);
//system("/bin/stty raw -echo");
system("/bin/stty sane ignbrk intr ^k eof ^@");
if (0 > (rxfd = open (pLightUartObj->uartfifo_buffer, O_RDONLY)))
{
perror("open(./uart_fifo/uartfifo)");
return -1;
}
if (0 > (txfd = open (pLightUartObj->uartfifo_tx_buffer, O_WRONLY)))
{
perror("open(./uart_fifo/uartfifo-tx)");
return -1;
}
pthread_create(&tid, NULL, &capture_keystrokes, NULL);
pthread_create(&thread_uart_data_process, NULL, &uart_data_process, NULL);
sprintf(pLightUartObj->tempuartlogfilename,"./%s.log",argv[2]);
pLightUartObj->uartlog = fopen(pLightUartObj->tempuartlogfilename,"w+");
while (read(rxfd,&pLightUartObj->c,1) == 1)
{
write(1,&pLightUartObj->c,1);
if ((pLightUartObj->c != '\r') && ( (int) pLightUartObj->c != 0))
{
fputc(pLightUartObj->c,pLightUartObj->uartlog);
fflush(pLightUartObj->uartlog);
}
}
fclose(pLightUartObj->uartlog);
return 0;
}