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

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

More minor improvements to the serial server, in particular improved timeout recovery.

File size: 2.9 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( -1, "No physical COM ports found" );
43    }
44
45    if( !strcmp( name, "PIPE" ) )
46    {
47        log( 0, "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( -1, "Could not CreateNamedPipe " PIPENAME );
52       
53        if( !ConnectNamedPipe( pipe, NULL ) )
54            log( -1, "Could not ConnectNamedPipe" );
55
56        if( baudRate->divisor > 3 )
57            log( -1, "Cannot simulate baud rates with hardware multipliers" );
58
59        speedEmulation = 1;
60        resetConnection = 1;
61    }
62    else
63    {
64        if( QueryDosDeviceA( name, buff2, sizeof(buff2) ) )
65        {
66            log( 0, "Opening %s (%lu baud)", name, baudRate->rate );
67           
68            pipe = CreateFileA( name, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
69            if( !pipe )
70                log( -1, "Could not Open \"%s\"", name );
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 ) )
79                log( -1, "Could not SetCommState" );
80
81            if( !SetCommTimeouts( pipe, &timeouts ) )
82                log( -1, "Could not SetCommTimeouts" );
83        }
84        else
85        {
86            char logbuff[ 1024 ];
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++ )
92            {
93                sprintf( buff1, "COM%d", t );
94                if( QueryDosDeviceA( buff1, buff2, sizeof(buff2) ) )
95                {
96                    strcat( logbuff, "\n    " );
97                    strcat( logbuff, buff1 );
98                    found = 1;
99                }
100            }
101            if( !found )
102                strcat( logbuff, "\n    (none)" );
103
104            log( -1, logbuff );
105        }
106    }
107}
108
109unsigned 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
117unsigned 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
Note: See TracBrowser for help on using the repository browser.