-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathread-serial-log.c
217 lines (183 loc) · 4.15 KB
/
read-serial-log.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* Example code that reads frames from the geophone from the serial port
* and writes the frames together with a timestamp if the checksum is valid.
*/
#define _GNU_SOURCE
#include <glib-2.0/glib.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
typedef struct
{
gchar *frame;
int size;
} frame_t;
/**
* Determine if the sum of bytes matches the last value in the frame, and
* NULL-terminate the string where the checksum began.
*/
bool check_checksum ( char *frame )
{
unsigned char checksum = 0;
unsigned char reported_checksum = 0;
/* Find the last ',' in the string. */
char *last_comma = &frame[ strlen( frame ) ];
while( last_comma >= frame )
{
if( *last_comma == ',' )
{
break;
}
else
{
last_comma--;
}
}
if( *last_comma == ',' )
{
const char *checksum_pos = &last_comma[ 1 ];
reported_checksum = atoi( checksum_pos );
/* Compute the checksum of the received string. */
for( int pos = 0; frame < last_comma; frame++ )
{
checksum = checksum + frame[ pos ];
}
if( checksum == reported_checksum )
{
*last_comma = '\0';
return( true );
}
}
fprintf( stderr, "Invalid checksum %d vs. reported %d\n", checksum, reported_checksum );
return( false );
}
/**
* Read bytes from the serial port until a newline is received.
*/
static frame_t *read_frame( int serial )
{
gchar *frame = g_malloc( 16384 );
int amount_allocated = 16384;
int pos = 0;
int checksum_pos_begin = 0;
time_t timestamp = 0;
struct tm time_result;
unsigned char ch = 0;
do
{
int bytes_read;
do
{
bytes_read = read( serial, &ch, 1 );
} while( bytes_read < 1 );
if( timestamp == 0 )
{
timestamp = time( NULL );
localtime_r( ×tamp, &time_result );
asctime_r( &time_result, &frame[ pos ] );
pos = pos + strlen( frame ) - 1;
frame[ pos++ ] = ' ';
checksum_pos_begin = pos;
}
if( ch != '\n' && ch != '\r' )
{
frame[ pos++ ] = ch;
}
else
{
frame[ pos++ ] = '\0';
}
/* Dynamically expand the buffer as needed. */
if( pos == amount_allocated )
{
frame = g_realloc( frame, amount_allocated + 4096 );
amount_allocated += 4096;
}
} while( ch != '\n' );
/* Check the checksum. */
if( check_checksum( &frame[ checksum_pos_begin ] ) == true )
{
frame_t *frame_info = g_new( frame_t, 1 );
frame_info->size = pos - 1;
frame_info->frame = frame;
return( frame_info );
}
else
{
g_free( frame );
return( NULL );
}
}
int main( int argc, char *argv[ ] )
{
if( argc != 2 )
{
fprintf( stderr, "USAGE: %s <serial port>\n", argv[ 0 ] );
exit( EXIT_FAILURE );
}
// while( 1 )
// {
int serial = open( argv[ 1 ], O_RDWR| O_NOCTTY );
struct termios tty;
bzero( &tty, sizeof( tty ) );
if ( tcgetattr ( serial, &tty ) != 0 )
{
fprintf( stderr, "Can't open serial port %s: ", argv[ 1 ] );
perror( "" );
exit( EXIT_FAILURE );
}
else
{
/* Set Baud Rate */
cfsetospeed( &tty, B115200 );
cfsetispeed( &tty, B115200 );
/* Setting other Port Stuff */
tty.c_cflag &= ~PARENB;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~CRTSCTS;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 5;
tty.c_cflag |= CREAD | CLOCAL;
/* Make raw */
cfmakeraw( &tty );
/* Flush port, then apply attributes */
tcflush( serial, TCIFLUSH );
if ( tcsetattr ( serial, TCSANOW, &tty ) != 0)
{
perror( "Can't set serial port attributes: " );
exit( EXIT_FAILURE );
}
else
{
/* Make the output unbuffered. */
//setbuf( out_fhd, NULL );
FILE *out_fhd = stdout;
setvbuf( out_fhd, NULL, _IONBF, 0 );
while( 1 )
{
frame_t *frame = read_frame( serial );
if( frame != NULL )
{
printf( "%s\n", frame->frame );
fflush( out_fhd );
g_free( frame->frame );
g_free( frame );
}
}
}
// }
/* Some error must have occurred. Close the serial port, wait
a while, and start over. */
// close( serial );
// sleep( 1 );
}
exit( EXIT_FAILURE );
}