forked from ChrisVeigl/BrainBay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ob_comreader.cpp
316 lines (257 loc) · 8.1 KB
/
ob_comreader.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* -----------------------------------------------------------------------------
BrainBay Version 1.7, GPL 2003-2010, contact: chris@shifz.org
MODULE: OB_COMREADER.H: implementation of the ComPort Reader Element
Authors: Chris Veigl
The ComPort Reader can be used to open a serial port and read data from it
Thus, external devices or Software like Mitsar Psytask can be connected and
transfer information to a running BrainBay Session
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; See the
GNU General Public License for more details.
-----------------------------------------------------------------------------*/
#include "brainBay.h"
#include "ob_comreader.h"
extern char * szBaud[];
extern DWORD BaudTable[];
LRESULT CALLBACK ComReaderDlgHandler(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
char szBuffer[50];
int wPosition,t;
COMREADEROBJ * st;
st = (COMREADEROBJ *) actobject;
if ((st==NULL)||(st->type!=OB_COMREADER)) return(FALSE);
switch( message )
{
case WM_INITDIALOG:
for (t = 0; t < MAX_COMPORT; t++)
{
wsprintf( szBuffer, "COM%d", t + 1 ) ;
SendDlgItemMessage( hDlg, IDC_PORTCOMBO, CB_ADDSTRING, 0,(LPARAM) (LPSTR) szBuffer ) ;
}
if (st->comport) SendDlgItemMessage( hDlg, IDC_PORTCOMBO, CB_SETCURSEL, (WPARAM) (st->comport - 1), 0L ) ;
else SetDlgItemText( hDlg, IDC_PORTCOMBO, "none") ;
for (t = 0; BaudTable[t]!=0 ; t++)
{
wPosition = LOWORD( SendDlgItemMessage( hDlg, IDC_BAUDCOMBO, CB_ADDSTRING, 0, (LPARAM) (LPSTR) szBaud[t] ) ) ;
SendDlgItemMessage( hDlg, IDC_BAUDCOMBO, CB_SETITEMDATA, (WPARAM) wPosition, (LPARAM) BaudTable[t]) ;
if (BaudTable[t] == st->baudrate) SendDlgItemMessage( hDlg, IDC_BAUDCOMBO, CB_SETCURSEL, (WPARAM) wPosition, 0L ) ;
}
if (st->comdev==INVALID_HANDLE_VALUE)
CheckDlgButton(hDlg, IDC_CONNECTED, FALSE);
else CheckDlgButton(hDlg, IDC_CONNECTED, TRUE);
SetDlgItemInt(hDlg,IDC_RECEIVED,st->received,0);
SetDlgItemInt(hDlg,IDC_PROCESSED,st->processed,0);
break;
case WM_CLOSE:
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_PORTCOMBO:
if (HIWORD(wParam)==CBN_SELCHANGE)
{
st->comport=SendDlgItemMessage(hDlg, IDC_PORTCOMBO, CB_GETCURSEL, 0, 0 )+1 ;
st->BreakDownComPort();
CheckDlgButton(hDlg,IDC_CONNECTED,FALSE);
}
break;
case IDC_BAUDCOMBO:
if (HIWORD(wParam)==CBN_SELCHANGE)
{ int sel;
sel=SendDlgItemMessage(hDlg, IDC_BAUDCOMBO, CB_GETCURSEL, 0, 0 ) ;
st->baudrate=BaudTable[sel];
if (st->comdev!=INVALID_HANDLE_VALUE)
{
st->BreakDownComPort();
st->connected=st->SetupComPort(st->comport);
CheckDlgButton(hDlg, IDC_CONNECTED, st->connected);
}
}
break;
case IDC_CONNECT:
if (st->connected)
{
st->BreakDownComPort();
CheckDlgButton(hDlg, IDC_CONNECTED, FALSE);
}
else
{
st->connected=st->SetupComPort(st->comport);
CheckDlgButton(hDlg, IDC_CONNECTED, st->connected);
}
st->received=st->processed=0;
st->inpos=st->outpos=0;
break;
case IDC_CONNECTED:
CheckDlgButton(hDlg,IDC_CONNECTED, st->connected);
break;
}
return TRUE;
break;
case WM_SIZE:
case WM_MOVE: update_toolbox_position(hDlg);
break;
}
return FALSE;
}
BOOL COMREADEROBJ::SetupComPort(int port)
{
connected=FALSE;
int sav_port;
DCB dcb = {0};
char PORTNAME[10];
sav_port=comport;
BreakDownComPort();
comport= port ;
// set tty structure for the specified port
if (!port) goto failed;
sprintf(PORTNAME,"\\\\.\\COM%d",port);
comdev = CreateFile( PORTNAME,GENERIC_READ | GENERIC_WRITE,
0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if (comdev == INVALID_HANDLE_VALUE) goto failed;
if (!GetCommState(comdev, &dcb)) // get current DCB settings
{ report_error("GetCommState");goto failed; }
// update DCB rate, byte size, parity, and stop bits size
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = baudrate;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.EvtChar = '\0';
// update flow control settings
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDsrSensitivity = FALSE;;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fTXContinueOnXoff = FALSE;
dcb.XonChar = 0;
dcb.XoffChar = 0;
dcb.XonLim = 0;
dcb.XoffLim = 0;
dcb.fParity = FALSE; //TRUE;
if (!SetCommState(comdev, &dcb)) // set new state
{ report_error("SetCommState failed"); goto failed;}
if (!SetupComm(comdev, 1024, 1024)) // set comm buffer sizes
{ report_error("SetupComm failed");goto failed;}
if (!EscapeCommFunction(comdev, SETDTR)) // raise DTR
{ report_error("EscapeCommFunction failed");goto failed;}
SetCommMask (comdev, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD | EV_RING);
return(TRUE);
failed:
{
char sztemp[100];
sprintf(sztemp, "The Port COM%d is not available. Please select another Com-Port.",port);
write_logfile("COMPORT %d open failed.",port );
report_error(sztemp);
comport=sav_port;
comdev=INVALID_HANDLE_VALUE;
connected=FALSE;
return FALSE;
}
}
int COMREADEROBJ::ReadComPort(HANDLE device, unsigned char * buffer, unsigned int pos, unsigned int maxlen)
{
DWORD dwRead;
DWORD dwBytesTransferred,i;
struct _COMSTAT status;
unsigned long etat;
//
if (device==INVALID_HANDLE_VALUE) return(FALSE);
dwBytesTransferred = 0;
if (ClearCommError(device, &etat, &status))
dwBytesTransferred = status.cbInQue;
if (dwBytesTransferred)
{
i=0;
if (dwBytesTransferred+pos >= maxlen)
{
if (dwBytesTransferred > maxlen) dwBytesTransferred=maxlen;
i=maxlen-pos;
ReadFile (device, buffer+pos, i, &dwRead, 0);
pos=0;
}
ReadFile (device, buffer+pos, dwBytesTransferred-i, &dwRead, 0);
}
return((int)dwBytesTransferred);
}
BOOL COMREADEROBJ::BreakDownComPort()
{
connected=FALSE;
if (comdev==INVALID_HANDLE_VALUE) return TRUE;
if (!PurgeComm(comdev, PURGE_FLAGS)) report_error("PurgeComm failed..");
if (!EscapeCommFunction(comdev, CLRDTR)) report_error("EscapeCommFunction failed");
CloseHandle(comdev);
comdev=INVALID_HANDLE_VALUE;
return TRUE;
}
COMREADEROBJ::COMREADEROBJ(int num) : BASE_CL()
{
inports = 0;
outports = 1;
width=75;
strcpy(out_ports[0].out_name,"data");
comport=0;
inpos=0;
outpos=0;
received=0;
processed=0;
baudrate=57600;
comdev=INVALID_HANDLE_VALUE;
}
void COMREADEROBJ::make_dialog(void)
{ display_toolbox(hDlg=CreateDialog(hInst, (LPCTSTR)IDD_COMREADER, ghWndStatusbox, (DLGPROC)ComReaderDlgHandler)); }
void COMREADEROBJ::load(HANDLE hFile)
{
load_object_basics(this);
load_property("comport",P_INT,&comport);
load_property("baudrate",P_INT,&baudrate);
load_property("connected",P_INT,&connected);
if (connected)
{
connected=SetupComPort(comport);
}
}
void COMREADEROBJ::save(HANDLE hFile)
{
save_object_basics(hFile, this);
save_property(hFile,"comport",P_INT,&comport);
save_property(hFile,"baudrate",P_INT,&baudrate);
save_property(hFile,"connected",P_INT,&connected);
}
void COMREADEROBJ::incoming_data(int port, float value)
{
// if (port==0) input1=value;
}
void COMREADEROBJ::work(void)
{
int i;
if (connected)
{
// attention: data lost in case of buffer overrun ..
i=ReadComPort(comdev,buffer, inpos,COMREADERBUFLEN);
received+=i;
inpos=(inpos+i) % COMREADERBUFLEN;
if (inpos!=outpos)
{
pass_values(0,(float)buffer[outpos]);
outpos= (++outpos) % COMREADERBUFLEN;
processed++;
}
else pass_values(0,0);
}
if ((hDlg==ghWndToolbox) && (!TIMING.dialog_update))
{
SetDlgItemInt(hDlg,IDC_RECEIVED,inpos,0);
SetDlgItemInt(hDlg,IDC_PROCESSED,outpos,0);
}
}
COMREADEROBJ::~COMREADEROBJ()
{
BreakDownComPort();
}