Changeset 219 in xtideuniversalbios for trunk/Serial_Server/win32
- Timestamp:
- Jan 25, 2012, 7:04:43 AM (13 years ago)
- google:author:
- gregli@hotmail.com
- Location:
- trunk/Serial_Server/win32
- Files:
-
- 1 added
- 1 deleted
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Serial_Server/win32/Win32.cpp
r217 r219 16 16 #include "../library/library.h" 17 17 #include "../library/flatimage.h" 18 #include "Win32Serial.h"19 18 20 19 void usage(void) … … 30 29 "", 31 30 " -n [megabytes] Create new disk with given size or use -g geometry", 32 " Maximum size is 137.4 GB (LBA28 limit)",31 " Maximum size is " USAGE_MAXSECTORS, 33 32 " (default is a 32 MB disk, with CHS geometry 65:63:16)", 34 33 "", … … 74 73 unsigned short wbuff[256]; 75 74 76 Serial *serial;75 SerialAccess serial; 77 76 Image *img; 78 77 struct baudRate *baudRate = NULL; … … 178 177 do 179 178 { 180 serial = new Win32Serial( ComPort, baudRate );181 182 processRequests( serial, images[0], images[1], timeoutEnabled, verbose );183 184 delete serial;185 186 if( serial ->resetConnection )187 log( 0, " Connection closed, reset..." );188 } 189 while( serial ->resetConnection );179 serial.Connect( ComPort, baudRate ); 180 181 processRequests( &serial, images[0], images[1], timeoutEnabled, verbose ); 182 183 serial.Disconnect(); 184 185 if( serial.resetConnection ) 186 log( 0, "Serial Connection closed, reset..." ); 187 } 188 while( serial.resetConnection ); 190 189 } 191 190 -
trunk/Serial_Server/win32/Win32Serial.h
r209 r219 6 6 // 7 7 8 #include <stdio.h> 8 9 #include "windows.h" 9 10 #include "../library/library.h" … … 11 12 #define PIPENAME "\\\\.\\pipe\\xtide" 12 13 13 class Win32Serial : public Serial14 class SerialAccess 14 15 { 15 16 public: 16 Win32Serial( char *name, struct baudRate *baudRate ); 17 ~Win32Serial(); 17 void Connect( char *name, struct baudRate *p_baudRate ) 18 { 19 char buff1[20], buff2[1024]; 18 20 19 unsigned long readCharacters( void *buff, unsigned long len ); 20 unsigned long writeCharacters( void *buff, unsigned long len ); 21 baudRate = p_baudRate; 22 23 pipe = NULL; 24 25 if( !name ) 26 { 27 for( int t = 1; t <= 30 && !name; t++ ) 28 { 29 sprintf( buff1, "COM%d", t ); 30 if( QueryDosDeviceA( buff1, buff2, sizeof(buff2) ) ) 31 name = buff1; 32 } 33 if( !name ) 34 log( -1, "No physical COM ports found" ); 35 } 36 37 if( !strcmp( name, "PIPE" ) ) 38 { 39 log( 0, "Opening named pipe %s (simulating %lu baud)", PIPENAME, baudRate->rate ); 40 41 pipe = CreateNamedPipeA( PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE|PIPE_REJECT_REMOTE_CLIENTS, 2, 1024, 1024, 0, NULL ); 42 if( !pipe ) 43 log( -1, "Could not CreateNamedPipe " PIPENAME ); 44 45 if( !ConnectNamedPipe( pipe, NULL ) ) 46 log( -1, "Could not ConnectNamedPipe" ); 47 48 if( baudRate->divisor > 3 ) 49 log( -1, "Cannot simulate baud rates with hardware multipliers" ); 50 51 speedEmulation = 1; 52 resetConnection = 1; 53 } 54 else 55 { 56 if( QueryDosDeviceA( name, buff2, sizeof(buff2) ) ) 57 { 58 COMMTIMEOUTS timeouts; 59 DCB dcb; 60 61 log( 0, "Opening %s (%lu baud)", name, baudRate->rate ); 62 63 pipe = CreateFileA( name, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 ); 64 if( !pipe ) 65 log( -1, "Could not Open \"%s\"", name ); 66 67 FillMemory(&dcb, sizeof(dcb), 0); 68 FillMemory(&timeouts, sizeof(timeouts), 0); 69 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 106 void Disconnect() 107 { 108 if( pipe ) 109 { 110 CloseHandle( pipe ); 111 pipe = NULL; 112 } 113 } 114 115 unsigned long readCharacters( void *buff, unsigned long len ) 116 { 117 unsigned long readLen; 118 int ret; 119 120 ret = ReadFile( pipe, buff, len, &readLen, NULL ); 121 122 if( !ret || readLen == 0 ) 123 { 124 if( GetLastError() == ERROR_BROKEN_PIPE ) 125 return( 0 ); 126 else 127 log( -1, "read serial failed (error code %d)", GetLastError() ); 128 } 129 130 return( readLen ); 131 } 132 133 int writeCharacters( void *buff, unsigned long len ) 134 { 135 unsigned long writeLen; 136 int ret; 137 138 ret = WriteFile( pipe, buff, len, &writeLen, NULL ); 139 140 if( !ret || len != writeLen ) 141 { 142 if( GetLastError() == ERROR_BROKEN_PIPE ) 143 return( 0 ); 144 else 145 log( -1, "write serial failed (error code %d)", GetLastError() ); 146 } 147 148 return( 1 ); 149 } 150 151 SerialAccess() 152 { 153 pipe = NULL; 154 speedEmulation = 0; 155 resetConnection = 0; 156 baudRate = NULL; 157 } 158 159 ~SerialAccess() 160 { 161 Disconnect(); 162 } 163 164 int speedEmulation; 165 int resetConnection; 166 167 struct baudRate *baudRate; 21 168 22 169 private:
Note:
See TracChangeset
for help on using the changeset viewer.