1919using Android . Views ;
2020using Android . Widget ;
2121using Android . Util ;
22+ using Java . Util ;
2223
2324namespace Hoho . Android . UsbSerial . Driver
2425{
2526 public class Ch34xSerialDriver : UsbSerialDriver
2627 {
27- private readonly string TAG = typeof ( ProlificSerialDriver ) . Name ;
28+ private readonly string TAG = typeof ( ProlificSerialDriver ) . Name ;
2829
2930 public Ch34xSerialDriver ( UsbDevice device )
3031 {
@@ -38,6 +39,20 @@ public class Ch340SerialPort : CommonUsbSerialPort
3839
3940 private int DEFAULT_BAUD_RATE = 9600 ;
4041
42+ private const int SCL_DTR = 0x20 ;
43+ private const int SCL_RTS = 0x40 ;
44+ private const int LCR_ENABLE_RX = 0x80 ;
45+ private const int LCR_ENABLE_TX = 0x40 ;
46+ private const int LCR_STOP_BITS_2 = 0x04 ;
47+ private const int LCR_CS8 = 0x03 ;
48+ private const int LCR_CS7 = 0x02 ;
49+ private const int LCR_CS6 = 0x01 ;
50+ private const int LCR_CS5 = 0x00 ;
51+
52+ private const int LCR_MARK_SPACE = 0x20 ;
53+ private const int LCR_PAR_EVEN = 0x10 ;
54+ private const int LCR_ENABLE_PAR = 0x08 ;
55+
4156 private Boolean dtr = false ;
4257 private Boolean rts = false ;
4358
@@ -85,9 +100,9 @@ public override void Open(UsbDeviceConnection connection)
85100 for ( int i = 0 ; i < dataIface . EndpointCount ; i ++ )
86101 {
87102 UsbEndpoint ep = dataIface . GetEndpoint ( i ) ;
88- if ( ep . Type == ( UsbAddressing ) UsbSupport . UsbEndpointXferBulk )
103+ if ( ep . Type == ( UsbAddressing ) UsbSupport . UsbEndpointXferBulk )
89104 {
90- if ( ep . Direction == ( UsbAddressing ) UsbSupport . UsbDirIn )
105+ if ( ep . Direction == ( UsbAddressing ) UsbSupport . UsbDirIn )
91106 {
92107 mReadEndpoint = ep ;
93108 }
@@ -202,16 +217,16 @@ public override int Write(byte[] src, int timeoutMillis)
202217
203218 private int ControlOut ( int request , int value , int index )
204219 {
205- int REQTYPE_HOST_TO_DEVICE = 0x41 ;
206- return mConnection . ControlTransfer ( ( UsbAddressing ) REQTYPE_HOST_TO_DEVICE , request ,
220+ int REQTYPE_HOST_TO_DEVICE = UsbConstants . UsbTypeVendor | UsbSupport . UsbDirOut ;
221+ return mConnection . ControlTransfer ( ( UsbAddressing ) REQTYPE_HOST_TO_DEVICE , request ,
207222 value , index , null , 0 , USB_TIMEOUT_MILLIS ) ;
208223 }
209224
210225
211226 private int ControlIn ( int request , int value , int index , byte [ ] buffer )
212227 {
213228 int REQTYPE_HOST_TO_DEVICE = UsbConstants . UsbTypeVendor | UsbSupport . UsbDirIn ;
214- return mConnection . ControlTransfer ( ( UsbAddressing ) REQTYPE_HOST_TO_DEVICE , request ,
229+ return mConnection . ControlTransfer ( ( UsbAddressing ) REQTYPE_HOST_TO_DEVICE , request ,
215230 value , index , buffer , buffer . Length , USB_TIMEOUT_MILLIS ) ;
216231 }
217232
@@ -245,17 +260,17 @@ private void CheckState(String msg, int request, int value, int[] expected)
245260 }
246261 }
247262
248- private void WriteHandshakeByte ( )
263+ private void SetControlLines ( )
249264 {
250- if ( ControlOut ( 0xa4 , ~ ( ( dtr ? 1 << 5 : 0 ) | ( rts ? 1 << 6 : 0 ) ) , 0 ) < 0 )
265+ if ( ControlOut ( 0xa4 , ~ ( ( dtr ? SCL_DTR : 0 ) | ( rts ? SCL_RTS : 0 ) ) , 0 ) < 0 )
251266 {
252- throw new IOException ( "Failed to set handshake byte " ) ;
267+ throw new IOException ( "Failed to set control lines " ) ;
253268 }
254269 }
255270
256271 private void Initialize ( )
257272 {
258- CheckState ( "init #1" , 0x5f , 0 , new int [ ] { - 1 /* 0x27, 0x30 */ , 0x00 } ) ;
273+ CheckState ( "init #1" , 0x5f , 0 , new int [ ] { - 1 /* 0x27, 0x30 */ , 0x00 } ) ;
259274
260275 if ( ControlOut ( 0xa1 , 0 , 0 ) < 0 )
261276 {
@@ -264,14 +279,14 @@ private void Initialize()
264279
265280 SetBaudRate ( DEFAULT_BAUD_RATE ) ;
266281
267- CheckState ( "init #4" , 0x95 , 0x2518 , new int [ ] { - 1 /* 0x56, c3*/ , 0x00 } ) ;
282+ CheckState ( "init #4" , 0x95 , 0x2518 , new int [ ] { - 1 /* 0x56, c3*/ , 0x00 } ) ;
268283
269284 if ( ControlOut ( 0x9a , 0x2518 , 0x0050 ) < 0 )
270285 {
271286 throw new IOException ( "init failed! #5" ) ;
272287 }
273288
274- CheckState ( "init #6" , 0x95 , 0x0706 , new int [ ] { 0xff , 0xee } ) ;
289+ CheckState ( "init #6" , 0x95 , 0x0706 , new int [ ] { - 1 /*0xf?*/ , - 1 /*0xec, 0xee*/ } ) ;
275290
276291 if ( ControlOut ( 0xa1 , 0x501f , 0xd90a ) < 0 )
277292 {
@@ -280,9 +295,9 @@ private void Initialize()
280295
281296 SetBaudRate ( DEFAULT_BAUD_RATE ) ;
282297
283- WriteHandshakeByte ( ) ;
298+ SetControlLines ( ) ;
284299
285- CheckState ( "init #10" , 0x95 , 0x0706 , new int [ ] { - 1 /* 0x9f, 0xff*/ , 0xee } ) ;
300+ CheckState ( "init #10" , 0x95 , 0x0706 , new int [ ] { - 1 /* 0x9f, 0xff*/ , 0xee } ) ;
286301 }
287302
288303 private void SetBaudRate ( int baudRate )
@@ -294,16 +309,16 @@ private void SetBaudRate(int baudRate)
294309 0x6403 , 0x000a , 115200 , 0xcc03 , 0x0008
295310 } ;
296311
297- for ( int i = 0 ; i < baud . Length / 3 ; i ++ )
312+ for ( int i = 0 ; i < baud . Length / 3 ; i ++ )
298313 {
299- if ( baud [ i * 3 ] == baudRate )
314+ if ( baud [ i * 3 ] == baudRate )
300315 {
301- int ret = ControlOut ( 0x9a , 0x1312 , baud [ i * 3 + 1 ] ) ;
316+ int ret = ControlOut ( 0x9a , 0x1312 , baud [ i * 3 + 1 ] ) ;
302317 if ( ret < 0 )
303318 {
304319 throw new IOException ( "Error setting baud rate. #1" ) ;
305320 }
306- ret = ControlOut ( 0x9a , 0x0f2c , baud [ i * 3 + 2 ] ) ;
321+ ret = ControlOut ( 0x9a , 0x0f2c , baud [ i * 3 + 2 ] ) ;
307322 if ( ret < 0 )
308323 {
309324 throw new IOException ( "Error setting baud rate. #1" ) ;
@@ -321,7 +336,41 @@ public override void SetParameters(int baudRate, int dataBits, StopBits stopBits
321336 {
322337 SetBaudRate ( baudRate ) ;
323338
324- // TODO databit, stopbit and paraty set not implemented
339+ int lcr = LCR_ENABLE_RX | LCR_ENABLE_TX ;
340+
341+ lcr |= dataBits switch
342+ {
343+ DATABITS_5 => LCR_CS5 ,
344+ DATABITS_6 => LCR_CS6 ,
345+ DATABITS_7 => LCR_CS7 ,
346+ DATABITS_8 => LCR_CS8 ,
347+ _ => throw new Java . Lang . IllegalArgumentException ( "Invalid data bits: " + dataBits ) ,
348+ } ;
349+
350+
351+ lcr |= ( int ) parity switch
352+ {
353+ PARITY_NONE => lcr ,
354+ PARITY_ODD => LCR_ENABLE_PAR ,
355+ PARITY_EVEN => LCR_ENABLE_PAR | LCR_PAR_EVEN ,
356+ PARITY_MARK => LCR_ENABLE_PAR | LCR_MARK_SPACE ,
357+ PARITY_SPACE => LCR_ENABLE_PAR | LCR_MARK_SPACE | LCR_PAR_EVEN ,
358+ _ => throw new Java . Lang . IllegalArgumentException ( "Invalid parity: " + parity ) ,
359+ } ;
360+
361+ lcr |= ( int ) stopBits switch
362+ {
363+ STOPBITS_1 => lcr ,
364+ STOPBITS_1_5 => throw new Java . Lang . UnsupportedOperationException ( "Unsupported stop bits: 1.5" ) ,
365+ STOPBITS_2 => LCR_STOP_BITS_2 ,
366+ _ => throw new Java . Lang . IllegalArgumentException ( "Invalid stop bits: " + stopBits )
367+ } ;
368+
369+ int ret = ControlOut ( 0x9a , 0x2518 , lcr ) ;
370+ if ( ret < 0 )
371+ {
372+ throw new IOException ( "Error setting control byte" ) ;
373+ }
325374 }
326375
327376 public override bool GetCD ( )
@@ -347,7 +396,7 @@ public override bool GetDTR()
347396 public override void SetDTR ( bool value )
348397 {
349398 dtr = value ;
350- WriteHandshakeByte ( ) ;
399+ SetControlLines ( ) ;
351400 }
352401
353402 public override bool GetRI ( )
@@ -363,18 +412,32 @@ public override bool GetRTS()
363412 public override void SetRTS ( bool value )
364413 {
365414 rts = value ;
366- WriteHandshakeByte ( ) ;
415+ SetControlLines ( ) ;
367416 }
368417
369- public override bool PurgeHwBuffers ( bool flushReadBuffers , bool flushWriteBuffers )
418+ /* public EnumSet<ControlLine> getControlLines( )
370419 {
371- return true ;
372- }
373- }
374420
375- public static Dictionary < int , int [ ] > GetSupportedDevices ( )
421+ int status = getStatus();
422+ EnumSet<ControlLine> set = EnumSet.noneOf(ControlLine.class);
423+ if(rts) set.add(ControlLine.RTS);
424+ if((status & GCL_CTS) == 0) set.add(ControlLine.CTS);
425+ if(dtr) set.add(ControlLine.DTR);
426+ if((status & GCL_DSR) == 0) set.add(ControlLine.DSR);
427+ if((status & GCL_CD) == 0) set.add(ControlLine.CD);
428+ if((status & GCL_RI) == 0) set.add(ControlLine.RI);
429+ return set;
430+ }*/
431+
432+ public override bool PurgeHwBuffers ( bool flushReadBuffers , bool flushWriteBuffers )
376433 {
377- return new Dictionary < int , int [ ] >
434+ return true ;
435+ }
436+ }
437+
438+ public static Dictionary < int , int [ ] > GetSupportedDevices ( )
439+ {
440+ return new Dictionary < int , int [ ] >
378441 {
379442 {
380443 UsbId . VENDOR_QINHENG , new int [ ]
@@ -383,6 +446,6 @@ public static Dictionary<int, int[]> GetSupportedDevices()
383446 }
384447 }
385448 } ;
386- }
387449 }
450+ }
388451}
0 commit comments