| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL) |
| 18 |
|
| 19 |
#include <conio.h> |
| 20 |
|
| 21 |
devType fast_ser_dev_type(icoms *p, int tryhard, void *, void *); |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
int serial_get_paths(icompaths *p, icom_type mask) { |
| 26 |
int i; |
| 27 |
LONG stat; |
| 28 |
HKEY sch; |
| 29 |
|
| 30 |
a1logd(p->log, 7, "serial_get_paths: called with mask = 0x%x\n",mask); |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#define MXKSIZE 500 |
| 35 |
#define MXVSIZE 300 |
| 36 |
|
| 37 |
if (mask & (icomt_serial | icomt_fastserial | icomt_btserial)) { |
| 38 |
|
| 39 |
a1logd(p->log, 6, "serial_get_paths: looking up the registry for serial ports\n"); |
| 40 |
|
| 41 |
if ((stat = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", |
| 42 |
0, KEY_READ, &sch)) != ERROR_SUCCESS) { |
| 43 |
a1logd(p->log, 1, "serial_get_paths: There don't appear to be any serial ports\n"); |
| 44 |
return ICOM_OK; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
a1logd(p->log, 8, "serial_get_paths: looking through all the values in the SERIALCOMM key\n"); |
| 49 |
|
| 50 |
for (i = 0; ; i++) { |
| 51 |
char valname[MXKSIZE], *vp; |
| 52 |
DWORD vnsize = MXKSIZE; |
| 53 |
DWORD vtype; |
| 54 |
char value[MXVSIZE]; |
| 55 |
DWORD vsize = MXVSIZE; |
| 56 |
icom_type dctype = icomt_unknown; |
| 57 |
|
| 58 |
stat = RegEnumValue( |
| 59 |
sch, |
| 60 |
i, |
| 61 |
valname, |
| 62 |
&vnsize, |
| 63 |
NULL, |
| 64 |
&vtype, |
| 65 |
value, |
| 66 |
&vsize |
| 67 |
); |
| 68 |
if (stat == ERROR_NO_MORE_ITEMS) { |
| 69 |
a1logd(p->log, 8, "serial_get_paths: got ERROR_NO_MORE_ITEMS\n"); |
| 70 |
break; |
| 71 |
} |
| 72 |
if (stat == ERROR_MORE_DATA |
| 73 |
|| stat != ERROR_SUCCESS) { |
| 74 |
a1logw(p->log, "serial_get_paths: RegEnumValue failed with %d\n",stat); |
| 75 |
break; |
| 76 |
} |
| 77 |
valname[MXKSIZE-1] = '\000'; |
| 78 |
value[MXVSIZE-1] = '\000'; |
| 79 |
|
| 80 |
if (vtype != REG_SZ) { |
| 81 |
a1logw(p->log, "serial_get_paths: RegEnumValue didn't return stringz type\n"); |
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
if ((vp = strrchr(valname, '\\')) == NULL) |
| 86 |
vp = valname; |
| 87 |
else |
| 88 |
vp++; |
| 89 |
|
| 90 |
a1logd(p->log, 8, "serial_get_paths: checking '%s'\n",vp); |
| 91 |
|
| 92 |
|
| 93 |
if (strncmp(vp, "VCP", 3) == 0) { |
| 94 |
dctype |= icomt_fastserial; |
| 95 |
} |
| 96 |
|
| 97 |
if (strncmp(vp, "USBSER", 6) == 0) { |
| 98 |
dctype |= icomt_fastserial; |
| 99 |
} |
| 100 |
|
| 101 |
if (strncmp(vp, "BtPort", 6) == 0 |
| 102 |
|| strncmp(vp, "BthModem", 8) == 0) { |
| 103 |
dctype |= icomt_fastserial; |
| 104 |
dctype |= icomt_btserial; |
| 105 |
} |
| 106 |
|
| 107 |
#ifndef ENABLE_SERIAL |
| 108 |
if (dctype & icomt_fastserial) { |
| 109 |
#endif |
| 110 |
if (((mask & icomt_serial) && !(dctype & icomt_fastserial)) |
| 111 |
|| ((mask & icomt_fastserial) && (dctype & icomt_fastserial) |
| 112 |
&& !(dctype & icomt_btserial)) |
| 113 |
|| ((mask & icomt_btserial) && (dctype & icomt_btserial))) { |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
p->add_serial(p, value, value, dctype); |
| 119 |
a1logd(p->log, 8, "serial_get_paths: Added '%s' path '%s' dctype 0x%x\n",vp, value,dctype); |
| 120 |
} |
| 121 |
#ifndef ENABLE_SERIAL |
| 122 |
} |
| 123 |
#endif |
| 124 |
|
| 125 |
|
| 126 |
if (dctype & icomt_fastserial) { |
| 127 |
icompath *path; |
| 128 |
icoms *icom; |
| 129 |
if ((path = p->get_last_path(p)) != NULL |
| 130 |
&& (icom = new_icoms(path, p->log)) != NULL) { |
| 131 |
if (!p->fs_excluded(p, path)) { |
| 132 |
devType itype = fast_ser_dev_type(icom, 0, NULL, NULL); |
| 133 |
if (itype != instUnknown) |
| 134 |
icompaths_set_serial_itype(path, itype); |
| 135 |
} |
| 136 |
icom->del(icom); |
| 137 |
} |
| 138 |
a1logd(p->log, 8, "serial_get_paths: Identified '%s' dctype 0x%x\n",inst_sname(path->dtype),path->dctype); |
| 139 |
} |
| 140 |
} |
| 141 |
if ((stat = RegCloseKey(sch)) != ERROR_SUCCESS) { |
| 142 |
a1logw(p->log, "serial_get_paths: RegCloseKey failed with %d\n",stat); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
return ICOM_OK; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
int serial_is_open(icoms *p) { |
| 153 |
return p->phandle != NULL; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
void serial_close_port(icoms *p) { |
| 158 |
|
| 159 |
if (p->is_open && p->phandle != NULL) { |
| 160 |
CloseHandle(p->phandle); |
| 161 |
p->phandle = NULL; |
| 162 |
msec_sleep(100); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
static void nt_ser_clearerr(icoms *p) { |
| 168 |
DWORD errs; |
| 169 |
|
| 170 |
if (!ClearCommError(p->phandle, &errs,NULL)) |
| 171 |
warning("nt_ser_clearerr: failed, and Clear error failed in %s at %d",__FILE__,__LINE__); |
| 172 |
|
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
#ifndef CBR_230400 |
| 179 |
# define CBR_230400 230400 |
| 180 |
#endif |
| 181 |
#ifndef CBR_460800 |
| 182 |
# define CBR_460800 460800 |
| 183 |
#endif |
| 184 |
#ifndef CBR_512000 |
| 185 |
# define CBR_512000 512000 |
| 186 |
#endif |
| 187 |
#ifndef CBR_921600 |
| 188 |
# define CBR_921600 921600 |
| 189 |
#endif |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
static int |
| 195 |
icoms_set_ser_port_ex( |
| 196 |
icoms *p, |
| 197 |
flow_control fc, |
| 198 |
baud_rate baud, |
| 199 |
parity parity, |
| 200 |
stop_bits stop, |
| 201 |
word_length word, |
| 202 |
int delayms) { |
| 203 |
|
| 204 |
a1logd(p->log, 8, "icoms_set_ser_port: About to set port characteristics:\n" |
| 205 |
" Port name = %s\n" |
| 206 |
" Flow control = %d\n" |
| 207 |
" Baud Rate = %s\n" |
| 208 |
" Parity = %d\n" |
| 209 |
" Stop bits = %d\n" |
| 210 |
" Word length = %d\n" |
| 211 |
" Open delay = %d ms\n" |
| 212 |
,p->name ,fc ,baud_rate_to_str(baud) ,parity ,stop ,word, delayms); |
| 213 |
|
| 214 |
#ifdef NEVER |
| 215 |
if (p->is_open) { |
| 216 |
a1logd(p->log, 8, "icoms_set_ser_port: closing port '%s'\n",p->name); |
| 217 |
p->close_port(p); |
| 218 |
} |
| 219 |
#endif |
| 220 |
|
| 221 |
if (p->port_type(p) == icomt_serial) { |
| 222 |
DCB dcb; |
| 223 |
|
| 224 |
a1logd(p->log, 8, "icoms_set_ser_port: Make sure serial port is open\n"); |
| 225 |
|
| 226 |
if (fc != fc_nc) |
| 227 |
p->fc = fc; |
| 228 |
if (baud != baud_nc) |
| 229 |
p->br = baud; |
| 230 |
if (parity != parity_nc) |
| 231 |
p->py = parity; |
| 232 |
if (stop != stop_nc) |
| 233 |
p->sb = stop; |
| 234 |
if (word != length_nc) |
| 235 |
p->wl = word; |
| 236 |
|
| 237 |
|
| 238 |
if (!p->is_open) { |
| 239 |
char buf[100]; |
| 240 |
|
| 241 |
a1logd(p->log, 8, "icoms_set_ser_port: about to open serial port '%s'\n",p->spath); |
| 242 |
|
| 243 |
|
| 244 |
sprintf(buf, "\\\\.\\%s", p->spath); |
| 245 |
|
| 246 |
if ((p->phandle = CreateFile( |
| 247 |
buf, |
| 248 |
GENERIC_READ|GENERIC_WRITE, |
| 249 |
0, |
| 250 |
NULL, |
| 251 |
OPEN_EXISTING, |
| 252 |
0, |
| 253 |
NULL) |
| 254 |
) == INVALID_HANDLE_VALUE) { |
| 255 |
a1logd(p->log, 1, "icoms_set_ser_port: open port '%s' failed with LastError %d\n",buf,GetLastError()); |
| 256 |
return ICOM_SYS; |
| 257 |
} |
| 258 |
|
| 259 |
if (delayms < 160) |
| 260 |
delayms = 160; |
| 261 |
|
| 262 |
msec_sleep(delayms); |
| 263 |
|
| 264 |
p->is_open = 1; |
| 265 |
} |
| 266 |
|
| 267 |
if (GetCommState(p->phandle, &dcb) == FALSE) { |
| 268 |
CloseHandle(p->phandle); |
| 269 |
msec_sleep(100); |
| 270 |
a1loge(p->log, ICOM_SYS, "icoms_set_ser_port: reading state '%s' failed with LastError %d\n",p->spath,GetLastError()); |
| 271 |
return ICOM_SYS; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
dcb.fBinary = TRUE; |
| 276 |
dcb.fOutxCtsFlow = FALSE; |
| 277 |
dcb.fOutxDsrFlow = FALSE; |
| 278 |
dcb.fDtrControl = DTR_CONTROL_ENABLE; |
| 279 |
dcb.fDsrSensitivity = FALSE; |
| 280 |
dcb.fTXContinueOnXoff = TRUE; |
| 281 |
dcb.fOutX = FALSE; |
| 282 |
dcb.fInX = FALSE; |
| 283 |
dcb.fErrorChar = FALSE; |
| 284 |
dcb.fNull = FALSE; |
| 285 |
dcb.fRtsControl = RTS_CONTROL_ENABLE; |
| 286 |
dcb.fAbortOnError = FALSE; |
| 287 |
|
| 288 |
switch (p->fc) { |
| 289 |
case fc_nc: |
| 290 |
CloseHandle(p->phandle); |
| 291 |
msec_sleep(100); |
| 292 |
a1loge(p->log, ICOM_SYS, "icoms_set_ser_port: illegal flow control %d\n",p->fc); |
| 293 |
return ICOM_SYS; |
| 294 |
case fc_None: |
| 295 |
|
| 296 |
break; |
| 297 |
case fc_XonXOff: |
| 298 |
|
| 299 |
dcb.fOutX = TRUE; |
| 300 |
dcb.fInX = TRUE; |
| 301 |
dcb.XonChar = 0x11; |
| 302 |
dcb.XoffChar = 0x13; |
| 303 |
|
| 304 |
break; |
| 305 |
case fc_Hardware: |
| 306 |
|
| 307 |
dcb.fOutxCtsFlow = TRUE; |
| 308 |
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; |
| 309 |
break; |
| 310 |
case fc_HardwareDTR: |
| 311 |
|
| 312 |
dcb.fOutxDsrFlow = TRUE; |
| 313 |
dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; |
| 314 |
break; |
| 315 |
default: |
| 316 |
break; |
| 317 |
} |
| 318 |
|
| 319 |
switch (p->py) { |
| 320 |
case parity_nc: |
| 321 |
CloseHandle(p->phandle); |
| 322 |
msec_sleep(100); |
| 323 |
a1loge(p->log, ICOM_SYS, "icoms_set_ser_port: illegal parity setting %d\n",p->py); |
| 324 |
return ICOM_SYS; |
| 325 |
case parity_none: |
| 326 |
dcb.fParity = FALSE; |
| 327 |
dcb.Parity = NOPARITY; |
| 328 |
break; |
| 329 |
case parity_odd: |
| 330 |
dcb.fParity = TRUE; |
| 331 |
dcb.Parity = ODDPARITY; |
| 332 |
break; |
| 333 |
case parity_even: |
| 334 |
dcb.fParity = TRUE; |
| 335 |
dcb.Parity = EVENPARITY; |
| 336 |
break; |
| 337 |
default: |
| 338 |
break; |
| 339 |
} |
| 340 |
|
| 341 |
switch (p->sb) { |
| 342 |
case stop_nc: |
| 343 |
CloseHandle(p->phandle); |
| 344 |
msec_sleep(100); |
| 345 |
a1loge(p->log, ICOM_SYS, "icoms_set_ser_port: illegal stop bits %d\n",p->sb); |
| 346 |
return ICOM_SYS; |
| 347 |
case stop_1: |
| 348 |
dcb.StopBits = ONESTOPBIT; |
| 349 |
break; |
| 350 |
case stop_2: |
| 351 |
dcb.StopBits = TWOSTOPBITS; |
| 352 |
break; |
| 353 |
} |
| 354 |
|
| 355 |
switch (p->wl) { |
| 356 |
case length_nc: |
| 357 |
CloseHandle(p->phandle); |
| 358 |
msec_sleep(100); |
| 359 |
a1loge(p->log, ICOM_SYS, "icoms_set_ser_port: illegal word length %d\n",p->wl); |
| 360 |
return ICOM_SYS; |
| 361 |
case length_5: |
| 362 |
dcb.ByteSize = 5; |
| 363 |
break; |
| 364 |
case length_6: |
| 365 |
dcb.ByteSize = 6; |
| 366 |
break; |
| 367 |
case length_7: |
| 368 |
dcb.ByteSize = 7; |
| 369 |
break; |
| 370 |
case length_8: |
| 371 |
dcb.ByteSize = 8; |
| 372 |
break; |
| 373 |
} |
| 374 |
|
| 375 |
switch (p->br) { |
| 376 |
case baud_110: |
| 377 |
dcb.BaudRate = CBR_110; |
| 378 |
break; |
| 379 |
case baud_300: |
| 380 |
dcb.BaudRate = CBR_300; |
| 381 |
break; |
| 382 |
case baud_600: |
| 383 |
dcb.BaudRate = CBR_600; |
| 384 |
break; |
| 385 |
case baud_1200: |
| 386 |
dcb.BaudRate = CBR_1200; |
| 387 |
break; |
| 388 |
case baud_2400: |
| 389 |
dcb.BaudRate = CBR_2400; |
| 390 |
break; |
| 391 |
case baud_4800: |
| 392 |
dcb.BaudRate = CBR_4800; |
| 393 |
break; |
| 394 |
case baud_9600: |
| 395 |
dcb.BaudRate = CBR_9600; |
| 396 |
break; |
| 397 |
case baud_14400: |
| 398 |
dcb.BaudRate = CBR_14400; |
| 399 |
break; |
| 400 |
case baud_19200: |
| 401 |
dcb.BaudRate = CBR_19200; |
| 402 |
break; |
| 403 |
case baud_38400: |
| 404 |
dcb.BaudRate = CBR_38400; |
| 405 |
break; |
| 406 |
case baud_57600: |
| 407 |
dcb.BaudRate = CBR_57600; |
| 408 |
break; |
| 409 |
case baud_115200: |
| 410 |
dcb.BaudRate = CBR_115200; |
| 411 |
break; |
| 412 |
case baud_230400: |
| 413 |
dcb.BaudRate = CBR_230400; |
| 414 |
break; |
| 415 |
case baud_921600: |
| 416 |
dcb.BaudRate = CBR_921600; |
| 417 |
break; |
| 418 |
default: |
| 419 |
CloseHandle(p->phandle); |
| 420 |
msec_sleep(100); |
| 421 |
a1loge(p->log, ICOM_SYS, "icoms_set_ser_port: illegal baud rate! (0x%x)\n",p->br); |
| 422 |
return ICOM_SYS; |
| 423 |
} |
| 424 |
|
| 425 |
PurgeComm(p->phandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR); |
| 426 |
|
| 427 |
if (!SetCommState(p->phandle, &dcb)) { |
| 428 |
CloseHandle(p->phandle); |
| 429 |
msec_sleep(100); |
| 430 |
a1loge(p->log, ICOM_SYS, "icoms_set_ser_port: SetCommState failed with LastError %d\n", |
| 431 |
GetLastError()); |
| 432 |
return ICOM_SYS; |
| 433 |
} |
| 434 |
|
| 435 |
PurgeComm(p->phandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR); |
| 436 |
|
| 437 |
msec_sleep(50); |
| 438 |
|
| 439 |
p->write = icoms_ser_write; |
| 440 |
p->read = icoms_ser_read; |
| 441 |
p->ser_clearerr = nt_ser_clearerr; |
| 442 |
|
| 443 |
} |
| 444 |
a1logd(p->log, 8, "icoms_set_ser_port: port characteristics set ok\n"); |
| 445 |
|
| 446 |
return ICOM_OK; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
static int |
| 453 |
icoms_set_ser_port( |
| 454 |
icoms *p, |
| 455 |
flow_control fc, |
| 456 |
baud_rate baud, |
| 457 |
parity parity, |
| 458 |
stop_bits stop, |
| 459 |
word_length word) |
| 460 |
{ |
| 461 |
return icoms_set_ser_port_ex(p, fc, baud, parity, stop, word, 0); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
int |
| 473 |
icoms_ser_write( |
| 474 |
icoms *p, |
| 475 |
char *wbuf, |
| 476 |
int nwch, |
| 477 |
double tout) |
| 478 |
{ |
| 479 |
COMMTIMEOUTS tmo; |
| 480 |
DWORD wbytes; |
| 481 |
int len; |
| 482 |
long ttop, top; |
| 483 |
unsigned int stime, etime; |
| 484 |
int retrv = ICOM_OK; |
| 485 |
|
| 486 |
a1logd(p->log, 8, "\nicoms_ser_write: writing '%s'\n", |
| 487 |
nwch > 0 ? icoms_tohex(wbuf, nwch) : icoms_fix(wbuf)); |
| 488 |
|
| 489 |
if (!p->is_open) { |
| 490 |
a1loge(p->log, ICOM_SYS, "icoms_ser_write: device not initialised\n"); |
| 491 |
p->lserr = ICOM_SYS; |
| 492 |
return p->lserr; |
| 493 |
} |
| 494 |
|
| 495 |
if (nwch != 0) |
| 496 |
len = nwch; |
| 497 |
else |
| 498 |
len = strlen(wbuf); |
| 499 |
|
| 500 |
ttop = (int)(tout * 1000.0 + 0.5); |
| 501 |
|
| 502 |
a1logd(p->log, 8, "\nicoms_ser_write: ep 0x%x, bytes %d, ttop %d, quant %d\n", p->rd_ep, len, ttop, p->rd_qa); |
| 503 |
|
| 504 |
|
| 505 |
tmo.ReadIntervalTimeout = 0; |
| 506 |
tmo.ReadTotalTimeoutMultiplier = 0; |
| 507 |
tmo.ReadTotalTimeoutConstant = ttop; |
| 508 |
tmo.WriteTotalTimeoutMultiplier = 0; |
| 509 |
tmo.WriteTotalTimeoutConstant = ttop; |
| 510 |
if (!SetCommTimeouts(p->phandle, &tmo)) { |
| 511 |
a1loge(p->log, ICOM_SYS, "icoms_ser_write: SetCommTimeouts failed with %d\n",GetLastError()); |
| 512 |
p->lserr = ICOM_SYS; |
| 513 |
return p->lserr; |
| 514 |
} |
| 515 |
|
| 516 |
etime = stime = msec_time(); |
| 517 |
|
| 518 |
|
| 519 |
for (top = ttop; top > 0 && len > 0;) { |
| 520 |
int rv; |
| 521 |
rv = WriteFile(p->phandle, wbuf, len, &wbytes, NULL); |
| 522 |
etime = msec_time(); |
| 523 |
|
| 524 |
if (wbytes > 0) { |
| 525 |
a1logd(p->log, 8, "icoms_ser_write: wrote %d bytes\n",wbytes); |
| 526 |
wbuf += wbytes; |
| 527 |
len -= wbytes; |
| 528 |
} |
| 529 |
if (rv == 0) { |
| 530 |
DWORD errs; |
| 531 |
if (!ClearCommError(p->phandle,&errs,NULL)) |
| 532 |
warning("icoms_ser_write: failed, and Clear error failed in %s at %d",__FILE__,__LINE__); |
| 533 |
if (errs & CE_BREAK) |
| 534 |
retrv |= ICOM_BRK; |
| 535 |
if (errs & CE_FRAME) |
| 536 |
retrv |= ICOM_FER; |
| 537 |
if (errs & CE_RXPARITY) |
| 538 |
retrv |= ICOM_PER; |
| 539 |
if (errs & CE_RXOVER) |
| 540 |
retrv |= ICOM_OER; |
| 541 |
a1logd(p->log, 8, "icoms_ser_write: read failed with 0x%x\n",rv); |
| 542 |
break; |
| 543 |
} |
| 544 |
|
| 545 |
top = ttop - (etime - stime); |
| 546 |
} |
| 547 |
if (top <= 0) { |
| 548 |
a1logd(p->log, 8, "icoms_ser_write: timeout, took %d msec out of %d\n",etime - stime,ttop); |
| 549 |
retrv |= ICOM_TO; |
| 550 |
} |
| 551 |
|
| 552 |
a1logd(p->log, 8, "icoms_ser_write: took %d msec, returning ICOM err 0x%x\n",etime - stime,retrv); |
| 553 |
|
| 554 |
p->lserr = retrv; |
| 555 |
return p->lserr; |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
int |
| 562 |
icoms_ser_read( |
| 563 |
icoms *p, |
| 564 |
char *rbuf, |
| 565 |
int bsize, |
| 566 |
int *pbread, |
| 567 |
char *tc, |
| 568 |
int ntc, |
| 569 |
double tout |
| 570 |
) { |
| 571 |
COMMTIMEOUTS tmo; |
| 572 |
DWORD rbytes; |
| 573 |
int j; |
| 574 |
long ttop, top; |
| 575 |
unsigned int stime, etime; |
| 576 |
char *rrbuf = rbuf; |
| 577 |
int retrv = ICOM_OK; |
| 578 |
int nreads; |
| 579 |
|
| 580 |
if (p->phandle == NULL) { |
| 581 |
a1loge(p->log, ICOM_SYS, "icoms_ser_read: device not initialised\n"); |
| 582 |
p->lserr = ICOM_SYS; |
| 583 |
return p->lserr; |
| 584 |
} |
| 585 |
|
| 586 |
if (bsize < 3) { |
| 587 |
a1loge(p->log, ICOM_SYS, "icoms_ser_read: given too small a buffer (%d)\n",bsize); |
| 588 |
p->lserr = ICOM_SYS; |
| 589 |
return p->lserr; |
| 590 |
} |
| 591 |
|
| 592 |
for (j = 0; j < bsize; j++) |
| 593 |
rbuf[j] = 0; |
| 594 |
|
| 595 |
ttop = (int)(tout * 1000.0 + 0.5); |
| 596 |
|
| 597 |
a1logd(p->log, 8, "\nicoms_ser_read: bytes %d, ttop %d, ntc %d\n", bsize, ttop, ntc); |
| 598 |
|
| 599 |
|
| 600 |
tmo.ReadIntervalTimeout = 20; |
| 601 |
tmo.ReadTotalTimeoutMultiplier = 0; |
| 602 |
tmo.ReadTotalTimeoutConstant = ttop; |
| 603 |
tmo.WriteTotalTimeoutMultiplier = 0; |
| 604 |
tmo.WriteTotalTimeoutConstant = ttop; |
| 605 |
if (!SetCommTimeouts(p->phandle, &tmo)) { |
| 606 |
a1loge(p->log, ICOM_SYS, "icoms_ser_read: SetCommTimeouts failed with %d\n",GetLastError()); |
| 607 |
p->lserr = ICOM_SYS; |
| 608 |
return p->lserr; |
| 609 |
} |
| 610 |
|
| 611 |
bsize -= 1; |
| 612 |
|
| 613 |
|
| 614 |
etime = stime = msec_time(); |
| 615 |
top = ttop; |
| 616 |
j = (tc == NULL && ntc <= 0) ? -1 : 0; |
| 617 |
|
| 618 |
|
| 619 |
for (nreads = 0; top > 0 && bsize > 0 && j < ntc ;) { |
| 620 |
int rv; |
| 621 |
rv = ReadFile(p->phandle, rbuf, bsize, &rbytes, NULL); |
| 622 |
etime = msec_time(); |
| 623 |
nreads++; |
| 624 |
|
| 625 |
if (rbytes > 0) { |
| 626 |
|
| 627 |
a1logd(p->log, 8, "icoms_ser_read: read %d bytes, rbuf = '%s'\n",rbytes,icoms_fix(rrbuf)); |
| 628 |
|
| 629 |
bsize -= rbytes; |
| 630 |
if (tc != NULL) { |
| 631 |
while(rbytes--) { |
| 632 |
char ch = *rbuf++, *tcp = tc; |
| 633 |
|
| 634 |
while(*tcp != '\000') { |
| 635 |
if (ch == *tcp) |
| 636 |
j++; |
| 637 |
tcp++; |
| 638 |
} |
| 639 |
} |
| 640 |
a1logd(p->log, 8, "icoms_ser_read: tc count %d\n",j); |
| 641 |
} else { |
| 642 |
if (ntc > 0) |
| 643 |
j += rbytes; |
| 644 |
rbuf += rbytes; |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
if (rv == 0) { |
| 650 |
DWORD errs; |
| 651 |
if (!ClearCommError(p->phandle,&errs,NULL)) |
| 652 |
warning("icoms_ser_read: failed, and Clear error failed in %s at %d",__FILE__,__LINE__); |
| 653 |
if (errs & CE_BREAK) |
| 654 |
retrv |= ICOM_BRK; |
| 655 |
if (errs & CE_FRAME) |
| 656 |
retrv |= ICOM_FER; |
| 657 |
if (errs & CE_RXPARITY) |
| 658 |
retrv |= ICOM_PER; |
| 659 |
if (errs & CE_RXOVER) |
| 660 |
retrv |= ICOM_OER; |
| 661 |
a1logd(p->log, 8, "icoms_ser_read: read failed with 0x%x, rbuf = '%s'\n",rv,icoms_fix(rrbuf)); |
| 662 |
break; |
| 663 |
} |
| 664 |
top = ttop - (etime - stime); |
| 665 |
} |
| 666 |
|
| 667 |
*rbuf = '\000'; |
| 668 |
a1logd(p->log, 8, "icoms_ser_read: read %d total bytes with %d reads\n",rbuf - rrbuf, nreads); |
| 669 |
if (pbread != NULL) |
| 670 |
*pbread = (rbuf - rrbuf); |
| 671 |
|
| 672 |
|
| 673 |
a1logd(p->log, 8, "icoms_ser_read: took %d msec\n",etime - stime); |
| 674 |
if (top <= 0 && bsize > 0 && j < ntc) { |
| 675 |
a1logd(p->log, 8, "icoms_ser_read: timeout, took %d msec out of %d\n",etime - stime,ttop); |
| 676 |
retrv |= ICOM_TO; |
| 677 |
} |
| 678 |
|
| 679 |
a1logd(p->log, 8, "icoms_ser_read: took %d msec, returning '%s' ICOM err 0x%x\n", |
| 680 |
etime - stime, tc == NULL && ntc > 0 ? icoms_tohex(rrbuf, rbuf - rrbuf) |
| 681 |
: icoms_fix(rrbuf), retrv); |
| 682 |
|
| 683 |
p->lserr = retrv; |
| 684 |
return p->lserr; |
| 685 |
} |
| 686 |
|
| 687 |
#endif |
| 688 |
|