1
/* Hey EMACS -*- win32-c -*- */
2
/* $Id: link_gry.c 1622 2005-09-10 06:33:36Z roms $ */
4
/* libticables2 - link cable library, a part of the TiLP project
5
* Copyright (C) 1999-2005 Romain Lievin
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
//!!!!!!!!!!!!!!!!!! Not used, for use of overlapped i/o !!!!!!!!!!!!!!!!!!!!
24
/* "Grey TIGraphLink" link cable unit */
29
#include "../ticables.h"
30
#include "../logging.h"
32
#include "../gettext.h"
35
#define hCom (HANDLE)(h->priv)
37
static int gry_prepare(CableHandle *h)
41
case PORT_1: h->address = 0x3f8; h->device = strdup("COM1"); break;
42
case PORT_2: h->address = 0x2f8; h->device = strdup("COM2"); break;
43
case PORT_3: h->address = 0x3e8; h->device = strdup("COM3"); break;
44
case PORT_4: h->address = 0x3e8; h->device = strdup("COM4"); break;
45
default: return ERR_ILLEGAL_ARG;
51
static int gry_open(CableHandle *h)
58
h->priv = (void *)CreateFile(h->device, GENERIC_READ | GENERIC_WRITE, 0,
60
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED /*| FILE_FLAG_NO_BUFFERING*/,
62
if (hCom == INVALID_HANDLE_VALUE)
64
ticables_warning("CreateFile");
65
return ERR_GRY_CREATEFILE;
69
fSuccess = SetupComm(hCom, 1024, 1024);
72
ticables_warning("SetupComm");
73
return ERR_GRY_SETUPCOMM;
76
// Retrieve config structure
77
fSuccess = GetCommState(hCom, &dcb);
80
ticables_warning("GetCommState");
81
return ERR_GRY_GETCOMMSTATE;
84
// Fills the structure with config
85
dcb.BaudRate = CBR_9600; // 9600 bauds
86
dcb.fBinary = TRUE; // Binary mode
87
dcb.fParity = FALSE; // Parity checking disabled
88
dcb.fOutxCtsFlow = FALSE; // No output flow control
89
dcb.fOutxDsrFlow = FALSE; // Idem
90
dcb.fDtrControl = DTR_CONTROL_DISABLE; // Provide power supply
91
dcb.fDsrSensitivity = FALSE; // ignore DSR status
92
dcb.fOutX = FALSE; // no XON/XOFF flow control
93
dcb.fInX = FALSE; // idem
94
dcb.fErrorChar = FALSE; // no replacement
95
dcb.fNull = FALSE; // don't discard null chars
96
dcb.fRtsControl = RTS_CONTROL_ENABLE; // Provide power supply
97
dcb.fAbortOnError = FALSE; // do not report errors
99
dcb.ByteSize = 8; // 8 bits
100
dcb.Parity = NOPARITY; // no parity checking
101
dcb.StopBits = ONESTOPBIT; // 1 stop bit
104
fSuccess = SetCommState(hCom, &dcb);
107
ticables_warning("SetCommState");
108
return ERR_GRY_SETCOMMSTATE;
111
// Wait for GrayLink to be ready
115
fSuccess = GetCommTimeouts(hCom, &cto);
118
ticables_warning("GetCommTimeouts");
119
return ERR_GRY_GETCOMMTIMEOUT;
122
cto.ReadIntervalTimeout = 0;
124
cto.ReadTotalTimeoutMultiplier = 0;
125
cto.ReadTotalTimeoutConstant = 100 * h->timeout;
127
cto.WriteTotalTimeoutMultiplier = 0;
128
cto.WriteTotalTimeoutConstant = 100 * h->timeout;
130
fSuccess = SetCommTimeouts(hCom, &cto);
133
ticables_warning("SetCommTimeouts");
134
return ERR_GRY_SETCOMMTIMEOUT;
137
// Monitor receiving of chars
138
fSuccess = SetCommMask(hCom, EV_RXCHAR);
141
ticables_warning("SetCommMask");
142
return ERR_GRY_SETCOMMMASK;
145
// Flush/Dicard buffers
146
fSuccess = PurgeComm(hCom, PURGE_TXCLEAR | PURGE_RXCLEAR);
149
ticables_warning("PurgeComm");
150
return ERR_GRY_PURGECOMM;
156
static int gry_close(CableHandle *h)
161
hCom = INVALID_HANDLE_VALUE;
167
static int gry_reset(CableHandle *h)
171
fSuccess = PurgeComm(hCom, PURGE_TXCLEAR | PURGE_RXCLEAR);
174
ticables_warning("PurgeComm");
175
return ERR_GRY_PURGECOMM;
181
static int gry_probe(CableHandle *h)
183
DWORD status; //MS_CTS_ON or MS_DTR_ON
185
EscapeCommFunction(hCom, SETDTR);
186
EscapeCommFunction(hCom, SETRTS);
187
GetCommModemStatus(hCom, &status); // Get MCR values
189
return ERR_PROBE_FAILED;
191
EscapeCommFunction(hCom, SETDTR);
192
EscapeCommFunction(hCom, CLRRTS);
193
GetCommModemStatus(hCom, &status);
195
return ERR_PROBE_FAILED;
197
EscapeCommFunction(hCom, CLRDTR);
198
EscapeCommFunction(hCom, CLRRTS);
199
GetCommModemStatus(hCom, &status);
201
return ERR_PROBE_FAILED;
203
EscapeCommFunction(hCom, CLRDTR);
204
EscapeCommFunction(hCom, SETRTS);
205
GetCommModemStatus(hCom, &status);
207
return ERR_PROBE_FAILED;
209
EscapeCommFunction(hCom, SETDTR);
210
EscapeCommFunction(hCom, SETRTS);
211
GetCommModemStatus(hCom, &status);
213
return ERR_PROBE_FAILED;
218
static int gry_put(CableHandle* h, uint8_t *data, uint32_t len)
224
memset(&ol, 0, sizeof(OVERLAPPED));
225
fSuccess = WriteFile(hCom, data, len, &nBytesWritten, &ol);
227
while(HasOverlappedIoCompleted(&ol) == FALSE) Sleep(0);
229
fSuccess = GetOverlappedResult(hCom, &ol, &nBytesWritten, FALSE);
232
ticables_warning("WriteFile");
233
return ERR_WRITE_ERROR;
235
else if (nBytesWritten == 0)
237
ticables_warning("WriteFile");
238
return ERR_WRITE_TIMEOUT;
240
else if (nBytesWritten < len)
242
ticables_warning("WriteFile");
243
return ERR_WRITE_ERROR;
249
static int gry_get(CableHandle* h, uint8_t *data, uint32_t len)
258
memset(&ol, 0, sizeof(OVERLAPPED));
259
fSuccess = ReadFile(hCom, data + i, len - i, &nBytesRead, &ol);
261
while(HasOverlappedIoCompleted(&ol) == FALSE) Sleep(0);
262
fSuccess = GetOverlappedResult(hCom, &ol, &nBytesRead, FALSE);
266
ticables_warning("ReadFile");
267
return ERR_READ_ERROR;
269
else if (nBytesRead == 0)
271
ticables_warning("ReadFile");
272
return ERR_READ_TIMEOUT;
277
printf("get : %i %i %i\n", fSuccess, nBytesRead, len);
282
// pb with this code: works if no receiving of chars has been previously done
283
static int gry_check(CableHandle *h, int *status)
286
static DWORD dwEvtMask = 0;
287
static OVERLAPPED ol = { 0 };
290
static BOOL ioPending = FALSE;
292
if(ioPending == FALSE)
294
memset(&ol, 0, sizeof(OVERLAPPED));
295
fSuccess = WaitCommEvent(hCom, &dwEvtMask, &ol);
298
printf("$ (%i)\n", ioPending);
302
if(HasOverlappedIoCompleted(&ol))
303
if(dwEvtMask & EV_RXCHAR)
314
static int gry_set_red_wire(CableHandle *h, int b)
319
static int gry_set_white_wire(CableHandle *h, int b)
324
static int gry_get_red_wire(CableHandle *h)
329
static int gry_get_white_wire(CableHandle *h)
334
static int gry_timeout(CableHandle *h)
339
cto.ReadIntervalTimeout = 0;
341
cto.ReadTotalTimeoutMultiplier = 0;
342
cto.ReadTotalTimeoutConstant = 100 * h->timeout;
344
cto.WriteTotalTimeoutMultiplier = 0;
345
cto.WriteTotalTimeoutConstant = 100 * h->timeout;
347
fSuccess = SetCommTimeouts(hCom, &cto);
350
ticables_warning("SetCommTimeouts");
351
return ERR_GRY_SETCOMMTIMEOUT;
357
const CableFncts cable_gry =
362
N_("GrayLink serial cable"),
365
&gry_open, &gry_close, &gry_reset, &gry_probe, &gry_timeout,
366
&gry_put, &gry_get, &gry_check,
367
&gry_set_red_wire, &gry_set_white_wire,
368
&gry_get_red_wire, &gry_get_white_wire,