summaryrefslogtreecommitdiff
path: root/spectro/icoms_nt.c
blob: 3caa8e0aafac2fe7f9c342a0c5af658263efd7e6 (plain)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737

 /* Windows NT serial I/O class */

/* 
 * Argyll Color Correction System
 *
 * Author: Graeme W. Gill
 * Date:   28/9/97
 *
 * Copyright 1997 - 2013 Graeme W. Gill
 * All rights reserved.
 *
 * This material is licenced under the GNU GENERAL PUBLIC LICENSE Version 2 or later :-
 * see the License2.txt file for licencing details.
 */

#include <conio.h>

/* Link list element to hold fast_serial port names */
typedef struct fast_com_name {
	char name[100];
	struct fast_com_name *next;
} fast_com_name;

#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL)
instType fast_ser_inst_type(icoms *p, int tryhard, void *, void *); 
#endif /* ENABLE_SERIAL */

/* Create and return a list of available serial ports or USB instruments for this system. */
/* We look at the registry key "HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM" */
/* to determine serial ports, and use libusb to discover USB instruments. */
/* Create and return a list of available serial ports or USB instruments for this system */
/* return icom error */
int icompaths_refresh_paths(icompaths *p) {
	int rv, usbend = 0;
	int i, j;
	LONG stat;
	HKEY sch;		/* Serial coms handle */
	fast_com_name *fastlist = NULL, *fn, *fn2;

	a1logd(p->log, 8, "icoms_get_paths: called\n");

	/* Clear any existing paths */
	p->clear(p);

#ifdef ENABLE_USB
	if ((rv = hid_get_paths(p)) != ICOM_OK)
		return rv;
	if ((rv = usb_get_paths(p)) != ICOM_OK)
		return rv;
#endif /* ENABLE_USB */
	usbend = p->npaths;

	a1logd(p->log, 6, "icoms_get_paths: got %d paths, looking up the registry for serial ports\n",p->npaths);

#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL)
	// (Beware KEY_WOW64_64KEY ?)

	/* See if there are and FTDI fast_serial ports, and make a list of them */
	if ((stat = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Enum\\FTDIBUS",
	                         0, KEY_READ, &sch)) != ERROR_SUCCESS) {
		a1logd(p->log, 1, "icoms_get_paths: There don't appear to be any FTDI serial ports\n");
	} else {

#define MXKSIZE 500
#define MXVSIZE 300

		a1logd(p->log, 6, "icoms_get_paths: looking through FTDI ports\n");
		for (i = 0; ; i++) {
			char ftdiname[MXKSIZE];
			DWORD ftdisize = MXKSIZE;
			HKEY devkey;

			stat = RegEnumKeyEx(
				sch,		/* handle to key to enumerate */
				i,			/* index of subkey to enumerate */
				ftdiname,    /* address of buffer for value name */
				&ftdisize,	/* address for size of value name buffer */
				NULL,		/* reserved */
				NULL,		/* Address of value type */
				NULL,		/* Address of value buffer */
				NULL		/* Address of value buffer size */
			);
			if (stat == ERROR_NO_MORE_ITEMS) {
				a1logd(p->log, 9, "icoms_get_paths: got ERROR_NO_MORE_ITEMS\n");
				break;
			}
			if (stat == ERROR_MORE_DATA		/* Hmm. Should expand buffer size */
			 || stat != ERROR_SUCCESS) {
				a1logw(p->log, "icoms_get_paths: RegEnumValue failed with %d\n",stat);
				break;
			}
			ftdiname[MXKSIZE-1] = '\000';

			/* Enumerate subkeys, looking for Device Parameters/PortName */
			if ((stat = RegOpenKeyEx(sch, ftdiname, 0, KEY_READ, &devkey)) != ERROR_SUCCESS) {
				a1logw(p->log, "icoms_get_paths: OpenKey '%s' failed with %d\n",ftdiname,stat);
				continue;
			}
			
			a1logd(p->log, 6, "icoms_get_paths: looking through '%s'\n",ftdiname);

			for (j = 0; ; j++) {
				char skname[MXKSIZE + 50];		/* Allow for cat of "\Device Parameters" */
				DWORD sksize = MXKSIZE;
				HKEY skkey;
				DWORD vtype;
				char value[MXVSIZE];
				DWORD vsize = MXVSIZE;
	
				stat = RegEnumKeyEx(
					devkey,		/* handle to key to enumerate */
					j,			/* index of subkey to enumerate */
					skname,		/* address of buffer for value name */
					&sksize,	/* address for size of value name buffer */
					NULL,		/* reserved */
					NULL,		/* Address of value type */
					NULL,		/* Address of value buffer */
					NULL		/* Address of value buffer size */
				);
				if (stat == ERROR_NO_MORE_ITEMS) {
					a1logd(p->log, 9, "icoms_get_paths: got ERROR_NO_MORE_ITEMS\n");
					break;
				}
				if (stat == ERROR_MORE_DATA		/* Hmm. Should expand buffer size */
				 || stat != ERROR_SUCCESS) {
					a1logw(p->log, "icoms_get_paths: RegEnumValue failed with %d\n",stat);
					break;
				}
				skname[MXKSIZE-1] = '\000';
	
				/* See if there is a Device Parameters\PortName */
				strcat(skname, "\\Device Parameters"); 

				if ((stat = RegOpenKeyEx(devkey, skname, 0, KEY_READ, &skkey)) != ERROR_SUCCESS) {
					a1logw(p->log, "icoms_get_paths: OpenKey '%s' failed with %d\n",skname,stat);
					continue;
				}
				stat = RegQueryValueEx(
					skkey,		/* handle to key to enumerate */
					"PortName",		/* address of buffer for value name */
					NULL,		/* reserved */
					&vtype,		/* Address of value type */
					value,		/* Address of value buffer */
					&vsize		/* Address of value buffer size */
				);
				RegCloseKey(skkey);

				if (stat == ERROR_MORE_DATA		/* Hmm. Should expand buffer size */
				 || stat != ERROR_SUCCESS) {
					a1logw(p->log, "icoms_get_paths: RegQueryValueEx '%s' failed with %d\n",skname,stat);
					break;
				}
				if (vtype != REG_SZ) {
					a1logw(p->log, "icoms_get_paths: RegEnumValue '%s' didn't return stringz type\n",skname);
					continue;
				}
				value[MXVSIZE-1] = '\000';

				if ((fn = malloc(sizeof(fast_com_name))) == NULL) {
					a1loge(p->log, 1, "icoms_get_paths: malloc failed\n");
					continue;
				}
				strcpy(fn->name, value);
				fn->next = fastlist;
				fastlist = fn;
				a1logd(p->log, 2, "icoms_get_paths: got FTDI port '%s'\n",value);
		
			}
			if ((stat = RegCloseKey(devkey)) != ERROR_SUCCESS) {
				a1logw(p->log, "icoms_get_paths: RegCloseKey failed with %d\n",stat);
			}

		}
		if ((stat = RegCloseKey(sch)) != ERROR_SUCCESS) {
			a1logw(p->log, "icoms_get_paths: RegCloseKey failed with %d\n",stat);
		}
	}

	
	/* Look in the registry for serial ports */
	if ((stat = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM",
	                         0, KEY_READ, &sch)) != ERROR_SUCCESS) {
		a1logd(p->log, 1, "icoms_get_paths: There don't appear to be any serial ports\n");
		return ICOM_OK;			/* Maybe they have USB ports */
	}

	/* Look at all the values in this key */
	a1logd(p->log, 8, "icoms_get_paths: looking through all the values in the SERIALCOMM key\n");

	for (i = 0; ; i++) {
		char valname[MXKSIZE];
		DWORD vnsize = MXKSIZE;
		DWORD vtype;
		char value[MXVSIZE];
		DWORD vsize = MXVSIZE;
		int fast = 0;

		stat = RegEnumValue(
			sch,		/* handle to key to enumerate */
			i,			/* index of subkey to enumerate */
			valname,    /* address of buffer for value name */
			&vnsize,	/* address for size of value name buffer */
			NULL,		/* reserved */
			&vtype,		/* Address of value type */
			value,		/* Address of value buffer */
			&vsize		/* Address of value buffer size */
		);
		if (stat == ERROR_NO_MORE_ITEMS) {
			a1logd(p->log, 8, "icoms_get_paths: got ERROR_NO_MORE_ITEMS\n");
			break;
		}
		if (stat == ERROR_MORE_DATA		/* Hmm. Should expand buffer size */
		 || stat != ERROR_SUCCESS) {
			a1logw(p->log, "icoms_get_paths: RegEnumValue failed with %d\n",stat);
			break;
		}
		valname[MXKSIZE-1] = '\000';
		value[MXVSIZE-1] = '\000';

		if (vtype != REG_SZ) {
			a1logw(p->log, "icoms_get_paths: RegEnumValue didn't return stringz type\n");
			continue;
		}

		/* Check if it's a fast serial port */
		for (fn = fastlist; fn != NULL; fn = fn->next) {
			if (strcmp(fn->name, value) == 0)
				fast = 1;
		}

		/* Add the port to the list */
		p->add_serial(p, value, value, fast);
		a1logd(p->log, 8, "icoms_get_paths: Added path '%s' fast %d\n",value,fast);

		/* If fast, try and identify it */
		if (fast) {
			icompath *path;
			icoms *icom;
			if ((path = p->get_last_path(p)) != NULL
			 && (icom = new_icoms(path, p->log)) != NULL) {
				instType itype = fast_ser_inst_type(icom, 0, NULL, NULL);
				if (itype != instUnknown)
					icompaths_set_serial_itype(path, itype);
				icom->del(icom);
			}
		}
	}
	if ((stat = RegCloseKey(sch)) != ERROR_SUCCESS) {
		a1logw(p->log, "icoms_get_paths: RegCloseKey failed with %d\n",stat);
	}
#endif /* ENABLE_SERIAL */

	/* Sort the COM keys so people don't get confused... */
	/* Sort identified instruments ahead of unknown serial ports */
	a1logd(p->log, 6, "icoms_get_paths: we now have %d entries and are about to sort them\n",p->npaths);
	for (i = usbend; i < (p->npaths-1); i++) {
		for (j = i+1; j < p->npaths; j++) {
			if ((p->paths[i]->itype == instUnknown && p->paths[j]->itype != instUnknown)
			 || (((p->paths[i]->itype == instUnknown && p->paths[j]->itype == instUnknown)
			   || (p->paths[i]->itype != instUnknown && p->paths[j]->itype != instUnknown))
			  && strcmp(p->paths[i]->name, p->paths[j]->name) > 0)) {
				icompath *tt = p->paths[i];
				p->paths[i] = p->paths[j];
				p->paths[j] = tt;
			}
		}
	}

	a1logd(p->log, 8, "icoms_get_paths: returning %d paths and ICOM_OK\n",p->npaths);

	/* Free fast list */
	for (fn = fastlist; fn != NULL; fn = fn2) {
		fn2 = fn->next;
		free(fn);
	}

	return ICOM_OK;
}


/* Close the port */
static void icoms_close_port(icoms *p) {
	if (p->is_open) {
#ifdef ENABLE_USB
		if (p->usbd) {
			usb_close_port(p);
		} else if (p->hidd) {
			hid_close_port(p);
		}
#endif
#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL)
		if (p->phandle != NULL) {
			CloseHandle(p->phandle);
		}
#endif /* ENABLE_SERIAL */
		p->is_open = 0;
	}
}

#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL)

static int icoms_ser_write(icoms *p, char *wbuf, double tout);
static int icoms_ser_read(icoms *p, char *rbuf, int bsize, char *tc, int ntc, double tout);

#ifndef CBR_921600
# define CBR_921600 921600
#endif

/* Set the serial port characteristics */
/* This always re-opens the port */
/* return an icom error */
static int
icoms_set_ser_port(
icoms *p, 
flow_control fc,
baud_rate	 baud,
parity		 parity,
stop_bits	 stop,
word_length	 word)
{

	a1logd(p->log, 8, "icoms_set_ser_port: About to set port characteristics:\n"
		              "       Port name = %s\n"
		              "       Flow control = %d\n"
		              "       Baud Rate = %d\n"
		              "       Parity = %d\n"
		              "       Stop bits = %d\n"
		              "       Word length = %d\n"
		              ,p->name ,fc ,baud ,parity ,stop ,word);

	if (p->is_open)
		p->close_port(p);

	if (p->port_type(p) == icomt_serial) {
		DCB dcb;

		a1logd(p->log, 8, "icoms_set_ser_port: Make sure serial port is open\n");

		if (fc != fc_nc)
			p->fc = fc;
		if (baud != baud_nc)
			p->br = baud;
		if (parity != parity_nc)
			p->py = parity;
		if (stop != stop_nc)
			p->sb = stop;
		if (word != length_nc)
			p->wl = word;

		/* Make sure the port is open */
		if (!p->is_open) {
			char buf[50];	/* Temporary for COM device path */

			a1logd(p->log, 8, "icoms_set_ser_port: about to open serial port '%s'\n",p->spath);

			/* Workaround bug of ports > COM9 */
			sprintf(buf, "\\\\.\\%s", p->spath);

			if ((p->phandle = CreateFile(
				buf,
				GENERIC_READ|GENERIC_WRITE,
				0,				/* Exclusive access */
				NULL,			/* No security */
				OPEN_EXISTING,	/* Does not make sense to create */
				0,				/* No overlapped I/O */
				NULL)			/* NULL template */
			) == INVALID_HANDLE_VALUE) {
				a1logd(p->log, 1, "icoms_set_ser_port: open port '%s' failed with LastError %d\n",buf,GetLastError());
				return ICOM_SYS;
			}
			p->is_open = 1;
		}

		if (GetCommState(p->phandle, &dcb) == FALSE) {
			CloseHandle(p->phandle);
			a1loge(p->log, ICOM_SYS, "icoms_set_ser_port: reading state '%s' failed with LastError %d\n",p->spath,GetLastError());
			return ICOM_SYS;
		}

		/* Set misc stuff to default */
		dcb.fBinary = TRUE; 						/* Binary mode is only one that works */
		dcb.fOutxCtsFlow = FALSE;					/* Not using Cts flow control */
		dcb.fOutxDsrFlow = FALSE;					/* Not using Dsr Flow control */
		dcb.fDtrControl = DTR_CONTROL_ENABLE;		/* Enable DTR during connection */
		dcb.fDsrSensitivity = FALSE;				/* Not using Dsr Flow control */
		dcb.fTXContinueOnXoff = TRUE;				/* */
		dcb.fOutX = FALSE;							/* No default Xon/Xoff flow control */
		dcb.fInX = FALSE;							/* No default Xon/Xoff flow control */
		dcb.fErrorChar = FALSE;
		dcb.fNull = FALSE;
		dcb.fRtsControl = RTS_CONTROL_ENABLE;		/* Turn RTS on during connection */
//		dcb.fAbortOnError = TRUE;					// Hmm. Stuffs up FTDI. Is it needed ?
		dcb.fAbortOnError = FALSE;

		switch (p->fc) {
			case fc_nc:
				CloseHandle(p->phandle);
				a1loge(p->log, ICOM_SYS, "icoms_set_ser_port: illegal flow control %d\n",p->fc);
				return ICOM_SYS;
			case fc_XonXOff:
				/* Use Xon/Xoff bi-directional flow control */
				dcb.fOutX = TRUE;
				dcb.fInX = TRUE;
				dcb.XonChar = 0x11;					/* ^Q */
				dcb.XoffChar = 0x13;				/* ^S */
				// dcb.fTXContinueOnXoff = FALSE;		/* Stop transmitting if input is full */
				break;
			case fc_Hardware:
				/* Use RTS/CTS bi-directional flow control */
				dcb.fOutxCtsFlow = TRUE;
				dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; 
				break;
			default:
				break;
		}

		switch (p->py) {
			case parity_nc:
				CloseHandle(p->phandle);
				a1loge(p->log, ICOM_SYS, "icoms_set_ser_port: illegal parity setting %d\n",p->py);
				return ICOM_SYS;
			case parity_none:
				dcb.fParity = FALSE;
				dcb.Parity = NOPARITY;
				break;
			case parity_odd:
				dcb.fParity = TRUE;
				dcb.Parity = ODDPARITY;
				break;
			case parity_even:
				dcb.fParity = TRUE;
				dcb.Parity = EVENPARITY;
				break;
			default:
				break;
		}

		switch (p->sb) {
			case stop_nc:
				CloseHandle(p->phandle);
				a1loge(p->log, ICOM_SYS, "icoms_set_ser_port: illegal stop bits %d\n",p->sb);
				return ICOM_SYS;
			case stop_1:
				dcb.StopBits = ONESTOPBIT;
				break;
			case stop_2:
				dcb.StopBits = TWOSTOPBITS;
				break;
		}

		switch (p->wl) {
			case length_nc:
				CloseHandle(p->phandle);
				a1loge(p->log, ICOM_SYS, "icoms_set_ser_port: illegal word length %d\n",p->wl);
				return ICOM_SYS;
			case length_5:
				dcb.ByteSize = 5;
				break;
			case length_6:
				dcb.ByteSize = 6;
				break;
			case length_7:
				dcb.ByteSize = 7;
				break;
			case length_8:
				dcb.ByteSize = 8;
				break;
		}

		switch (p->br) {
			case baud_110:
				dcb.BaudRate = CBR_110;
				break;
			case baud_300:
				dcb.BaudRate = CBR_300;
				break;
			case baud_600:
				dcb.BaudRate = CBR_600;
				break;
			case baud_1200:
				dcb.BaudRate = CBR_1200;
				break;
			case baud_2400:
				dcb.BaudRate = CBR_2400;
				break;
			case baud_4800:
				dcb.BaudRate = CBR_4800;
				break;
			case baud_9600:
				dcb.BaudRate = CBR_9600;
				break;
			case baud_14400:
				dcb.BaudRate = CBR_14400;
				break;
			case baud_19200:
				dcb.BaudRate = CBR_19200;
				break;
			case baud_38400:
				dcb.BaudRate = CBR_38400;
				break;
			case baud_57600:
				dcb.BaudRate = CBR_57600;
				break;
			case baud_115200:
				dcb.BaudRate = CBR_115200;
				break;
			case baud_921600:
				dcb.BaudRate = CBR_921600;
				break;
			default:
				CloseHandle(p->phandle);
				a1loge(p->log, ICOM_SYS, "icoms_set_ser_port: illegal baud rate! (0x%x)\n",p->br);
				return ICOM_SYS;
		}

		PurgeComm(p->phandle, PURGE_TXCLEAR | PURGE_RXCLEAR);

		if (!SetCommState(p->phandle, &dcb)) {
			CloseHandle(p->phandle);
			a1loge(p->log, ICOM_SYS, "icoms_set_ser_port: SetCommState failed with LastError %d\n",
			                                                                   GetLastError());
			return ICOM_SYS;
		}

		PurgeComm(p->phandle, PURGE_TXCLEAR | PURGE_RXCLEAR);

		p->write = icoms_ser_write;
		p->read = icoms_ser_read;

	}
	a1logd(p->log, 8, "icoms_set_ser_port: port characteristics set ok\n");

	return ICOM_OK;
}

/* ---------------------------------------------------------------------------------*/
/* Serial write/read */

/* Write the characters in the buffer out */
/* Data will be written up to the terminating nul */
/* Return relevant error status bits */
/* Set the icoms lserr value */
static int
icoms_ser_write(
icoms *p,
char *wbuf,
double tout)
{
	COMMTIMEOUTS tmo;
	DWORD wbytes;
	int len;
	long toc, i, top;		/* Timout count, counter, timeout period */
	int rv = ICOM_OK;

	a1logd(p->log, 8, "icoms_ser_write: About to write '%s' ",icoms_fix(wbuf));
	if (!p->is_open) {
		a1loge(p->log, ICOM_SYS, "icoms_ser_write: device not initialised\n");
		p->lserr = rv = ICOM_SYS;
		return rv;
	}

	len = strlen(wbuf);
	tout *= 1000.0;		/* Timout in msec */

	top = 20;						/* Timeout period in msecs */
	toc = (int)(tout/top + 0.5);	/* Number of timout periods in timeout */
	if (toc < 1)
		toc = 1;

	/* Set the timout value */
	tmo.ReadIntervalTimeout = top;
	tmo.ReadTotalTimeoutMultiplier = 0;
	tmo.ReadTotalTimeoutConstant = top;
	tmo.WriteTotalTimeoutMultiplier = 0;
	tmo.WriteTotalTimeoutConstant = top;
	if (!SetCommTimeouts(p->phandle, &tmo)) {
		a1loge(p->log, ICOM_SYS, "icoms_ser_write: SetCommTimeouts failed with %d\n",GetLastError());
		p->lserr = rv = ICOM_SYS;
		return rv;
	}

	/* Until data is all written or we time out */
	for (i = toc; i > 0 && len > 0;) {
		if (!WriteFile(p->phandle, wbuf, len, &wbytes, NULL)) {
			DWORD errs;
			if (!ClearCommError(p->phandle,&errs,NULL))
				error("Write to COM port failed, and Clear error failed");
			if (errs & CE_BREAK)
				rv |= ICOM_BRK; 
			if (errs & CE_FRAME)
				rv |= ICOM_FER; 
			if (errs & CE_RXPARITY)
				rv |= ICOM_PER; 
			if (errs & CE_RXOVER)
				rv |= ICOM_OER; 
			break;
		} else if (wbytes == 0) { 
			i--;			/* Timeout */
		} else if (wbytes > 0) { /* Account for bytes done */
			i = toc;
			len -= wbytes;
			wbuf += len;
		}
	}
	if (i <= 0) {		/* Timed out */
		rv |= ICOM_TO;
	}
	a1logd(p->log, 8, "icoms_ser_write: returning ICOM err 0x%x\n",rv);

	p->lserr = rv;
	return rv;
}


/* Read characters into the buffer */
/* Return string will be terminated with a nul */
static int
icoms_ser_read(
icoms *p,
char *rbuf,			/* Buffer to store characters read */
int bsize,			/* Buffer size */
char *tc,			/* Terminating characers, NULL for none */
int ntc,			/* Number of terminating characters seen */
double tout			/* Time out in seconds */
) {
	COMMTIMEOUTS tmo;
	DWORD rbytes;
	int j;
	long toc, i, top;		/* Timout count, counter, timeout period */
	char *rrbuf = rbuf;		/* Start of return buffer */
	DCB dcb;
	int rv = ICOM_OK;

	if (p->phandle == NULL) {
		a1loge(p->log, ICOM_SYS, "icoms_read: device not initialised\n");
		p->lserr = rv = ICOM_SYS;
		return rv;
	}

	if (bsize < 3) {
		a1loge(p->log, ICOM_SYS, "icoms_read: given too small a bufferi (%d)\n",bsize);
		p->lserr = rv = ICOM_SYS;
		return rv;
	}

	tout *= 1000.0;		/* Timout in msec */
	bsize--;	/* Allow space for null */

	top = 20;						/* Timeout period in msecs */
	toc = (int)(tout/top + 0.5);	/* Number of timout periods in timeout */
	if (toc < 1)
		toc = 1;

	/* Set the timout value */
	tmo.ReadIntervalTimeout = top;
	tmo.ReadTotalTimeoutMultiplier = 0;
	tmo.ReadTotalTimeoutConstant = top;
	tmo.WriteTotalTimeoutMultiplier = 0;
	tmo.WriteTotalTimeoutConstant = top;
	if (!SetCommTimeouts(p->phandle, &tmo)) {
		a1loge(p->log, ICOM_SYS, "icoms_ser_read: SetCommTimeouts failed with %d\n",GetLastError());
		p->lserr = rv = ICOM_SYS;
		return rv;
	}

	/* Until data is all read or we time out */
	for (i = toc, j = 0; i > 0 && bsize > 1 && j < ntc ;) {
		if (!ReadFile(p->phandle, rbuf, bsize, &rbytes, NULL)) {
			DWORD errs;
			if (!ClearCommError(p->phandle,&errs,NULL))
				error("Read from COM port failed, and Clear error failed");
			if (errs & CE_BREAK)
				rv |= ICOM_BRK; 
			if (errs & CE_FRAME)
				rv |= ICOM_FER; 
			if (errs & CE_RXPARITY)
				rv |= ICOM_PER; 
			if (errs & CE_RXOVER)
				rv |= ICOM_OER; 
			break;
		} else if (rbytes == 0) { 
			i--;			/* Timeout */
		} else if (rbytes > 0) { /* Account for bytes done */
			i = toc;
			bsize -= rbytes;
			if (tc != NULL) {
				while(rbytes--) {	/* Count termination characters */
					char ch = *rbuf++, *tcp = tc;
					
					while(*tcp != '\000') {
						if (ch == *tcp)
							j++;
						tcp++;
					}
				}
			} else
				rbuf += rbytes;
		}
	}
	if (i <= 0) {			/* timed out */
		rv |= ICOM_TO;
	}
	*rbuf = '\000';
	a1logd(p->log, 8, "icoms_ser_read: returning '%s' ICOM err 0x%x\n",icoms_fix(rrbuf),rv);

	p->lserr = rv;
	return rv;
}

#endif /* ENABLE_SERIAL */
/* ---------------------------------------------------------------------------------*/


/* Destroy ourselves */
static void
icoms_del(icoms *p) {
	a1logd(p->log, 8, "icoms_del: called\n");
	if (p->is_open) {
		a1logd(p->log, 8, "icoms_del: closing port\n");
		p->close_port(p);
	}
#ifdef ENABLE_USB
	usb_del_usb(p);
	hid_del_hid(p);
#endif
#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL)
	if (p->spath != NULL)
		free(p->spath);
#endif
	p->log = del_a1log(p->log);
	if (p->name != NULL)
		free(p->name);
	p->log = del_a1log(p->log);		/* unref */
	free (p);
}