forked from DragonMinded/serialdvdemu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerial.cpp
More file actions
195 lines (162 loc) · 3.71 KB
/
Copy pathSerial.cpp
File metadata and controls
195 lines (162 loc) · 3.71 KB
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
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <dirent.h>
#include "Serial.h"
/* Blocking wait time */
#define WAIT_TIME 1000
int OpenSerial( const char *port, int baud, int parity )
{
int rate;
switch( baud )
{
case 4800:
rate = B4800;
break;
case 9600:
rate = B9600;
break;
case 19200:
rate = B19200;
break;
case 38400:
rate = B38400;
break;
case 57600:
rate = B57600;
break;
case 115200:
rate = B115200;
break;
default:
/* Invalid baud rate */
return -1;
}
struct termios tio;
int hSerial = 0;
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_oflag=0;
if( parity == PARITY_EVEN )
{
tio.c_cflag=CS8|CREAD|PARENB|CLOCAL;
}
else if( parity == PARITY_ODD )
{
tio.c_cflag=CS8|CREAD|PARENB|PARODD|CLOCAL;
}
else
{
tio.c_cflag=CS8|CREAD|CLOCAL;
}
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=0;
hSerial=open(port, O_RDWR | O_NONBLOCK);
if( hSerial < 0 ) { return -1; }
cfsetospeed(&tio,rate);
cfsetispeed(&tio,rate);
tcsetattr(hSerial,TCSANOW,&tio);
return hSerial;
}
uint64_t getMS()
{
struct timeval tv;
gettimeofday(&tv, NULL );
return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
}
bool ReadFile( int file, void *buf, uint32_t length, uint32_t *readAmt, void *reserved )
{
uint64_t start = getMS();
*readAmt = 0;
while( true )
{
int amount = read( file, buf, length );
if( amount < 0 )
{
if( errno == EAGAIN || errno == EWOULDBLOCK )
{
if( (getMS() - start) > WAIT_TIME )
{
return false;
}
/* Try again */
continue;
}
return false;
}
*readAmt = amount;
return true;
}
}
bool WriteFile( int file, const void * const buf, uint32_t length, uint32_t *writeAmt, void *reserved )
{
uint64_t start = getMS();
while( true )
{
int amount = write( file, buf, length );
if( amount < 0 )
{
if( errno == EAGAIN || errno == EWOULDBLOCK )
{
if( (getMS() - start) > 1000 )
{
return false;
}
/* Try again */
continue;
}
return false;
}
else
{
*writeAmt = amount;
return true;
}
}
}
void SetFilePointer( int file, int off, void *reserved, int origin )
{
lseek( file, off, origin );
}
void CloseHandle( int file )
{
close( file );
}
bool PurgeComm( int serial )
{
/* Clear out old data */
do
{
unsigned char data[1024];
int length = read( serial, data, sizeof( data ) );
if( length <= 0 ) { break; }
} while( 1 );
return true;
}
uint32_t ReadSerial( int serial, unsigned char *data, unsigned int length )
{
uint32_t readLength = 0;
ReadFile( serial, data, length, &readLength, 0 );
return readLength;
}
uint32_t WriteSerial( int serial, const unsigned char *data, unsigned int length )
{
uint32_t wroteLength = 0;
WriteFile( serial, data, length, &wroteLength, 0 );
return wroteLength;
}
void CloseSerial( int serial )
{
CloseHandle( serial );
}