forked from apsystems/GrblHoming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rs232.cpp
233 lines (185 loc) · 5.77 KB
/
rs232.cpp
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "rs232.h"
RS232::RS232()
{
}
#ifdef Q_WS_X11
int Cport[12],
error;
struct termios new_port_settings,
old_port_settings[22];
char comports[12][13]={"/dev/ttyUSB0","/dev/ttyUSB1","/dev/ttyUSB2","/dev/ttyUSB3","/dev/ttyUSB4",
"/dev/ttyUSB5","/dev/ttyACM0","/dev/ttyACM1","/dev/ttyACM2","/dev/ttyACM3",
"/dev/ttyACM4","/dev/ttyACM5"};
int RS232::OpenComport(int comport_number)
{
int baudr= B9600;
if((comport_number>27)||(comport_number<0))
{
printf("illegal comport number\n");
return(1);
}
//baudr = B9600;
Cport[comport_number] = open(comports[comport_number], O_RDWR | O_NOCTTY | O_NDELAY);
if(Cport[comport_number]==-1)
{
perror("unable to open comport ");
return(1);
}
error = tcgetattr(Cport[comport_number], old_port_settings + comport_number);
if(error==-1)
{
close(Cport[comport_number]);
perror("unable to read portsettings ");
return(1);
}
memset(&new_port_settings, 0, sizeof(new_port_settings)); /* clear the new struct */
new_port_settings.c_cflag = baudr | CS8 | CLOCAL | CREAD;
new_port_settings.c_iflag = IGNPAR;
new_port_settings.c_oflag = 0;
new_port_settings.c_lflag = 0;
new_port_settings.c_cc[VMIN] = 0; /* block untill n bytes are received */
new_port_settings.c_cc[VTIME] = 0; /* block untill a timer expires (n * 100 mSec.) */
error = tcsetattr(Cport[comport_number], TCSANOW, &new_port_settings);
if(error==-1)
{
close(Cport[comport_number]);
perror("unable to adjust portsettings ");
return(1);
}
return(0);
}
int RS232::PollComport(int comport_number, char *buf, int size)
{
int n;
#ifndef __STRICT_ANSI__ /* __STRICT_ANSI__ is defined when the -ansi option is used for gcc */
if(size>SSIZE_MAX) size = (int)SSIZE_MAX; /* SSIZE_MAX is defined in limits.h */
#else
if(size>4096) size = 4096;
#endif
n = read(Cport[comport_number], buf, size);
return(n);
}
int RS232::SendBuf(int comport_number, char *buf, int size)
{
return(write(Cport[comport_number], buf, size));
}
void RS232::CloseComport(int comport_number)
{
close(Cport[comport_number]);
tcsetattr(Cport[comport_number], TCSANOW, old_port_settings + comport_number);
}
void RS232::Reset(int comport_number) //still to test
{
//OpenComport(comport_number);
int mcr=!TIOCM_DTR;
ioctl(Cport[comport_number],TIOCMSET,&mcr);
//CloseComport(comport_number);
}
#else
HANDLE Cport[16];
char comports[16][10]={"\\\\.\\COM1", "\\\\.\\COM2", "\\\\.\\COM3", "\\\\.\\COM4",
"\\\\.\\COM5", "\\\\.\\COM6", "\\\\.\\COM7", "\\\\.\\COM8",
"\\\\.\\COM9", "\\\\.\\COM10", "\\\\.\\COM11", "\\\\.\\COM12",
"\\\\.\\COM13", "\\\\.\\COM14", "\\\\.\\COM15", "\\\\.\\COM16"};
char baudr[64];
int RS232::OpenComport(int comport_number)
{
if((comport_number>15)||(comport_number<0))
{
printf("illegal comport number\n");
return(1);
}
strcpy_s(baudr, "baud=9600 data=8 parity=N stop=1");
Cport[comport_number] = CreateFileA(comports[comport_number],
GENERIC_READ|GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if(Cport[comport_number]==INVALID_HANDLE_VALUE)
{
printf("unable to open comport\n");
return(1);
}
DCB port_settings;
memset(&port_settings, 0, sizeof(port_settings)); /* clear the new struct */
port_settings.DCBlength = sizeof(port_settings);
if (!GetCommState(Cport[comport_number], &port_settings))
{
printf("Error getting state\n");
CloseHandle(Cport[comport_number]);
return(1);
}
/*if(!BuildCommDCBA(baudr, &port_settings))
{
printf("unable to set comport dcb settings\n");
CloseHandle(Cport[comport_number]);
return(1);
}*/
port_settings.BaudRate=CBR_9600;
port_settings.ByteSize=8;
port_settings.StopBits=ONESTOPBIT;
port_settings.Parity=NOPARITY;
port_settings.fDtrControl=DTR_CONTROL_ENABLE;
if(!SetCommState(Cport[comport_number], &port_settings))
{
printf("unable to set comport cfg settings\n");
CloseHandle(Cport[comport_number]);
return(1);
}
COMMTIMEOUTS Cptimeouts={0};
Cptimeouts.ReadIntervalTimeout = 250;
Cptimeouts.ReadTotalTimeoutMultiplier = 0;
Cptimeouts.ReadTotalTimeoutConstant = 250;
Cptimeouts.WriteTotalTimeoutMultiplier = 0;
Cptimeouts.WriteTotalTimeoutConstant = 200;
if(!SetCommTimeouts(Cport[comport_number], &Cptimeouts))
{
QMessageBox(QMessageBox::Warning,"Error","Could not read port.",QMessageBox::Ok).exec();
printf("unable to set comport time-out settings\n");
CloseHandle(Cport[comport_number]);
return(1);
}
return(0);
}
int RS232::PollComport(int comport_number, char *buf, int size)
{
DWORD n;
int b;
if(size>4096) size = 4096;
/* added the void pointer cast, otherwise gcc will complain about */
/* "warning: dereferencing type-punned pointer will break strict aliasing rules" */
ReadFile(Cport[comport_number], buf, size, &n, NULL);
//ReadFile(Cport[comport_number], buf, size, &n, 0);
b=int(n);
return(b);
}
int RS232::SendBuf(int comport_number, char *buf, int size)
{
int n;
if(WriteFile(Cport[comport_number], buf, size, (LPDWORD)((void *)&n), NULL))
{
return(n);
}
return(-1);
}
void RS232::CloseComport(int comport_number)
{
CloseHandle(Cport[comport_number]);
}
void RS232::Reset(int comport_number) //Tested 24/02/12
{
//OpenComport(comport_number);
DWORD dtr=5;
EscapeCommFunction(Cport[comport_number],dtr);
//CloseComport(comport_number);
}
#endif
void RS232::flush(int comport_number)
{
int n=1;
char buf[255];
while(n>0)
n=PollComport(comport_number,buf,255);
}