~ubuntu-branches/ubuntu/edgy/rxtx/edgy-201105201527

« back to all changes in this revision

Viewing changes to src/termios.c

  • Committer: Bazaar Package Importer
  • Author(s): Mario Joussen
  • Date: 2006-03-01 18:56:52 UTC
  • mfrom: (1.1.2 upstream) (2.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20060301185652-ri9941gi01goklvz
Tags: 2.1.7-2
Fixed stupid bug in clean target.
(closes: Bug#354859)

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
extern void report_error( char * );
13
13
/*-------------------------------------------------------------------------
14
14
|   rxtx is a native interface to serial ports in java.
15
 
|   Copyright 1997-2002 by Trent Jarvi taj@www.linux.org.uk.
 
15
|   Copyright 1997-2006 by Trent Jarvi taj@www.linux.org.uk.
16
16
|   Copyright 1998-2002 by Wayne roberts wroberts1@home.com
17
17
|
18
18
|   This library is free software; you can redistribute it and/or
52
52
#define SIGIO 0
53
53
 
54
54
int my_errno;
 
55
extern int errno;
55
56
struct termios_list
56
57
{
57
58
        char filename[80];
76
77
};
77
78
struct termios_list *first_tl = NULL;
78
79
 
79
 
/*
80
 
#define DEBUG_SELECT
81
 
*/
82
 
#ifdef DEBUG_SELECT
83
 
void MexPrintf( char *string )
84
 
{
85
 
        mexPrintf(string);
86
 
}
87
 
#else
88
 
void MexPrintf( char *string )
89
 
{
90
 
}
91
 
#endif DEBUG_SELECT
92
 
 
93
80
/*----------------------------------------------------------
94
81
serial_test
95
82
 
106
93
        int ret;
107
94
        hcomm = CreateFile( filename, GENERIC_READ |GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0 );
108
95
        if ( hcomm == INVALID_HANDLE_VALUE )
109
 
                ret = 0;
 
96
        {
 
97
                if (GetLastError() == ERROR_ACCESS_DENIED)
 
98
                {
 
99
                        ret = 1;
 
100
                }
 
101
                else
 
102
                {
 
103
                        ret = 0;
 
104
                }
 
105
        }
110
106
        else
 
107
        {
111
108
                ret = 1;
 
109
        }
112
110
        CloseHandle( hcomm );
113
111
        return(ret);
114
112
}
118
116
        struct termios_list *index = find_port( fd );
119
117
        int i, result;
120
118
        int windows_flags[11] = { 0, EV_RXCHAR, EV_TXEMPTY, EV_CTS, EV_DSR,
121
 
/*
122
 
                                        EV_RING, EV_RLSD, EV_ERR,
123
 
*/
124
119
                                        EV_RING|0x2000, EV_RLSD, EV_ERR,
125
120
                                        EV_ERR, EV_ERR, EV_BREAK
126
121
                                };
127
 
/*
128
 
                                        EV_RING|0x2000, EV_RLSD, EV_ERR,
129
 
                                         NT service pack 4+ breaks 0x2000
130
 
*/
131
 
        if( index == NULL ) report_error("index=null termois_setflags\n");
 
122
        if( !index )
 
123
        {
 
124
                LEAVE( "termios_setflags" );
 
125
                return;
 
126
        }
132
127
        index->event_flag = 0;
133
128
        for(i=0;i<11;i++)
134
129
                if( termios_flags[i] )
157
152
/*----------------------------------------------------------
158
153
get_fd()
159
154
 
160
 
   accept:      
161
 
   perform:     
162
 
   return:      
 
155
   accept:      filename
 
156
   perform:     find the file descriptor associated with the filename
 
157
   return:      fd
163
158
   exceptions:  
164
159
   win32api:    None
165
 
   comments:    
 
160
   comments:    This is not currently called by anything
166
161
----------------------------------------------------------*/
167
162
 
168
163
int get_fd( char *filename )
188
183
/*----------------------------------------------------------
189
184
get_filename()
190
185
 
191
 
   accept:      
192
 
   perform:     
193
 
   return:      
194
 
   exceptions:  
 
186
   accept:      file descriptor
 
187
   perform:     find the filename associated with the file descriptor
 
188
   return:      the filename associated with the fd
 
189
   exceptions:  None
195
190
   win32api:    None
196
 
   comments:    
 
191
   comments:    This is not currently called by anything
197
192
----------------------------------------------------------*/
198
193
 
199
194
char *get_filename( int fd )
216
211
/*----------------------------------------------------------
217
212
dump_termios_list()
218
213
 
219
 
   accept:      
 
214
   accept:      string to print out.
220
215
   perform:     
221
216
   return:      
222
217
   exceptions:  
223
218
   win32api:    None
224
 
   comments:    
 
219
   comments:    used only for debugging eg serial_close()
225
220
----------------------------------------------------------*/
226
221
 
227
222
void dump_termios_list( char *foo )
306
301
                case CBR_2400:          return( B2400 );
307
302
                case CBR_4800:          return( B4800 );
308
303
                case CBR_9600:          return( B9600 );
 
304
                case CBR_14400:         return( B14400 );
309
305
                case CBR_19200:         return( B19200 );
310
306
                case CBR_28800:         return( B28800 );
311
307
                case CBR_38400:         return( B38400 );
312
308
                case CBR_57600:         return( B57600 );
313
309
                case CBR_115200:        return( B115200 );
314
 
                /*  14400, 128000 and 256000 are windows specific but need to
315
 
                 *  work.
316
 
                 *  hosed on my hardware....
317
 
                 */
318
 
                case CBR_14400:         return( B14400 );
319
310
                case CBR_128000:        return( B128000 );
 
311
                case CBR_230400:        return( B230400 );
320
312
                case CBR_256000:        return( B256000 );
321
 
 
322
 
                /*  The following could be used on linux and should be able to
323
 
                 *  work on windows if we get control of baud/divisor.
324
 
                 */
325
 
 
326
 
                case CBR_230400:        return( B230400 );
327
313
                case CBR_460800:        return( B460800 );
328
314
                case CBR_500000:        return( B500000 );
329
315
                case CBR_576000:        return( B576000 );
373
359
                case B2400:     ret = CBR_2400;         break;
374
360
                case B4800:     ret = CBR_4800;         break;
375
361
                case B9600:     ret = CBR_9600;         break;
 
362
                case B14400:    ret = CBR_14400;        break;
376
363
                case B19200:    ret = CBR_19200;        break;
377
364
                case B38400:    ret = CBR_38400;        break;
378
365
                case B57600:    ret = CBR_57600;        break;
379
366
                case B115200:   ret = CBR_115200;       break;
380
 
 
381
 
                /*  14400, 128000 and 256000 are windows specific but need to
382
 
                 *  work.
383
 
                 */
384
 
                case B14400:    ret = CBR_14400;        break;
385
367
                case B128000:   ret = CBR_128000;       break;
386
368
                case B256000:   ret = CBR_256000;       break;
387
 
 
388
 
                /*  The following could be used on linux and should be able to
389
 
                 *  work on windows if we get control of baud/divisor.
390
 
                 */
391
369
                case B230400:   ret = CBR_230400;       break;
392
370
                case B460800:   ret = CBR_460800;       break;
393
371
                case B500000:   ret = CBR_500000;       break;
525
503
                index->sis->frame++;
526
504
                ErrCode &= ~CE_FRAME;
527
505
        }
 
506
#ifdef LIFE_IS_GOOD
 
507
  FIXME OVERRUN is spewing
528
508
        if( ErrCode & CE_OVERRUN )
529
509
        {
530
510
                index->sis->overrun++;
536
516
                index->sis->overrun++;
537
517
                ErrCode &= ~CE_OVERRUN;
538
518
        }
 
519
#endif /* LIFE_IS_GOOD */
539
520
        if( ErrCode & CE_RXPARITY )
540
521
        {
541
522
                index->sis->parity++;
621
602
{
622
603
        struct termios_list *index;
623
604
        /* char message[80]; */
624
 
        MexPrintf("C");
625
605
 
626
 
        ENTER( "close" );
 
606
        ENTER( "serial_close" );
627
607
        if( !first_tl || !first_tl->hComm )
628
608
        {
629
609
                report( "gotit!" );
630
610
                return( 0 );
631
611
        }
632
 
        if ( fd <= 0 )
633
 
        {
634
 
                report( "close -fd" );
635
 
                return 0;
636
 
        }
637
612
        index = find_port( fd );
638
613
        if ( !index )
639
614
        {
640
 
                report( "close !index" );
641
 
/*
642
 
                fprintf( stderr, "No info known about the port being closed %i\n", fd );
643
 
*/
 
615
                LEAVE( "serial_close" );
644
616
                return -1;
645
617
        }
646
618
 
657
629
        }
658
630
        else
659
631
        {
660
 
                sprintf( message, "close():  Invalid Port Reference for %s\n",
 
632
                sprintf( message, "serial_ close():  Invalid Port Reference for %s\n",
661
633
                        index->filename );
662
634
                report( message );
663
635
        }
693
665
                */
694
666
                free( index );
695
667
        }
696
 
/*
697
 
        dump_termios_list( "close" );
698
 
*/
699
 
        LEAVE( "close" );
 
668
        LEAVE( "serial_close" );
700
669
        return 0;
701
670
}
702
671
 
891
860
                YACK();
892
861
                set_errno( EINVAL );
893
862
/*
894
 
                printf( "open failed %s\n", port->filename );
 
863
                printf( "serial_open failed %s\n", port->filename );
895
864
*/
896
865
                return -1;
897
866
        }
930
899
                report( "Could not create write overlapped\n" );
931
900
                goto fail;
932
901
        }
933
 
        LEAVE("open_port" );
 
902
        LEAVE( "open_port" );
934
903
        return( 0 );
935
904
fail:
936
905
        return( -1 );
939
908
/*----------------------------------------------------------
940
909
termios_list()
941
910
 
942
 
   accept:      
943
 
   perform:     
944
 
   return:      
945
 
   exceptions:  
 
911
   accept:       fd which is a fake # for the port assigned when the port
 
912
                 is opened
 
913
   perform:      walk through a double linked list to see if the given
 
914
                 fd is in any of the termios_list members.
 
915
   return:       the termios_list if it is found.
 
916
                 NULL if no matches are found.
 
917
   exceptions:   None
946
918
   win32api:     None
947
919
   comments:    
948
920
----------------------------------------------------------*/
950
922
struct termios_list *find_port( int fd )
951
923
{
952
924
 
 
925
        char message[80];
953
926
        struct termios_list *index = first_tl;
954
927
 
955
 
#ifdef DEBUG_VERBOSE
956
928
        ENTER( "find_port" );
957
 
#endif /* DEBUG_VERBOSE */
958
 
        if( !first_tl )
959
 
        {
960
 
#ifdef DEBUG_VERBOSE
961
 
                LEAVE( "find_port" );
962
 
#endif /* DEBUG_VERBOSE */
963
 
                return NULL;
964
 
        }
 
929
        if ( fd <= 0 || !first_tl ) goto fail;
965
930
 
966
931
        while( index->fd )
967
932
        {
968
933
                if ( index->fd == fd )
969
934
                {
970
 
#ifdef DEBUG_VERBOSE
971
935
                        LEAVE( "find_port" );
972
 
#endif /* DEBUG_VERBOSE */
973
936
                        return index;
974
937
                }
975
938
                if ( !index->next )
976
939
                        break;
977
940
                index = index->next;
978
941
        }
979
 
#ifdef DEBUG_VERBOSE
 
942
fail:
 
943
        sprintf( message, "No info known about the port. %i\n", fd );
 
944
        report( message );
 
945
        set_errno( EBADF );
980
946
        LEAVE( "find_port" );
981
 
#endif /* DEBUG_VERBOSE */
982
947
        return NULL;
983
948
}
984
949
 
1202
1167
        index = add_port( filename );
1203
1168
        if( !index )
1204
1169
        {
1205
 
                report( "open !index\n" );
 
1170
                report( "serial_open !index\n" );
1206
1171
                return( -1 );
1207
1172
        }
1208
1173
        
1210
1175
        index->tx_happened = 0;
1211
1176
        if ( open_port( index ) )
1212
1177
        {
1213
 
                sprintf( message, "open():  Invalid Port Reference for %s\n",
 
1178
                sprintf( message, "serial_open():  Invalid Port Reference for %s\n",
1214
1179
                        filename );
1215
1180
                report( message );
1216
 
                close( index->fd );
 
1181
                serial_close( index->fd );
1217
1182
                return -1;
1218
1183
        }
1219
1184
 
1220
1185
        if( check_port_capabilities( index ) )
1221
1186
        {
1222
1187
                report( "check_port_capabilites!" );
1223
 
                close( index->fd );
 
1188
                serial_close( index->fd );
1224
1189
                return -1;
1225
1190
        }
1226
1191
 
1237
1202
                index->open_flags = 0;
1238
1203
 
1239
1204
 
1240
 
/*
1241
 
        dump_termios_list( "open filename" );
1242
 
*/
1243
1205
        if( !first_tl->hComm )
1244
1206
        {
1245
1207
                sprintf( message, "open():  Invalid Port Reference for %s\n",
1270
1232
{
1271
1233
        unsigned long nBytes;
1272
1234
        struct termios_list *index;
1273
 
        char message[80];
1274
 
        COMSTAT Stat;
 
1235
        /* COMSTAT Stat; */
1275
1236
        int old_flag;
1276
1237
 
1277
 
#ifdef DEBUG_VERBOSE
1278
1238
        ENTER( "serial_write" );
1279
 
#endif /* DEBUG_VERBOSE */
 
1239
 
1280
1240
        if ( fd <= 0 )
 
1241
        {
1281
1242
                return 0;
 
1243
        }
1282
1244
        index = find_port( fd );
1283
1245
        if ( !index )
1284
1246
        {
1285
 
                sprintf( message,
1286
 
                        "No info known about the port. serial_write %i\n", fd );
1287
 
                report( message );
 
1247
                LEAVE( "serial_write");
1288
1248
                return -1;
1289
1249
        }
1290
1250
        old_flag = index->event_flag;
1293
1253
        SetCommMask( index->hComm, index->event_flag );
1294
1254
        index->tx_happened = 1; 
1295
1255
*/
1296
 
        /***** output mode flags (c_oflag) *****/
1297
 
        /* FIXME: OPOST: enable ONLCR, OXTABS & ONOEOT */
1298
 
        /* FIXME: ONLCR: convert newline char to CR & LF */
1299
 
        /* FIXME: OXTABS: convert tabs to spaces */
1300
 
        /* FIXME: ONOEOT: discard ^D (004) */
1301
 
 
1302
 
#ifdef DEBUG_VERBOSE
1303
 
        /* warning Roy Rogers! */
1304
 
        sprintf( message, "===== trying to write %s\n", Str );
1305
 
        report( message );
1306
 
#endif /* DEBUG_VERBOSE */
1307
 
/*
1308
 
        index->tx_happened = 1; 
1309
 
*/
 
1256
        index->wol.Offset = index->wol.OffsetHigh = 0; 
 
1257
        ResetEvent( index->wol.hEvent );
1310
1258
        if ( !WriteFile( index->hComm, Str, length, &nBytes, &index->wol ) )
1311
1259
        {
1312
 
                /* this causes problems just do it async
1313
1260
                WaitForSingleObject( index->wol.hEvent,100 );
1314
 
                */
1315
1261
                if ( GetLastError() != ERROR_IO_PENDING )
1316
1262
                {
1317
 
                        ClearErrors( index, &Stat );
1318
 
                        set_errno(EIO);
 
1263
                        /* ClearErrors( index, &Stat ); */
1319
1264
                        report( "serial_write error\n" );
 
1265
                        /* report("Condition 1 Detected in write()\n"); */
 
1266
                        YACK();
 
1267
                        errno = EIO;
1320
1268
                        nBytes=-1;
1321
1269
                        goto end;
1322
1270
                }
1323
 
                else
 
1271
                /* This is breaking on Win2K, WinXP for some reason */
 
1272
                else while( !GetOverlappedResult( index->hComm, &index->wol,
 
1273
                                        &nBytes, TRUE ) )
1324
1274
                {
1325
 
                        while( !GetOverlappedResult( index->hComm, &index->wol,
1326
 
                                                &nBytes, TRUE ) )
 
1275
                        if ( GetLastError() != ERROR_IO_INCOMPLETE )
1327
1276
                        {
1328
 
                                if ( GetLastError() != ERROR_IO_INCOMPLETE )
1329
 
                                {
1330
 
                                        ClearErrors( index, &Stat );
1331
 
/*
1332
 
                                        usleep(1000);
1333
 
*/
1334
 
                                }
 
1277
                                /* report("Condition 2 Detected in write()\n"); */
 
1278
                                YACK();
 
1279
                                errno = EIO;
 
1280
                                nBytes = -1;
 
1281
                                goto end;
 
1282
                                /* ClearErrors( index, &Stat ); */
1335
1283
                        }
1336
1284
                }
1337
1285
        }
1338
1286
        else
1339
1287
        {
1340
 
                ClearErrors( index, &Stat );
1341
 
                set_errno(EIO);
1342
 
/*
1343
 
                usleep(1000);
1344
 
*/
1345
 
                report( "serial_write bailing!\n" );
1346
 
                return(-1);
1347
 
                
 
1288
                /* Write finished synchronously.  That is ok!
 
1289
                 * I have seen this with USB to Serial
 
1290
                 * devices like TI's.
 
1291
                 */
1348
1292
        }
1349
1293
end:
1350
 
        /* yaya...  This really does help */
1351
1294
        /* FlushFileBuffers( index->hComm ); */
1352
 
        MexPrintf("W");
1353
1295
        index->event_flag |= EV_TXEMPTY;
 
1296
        /* ClearErrors( index, &Stat ); */
1354
1297
        SetCommMask( index->hComm, index->event_flag );
 
1298
        /* ClearErrors( index, &Stat ); */
1355
1299
        index->event_flag = old_flag;
1356
1300
        index->tx_happened = 1; 
1357
 
#ifdef DEBUG
1358
 
/*
1359
 
        sprintf( message, "serial_write: returning %i\n", (int) nBytes );
1360
 
*/
1361
1301
        LEAVE( "serial_write" );
1362
 
#endif /* DEBUG */
1363
1302
        return nBytes;
1364
1303
}
1365
1304
 
1386
1325
        char message[80];
1387
1326
        COMSTAT stat;
1388
1327
        clock_t c;
 
1328
        unsigned char *dest = vb;
1389
1329
        
1390
 
 
1391
1330
        start = GetTickCount();
1392
 
#ifdef DEBUG
1393
1331
        ENTER( "serial_read" );
1394
 
#endif /* DEBUG */
1395
1332
 
1396
1333
        if ( fd <= 0 )
 
1334
        {
1397
1335
                return 0;
 
1336
        }
1398
1337
        index = find_port( fd );
1399
1338
        if ( !index )
1400
1339
        {
1401
 
                sprintf( message, "No info known about the port. read %i\n",
1402
 
                        fd );
1403
 
                report( message );
 
1340
                LEAVE( "serial_read" );
1404
1341
                return -1;
1405
1342
        }
1406
1343
 
1418
1355
                /* pull mucho-cpu here? */
1419
1356
                do {
1420
1357
#ifdef DEBUG_VERBOSE
1421
 
                        report("vmin=0\n");
 
1358
                        report( "vmin=0\n" );
1422
1359
#endif /* DEBUG_VERBOSE */
1423
1360
                        ret = ClearErrors( index, &stat);
1424
1361
/*
1442
1379
                /* VTIME is in units of 0.1 seconds */
1443
1380
 
1444
1381
#ifdef DEBUG_VERBOSE
1445
 
                report("vmin!=0\n");
 
1382
                report( "vmin!=0\n" );
1446
1383
#endif /* DEBUG_VERBOSE */
1447
1384
                vmin = index->ttyset->c_cc[VMIN];
1448
1385
 
1450
1387
                do {
1451
1388
                        error = ClearErrors( index, &stat);
1452
1389
                        usleep(1000);
1453
 
/*
1454
 
                        mexPrintf("%i/n", CLOCKS_PER_SEC/40);
1455
 
*/
1456
1390
                } while ( c > clock() );
1457
1391
 
1458
1392
        }
1459
 
        
1460
1393
 
1461
1394
        total = 0;
1462
 
        MexPrintf("R");
1463
1395
        while ( size > 0 )
1464
1396
        {
1465
1397
                nBytes = 0;
1466
 
                err = ReadFile( index->hComm, vb + total, size, &nBytes, &index->rol ); 
1467
 
                WaitForSingleObject( index->wol.hEvent, INFINITE );
 
1398
                /* ret = ClearErrors( index, &stat); */
 
1399
 
 
1400
                index->rol.Offset = index->rol.OffsetHigh = 0;
 
1401
                ResetEvent( index->rol.hEvent );
 
1402
 
 
1403
                err = ReadFile( index->hComm, dest + total, size, &nBytes, &index->rol ); 
1468
1404
#ifdef DEBUG_VERBOSE
1469
1405
        /* warning Roy Rogers! */
1470
1406
                sprintf(message, " ========== ReadFile = %i %s\n",
1471
 
                        ( int ) nBytes, (char *) vb + total );
 
1407
                        ( int ) nBytes, (char *) dest + total );
1472
1408
                report( message );
1473
1409
#endif /* DEBUG_VERBOSE */
1474
1410
                size -= nBytes;
1479
1415
                        switch ( GetLastError() )
1480
1416
                        {
1481
1417
                                case ERROR_BROKEN_PIPE:
1482
 
                                        report("ERROR_BROKEN_PIPE\n");
 
1418
                                        report( "ERROR_BROKEN_PIPE\n ");
1483
1419
                                        nBytes = 0;
1484
1420
                                        break;
1485
1421
                                case ERROR_MORE_DATA:
1486
1422
/*
1487
1423
                                        usleep(1000);
1488
1424
*/
1489
 
                                        report("ERROR_MORE_DATA\n");
 
1425
                                        report( "ERROR_MORE_DATA\n" );
1490
1426
                                        break;
1491
1427
                                case ERROR_IO_PENDING:
1492
1428
                                        while( ! GetOverlappedResult(
1513
1449
                                                /* we should use -1 for disabled
1514
1450
                                                   timouts */
1515
1451
                                                if ( index->ttyset->c_cc[VTIME] && now-start >= (index->ttyset->c_cc[VTIME]*100)) {
1516
 
                                                        report("TO ");
 
1452
                                                        report( "TO " );
1517
1453
                                                        /* read timeout */
1518
1454
                                                        return total;
1519
1455
                                                }
1523
1459
/*
1524
1460
                                        usleep(1000);
1525
1461
*/
1526
 
                                        report("ERROR_IO_PENDING\n");
 
1462
                                        report( "ERROR_IO_PENDING\n" );
1527
1463
                                        break;
1528
1464
                                default:
1529
1465
/*
1542
1478
                        return( total );
1543
1479
                }
1544
1480
        }
1545
 
#ifdef DEBUG
1546
 
        LEAVE( "serial_read" );
1547
 
#endif /* DEBUG */
1548
 
        return total;
1549
 
}
 
1481
        LEAVE( "serial_read" );
 
1482
        return total;
 
1483
}
 
1484
 
 
1485
#ifdef asdf
 
1486
int serial_read( int fd, void *vb, int size )
 
1487
{
 
1488
        long start, now;
 
1489
        unsigned long nBytes = 0, total = 0, error;
 
1490
        /* unsigned long waiting = 0; */
 
1491
        int err, vmin; 
 
1492
        struct termios_list *index;
 
1493
        char message[80];
 
1494
        COMSTAT Stat;
 
1495
        clock_t c;
 
1496
        unsigned char *dest = vb;
 
1497
        
 
1498
        start = GetTickCount();
 
1499
        ENTER( "serial_read" );
 
1500
 
 
1501
        if ( fd <= 0 )
 
1502
        {
 
1503
                printf("1\n");
 
1504
                return 0;
 
1505
        }
 
1506
        index = find_port( fd );
 
1507
        if ( !index )
 
1508
        {
 
1509
                LEAVE( "serial_read 7" );
 
1510
                errno = EIO;
 
1511
                printf("2\n");
 
1512
                return -1;
 
1513
        }
 
1514
 
 
1515
        /* FIXME: CREAD: without this, data cannot be read
 
1516
           FIXME: PARMRK: mark framing & parity errors
 
1517
           FIXME: IGNCR: ignore \r
 
1518
           FIXME: ICRNL: convert \r to \n
 
1519
           FIXME: INLCR: convert \n to \r
 
1520
        */
 
1521
 
 
1522
        ClearErrors( index, &Stat );
 
1523
        if ( index->open_flags & O_NONBLOCK  )
 
1524
        {
 
1525
                int ret;
 
1526
                vmin = 0;
 
1527
                /* pull mucho-cpu here? */
 
1528
                do {
 
1529
#ifdef DEBUG_VERBOSE
 
1530
                        report( "vmin=0\n" );
 
1531
#endif /* DEBUG_VERBOSE */
 
1532
                        ret = ClearErrors( index, &Stat);
 
1533
/*
 
1534
                        usleep(1000);
 
1535
                        usleep(50);
 
1536
*/
 
1537
                        /* we should use -1 instead of 0 for disabled timeout */
 
1538
                        now = GetTickCount();
 
1539
                        if ( index->ttyset->c_cc[VTIME] &&
 
1540
                                now-start >= (index->ttyset->c_cc[VTIME]*100)) {
 
1541
/*
 
1542
                                sprintf( message, "now = %i start = %i time = %i total =%i\n", now, start, index->ttyset->c_cc[VTIME]*100, total);
 
1543
                                report( message );
 
1544
*/
 
1545
                                errno = EAGAIN;
 
1546
                                printf("3\n");
 
1547
                                return -1;      /* read timeout */
 
1548
                        }
 
1549
                } while( Stat.cbInQue < size && size > 1 );
 
1550
        }
 
1551
        else
 
1552
        {
 
1553
                /* VTIME is in units of 0.1 seconds */
 
1554
 
 
1555
#ifdef DEBUG_VERBOSE
 
1556
                report( "vmin!=0\n" );
 
1557
#endif /* DEBUG_VERBOSE */
 
1558
                vmin = index->ttyset->c_cc[VMIN];
 
1559
 
 
1560
                c = clock() + index->ttyset->c_cc[VTIME] * CLOCKS_PER_SEC / 10;
 
1561
                do {
 
1562
                        error = ClearErrors( index, &Stat);
 
1563
                        usleep(1000);
 
1564
                } while ( c > clock() );
 
1565
 
 
1566
        }
 
1567
 
 
1568
        total = 0;
 
1569
        while ( size > 0 )
 
1570
        {
 
1571
                nBytes = 0;
 
1572
                /* ret = ClearErrors( index, &Stat); */
 
1573
 
 
1574
                index->rol.Offset = index->rol.OffsetHigh = 0;
 
1575
                ResetEvent( index->rol.hEvent );
 
1576
 
 
1577
                err = ReadFile( index->hComm, dest + total, size, &nBytes, &index->rol ); 
 
1578
#ifdef DEBUG_VERBOSE
 
1579
        /* warning Roy Rogers! */
 
1580
                sprintf(message, " ========== ReadFile = %i %s\n",
 
1581
                        ( int ) nBytes, (char *) dest + total );
 
1582
                report( message );
 
1583
#endif /* DEBUG_VERBOSE */
 
1584
                size -= nBytes;
 
1585
                total += nBytes;
 
1586
                
 
1587
                if ( !err )
 
1588
                {
 
1589
                        switch ( GetLastError() )
 
1590
                        {
 
1591
                                case ERROR_BROKEN_PIPE:
 
1592
                                        report( "ERROR_BROKEN_PIPE\n ");
 
1593
                                        nBytes = 0;
 
1594
                                        break;
 
1595
                                case ERROR_MORE_DATA:
 
1596
/*
 
1597
                                        usleep(1000);
 
1598
*/
 
1599
                                        report( "ERROR_MORE_DATA\n" );
 
1600
                                        break;
 
1601
                                case ERROR_IO_PENDING:
 
1602
                                        while( ! GetOverlappedResult(
 
1603
                                                        index->hComm,
 
1604
                                                        &index->rol,
 
1605
                                                        &nBytes,
 
1606
                                                        TRUE ) )
 
1607
                                        {
 
1608
                                                if( GetLastError() !=
 
1609
                                                        ERROR_IO_INCOMPLETE )
 
1610
                                                {
 
1611
                                                        ClearErrors(
 
1612
                                                                index,
 
1613
                                                                &Stat);
 
1614
                                                        printf("4\n");
 
1615
                                                        return( total );
 
1616
                                                }
 
1617
                                        }
 
1618
                                        size -= nBytes;
 
1619
                                        total += nBytes;
 
1620
                                        if (size > 0) {
 
1621
                                                now = GetTickCount();
 
1622
                                                sprintf(message, "size > 0: spent=%ld have=%d\n", now-start, index->ttyset->c_cc[VTIME]*100);
 
1623
                                                report( message );
 
1624
                                                /* we should use -1 for disabled
 
1625
                                                   timouts */
 
1626
                                                if ( index->ttyset->c_cc[VTIME] && now-start >= (index->ttyset->c_cc[VTIME]*100)) {
 
1627
                                                        report( "TO " );
 
1628
                                                        /* read timeout */
 
1629
                                                        printf("5\n");
 
1630
                                                        return total;
 
1631
                                                }
 
1632
                                        }
 
1633
                                        sprintf(message, "end nBytes=%ld] ", nBytes);
 
1634
                                        report( message );
 
1635
/*
 
1636
                                        usleep(1000);
 
1637
*/
 
1638
                                        report( "ERROR_IO_PENDING\n" );
 
1639
                                        break;
 
1640
                                default:
 
1641
/*
 
1642
                                        usleep(1000);
 
1643
*/
 
1644
                                        YACK();
 
1645
                                        errno = EIO;
 
1646
                                        printf("6\n");
 
1647
                                        return -1;
 
1648
                        }
 
1649
                }
 
1650
                else
 
1651
                {
 
1652
/*
 
1653
                        usleep(1000);
 
1654
*/
 
1655
                        ClearErrors( index, &Stat);
 
1656
                        printf("7\n");
 
1657
                        return( total );
 
1658
                }
 
1659
        }
 
1660
        LEAVE( "serial_read" );
 
1661
        ClearErrors( index, &Stat);
 
1662
        return total;
 
1663
}
 
1664
#endif /* asdf */
1550
1665
 
1551
1666
/*----------------------------------------------------------
1552
1667
cfsetospeed()
1652
1767
}
1653
1768
 
1654
1769
/*----------------------------------------------------------
1655
 
TermiosToDCB()
1656
 
 
1657
 
   accept:      
1658
 
   perform:     
1659
 
   return:      
1660
 
   exceptions:  
1661
 
   win32api:     None
1662
 
   comments:    
1663
 
----------------------------------------------------------*/
1664
 
 
1665
 
int TermiosToDCB( struct termios *s_termios, DCB *dcb )
1666
 
{
1667
 
        ENTER( "TermiosToDCB" );
1668
 
        s_termios->c_ispeed = s_termios->c_cflag & CBAUD;
1669
 
        s_termios->c_ospeed = s_termios->c_ispeed;
 
1770
serial_struct_to_DCB()
 
1771
 
 
1772
   accept:      
 
1773
   perform:     
 
1774
   return:      
 
1775
   exceptions:  
 
1776
   win32api:     None
 
1777
   comments:    
 
1778
----------------------------------------------------------*/
 
1779
int serial_struct_to_DCB( struct serial_struct *sstruct, DCB *dcb )
 
1780
{
 
1781
        /* 5 Baud rate fix
 
1782
        sstruct.baud_base
 
1783
        sstruct.custom_divisor = ( sstruct.baud_base/cspeed );
 
1784
        */
 
1785
        return(0);
 
1786
}
 
1787
 
 
1788
/*----------------------------------------------------------
 
1789
termios_to_DCB()
 
1790
 
 
1791
   accept:      
 
1792
   perform:     
 
1793
   return:      
 
1794
   exceptions:  
 
1795
   win32api:     None
 
1796
   comments:    
 
1797
----------------------------------------------------------*/
 
1798
int termios_to_DCB( struct termios *s_termios, DCB *dcb )
 
1799
{
 
1800
        ENTER( "termios_to_DCB" );
 
1801
        s_termios->c_ispeed = s_termios->c_ospeed = s_termios->c_cflag & CBAUD;
1670
1802
        dcb->BaudRate        = B_to_CBR( s_termios->c_ispeed );
1671
 
        LEAVE( "TermiosToDCB" );
 
1803
        dcb->ByteSize = termios_to_bytesize( s_termios->c_cflag );
 
1804
 
 
1805
        if ( s_termios->c_cflag & PARENB )
 
1806
        {
 
1807
                if ( s_termios->c_cflag & PARODD 
 
1808
                        && s_termios->c_cflag & CMSPAR )
 
1809
                {
 
1810
                        dcb->Parity = MARKPARITY;
 
1811
                }
 
1812
                else if ( s_termios->c_cflag & PARODD )
 
1813
                {
 
1814
                        dcb->Parity = ODDPARITY;
 
1815
                }
 
1816
                else if ( s_termios->c_cflag & CMSPAR )
 
1817
                {
 
1818
                        dcb->Parity = SPACEPARITY;
 
1819
                }
 
1820
                else
 
1821
                {
 
1822
                        dcb->Parity = EVENPARITY;
 
1823
                }
 
1824
        }
 
1825
        else
 
1826
        {
 
1827
                dcb->Parity = NOPARITY;
 
1828
        }       
 
1829
 
 
1830
        if ( s_termios->c_cflag & CSTOPB ) dcb->StopBits = TWOSTOPBITS;
 
1831
                else dcb->StopBits = ONESTOPBIT;
 
1832
 
 
1833
        if ( s_termios->c_cflag & HARDWARE_FLOW_CONTROL )
 
1834
        {
 
1835
                dcb->fRtsControl = RTS_CONTROL_HANDSHAKE;
 
1836
                dcb->fOutxCtsFlow = TRUE;
 
1837
        }
 
1838
        else
 
1839
        {
 
1840
                dcb->fRtsControl = RTS_CONTROL_DISABLE;
 
1841
                dcb->fOutxCtsFlow = FALSE;
 
1842
        }
 
1843
 
 
1844
        LEAVE( "termios_to_DCB" );
1672
1845
        return 0;
1673
1846
}
1674
1847
 
1675
1848
/*----------------------------------------------------------
1676
 
DCBToTermios()
1677
 
 
1678
 
   accept:      
1679
 
   perform:     
1680
 
   return:      
1681
 
   exceptions:  
1682
 
   win32api:     None
1683
 
   comments:    
1684
 
----------------------------------------------------------*/
1685
 
 
1686
 
void DCBToTermios( DCB *dcb, struct termios *s_termios )
1687
 
{
1688
 
        ENTER( "DCBToTermios" );
 
1849
DCB_to_serial_struct()
 
1850
 
 
1851
   accept:      
 
1852
   perform:     
 
1853
   return:      
 
1854
   exceptions:  
 
1855
   win32api:     None
 
1856
   comments:    
 
1857
----------------------------------------------------------*/
 
1858
int DCB_to_serial_struct( DCB *dcb, struct serial_struct *sstruct  )
 
1859
{
 
1860
        return( 0 );
 
1861
}
 
1862
/*----------------------------------------------------------
 
1863
DCB_to_termios()
 
1864
 
 
1865
   accept:      
 
1866
   perform:     
 
1867
   return:      
 
1868
   exceptions:  
 
1869
   win32api:     None
 
1870
   comments:    
 
1871
----------------------------------------------------------*/
 
1872
void DCB_to_termios( DCB *dcb, struct termios *s_termios )
 
1873
{
 
1874
        ENTER( "DCB_to_termios" );
1689
1875
        s_termios->c_ispeed = CBR_to_B( dcb->BaudRate );
1690
1876
        s_termios->c_ospeed = s_termios->c_ispeed;
1691
1877
        s_termios->c_cflag |= s_termios->c_ispeed & CBAUD;
1692
 
        LEAVE( "DCBToTermios" );
 
1878
        LEAVE( "DCB_to_termios" );
1693
1879
}
1694
1880
 
1695
1881
/*----------------------------------------------------------
1830
2016
        DCB myDCB;
1831
2017
        COMMTIMEOUTS timeouts;
1832
2018
        struct termios_list *index;
1833
 
        int flag;
1834
2019
        char message[80];
1835
2020
 
1836
2021
        ENTER( "tcgetattr" );
1839
2024
        index = find_port( fd );
1840
2025
        if ( !index )
1841
2026
        {
1842
 
                sprintf( message,
1843
 
                        "No info known about the port. tcgetattr %i\n",
1844
 
                        fd );
1845
 
                report( message );
 
2027
                LEAVE( "tcgetattr" );
1846
2028
                return -1;
1847
2029
        }
1848
2030
        if ( !GetCommState( index->hComm, &myDCB ) )
1867
2049
        }
1868
2050
        /* FIXME: IGNBRK: ignore break */
1869
2051
        /* FIXME: BRKINT: interrupt on break */
1870
 
        if ( myDCB.fOutX ) s_termios->c_iflag |= IXON;
1871
 
        /* IXON: output start/stop control */
1872
 
        else s_termios->c_iflag &= IXON;
1873
 
        if ( myDCB.fInX ) s_termios->c_iflag |= IXOFF;
1874
 
        /* IXOFF: input start/stop control */
1875
 
        else s_termios->c_iflag &= IXOFF;
1876
 
        if ( myDCB.fTXContinueOnXoff ) s_termios->c_iflag |= IXANY;
1877
 
        /* IXANY: any char restarts output */
1878
 
        else s_termios->c_iflag &= ~IXANY;
 
2052
 
 
2053
        if ( myDCB.fOutX )
 
2054
        {
 
2055
                s_termios->c_iflag |= IXON;
 
2056
        }
 
2057
        else
 
2058
        {
 
2059
                /* IXON: output start/stop control */
 
2060
                s_termios->c_iflag &= ~IXON;
 
2061
        }
 
2062
        if ( myDCB.fInX )
 
2063
        {
 
2064
                s_termios->c_iflag |= IXOFF;
 
2065
        }
 
2066
        else
 
2067
        {
 
2068
                /* IXOFF: input start/stop control */
 
2069
                s_termios->c_iflag &= ~IXOFF;
 
2070
        }
 
2071
 
 
2072
        if ( myDCB.fTXContinueOnXoff )
 
2073
        {
 
2074
                s_termios->c_iflag |= IXANY;
 
2075
        }
 
2076
        else
 
2077
        {
 
2078
                /* IXANY: any char restarts output */
 
2079
                s_termios->c_iflag &= ~IXANY;
 
2080
        }
1879
2081
        /* FIXME: IMAXBEL: if input buffer full, send bell */
1880
2082
 
1881
2083
        /***** control mode flags (c_cflag ) *****/
1883
2085
        /* FIXME: HUPCL: generate modem disconnect when all has closed or
1884
2086
                exited */
1885
2087
        /* CSTOPB two stop bits ( otherwise one) */
1886
 
        if ( myDCB.StopBits == TWOSTOPBITS ) s_termios->c_cflag |= CSTOPB;      
1887
 
        if ( myDCB.StopBits == ONESTOPBIT ) s_termios->c_cflag &= ~CSTOPB;      
 
2088
        if ( myDCB.StopBits == TWOSTOPBITS )
 
2089
        {
 
2090
                s_termios->c_cflag |= CSTOPB;   
 
2091
        }
 
2092
        if ( myDCB.StopBits == ONESTOPBIT )
 
2093
        {
 
2094
                s_termios->c_cflag &= ~CSTOPB;  
 
2095
        }
 
2096
 
 
2097
 
1888
2098
        /* PARENB enable parity bit */
1889
 
        if ( myDCB.fParity )
 
2099
        s_termios->c_cflag &= ~( PARENB | PARODD | CMSPAR );
 
2100
        myDCB.fParity = 1;
 
2101
        if( myDCB.fParity )
1890
2102
        {
1891
 
                report( "setting parity\n" );
1892
 
                flag = 0;
 
2103
                report( "tcgetattr getting parity\n" );
1893
2104
                s_termios->c_cflag |= PARENB;
1894
 
                flag |= PARENB;
1895
 
                if ( myDCB.Parity == ODDPARITY )
 
2105
                if ( myDCB.Parity == MARKPARITY )
 
2106
                {
 
2107
                        s_termios->c_cflag |= ( PARODD | CMSPAR );
 
2108
                }
 
2109
                else if ( myDCB.Parity == SPACEPARITY )
 
2110
                {
 
2111
                        s_termios->c_cflag |= CMSPAR;
 
2112
                }
 
2113
                else if ( myDCB.Parity == ODDPARITY )
1896
2114
                {
1897
2115
                        report( "ODDPARITY\n" );
1898
 
                        flag |= PARODD;
1899
2116
                        s_termios->c_cflag |= PARODD;
1900
2117
                }
1901
 
                if ( myDCB.Parity == EVENPARITY )
 
2118
                else if ( myDCB.Parity == EVENPARITY )
1902
2119
                {
1903
2120
                        report( "EVENPARITY\n" );
1904
 
                        flag &= ~PARODD;
1905
2121
                        s_termios->c_cflag &= ~PARODD;
1906
2122
                }
 
2123
                else if ( myDCB.Parity == NOPARITY )
 
2124
                {
 
2125
                        s_termios->c_cflag &= ~(PARODD | CMSPAR | PARENB);
 
2126
                }
1907
2127
        } else
1908
2128
        {
1909
 
                flag &= ~PARENB;
1910
2129
                s_termios->c_cflag &= ~PARENB;
1911
2130
        }
1912
2131
        /* CSIZE */
1913
2132
        s_termios->c_cflag |= bytesize_to_termios( myDCB.ByteSize );
1914
 
        /* CRTSCTS: hardware flow control */
 
2133
        /* HARDWARE_FLOW_CONTROL: hardware flow control */
1915
2134
        if (( myDCB.fOutxCtsFlow == TRUE ) ||
1916
2135
            ( myDCB.fRtsControl == RTS_CONTROL_HANDSHAKE))
1917
 
          s_termios->c_cflag |= CRTSCTS;
 
2136
        {
 
2137
                s_termios->c_cflag |= HARDWARE_FLOW_CONTROL;
 
2138
        }
1918
2139
        else
1919
 
          s_termios->c_cflag &= ~CRTSCTS;
 
2140
        {
 
2141
                s_termios->c_cflag &= ~HARDWARE_FLOW_CONTROL;
 
2142
        }
1920
2143
        /* MDMBUF: carrier based flow control of output */
1921
2144
        /* CIGNORE: tcsetattr will ignore control modes & baudrate */
1922
2145
 
1967
2190
 
1968
2191
        /***** line discipline ( c_line ) ( == c_cc[33] ) *****/
1969
2192
 
1970
 
        DCBToTermios( &myDCB, s_termios ); /* baudrate */
 
2193
        DCB_to_termios( &myDCB, s_termios ); /* baudrate */
1971
2194
        LEAVE( "tcgetattr" );
1972
2195
        return 0;
1973
2196
}
2012
2235
        DCB dcb;
2013
2236
        COMMTIMEOUTS timeouts;
2014
2237
        struct termios_list *index;
2015
 
        char message[80];
2016
2238
 
2017
2239
        ENTER( "tcsetattr" );
2018
2240
        if ( fd <= 0 )
2020
2242
        index = find_port( fd );
2021
2243
        if ( !index )
2022
2244
        {
2023
 
                sprintf( message, "No info known about the port. tcsetattr %i\n", fd );
2024
 
                report( message );
 
2245
                LEAVE( "tcsetattr" );
2025
2246
                return -1;
2026
2247
        }
2027
2248
        fflush( stdout );
2044
2265
                return -1;
2045
2266
        }
2046
2267
 
2047
 
        /* FIXME: CLOCAL: DONT send SIGHUP on modem disconnect */
2048
 
        /* FIXME: HUPCL: generate modem disconnect when all has closed or
2049
 
                        exited */
2050
 
        /* FIXME: CREAD: without this, data cannot be read */
2051
 
        /* FIXME: MDMBUF: carrier based flow control of output */
2052
 
 
2053
2268
        /*** control flags, c_cflag **/
2054
2269
        if ( !( s_termios->c_cflag & CIGNORE ) )
2055
2270
        {
 
2271
                dcb.fParity=1;
2056
2272
                /* CIGNORE: ignore control modes and baudrate */
2057
2273
                /* baudrate */
2058
 
                if ( TermiosToDCB( s_termios, &dcb ) < 0 )
2059
 
                        return -1;
2060
 
                dcb.ByteSize = termios_to_bytesize( s_termios->c_cflag );
2061
 
                if ( s_termios->c_cflag & PARENB )
2062
 
                {
2063
 
                        if ( s_termios->c_cflag & PARODD )
2064
 
                                dcb.Parity = ODDPARITY;
2065
 
                        else
2066
 
                                dcb.Parity = EVENPARITY;
2067
 
                } else
2068
 
                        dcb.Parity = NOPARITY;
2069
 
                if ( s_termios->c_cflag & CSTOPB ) dcb.StopBits = TWOSTOPBITS;
2070
 
                        else dcb.StopBits = ONESTOPBIT;
2071
 
                if ( s_termios->c_cflag & CRTSCTS ) {
2072
 
                  dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
2073
 
                  dcb.fOutxCtsFlow = TRUE;
2074
 
                } else {
2075
 
                  dcb.fRtsControl = RTS_CONTROL_ENABLE;
2076
 
                  dcb.fOutxCtsFlow = FALSE;
2077
 
                }
 
2274
                if ( termios_to_DCB( s_termios, &dcb ) < 0 ) return -1;
 
2275
        }
 
2276
        else
 
2277
        {
2078
2278
        }
2079
2279
 
2080
2280
        /*** input flags, c_iflag **/
 
2281
/*  This is wrong.  It disables Parity  FIXME
2081
2282
        if( ( s_termios->c_iflag & INPCK ) && !( s_termios->c_iflag & IGNPAR ) )
2082
2283
        {
2083
2284
                dcb.fParity = TRUE;
2084
 
        } else dcb.fParity = FALSE;
 
2285
        } else
 
2286
        {
 
2287
                dcb.fParity = FALSE;
 
2288
        }
 
2289
*/
2085
2290
        /* not in win95?
2086
2291
           Some years later...
2087
2292
           eww..  FIXME This is used for changing the Parity 
2098
2303
 
2099
2304
        /* FIXME: IGNBRK: ignore break */
2100
2305
        /* FIXME: BRKINT: interrupt on break */
2101
 
        dcb.fOutX = ( s_termios->c_iflag & IXON ) ? TRUE : FALSE;
2102
 
        dcb.fInX = ( s_termios->c_iflag & IXOFF ) ? TRUE : FALSE;
 
2306
        if ( s_termios->c_iflag & IXON )
 
2307
        {
 
2308
                dcb.fOutX = TRUE;
 
2309
        }
 
2310
        else
 
2311
        {
 
2312
                dcb.fOutX = FALSE;
 
2313
        }
 
2314
        if ( s_termios->c_iflag & IXOFF )
 
2315
        {
 
2316
                dcb.fInX = TRUE;
 
2317
        }
 
2318
        else
 
2319
        {
 
2320
                dcb.fInX = FALSE;
 
2321
        }
2103
2322
        dcb.fTXContinueOnXoff = ( s_termios->c_iflag & IXANY ) ? TRUE : FALSE;
2104
2323
        /* FIXME: IMAXBEL: if input buffer full, send bell */
2105
2324
 
2143
2362
        timeouts.ReadTotalTimeoutConstant = vtime;
2144
2363
        timeouts.ReadIntervalTimeout = 0;
2145
2364
        timeouts.ReadTotalTimeoutMultiplier = 0;
 
2365
 
2146
2366
        timeouts.WriteTotalTimeoutConstant = vtime;
2147
2367
        timeouts.WriteTotalTimeoutMultiplier = 0;
2148
2368
        /* max between bytes */
2193
2413
   exceptions:  
2194
2414
   win32api:     None
2195
2415
   comments:    
 
2416
                break for duration*0.25 seconds or
 
2417
                0.25 seconds if duration = 0.
2196
2418
----------------------------------------------------------*/
2197
2419
 
2198
2420
int tcsendbreak( int fd, int duration )
2199
2421
{
 
2422
        struct termios_list *index;
 
2423
        COMSTAT Stat;
 
2424
 
2200
2425
        ENTER( "tcsendbreak" );
2201
 
        /* FIXME send a stream of zero bits for duration */
 
2426
 
 
2427
        index = find_port( fd );
 
2428
 
 
2429
        if ( !index )
 
2430
        {
 
2431
                LEAVE( "tcdrain" );
 
2432
                return -1;
 
2433
        }
 
2434
 
 
2435
        if ( duration <= 0 ) duration = 1;
 
2436
 
 
2437
        if( !SetCommBreak( index->hComm ) )
 
2438
                ClearErrors( index, &Stat );
 
2439
        /* 0.25 seconds == 250000 usec */
 
2440
        usleep( duration * 250000 );
 
2441
        if( !ClearCommBreak( index->hComm ) )
 
2442
                ClearErrors( index, &Stat );
2202
2443
        LEAVE( "tcsendbreak" );
2203
2444
        return 1;
2204
2445
}
2225
2466
 
2226
2467
        if ( !index )
2227
2468
        {
2228
 
                sprintf( message, "No info known about the port. ioctl %i\n",
2229
 
                        fd );
2230
 
                report( message );
2231
 
                set_errno( EBADF );
2232
 
                
2233
2469
                LEAVE( "tcdrain" );
2234
2470
                return -1;
2235
2471
        }
2294
2530
int tcflush( int fd, int queue_selector )
2295
2531
{
2296
2532
        struct termios_list *index;
2297
 
        char message[80];
2298
2533
        int old_flag;
2299
2534
 
2300
2535
        index = find_port( fd );
 
2536
        if( !index)
 
2537
        {
 
2538
                LEAVE( "tclflush" );
 
2539
                return(-1);
 
2540
        }
2301
2541
 
2302
2542
        old_flag = index->event_flag;
2303
2543
/*
2305
2545
        SetCommMask( index->hComm, index->event_flag );
2306
2546
        index->tx_happened = 1; 
2307
2547
*/
2308
 
        ENTER("tcflush");
 
2548
        ENTER( "tcflush" );
2309
2549
        if ( !index )
2310
2550
        {
2311
 
                set_errno( EBADF );
2312
 
                sprintf( message, "No info known about the port. ioctl %i\n",
2313
 
                        fd );
2314
 
                report( message );
2315
 
                LEAVE("tcflush");
 
2551
                LEAVE( "tcflush" );
2316
2552
                return -1;
2317
2553
        }
2318
2554
 
2346
2582
                        set_errno( ENOTSUP );
2347
2583
*/
2348
2584
                        report( "tcflush: Unknown queue_selector\n" );
2349
 
                        LEAVE("tcflush");
 
2585
                        LEAVE( "tcflush" );
2350
2586
                        return -1;
2351
2587
        }
2352
2588
        index->event_flag |= EV_TXEMPTY;
2353
2589
        SetCommMask( index->hComm, index->event_flag );
2354
2590
        index->event_flag = old_flag;
2355
2591
        index->tx_happened = 1; 
2356
 
        LEAVE("tcflush");
 
2592
        LEAVE( "tcflush" );
2357
2593
        return( 0 );
2358
2594
 
2359
2595
/* FIXME  Need to figure out what the various errors are in
2362
2598
*/
2363
2599
 
2364
2600
fail:
2365
 
        LEAVE("tcflush");
 
2601
        LEAVE( "tcflush" );
2366
2602
        set_errno( EAGAIN );
2367
2603
        YACK();
2368
2604
        return -1;
2443
2679
{
2444
2680
        unsigned long dwStatus = 0;
2445
2681
        va_list ap;
2446
 
        int *arg, ret, result;
 
2682
        int *arg, ret, result, old_flag;
 
2683
        char message[80];
 
2684
 
2447
2685
#ifdef TIOCGSERIAL
2448
 
        unsigned long ErrCode;
 
2686
        DCB     *dcb;
2449
2687
        struct serial_struct *sstruct;
2450
2688
#endif /* TIOCGSERIAL */
 
2689
        COMSTAT Stat;
 
2690
 
 
2691
        struct termios_list *index;
2451
2692
        struct async_struct *astruct;
2452
2693
        struct serial_multiport_struct *mstruct;
2453
 
        COMSTAT Stat;
2454
2694
#ifdef TIOCGICOUNT
2455
2695
        struct serial_icounter_struct *sistruct;
2456
2696
#endif  /* TIOCGICOUNT */
2457
 
        struct termios_list *index;
2458
 
        char message[80];
2459
 
        int old_flag;
2460
2697
 
2461
 
#ifdef DEBUG_VERBOSE
2462
2698
        ENTER( "ioctl" );
2463
 
#endif /* DEBUG_VERBOSE */
2464
2699
        if ( fd <= 0 )
2465
2700
                return 0;
2466
2701
        index = find_port( fd );
2467
2702
        if ( !index )
2468
2703
        {
2469
 
                sprintf( message,
2470
 
                        "No info known about the port. ioctl %i\n", fd );
2471
 
                report( message );
 
2704
                LEAVE( "ioctl" );
2472
2705
                return -1;
2473
2706
        }
2474
2707
 
2475
2708
        va_start( ap, request );
2476
2709
        
 
2710
        ret = ClearErrors( index, &Stat );
 
2711
        if (ret == 0)
 
2712
        {
 
2713
                set_errno( EBADFD );
 
2714
                YACK();
 
2715
                report( "ClearError Failed! ernno EBADFD" );
 
2716
                arg = va_arg( ap, int * );
 
2717
                va_end( ap );
 
2718
                return -1;              
 
2719
        }
2477
2720
        switch( request )
2478
2721
        {
2479
2722
                case TCSBRK:
2497
2740
                        arg = va_arg( ap, int * );
2498
2741
                /* DORITOS */
2499
2742
                        if ( !GetCommModemStatus( index->hComm, &dwStatus ) )
2500
 
                                report_error("GetCommMOdemStatus failed!\n");
 
2743
                                report_error( "GetCommMOdemStatus failed!\n" );
2501
2744
                        if ( dwStatus & MS_RLSD_ON ) *arg |= TIOCM_CAR;
2502
2745
                        else *arg &= ~TIOCM_CAR;
2503
2746
                        if ( dwStatus & MS_RING_ON ) *arg |= TIOCM_RNG;
2537
2780
                        return -ENOIOCTLCMD;
2538
2781
                case TIOCMSET:
2539
2782
                        arg = va_arg( ap, int * );
2540
 
/*
2541
 
                        sprintf( message, "DTR = %i, RTS=%i was DTR = %i RTS = %i\n",
2542
 
                                *arg & TIOCM_DTR,
2543
 
                                *arg & TIOCM_RTS,
2544
 
                                index->MSR &TIOCM_DTR,
2545
 
                                index->MSR &TIOCM_RTS
2546
 
                        );
2547
 
                        mexPrintf( message );
2548
 
*/
2549
2783
                        if (( *arg & TIOCM_DTR) == (index->MSR & TIOCM_DTR) )
2550
2784
                        {
2551
 
                                report("DTR is unchanged\n");
 
2785
                                report( "DTR is unchanged\n" );
2552
2786
                        }
2553
2787
                        sprintf(message, "DTR %i %i\n", *arg&TIOCM_DTR, index->MSR & TIOCM_DTR );
2554
2788
                        report( message );
2563
2797
                        if ( EscapeCommFunction( index->hComm, 
2564
2798
                                ( *arg & TIOCM_DTR ) ? SETDTR :
2565
2799
                                CLRDTR ) )
2566
 
                                report("EscapeCommFunction: True\n");
 
2800
                                report( "EscapeCommFunction: True\n" );
2567
2801
                        else
2568
 
                                report("EscapeCommFunction: False\n");
 
2802
                                report( "EscapeCommFunction: False\n" );
2569
2803
                        if ( (*arg & TIOCM_RTS) == ( index->MSR & TIOCM_RTS) )
2570
2804
                        {
2571
 
                                report("RTS is unchanged\n");
 
2805
                                report( "RTS is unchanged\n" );
2572
2806
                        }
2573
2807
                        sprintf( message, "RTS %i %i\n", *arg&TIOCM_RTS, index->MSR & TIOCM_RTS );
2574
2808
                        report( message );
2584
2818
                        }
2585
2819
                        if( EscapeCommFunction( index->hComm,
2586
2820
                                ( *arg & TIOCM_RTS ) ? SETRTS : CLRRTS ) )
2587
 
                                report("EscapeCommFunction: True\n");
2588
 
                        else
2589
 
                                report("EscapeCommFunction: False\n");
2590
 
/*
2591
 
                        if ( *arg & TIOCM_DTR )
2592
 
                        {
2593
 
                                index->MSR |= TIOCM_DTR;
2594
 
                        }
2595
 
                        else
2596
 
                        {
2597
 
                                index->MSR &= ~TIOCM_DTR;
2598
 
                        }
2599
 
                        if ( *arg & TIOCM_RTS )
2600
 
                        {
2601
 
                                index->MSR |= TIOCM_RTS;
2602
 
                                result &= SETRTS;
2603
 
                        }
2604
 
                        else
2605
 
                        {
2606
 
                                index->MSR &= ~TIOCM_RTS;
2607
 
                                result &= CLRRTS;
2608
 
                        }
2609
 
                        if( EscapeCommFunction( index->hComm,
2610
 
                                ( *arg & TIOCM_RTS ) ? SETRTS : CLRRTS ) )
2611
 
                                report("EscapeCommFunction: True\n");
2612
 
                        else
2613
 
                                report("EscapeCommFunction: False\n");
2614
 
                        if ( EscapeCommFunction( index->hComm, 
2615
 
                                ( *arg & TIOCM_DTR ) ? SETDTR : CLRDTR ) )
2616
 
                                report("EscapeCommFunction: True\n");
2617
 
                        else
2618
 
                                report("EscapeCommFunction: False\n");
2619
 
*/
2620
 
/*
2621
 
                        if ( ! (*arg & TIOCM_DTR) != (index->MSR & TIOCM_DTR))
2622
 
                        {
2623
 
                                report("Changing DTR\n");
2624
 
                                EscapeCommFunction( index->hComm,
2625
 
                                ( *arg & TIOCM_DTR ) ? SETDTR : CLRDTR );
2626
 
                                if ( *arg & TIOCM_DTR )
2627
 
                                {
2628
 
                                        index->MSR |= TIOCM_DTR;
2629
 
                                }
2630
 
                                else
2631
 
                                {
2632
 
                                        index->MSR &= ~TIOCM_DTR;
2633
 
                                }
2634
 
                        }
2635
 
                        else
2636
 
                                report("not changing DTR\n");
2637
 
                        if ( ! (*arg & TIOCM_RTS) != (index->MSR & TIOCM_RTS))
2638
 
                        {
2639
 
                                report("Changing RTS\n");
2640
 
                                EscapeCommFunction( index->hComm,
2641
 
                                ( *arg & TIOCM_RTS ) ? SETRTS : CLRRTS );
2642
 
                                if ( *arg & TIOCM_RTS )
2643
 
                                {
2644
 
                                        index->MSR |= TIOCM_RTS;
2645
 
                                }
2646
 
                                else
2647
 
                                {
2648
 
                                        index->MSR &= ~TIOCM_RTS;
2649
 
                                }
2650
 
                        }
2651
 
                        else
2652
 
                                report("not changing RTS\n");
2653
 
*/
 
2821
                                report( "EscapeCommFunction: True\n" );
 
2822
                        else
 
2823
                                report( "EscapeCommFunction: False\n" );
2654
2824
                        break;
2655
 
                /* get the serial struct info from the underlying API
2656
 
                 * TIOCGSERIAL is not used on win32.  Its commented out
2657
 
                 * in win32termios.h
2658
 
                 */
 
2825
 
2659
2826
#ifdef TIOCGSERIAL
2660
2827
                case TIOCGSERIAL:
 
2828
                        report( "TIOCGSERIAL\n" );
 
2829
 
 
2830
                        dcb = malloc( sizeof( DCB ) );
 
2831
                        if( !dcb )
 
2832
                        {
 
2833
                                va_end( ap );
 
2834
                                return -1;
 
2835
                        }
 
2836
                        memset( dcb, 0, sizeof( DCB ) );
 
2837
                        GetCommState( index->hComm, dcb );
 
2838
 
2661
2839
                        sstruct = va_arg( ap, struct serial_struct * );
2662
 
                        sstruct = index->sstruct;
 
2840
                        if ( DCB_to_serial_struct( dcb, sstruct ) < 0 )
 
2841
                        {
 
2842
                                va_end( ap );
 
2843
                                return -1;
 
2844
                        }
 
2845
                        index->sstruct = sstruct;
 
2846
 
2663
2847
                        report( "TIOCGSERIAL\n" );
2664
 
                        va_end( ap );
2665
 
                        return 0;
 
2848
                        free(dcb);
 
2849
                        break;
 
2850
 
2666
2851
#endif /* TIOCGSERIAL */
2667
 
                /* set the serial struct info from the underlying API
2668
 
                 * this is not implemented.  TIOCSSERIAL is commented out
2669
 
                 * in win32termios.h
2670
 
                 */
2671
2852
#ifdef TIOCSSERIAL
2672
2853
                case TIOCSSERIAL:
2673
2854
                        report( "TIOCSSERIAL\n" );
2674
 
                        index->sstruct = sstruct;
2675
 
                        arg = va_arg( ap, int * );
2676
 
                        va_end( ap );
2677
 
                        return 0;
 
2855
 
 
2856
                        dcb = malloc( sizeof( DCB ) );
 
2857
                        if( !dcb )
 
2858
                        {
 
2859
                                va_end( ap );
 
2860
                                return -1;
 
2861
                        }
 
2862
                        memset( dcb, 0, sizeof( DCB ) );
 
2863
                        GetCommState( index->hComm, dcb );
 
2864
 
 
2865
                        index->sstruct = va_arg( ap, struct serial_struct * );
 
2866
                        if ( serial_struct_to_DCB( index->sstruct, dcb ) < 0 )
 
2867
                        {
 
2868
                                va_end( ap );
 
2869
                                return -1;
 
2870
                        }
 
2871
 
 
2872
                        report( "TIOCSSERIAL\n" );
 
2873
                        free(dcb);
 
2874
                        break;
 
2875
 
2678
2876
#endif /* TIOCSSERIAL */
2679
2877
                case TIOCSERCONFIG:
2680
2878
                case TIOCSERGETLSR:
2681
 
                        MexPrintf("t");
2682
2879
                        arg = va_arg( ap, int * );
2683
2880
                        /*
2684
2881
                        do {
2689
2886
                        if ( ret == 0 )
2690
2887
                        {
2691
2888
                                /* FIXME ? */
2692
 
                                MexPrintf("X");
2693
2889
                                set_errno( EBADFD );
2694
2890
                                YACK();
2695
2891
                                report( "TIOCSERGETLSR EBADFD" );
2699
2895
                        if ( (int ) Stat.cbOutQue == 0 )
2700
2896
                        {
2701
2897
                                /* output is empty */
2702
 
/*
2703
 
                                        && !index->interrupt ) 
2704
 
*/
2705
2898
                                if( index->tx_happened == 1 )
2706
2899
                                {
2707
2900
                                        old_flag = index->event_flag;
2709
2902
                                        SetCommMask( index->hComm,
2710
2903
                                                index->event_flag );
2711
2904
                                        index->event_flag = old_flag;
2712
 
                                        MexPrintf("+");
2713
2905
                                        *arg = 1;
2714
2906
                                        index->tx_happened = 0;
2715
2907
                                        report( "ioctl: ouput empty\n" );
2716
2908
                                }
2717
2909
                                else
2718
2910
                                {
2719
 
                                        if( index->interrupt )
2720
 
                                                MexPrintf("}");
2721
 
                                        if( index->tx_happened )
2722
 
                                                MexPrintf("]");
2723
 
                                        MexPrintf("-");
2724
2911
                                        *arg = 0;
2725
2912
                                }
2726
2913
                                ret = 0;
2728
2915
                        else
2729
2916
                        {
2730
2917
                                /* still data out there */
2731
 
                                MexPrintf("U");
2732
2918
                                *arg = 0;
2733
2919
                                ret = 0;
2734
 
/*
2735
 
                                *arg = TIOCSER_TEMP;
2736
 
*/
2737
2920
                        }
2738
 
                        MexPrintf("T");
2739
2921
                        va_end( ap );
2740
2922
                        return(0);
2741
2923
                        break;
2766
2948
                        if ( ret == 0 )
2767
2949
                        {
2768
2950
                                /* FIXME ? */
2769
 
                                report("TIOCGICOUNT failed\n");
 
2951
                                report( "TIOCGICOUNT failed\n" );
2770
2952
                                set_errno( EBADFD );
2771
2953
                                va_end( ap );
2772
2954
                                return -1;
2775
2957
                        {
2776
2958
                                sistruct->frame = index->sis->frame;
2777
2959
/*
2778
 
                                printf("---------------frame = %i\n", sistruct->frame++);
 
2960
                                printf( "---------------frame = %i\n", sistruct->frame++ );
2779
2961
*/
2780
2962
                        }
2781
2963
                        if( sistruct->overrun != index->sis->overrun )
2782
2964
                        {
2783
2965
/*
2784
 
                                printf("---------------overrun\n");
 
2966
                                printf( "---------------overrun\n" );
2785
2967
*/
2786
2968
                                sistruct->overrun = index->sis->overrun;
2787
 
                                ErrCode &= ~CE_OVERRUN;
 
2969
                                /* ErrCode &= ~CE_OVERRUN; */
2788
2970
                        }
2789
2971
                        if( sistruct->parity != index->sis->parity )
2790
2972
                        {
2791
2973
/*
2792
 
                                printf("---------------parity\n");
 
2974
                                printf( "---------------parity\n" );
2793
2975
*/
2794
2976
                                sistruct->parity = index->sis->parity;
2795
2977
                        }
2796
2978
                        if( sistruct->brk != index->sis->brk )
2797
2979
                        {
2798
2980
/*
2799
 
                                printf("---------------brk\n");
 
2981
                                printf( "---------------brk\n" );
2800
2982
*/
2801
2983
                                sistruct->brk = index->sis->brk;
2802
2984
                        }
2816
2998
                        if ( ret == 0 )
2817
2999
                        {
2818
3000
                                /* FIXME ? */
2819
 
                                report("FIONREAD failed\n");
 
3001
                                report( "FIONREAD failed\n" );
2820
3002
                                set_errno( EBADFD );
2821
3003
                                va_end( ap );
2822
3004
                                return -1;
2849
3031
                        return -ENOIOCTLCMD;
2850
3032
        }
2851
3033
        va_end( ap );
2852
 
#ifdef DEBUG_VERBOSE
2853
3034
        LEAVE( "ioctl" );
2854
 
#endif DEBUG_VERBOSE
2855
3035
        return 0;
2856
3036
}
2857
3037
 
2879
3059
        index = find_port( fd );
2880
3060
        if ( !index )
2881
3061
        {
2882
 
                sprintf( message, "No info known about the port. fcntl %i\n", fd );
2883
 
                report( message );
 
3062
                LEAVE( "fcntl" );
2884
3063
                return -1;
2885
3064
        }
2886
3065
 
2925
3104
void termios_interrupt_event_loop( int fd, int flag )
2926
3105
{
2927
3106
        struct termios_list * index = find_port( fd );
2928
 
/*
2929
 
        mexPrintf(":>");
2930
 
*/
2931
 
        if ( !index ) return;
 
3107
        if ( !index )
 
3108
        {
 
3109
                LEAVE( "termios_interrupt_event_loop" );
 
3110
                return;
 
3111
        }
2932
3112
/*
2933
3113
        index->event_flag = 0;
2934
3114
         TRENT SetCommMask( index->hComm, index->event_flag );
2937
3117
        SetEvent( index->sol.hEvent ); 
2938
3118
*/
2939
3119
        index->interrupt = flag;
2940
 
/*
2941
 
        mexPrintf(":<");
2942
 
*/
2943
3120
        return;
2944
3121
}
2945
3122
 
2954
3131
   comments:    
2955
3132
----------------------------------------------------------*/
2956
3133
#ifndef __LCC__
2957
 
 
 
3134
int  serial_select( int  n,  fd_set  *readfds,  fd_set  *writefds,
 
3135
                        fd_set *exceptfds, struct timeval *timeout )
 
3136
{
 
3137
 
 
3138
        unsigned long dwCommEvent, wait = WAIT_TIMEOUT;
 
3139
        int fd = n-1;
 
3140
        struct termios_list *index;
 
3141
        char message[80];
 
3142
        COMSTAT Stat;
 
3143
        int ret;
 
3144
 
 
3145
        ENTER( "serial_select" );
 
3146
        if ( fd <= 0 )
 
3147
        {
 
3148
                /*  Baby did a bad baad thing */
 
3149
                goto fail;
 
3150
        }
 
3151
 
 
3152
        index = find_port( fd );
 
3153
        if ( !index || !index->event_flag )
 
3154
        {
 
3155
                /* still setting up the port? hold off for a Sec so
 
3156
                   things can fire up
 
3157
 
 
3158
                   this does happen.  loops ~twice on a 350 Mzh with 
 
3159
                   usleep(1000000)
 
3160
                */
 
3161
                usleep(10000);
 
3162
                return(0);
 
3163
        }
 
3164
 
 
3165
        ResetEvent( index->wol.hEvent );
 
3166
        ResetEvent( index->sol.hEvent );
 
3167
        ResetEvent( index->rol.hEvent );
 
3168
        ret = ClearErrors( index, &Stat );
 
3169
        if (ret == 0) goto fail;
 
3170
        while ( wait == WAIT_TIMEOUT && index->sol.hEvent )
 
3171
        {
 
3172
                if( index->interrupt == 1 )
 
3173
                {
 
3174
                        goto fail;
 
3175
                }
 
3176
                SetCommMask( index->hComm, index->event_flag );
 
3177
                ClearErrors( index, &Stat );
 
3178
                if ( !WaitCommEvent( index->hComm, &dwCommEvent,
 
3179
                        &index->sol ) )
 
3180
                {
 
3181
                        /* WaitCommEvent failed probably overlapped though */
 
3182
                        if ( GetLastError() != ERROR_IO_PENDING )
 
3183
                        {
 
3184
                                ClearErrors( index, &Stat );
 
3185
                                goto fail;
 
3186
                        }
 
3187
                        /* thought so... */
 
3188
                }
 
3189
                /*  could use the select timeout here but it should not
 
3190
                    be needed
 
3191
                */
 
3192
                ClearErrors( index, &Stat );
 
3193
                wait = WaitForSingleObject( index->sol.hEvent, 100 );
 
3194
                switch ( wait )
 
3195
                {
 
3196
                        case WAIT_OBJECT_0:
 
3197
                                goto end;
 
3198
                        case WAIT_TIMEOUT:
 
3199
                                goto timeout;
 
3200
                        case WAIT_ABANDONED:
 
3201
                        default:
 
3202
                                goto fail;
 
3203
                        
 
3204
                }
 
3205
        }
 
3206
end:
 
3207
        /*  You may want to chop this out for lower latency */
 
3208
        usleep(1000);
 
3209
        LEAVE( "serial_select" );
 
3210
        return( 1 );
 
3211
timeout:
 
3212
        LEAVE( "serial_select" );
 
3213
        return( 0 );
 
3214
fail:
 
3215
        YACK();
 
3216
        sprintf( message, "< select called error %i\n", n );
 
3217
        report( message );
 
3218
        errno = EBADFD;
 
3219
        LEAVE( "serial_select" );
 
3220
        return( 1 );
 
3221
}
 
3222
#ifdef asdf
2958
3223
int  serial_select( int  n,  fd_set  *readfds,  fd_set  *writefds,
2959
3224
                        fd_set *exceptfds, struct timeval *timeout )
2960
3225
{
2963
3228
        int fd = n-1;
2964
3229
        struct termios_list *index;
2965
3230
        char message[80];
2966
 
        int loopcount = 0;
2967
3231
 
2968
 
#ifdef DEBUG_VERBOSE
2969
3232
        ENTER( "serial_select" );
2970
 
#endif /* DEBUG_VERBOSE */
2971
 
        MexPrintf(">");
2972
3233
        if ( fd <= 0 )
2973
3234
        {
2974
 
                MexPrintf("<!fd\n");
2975
3235
                usleep(1000);
2976
3236
                return 1;
2977
3237
        }
2978
3238
        index = find_port( fd );
2979
3239
        if ( !index )
2980
3240
        {
2981
 
                sprintf( message, "No info known about the port. select %i\n",
2982
 
                        fd );
2983
 
                report( message );
2984
 
                MexPrintf("<!I\n");
 
3241
                LEAVE( "serial_select" );
2985
3242
                return -1;
2986
3243
        }
2987
3244
        if( index->interrupt == 1 )
2988
3245
        {
2989
 
                MexPrintf("I");
2990
3246
                goto end;
2991
3247
        }
2992
3248
        while(!index->event_flag )
2993
3249
        {
2994
3250
                usleep(1000);
2995
 
                MexPrintf("!F<\n");
2996
3251
                return -1;
2997
3252
        }
2998
3253
        
2999
3254
        while ( wait == WAIT_TIMEOUT && index->sol.hEvent )
3000
3255
        {
3001
 
                sprintf(message, "%i", loopcount++ );
3002
 
                MexPrintf( message );
3003
3256
                if( index->interrupt == 1 )
3004
3257
                {
3005
 
                        MexPrintf("i");
3006
3258
                        goto end;
3007
3259
                }
3008
 
                if( !index->sol.hEvent ) return 1;
 
3260
                if( !index->sol.hEvent )
 
3261
                {
 
3262
                        return 1;
 
3263
                }
3009
3264
                if ( !WaitCommEvent( index->hComm, &dwCommEvent,
3010
3265
                        &index->sol ) )
3011
3266
                {
3012
3267
                        /* WaitCommEvent failed */
3013
3268
                        if( index->interrupt == 1 )
3014
3269
                        {
3015
 
                                MexPrintf("i");
3016
3270
                                goto end;
3017
3271
                        }
3018
3272
                        if ( GetLastError() != ERROR_IO_PENDING )
3019
3273
                        {
3020
3274
                                sprintf( message, "WaitCommEvent filename = %s\n", index->filename);
3021
3275
                                report( message );
3022
 
                                MexPrintf("-");
3023
3276
                                return(1);
3024
3277
/*
3025
3278
                                goto fail;
3029
3282
                }
3030
3283
                if( index->interrupt == 1 )
3031
3284
                {
3032
 
                        MexPrintf("i");
3033
3285
                        goto end;
3034
3286
                }
3035
3287
                wait = WaitForSingleObject( index->sol.hEvent, 1000 );
3036
3288
                switch ( wait )
3037
3289
                {
3038
3290
                        case WAIT_OBJECT_0:
3039
 
                                MexPrintf("o");
3040
3291
                                if( index->interrupt == 1 )
3041
3292
                                {
3042
 
                                        MexPrintf("i");
3043
3293
                                        goto end;
3044
3294
                                }
3045
3295
                                if( !index->sol.hEvent ) return(1);
3046
3296
                                if (!GetOverlappedResult( index->hComm,
3047
3297
                                        &index->sol, &nBytes, TRUE ))
3048
3298
                                {
3049
 
                                        MexPrintf("G");
3050
3299
                                        goto end;
3051
3300
                                }
3052
3301
                                else if( index->tx_happened == 1 )
3053
3302
                                {
3054
 
                                /*
3055
 
                                        ClearErrors( index, &Stat );
3056
 
                                        if ( (int ) Stat.cbOutQue == 0 )
3057
 
                                        {
3058
 
                                                MexPrintf(".");
3059
 
                                        }
3060
 
                                */
3061
3303
                                        goto end;
3062
3304
                                }
3063
3305
                                else
3064
3306
                                        goto end;
3065
3307
                                break;
3066
3308
                        case WAIT_TIMEOUT:
3067
 
                                MexPrintf("T");
3068
3309
                        default:
3069
 
                                MexPrintf("W");
3070
3310
                                return(1); /* WaitFor error */
3071
3311
                        
3072
3312
                }
3075
3315
/*
3076
3316
        usleep(1000);
3077
3317
*/
3078
 
        MexPrintf("<");
3079
 
#ifdef DEBUG_VERBOSE
3080
3318
        LEAVE( "serial_select" );
3081
 
#endif /* DEBUG_VERBOSE */
3082
3319
        return( 1 );
3083
3320
#ifdef asdf
3084
3321
        /* FIXME this needs to be cleaned up... */
3085
3322
fail:
3086
 
        MexPrintf("f<\n");
3087
3323
        sprintf( message, "< select called error %i\n", n );
3088
3324
        YACK();
3089
3325
        report( message );
3090
3326
        set_errno( EBADFD );
3091
 
#ifdef DEBUG_VERBOSE
3092
3327
        LEAVE( "serial_select" );
3093
 
#endif /* DEBUG_VERBOSE */
3094
3328
        return( 1 );
3095
3329
#endif /* asdf */
3096
3330
        
3097
3331
}
 
3332
#endif /* asdf */
 
3333
#endif /* __LCC__ */
3098
3334
 
3099
3335
/*----------------------------------------------------------
3100
3336
termiosSetParityError()
3114
3350
 
3115
3351
        ENTER( "termiosGetParityErrorChar" );
3116
3352
        index = find_port( fd );
 
3353
        if( !index )
 
3354
        {
 
3355
                LEAVE( "termiosGetParityErrorChar" );
 
3356
                return(-1);
 
3357
        }
3117
3358
        GetCommState( index->hComm, &dcb );
3118
3359
        LEAVE( "termiosGetParityErrorChar" );
3119
3360
        return( dcb.ErrorChar );
3135
3376
        DCB     dcb;
3136
3377
        struct termios_list *index;
3137
3378
 
 
3379
        ENTER( "termiosGetParityErrorChar" );
3138
3380
        index = find_port( fd );
3139
 
        ENTER( "termiosGetParityErrorChar" );
 
3381
        if ( !index )
 
3382
        {
 
3383
                LEAVE( "termiosSetParityError" );
 
3384
                return;
 
3385
        }
3140
3386
        GetCommState( index->hComm, &dcb );
3141
3387
        dcb.ErrorChar = value;
3142
3388
        SetCommState( index->hComm, &dcb );
3143
3389
        LEAVE( "termiosGetParityErrorChar" );
3144
3390
}
3145
3391
/*----------------------- END OF LIBRARY -----------------*/
3146
 
 
 
3392
#ifdef PLAYING_AROUND
3147
3393
static inline int inportb( int index )
3148
3394
{
3149
3395
   unsigned char value;
3150
 
  __asm__ volatile ("inb %1,%0"
 
3396
  __asm__ volatile ( "inb %1,%0"
3151
3397
                    : "=a" (value)
3152
 
                    : "d" ((unsigned short)index));
 
3398
                    : "d" ((unsigned short)index) );
3153
3399
   return value;
3154
3400
}
3155
3401
 
3161
3407
                    : "a" (val), "d" (index)
3162
3408
                    );
3163
3409
}
3164
 
 
3165
 
 
3166
 
#define O_RDONLY 00
3167
 
#define PORT_SERIAL 1
3168
 
/*
3169
 
        NT Port Enumeration.
3170
 
 
3171
 
        The basic idea is to open a port and try to read a byte from it.
3172
 
        The The read should time out and return 0.
3173
 
 
3174
 
        The hardware setup is:
3175
 
        
3176
 
        COM1 valid
3177
 
        COM2 valid
3178
 
        COM3 invalid
3179
 
        COM4 invalid
3180
 
 
3181
 
        If only COM3 is opened a valid reference for the port is obtained.
3182
 
        read times out.  The port looks valid.
3183
 
 
3184
 
        if COM1 is opened the open on COM3 fails but the open on COM4 works.
3185
 
        if COM2 is opened the open on COM4 fails but the open on COM3 works.
3186
 
 
3187
 
        The registry would be another approach.  The registry on NT shows
3188
 
        COM1-4 in the above configuration.
3189
 
 
3190
 
 
3191
 
*/
3192
 
#ifdef asdf
3193
 
int main( int argc, char *argv[] )
3194
 
{
3195
 
        struct termios ttyset;
3196
 
        int fd =open( "COM1" , O_RDONLY | CLOCAL);
3197
 
        int cspeed = translate_speed( env, speed );
3198
 
        if( !cspeed )
3199
 
        {
3200
 
                fprintf( stderr, "Invalid Speed Selected\n");
3201
 
                return;
3202
 
        }
3203
 
        if( tcgetattr( fd, &ttyset ) < 0 )
3204
 
        {
3205
 
                fprintf( stderr, "Cannot Get Serial Port Settings\n");
3206
 
                goto fail;
3207
 
        }
3208
 
        printf( "Set: %i\n", ttyset.c_cflag);
3209
 
                                        /* FIXME */
3210
 
        if( !translate_data_bits( env, &( ttyset.c_cflag), dataBits ) )
3211
 
        {
3212
 
                fprintf( stderr, "Invalid Data Bits Selected\n");
3213
 
                return;
3214
 
        }
3215
 
        if( !translate_stop_bits( env, &( ttyset.c_cflag), stopBits ) )
3216
 
        {
3217
 
                fprintf( stderr, "Invalid Stop Bits Selected\n");
3218
 
                return;
3219
 
        }
3220
 
        if( !translate_parity( env, &( ttyset.c_cflag), parity ) )
3221
 
        {
3222
 
                fprintf( stderr, "Invalid Parity Selected\n");
3223
 
                return;
3224
 
        }
3225
 
#ifdef __FreeBSD__
3226
 
        if( cfsetspeed( &ttyset, cspeed ) < 0 )
3227
 
        {
3228
 
                fprintf( stderr, "Cannot Set Speed\n");
3229
 
                goto fail;
3230
 
        }
3231
 
#else
3232
 
        if( cfsetispeed( &ttyset, cspeed ) < 0 )
3233
 
        {
3234
 
                fprintf( stderr, "Cannot Set Input Speed\n");
3235
 
                goto fail;
3236
 
        }
3237
 
        if( cfsetospeed( &ttyset, cspeed ) < 0 )
3238
 
        {
3239
 
                fprintf( stderr, "Cannot Set Output Speed\n");
3240
 
                goto fail;
3241
 
        }
3242
 
#endif  /* __FreeBSD__ */
3243
 
        if( tcsetattr( fd, TCSANOW, &ttyset ) < 0 )
3244
 
        {
3245
 
                fprintf( stderr, "Cannot Set Serial Port Parameters.\n");
3246
 
                goto fail;
3247
 
        }
3248
 
        return;
3249
 
 
3250
 
fail:
3251
 
        throw_java_exception( env, UNSUPPORTED_COMM_OPERATION,
3252
 
                "nativeSetSerialPortParams", strerror( errno ) );
3253
 
}
3254
 
#endif
3255
 
#ifdef asdf
3256
 
int main( int argc, char *argv[] )
3257
 
{
3258
 
        struct termios ttyset;
3259
 
        char c;
3260
 
        int fd[4];
3261
 
/*
3262
 
        char *name = "COM3";
3263
 
*/
3264
 
        int ret = 1;
3265
 
        int port_type = 1;
3266
 
 
3267
 
        /* CLOCAL eliminates open blocking on modem status lines */
3268
 
        printf("trying testRead()\n");
3269
 
        if ((fd[3] = open("COM1" , O_RDONLY | CLOCAL)) < 0) {
3270
 
                ret = 0;
3271
 
                goto END;
3272
 
        }
3273
 
/*
3274
 
        if ((fd[1] = open("COM2", O_RDONLY | CLOCAL)) < 0) {
3275
 
                ret = 0;
3276
 
                goto END;
3277
 
        }
3278
 
        if ((fd[3] = open("COM4", O_RDONLY | CLOCAL)) < 0) {
3279
 
                ret = 0;
3280
 
                goto END;
3281
 
        }
3282
 
        ret = write(fd[3],"test",4);
3283
 
        if ( ret < 0 )
3284
 
        {
3285
 
                ret = 0;
3286
 
                goto END;
3287
 
        }
3288
 
        ret = read(fd[3],name,1);
3289
 
        if ( ret < 0 )
3290
 
        {
3291
 
                ret = 0;
3292
 
                goto END;
3293
 
        }
3294
 
        ret = read(fd[3],name,4);
3295
 
        if ( ret < 0 )
3296
 
        {
3297
 
                ret = 0;
3298
 
                goto END;
3299
 
        }
3300
 
        if ((fd[2] = open("COM3", O_RDONLY | CLOCAL)) < 0) {
3301
 
                ret = 0;
3302
 
                goto END;
3303
 
        }
3304
 
*/
3305
 
 
3306
 
        if ( port_type == PORT_SERIAL )
3307
 
        {
3308
 
                int saved_flags;
3309
 
                struct termios saved_termios;
3310
 
 
3311
 
                if (tcgetattr(fd[3], &ttyset) < 0) {
3312
 
                        ret = 0;
3313
 
                        goto END;
3314
 
                }
3315
 
 
3316
 
                /* save, restore later */
3317
 
                if ((saved_flags = fcntl(fd[3], F_GETFL)) < 0) {
3318
 
                        ret = 0;
3319
 
                        goto END;
3320
 
                }
3321
 
 
3322
 
                memcpy(&saved_termios, &ttyset, sizeof(struct termios));
3323
 
 
3324
 
                if (fcntl(fd[3], F_SETFL, O_NONBLOCK) < 0) {
3325
 
                        ret = 0;
3326
 
                        goto END;
3327
 
                }
3328
 
 
3329
 
                cfmakeraw(&ttyset);
3330
 
                ttyset.c_cc[VMIN] = ttyset.c_cc[VTIME] = 0;
3331
 
 
3332
 
                if (tcsetattr(fd[3], TCSANOW, &ttyset) < 0) {
3333
 
                        ret = 0;
3334
 
                        tcsetattr(fd[3], TCSANOW, &saved_termios);
3335
 
                        goto END;
3336
 
                }
3337
 
                if (tcgetattr(fd[3], &ttyset) < 0) {
3338
 
                        ret = 0;
3339
 
                        goto END;
3340
 
                }
3341
 
                if (tcsetattr(fd[3], TCSANOW, &ttyset) < 0) {
3342
 
                        ret = 0;
3343
 
                        tcsetattr(fd[3], TCSANOW, &saved_termios);
3344
 
                        goto END;
3345
 
                }
3346
 
                if (tcgetattr(fd[3], &ttyset) < 0) {
3347
 
                        ret = 0;
3348
 
                        goto END;
3349
 
                }
3350
 
                if (read(fd[3], &c, 1) < 0)
3351
 
                {
3352
 
#ifdef EWOULDBLOCK
3353
 
                        if ( errno != EWOULDBLOCK )
3354
 
                        {
3355
 
                                ret = 0;
3356
 
                        }
3357
 
#else
3358
 
                        ret = 0;
3359
 
#endif /* EWOULDBLOCK */
3360
 
                }
3361
 
 
3362
 
                /* dont walk over unlocked open devices */
3363
 
                tcsetattr(fd[3], TCSANOW, &saved_termios);
3364
 
                fcntl(fd[3], F_SETFL, saved_flags);
3365
 
        }
3366
 
END:
3367
 
        close(fd[3]);
3368
 
        return ret;
3369
 
}
3370
 
#endif /* asdf */
3371
 
#ifdef asdf
3372
 
int main( int argc, char *argv[] )
3373
 
{
3374
 
        struct termios ttyset;
3375
 
        int fd = open("COM1", 1);
3376
 
/*
3377
 
        int cspeed = translate_speed( env, speed );
3378
 
*/
3379
 
        int cspeed = B9600;
3380
 
        for(;;)
3381
 
        {
3382
 
                if( !cspeed ) return;
3383
 
                if( tcgetattr( fd, &ttyset ) < 0 ) goto fail;
3384
 
                ttyset.c_cflag &= ~CSIZE;
3385
 
                ttyset.c_cflag |= CS8;
3386
 
                ttyset.c_cflag |= PARENB;
3387
 
                ttyset.c_cflag &= ~CSTOPB;
3388
 
#ifdef __FreeBSD__
3389
 
                if( cfsetspeed( &ttyset, cspeed ) < 0 ) goto fail;
3390
 
#else
3391
 
                if( cfsetispeed( &ttyset, cspeed ) < 0 ) goto fail;
3392
 
                if( cfsetospeed( &ttyset, cspeed ) < 0 ) goto fail;
3393
 
#endif  /* __FreeBSD__ */
3394
 
                if( tcsetattr( fd, TCSANOW, &ttyset ) < 0 ) goto fail;
3395
 
                printf(".");
3396
 
        }
3397
 
fail:
3398
 
        return;
3399
 
}
3400
 
 
3401
 
int main( int argc, char *argv[] )
3402
 
{
3403
 
        int fd[4] = { 0,0,0,0 };
3404
 
        int res, seed, foo;
3405
 
        char file[8], vb[80];
3406
 
        int printflag = 0;
3407
 
 
3408
 
        fd_set  *readfds;
3409
 
        fd_set  *writefds;
3410
 
        fd_set *exceptfds;
3411
 
        struct timeval *timeout;
3412
 
        int Fd, ret, change;
3413
 
        fd_set rfds;
3414
 
        struct timeval tv_sleep;
3415
 
        unsigned int mflags, omflags;
3416
 
#undef TIOCSERGETLSR
3417
 
#if defined TIOCSERGETLSR
3418
 
        struct stat fstatbuf;
3419
 
#endif /* TIOCSERGETLSR */
3420
 
 
3421
 
#if defined(TIOCGICOUNT)
3422
 
        struct serial_icounter_struct sis, osis;
3423
 
        /* JK00: flag if this can be used on this port */
3424
 
        int has_tiocgicount = 1;
3425
 
#endif /* TIOCGICOUNT */
3426
 
 
3427
 
#if defined(TIOCSERGETLSR)
3428
 
        int has_tiocsergetlsr = 1;
3429
 
#endif /* TIOCSERGETLSR */
3430
 
 
3431
 
 
3432
 
        Fd = open( "COM1", 1 );
3433
 
#if defined(TIOCGICOUNT)
3434
 
        /* Some multiport serial cards do not implement TIOCGICOUNT ... */
3435
 
        /* So use the 'dumb' mode to enable using them after all! JK00 */
3436
 
        if( ioctl( Fd, TIOCGICOUNT, &osis ) < 0 )
3437
 
        {
3438
 
                report("Port does not support TIOCGICOUNT events\n" );
3439
 
                has_tiocgicount = 0;
3440
 
        }
3441
 
#endif /*  TIOCGICOUNT */
3442
 
 
3443
 
#if defined(TIOCSERGETLSR)
3444
 
        /* JK00: work around for multiport cards without TIOCSERGETLSR */
3445
 
        /* Cyclades is one of those :-(                                */
3446
 
        if( ioctl( Fd, TIOCSERGETLSR, &change ) )
3447
 
        {
3448
 
                report("Port does not support TIOCSERGETLSR\n" );
3449
 
                        has_tiocsergetlsr = 0;
3450
 
        }
3451
 
#endif /* TIOCSERGETLSR */
3452
 
 
3453
 
        if( ioctl( Fd, TIOCMGET, &omflags) <0 )
3454
 
        {
3455
 
                report("Port does not support events\n" );
3456
 
                return;
3457
 
        }
3458
 
 
3459
 
        FD_ZERO( &rfds );
3460
 
        while( 1 )
3461
 
        {
3462
 
                printf(".");
3463
 
                FD_SET( Fd, &rfds );
3464
 
                tv_sleep.tv_sec = 0;
3465
 
                tv_sleep.tv_usec = 100000;
3466
 
                do {
3467
 
                        ret=serial_select( Fd + 1, &rfds, NULL, NULL, &tv_sleep );
3468
 
                }  while (ret < 0 && errno==EINTR);
3469
 
                if( ret < 0 ) break;
3470
 
 
3471
 
#if defined TIOCSERGETLSR
3472
 
                /* JK00: work around for Multi IO cards without TIOCSERGETLSR */
3473
 
                if( has_tiocsergetlsr )
3474
 
                {
3475
 
                        if (fstat(Fd, &fstatbuf))  break;
3476
 
                        if( ioctl( Fd, TIOCSERGETLSR, &change ) ) break;
3477
 
                        else if( change )
3478
 
                        {
3479
 
                                send_event( env, jobj, SPE_OUTPUT_BUFFER_EMPTY,
3480
 
                                        1 );
3481
 
                        }
3482
 
                }
3483
 
#endif /* TIOCSERGETLSR */
3484
 
#if defined(TIOCGICOUNT)
3485
 
        /*      wait for RNG, DSR, CD or CTS  but not DataAvailable
3486
 
         *      The drawback here is it never times out so if someone
3487
 
         *      reads there will be no chance to try again.
3488
 
         *      This may make sense if the program does not want to
3489
 
         *      be notified of data available or errors.
3490
 
         *      ret=ioctl(Fd,TIOCMIWAIT);
3491
 
         */
3492
 
                /* JK00: only use it if supported by this port */
3493
 
                if (has_tiocgicount)
3494
 
                {
3495
 
                        if( ioctl( Fd, TIOCGICOUNT, &sis ) ) break;
3496
 
                        while( sis.frame != osis.frame )
3497
 
                        {
3498
 
                                send_event( env, jobj, SPE_FE, 1);
3499
 
                                osis.frame++;
3500
 
                        }
3501
 
                        while( sis.overrun != osis.overrun )
3502
 
                        {
3503
 
                                send_event( env, jobj, SPE_OE, 1);
3504
 
                                osis.overrun++;
3505
 
                        }
3506
 
                        while( sis.parity != osis.parity )
3507
 
                        {
3508
 
                                send_event( env, jobj, SPE_PE, 1);
3509
 
                                osis.parity++;
3510
 
                        }
3511
 
                        while( sis.brk != osis.brk )
3512
 
                        {
3513
 
                                send_event( env, jobj, SPE_BI, 1);
3514
 
                                osis.brk++;
3515
 
                        }
3516
 
                        osis = sis;
3517
 
                }
3518
 
#endif /*  TIOCGICOUNT */
3519
 
               /* A Portable implementation */
3520
 
 
3521
 
                if( ioctl( Fd, TIOCMGET, &mflags ) ) break;
3522
 
 
3523
 
                change = (mflags&TIOCM_CTS) - (omflags&TIOCM_CTS);
3524
 
                if( change ) send_event( env, jobj, SPE_CTS, change );
3525
 
 
3526
 
                change = (mflags&TIOCM_DSR) - (omflags&TIOCM_DSR);
3527
 
                if( change ) send_event( env, jobj, SPE_DSR, change );
3528
 
 
3529
 
                change = (mflags&TIOCM_RNG) - (omflags&TIOCM_RNG);
3530
 
                if( change ) send_event( env, jobj, SPE_RI, change );
3531
 
 
3532
 
                change = (mflags&TIOCM_CD) - (omflags&TIOCM_CD);
3533
 
                if( change ) send_event( env, jobj, SPE_CD, change );
3534
 
 
3535
 
                omflags = mflags;
3536
 
 
3537
 
                ioctl( Fd, FIONREAD, &change );
3538
 
                if( change )
3539
 
                {
3540
 
                        if(!send_event( env, jobj, SPE_DATA_AVAILABLE, 1 ))
3541
 
                        {
3542
 
/*
3543
 
                                usleep(100000);  select wont block 
3544
 
                                usleep(100);
3545
 
*/
3546
 
                        }
3547
 
                }
3548
 
        }
3549
 
#ifdef asdf
3550
 
 
3551
 
        res = open( "COM1", 1 );
3552
 
        seed = open( "COM2", 1 );
3553
 
        if ( res >= 0 )
3554
 
        {
3555
 
                strcpy(vb,"test");
3556
 
                for(foo= 0;;foo++)
3557
 
                {
3558
 
                        if( foo%100 == 0 ) printf("%i\n", foo);
3559
 
                        serial_select(res + 1, readfds, writefds, exceptfds, timeout);
3560
 
                        serial_select(seed + 1, readfds, writefds, exceptfds, timeout);
3561
 
                        serial_write(res, vb, 4);
3562
 
                        serial_write(seed, vb, 4);
3563
 
                }
3564
 
        }
3565
 
#endif/* asdf */
3566
 
#ifdef asdf
3567
 
        res = open( "COM1", 1 );
3568
 
        seed = open( "COM2", 1 );
3569
 
        if ( res <= 0 )
3570
 
        {
3571
 
                strcpy(vb,"test");
3572
 
                for(foo= 0;;foo++)
3573
 
                {
3574
 
                        if( foo%100 == 0 ) printf("%i\n", foo);
3575
 
                        serial_write(seed, vb, 4);
3576
 
                        serial_read(res, file, 4);
3577
 
                }
3578
 
        }
3579
 
        else
3580
 
        {
3581
 
                strcpy(vb,"test");
3582
 
                for(foo= 0;;foo++)
3583
 
                {
3584
 
                        if( foo%100 == 0 ) printf("%i\n", foo);
3585
 
                        serial_write(seed, vb, 4);
3586
 
                        serial_read(res, file,  4);
3587
 
                }
3588
 
        }
3589
 
#endif/* asdf */
3590
 
#ifdef asdf
3591
 
        for(;;)
3592
 
        {
3593
 
                res = open( "COM1", 1 );
3594
 
                if ( res <= 0 )
3595
 
                {
3596
 
                        printf("Open Failed\n");
3597
 
                }
3598
 
                else
3599
 
                {
3600
 
                        serial_read(res, vb,  1);
3601
 
                        res = close(res);
3602
 
                        if ( res != 0 )
3603
 
                                printf("close Failed\n");
3604
 
                }
3605
 
                res = open( "COM2", 1 );
3606
 
                if ( res <= 0 )
3607
 
                {
3608
 
                        printf("Open Failed\n");
3609
 
                }
3610
 
                else
3611
 
                {
3612
 
                        serial_read(res, vb,  1);
3613
 
                        res = close(res);
3614
 
                        if ( res != 0 )
3615
 
                                printf("close Failed\n");
3616
 
                }
3617
 
                res = open( "COM3", 1 );
3618
 
                if ( res <= 0 )
3619
 
                {
3620
 
                        printf("Open Failed\n");
3621
 
                }
3622
 
                else
3623
 
                {
3624
 
                        serial_read(res, vb,  1);
3625
 
                        res = close(res);
3626
 
                        if ( res != 0 )
3627
 
                                printf("close Failed\n");
3628
 
                }
3629
 
                res = open( "COM4", 1 );
3630
 
                if ( res <= 0 )
3631
 
                {
3632
 
                        printf("Open Failed\n");
3633
 
                }
3634
 
                else
3635
 
                {
3636
 
                        serial_read(res, vb,  1);
3637
 
                        res = close(res);
3638
 
                        if ( res != 0 )
3639
 
                                printf("close Failed\n");
3640
 
                }
3641
 
                printf(".");
3642
 
        }
3643
 
#endif /* asdf */
3644
 
#ifdef asdf
3645
 
        for(;;)
3646
 
        {
3647
 
 
3648
 
                seed = (int) (4.0*rand()/RAND_MAX + 1.0);
3649
 
                foo  = (int) (2*rand()/RAND_MAX + 1.0);
3650
 
                res = -1;
3651
 
 
3652
 
                if( foo == 1 )
3653
 
                {
3654
 
                        if ( fd[ seed - 1] == 0 )
3655
 
                        {
3656
 
                                sprintf(file, "COM%i",
3657
 
                                        seed );
3658
 
                                res = open( file, 1 );
3659
 
                                if(res > 0 )
3660
 
                                {
3661
 
                                        fd[ seed - 1] = res;
3662
 
                                }
3663
 
                                res = -1;
3664
 
/*
3665
 
                                serial_read(res, vb,  1);
3666
 
*/
3667
 
                                printflag = 1;
3668
 
                        }
3669
 
                }
3670
 
                else
3671
 
                {
3672
 
                        if ( fd[ seed - 1] != 0 )
3673
 
                        {
3674
 
                                res = close ( fd[ seed - 1 ] );
3675
 
                                if (res != -1)
3676
 
                                        fd[ seed - 1 ] = 0;
3677
 
                                res = -1;
3678
 
                                printflag = 1;
3679
 
                        }
3680
 
                }
3681
 
                if ( printflag )
3682
 
                {
3683
 
                        printf("\n%5i %5i %5i %5i",fd[0],fd[1],fd[2],fd[3] );
3684
 
                        printflag = 0;
3685
 
                }
3686
 
                /* blah */
3687
 
                usleep( 1000 );
3688
 
        }
3689
 
#endif /* asdf */
3690
 
}
3691
 
#endif /* asdf */
3692
 
#endif
 
3410
#endif /* PLAYING_AROUND */