source: xtideuniversalbios/trunk/Serial_Server/win32/Win32Serial.cpp @ 209

Last change on this file since 209 was 209, checked in by gregli@…, 12 years ago

Initial checkin for the Serial Server code, to be run on a host computer with a hard disk image file. Connected via a serial line, this provides the I/O for the serial port support in the XTIDE bios. At present, this is a Win32 command line program, run without parameters for usage information.

File size: 2.8 KB
Line 
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
15COMMTIMEOUTS timeouts;
16
17DCB dcb;
18
19Win32Serial::~Win32Serial()
20{
21    if( pipe )
22        CloseHandle( pipe );
23
24    pipe = NULL;
25}
26
27Win32Serial::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 )
42            log( 0, "No Physical COM Ports Found" );
43    }
44
45    if( !strcmp( name, "PIPE" ) )
46    {
47        log( 1, "Opening named pipe %s (simulating %lu baud)", PIPENAME, baudRate->rate );
48       
49        pipe = CreateNamedPipeA( PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE|PIPE_REJECT_REMOTE_CLIENTS, 2, 1024, 1024, 0, NULL );
50        if( !pipe )
51            log( 0, "Could not CreateNamedPipe " PIPENAME );
52       
53        if( !ConnectNamedPipe( pipe, NULL ) )
54            log( 0, "Could not ConnectNamedPipe" );
55
56        speedEmulation = 1;
57        resetConnection = 1;
58    }
59    else
60    {
61        if( QueryDosDeviceA( name, buff2, sizeof(buff2) ) )
62        {
63            log( 1, "Opening %s (%lu baud)", name, baudRate->rate );
64           
65            pipe = CreateFileA( name, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
66            if( !pipe )
67                log( 0, "Could not Open \"%s\"", name );
68           
69            FillMemory(&dcb, sizeof(dcb), 0);
70            dcb.DCBlength = sizeof(dcb);
71            dcb.BaudRate = baudRate->rate;
72            dcb.ByteSize = 8;
73            dcb.StopBits = ONESTOPBIT;
74            dcb.Parity = NOPARITY;
75            if( !SetCommState( pipe, &dcb ) )
76                log( 0, "Could not SetCommState" );
77
78            if( !SetCommTimeouts( pipe, &timeouts ) )
79                log( 0, "Could not SetCommTimeouts" );
80        }
81        else
82        {
83            int first = 1;
84            char logbuff[ 1024 ];
85            sprintf( logbuff, "Serial Port Not Found \"%s\", Available: ", name );
86            for( int t = 1; t <= 30 && !name; t++ )
87            {
88                sprintf( buff1, "COM%d", t );
89                if( QueryDosDeviceA( buff1, buff2, sizeof(buff2) ) )
90                {
91                    if( !first )
92                        strcat( logbuff, ", " );
93                    else
94                        first = 0;
95                    strcat( logbuff, buff1 );
96                }
97            }
98            log( 0, logbuff );
99        }
100    }
101}
102
103unsigned long Win32Serial::readCharacters( void *buff, unsigned long len )
104{
105    unsigned long readLen;
106    int ret;
107    ret = ReadFile( pipe, buff, len, &readLen, NULL );
108    return( ret ? readLen : 0 );
109}
110
111unsigned long Win32Serial::writeCharacters( void *buff, unsigned long len )
112{
113    unsigned long writeLen;
114    int ret;
115    ret = WriteFile( pipe, buff, len, &writeLen, NULL );
116    return( ret ? writeLen : 0 );
117}
118
119
120
121
122
123
124
Note: See TracBrowser for help on using the repository browser.