[209] | 1 | //======================================================================
|
---|
| 2 | //
|
---|
| 3 | // Project: XTIDE Universal BIOS, Serial Port Server
|
---|
| 4 | //
|
---|
| 5 | // File: Win32Serial.cpp - Microsoft Windows serial code
|
---|
| 6 | //
|
---|
| 7 | // Support for the serial port under Win32.
|
---|
| 8 | //
|
---|
| 9 |
|
---|
| 10 | #include <stdio.h>
|
---|
| 11 |
|
---|
| 12 | #include "..\library\library.h"
|
---|
| 13 | #include "Win32Serial.h"
|
---|
| 14 |
|
---|
| 15 | COMMTIMEOUTS timeouts;
|
---|
| 16 |
|
---|
| 17 | DCB dcb;
|
---|
| 18 |
|
---|
| 19 | Win32Serial::~Win32Serial()
|
---|
| 20 | {
|
---|
| 21 | if( pipe )
|
---|
| 22 | CloseHandle( pipe );
|
---|
| 23 |
|
---|
| 24 | pipe = NULL;
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | Win32Serial::Win32Serial( char *name, struct baudRate *baudRate ) : Serial( name, baudRate )
|
---|
| 28 | {
|
---|
| 29 | char buff1[20], buff2[1024];
|
---|
| 30 |
|
---|
| 31 | pipe = NULL;
|
---|
| 32 |
|
---|
| 33 | if( !name )
|
---|
| 34 | {
|
---|
| 35 | for( int t = 1; t <= 30 && !name; t++ )
|
---|
| 36 | {
|
---|
| 37 | sprintf( buff1, "COM%d", t );
|
---|
| 38 | if( QueryDosDeviceA( buff1, buff2, sizeof(buff2) ) )
|
---|
| 39 | name = buff1;
|
---|
| 40 | }
|
---|
| 41 | if( !name )
|
---|
[211] | 42 | log( -1, "No physical COM ports found" );
|
---|
[209] | 43 | }
|
---|
| 44 |
|
---|
| 45 | if( !strcmp( name, "PIPE" ) )
|
---|
| 46 | {
|
---|
[211] | 47 | log( 0, "Opening named pipe %s (simulating %lu baud)", PIPENAME, baudRate->rate );
|
---|
[209] | 48 |
|
---|
| 49 | pipe = CreateNamedPipeA( PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE|PIPE_REJECT_REMOTE_CLIENTS, 2, 1024, 1024, 0, NULL );
|
---|
| 50 | if( !pipe )
|
---|
[211] | 51 | log( -1, "Could not CreateNamedPipe " PIPENAME );
|
---|
[209] | 52 |
|
---|
| 53 | if( !ConnectNamedPipe( pipe, NULL ) )
|
---|
[211] | 54 | log( -1, "Could not ConnectNamedPipe" );
|
---|
[209] | 55 |
|
---|
[215] | 56 | if( baudRate->divisor > 3 )
|
---|
| 57 | log( -1, "Cannot simulate baud rates with hardware multipliers" );
|
---|
| 58 |
|
---|
[209] | 59 | speedEmulation = 1;
|
---|
| 60 | resetConnection = 1;
|
---|
| 61 | }
|
---|
| 62 | else
|
---|
| 63 | {
|
---|
| 64 | if( QueryDosDeviceA( name, buff2, sizeof(buff2) ) )
|
---|
| 65 | {
|
---|
[211] | 66 | log( 0, "Opening %s (%lu baud)", name, baudRate->rate );
|
---|
[209] | 67 |
|
---|
| 68 | pipe = CreateFileA( name, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
|
---|
| 69 | if( !pipe )
|
---|
[211] | 70 | log( -1, "Could not Open \"%s\"", name );
|
---|
[209] | 71 |
|
---|
| 72 | FillMemory(&dcb, sizeof(dcb), 0);
|
---|
| 73 | dcb.DCBlength = sizeof(dcb);
|
---|
| 74 | dcb.BaudRate = baudRate->rate;
|
---|
| 75 | dcb.ByteSize = 8;
|
---|
| 76 | dcb.StopBits = ONESTOPBIT;
|
---|
| 77 | dcb.Parity = NOPARITY;
|
---|
| 78 | if( !SetCommState( pipe, &dcb ) )
|
---|
[211] | 79 | log( -1, "Could not SetCommState" );
|
---|
[209] | 80 |
|
---|
| 81 | if( !SetCommTimeouts( pipe, &timeouts ) )
|
---|
[211] | 82 | log( -1, "Could not SetCommTimeouts" );
|
---|
[209] | 83 | }
|
---|
| 84 | else
|
---|
| 85 | {
|
---|
| 86 | char logbuff[ 1024 ];
|
---|
[211] | 87 | int found = 0;
|
---|
| 88 |
|
---|
| 89 | sprintf( logbuff, "serial port '%s' not found, detected COM ports:", name );
|
---|
| 90 |
|
---|
| 91 | for( int t = 1; t <= 40; t++ )
|
---|
[209] | 92 | {
|
---|
| 93 | sprintf( buff1, "COM%d", t );
|
---|
| 94 | if( QueryDosDeviceA( buff1, buff2, sizeof(buff2) ) )
|
---|
| 95 | {
|
---|
[211] | 96 | strcat( logbuff, "\n " );
|
---|
[209] | 97 | strcat( logbuff, buff1 );
|
---|
[211] | 98 | found = 1;
|
---|
[209] | 99 | }
|
---|
| 100 | }
|
---|
[211] | 101 | if( !found )
|
---|
| 102 | strcat( logbuff, "\n (none)" );
|
---|
| 103 |
|
---|
| 104 | log( -1, logbuff );
|
---|
[209] | 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | unsigned long Win32Serial::readCharacters( void *buff, unsigned long len )
|
---|
| 110 | {
|
---|
| 111 | unsigned long readLen;
|
---|
| 112 | int ret;
|
---|
| 113 | ret = ReadFile( pipe, buff, len, &readLen, NULL );
|
---|
| 114 | return( ret ? readLen : 0 );
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | unsigned long Win32Serial::writeCharacters( void *buff, unsigned long len )
|
---|
| 118 | {
|
---|
| 119 | unsigned long writeLen;
|
---|
| 120 | int ret;
|
---|
| 121 | ret = WriteFile( pipe, buff, len, &writeLen, NULL );
|
---|
| 122 | return( ret ? writeLen : 0 );
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 |
|
---|
| 129 |
|
---|
| 130 |
|
---|