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

Last change on this file since 214 was 211, checked in by gregli@…, 13 years ago

More minor changes, improved usage message

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( -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 speedEmulation = 1;
57 resetConnection = 1;
58 }
59 else
60 {
61 if( QueryDosDeviceA( name, buff2, sizeof(buff2) ) )
62 {
63 log( 0, "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( -1, "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( -1, "Could not SetCommState" );
77
78 if( !SetCommTimeouts( pipe, &timeouts ) )
79 log( -1, "Could not SetCommTimeouts" );
80 }
81 else
82 {
83 char logbuff[ 1024 ];
84 int found = 0;
85
86 sprintf( logbuff, "serial port '%s' not found, detected COM ports:", name );
87
88 for( int t = 1; t <= 40; t++ )
89 {
90 sprintf( buff1, "COM%d", t );
91 if( QueryDosDeviceA( buff1, buff2, sizeof(buff2) ) )
92 {
93 strcat( logbuff, "\n " );
94 strcat( logbuff, buff1 );
95 found = 1;
96 }
97 }
98 if( !found )
99 strcat( logbuff, "\n (none)" );
100
101 log( -1, logbuff );
102 }
103 }
104}
105
106unsigned long Win32Serial::readCharacters( void *buff, unsigned long len )
107{
108 unsigned long readLen;
109 int ret;
110 ret = ReadFile( pipe, buff, len, &readLen, NULL );
111 return( ret ? readLen : 0 );
112}
113
114unsigned long Win32Serial::writeCharacters( void *buff, unsigned long len )
115{
116 unsigned long writeLen;
117 int ret;
118 ret = WriteFile( pipe, buff, len, &writeLen, NULL );
119 return( ret ? writeLen : 0 );
120}
121
122
123
124
125
126
127
Note: See TracBrowser for help on using the repository browser.