最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【整理】移植FTDI的FT232R的USB转串口驱动到Android平台以支持RTS,DTR等功能以便支持HART硬件通讯

Android crifan 8751浏览

【背景】

之前已经利用并修改原先的:

usb-serial-for-android

然后实现了,支持HART协议,从某Android PAD经过USB OTG转接头和HART猫,最终连通了某HART设备:

mobilehandheld hardware connection - show all cable - black theme

可以用DigiView抓取出对应的RTS,CTS等信号,实现了HART的硬件通讯了。

且之前已整理了一点资料:

【整理】Android平台下的FTDI的USB转串口(Serial, CDC)驱动

但是关于移植细节,并没记录。

现直接贴出代码,供参考:

(1)代码文件:

/UsbSerialLibrary/src/com/hoho/android/usbserial/driver/FtdiSerialDriver.java

修改后的源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/* Copyright 2011 Google Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 *
 */
   
package com.hoho.android.usbserial.driver;
   
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbRequest;
import android.util.Log;
   
import com.hoho.android.usbserial.util.HexDump;
   
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.LinkedHashMap;
import java.util.Map;
   
/**
 * A {@link CommonUsbSerialDriver} implementation for a variety of FTDI devices
 * <p>
 * This driver is based on
 * <a href="http://www.intra2net.com/en/developer/libftdi">libftdi</a>, and is
 * copyright and subject to the following terms:
 *
 * <pre>
 *   Copyright (C) 2003 by Intra2net AG
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License
 *   version 2.1 as published by the Free Software Foundation;
 *
 *   opensource@intra2net.com
 * </pre>
 *
 * </p>
 * <p>
 * Some FTDI devices have not been tested; see later listing of supported and
 * unsupported devices. Devices listed as "supported" support the following
 * features:
 * <ul>
 * <li>Read and write of serial data (see {@link #read(byte[], int)} and
 * {@link #write(byte[], int)}.
 * <li>Setting baud rate (see {@link #setBaudRate(int)}).
 * </ul>
 * </p>
 * <p>
 * Supported and tested devices:
 * <ul>
 * <li>{@value DeviceType#TYPE_R}</li>
 * </ul>
 * </p>
 * <p>
 * Unsupported but possibly working devices (please contact the author with
 * feedback or patches):
 * <ul>
 * <li>{@value DeviceType#TYPE_2232C}</li>
 * <li>{@value DeviceType#TYPE_2232H}</li>
 * <li>{@value DeviceType#TYPE_4232H}</li>
 * <li>{@value DeviceType#TYPE_AM}</li>
 * <li>{@value DeviceType#TYPE_BM}</li>
 * </ul>
 * </p>
 *
 * @author mike wakerly (opensource@hoho.com)
 * for Android project page</a>
 * @see <a href="http://www.ftdichip.com/">FTDI Homepage</a>
 * @see <a href="http://www.intra2net.com/en/developer/libftdi">libftdi</a>
 */
public class FtdiSerialDriver extends CommonUsbSerialDriver {
    private static final int DEFAULT_BAUD_RATE = 115200;
    private static final int DEFAULT_DATA_BITS = DATABITS_8;
    private static final int DEFAULT_PARITY = PARITY_NONE;
    private static final int DEFAULT_STOP_BITS = STOPBITS_1;
   
    public static final int USB_TYPE_STANDARD = 0x00 << 5;
    public static final int USB_TYPE_CLASS = 0x00 << 5;
    public static final int USB_TYPE_VENDOR = 0x00 << 5;
    public static final int USB_TYPE_RESERVED = 0x00 << 5;
   
    public static final int USB_RECIP_DEVICE = 0x00;
    public static final int USB_RECIP_INTERFACE = 0x01;
    public static final int USB_RECIP_ENDPOINT = 0x02;
    public static final int USB_RECIP_OTHER = 0x03;
   
    public static final int USB_ENDPOINT_IN = 0x80;
    public static final int USB_ENDPOINT_OUT = 0x00;
   
    public static final int USB_WRITE_TIMEOUT_MILLIS = 5000;
    public static final int USB_READ_TIMEOUT_MILLIS = 5000;
   
   
    /*
     * add support for RTS and DTR, by Crifan Li
     */
    private boolean mRts = false;
    private boolean mDtr = false;
   
    private static final int SEND_ENCAPSULATED_COMMAND  = 0x00;
    private static final int GET_ENCAPSULATED_COMMAND   = 0x01;
   
    /**
     * Set_Line_Coding
     *
     * Unload the line coding structure (7 bytes) sent in the data stage.
     * Apply this setting to the UART
     * Flush the communication buffer
     *
     * Line Coding Structure (7 bytes)
     * 0-3 dwDTERate    Data terminal rate (baudrate), in bits per second (LSB first)
     * 4   bCharFormat  Stop bits: 0 - 1 Stop bit, 1 - 1.5 Stop bits, 2 - 2 Stop bits
     * 5   bParityType  Parity:    0 - None, 1 - Odd, 2 - Even, 3 - Mark, 4 - Space
     * 6   bDataBits    Data bits: 5, 6, 7, 8, 16
     */
    private static final int SET_LINE_CODING = 0x20;  // USB CDC 1.1 section 6.2
    /**
     * Get_Line_Coding
     *
     * Return the line coding structure
     */
    private static final int GET_LINE_CODING = 0x21;
    /**
     * Set_ControlLine_State
     *
     * Set/reset RTS/DTR according to wValue
     * wValue
     *  bit 1  RTS
     *  bit 0  DTR
     */
    private static final int SET_CONTROL_LINE_STATE = 0x22;
    /**
     * Send_Break
     *
     * Send break from UART TX port, for wValue (msec) duration.
     * wValue
     *  0xFFFF: continuous break
     *  0x0000: stop break
     */
    private static final int SEND_BREAK = 0x23;
       
    //SET_CONTROL_LINE_STATE bit fields
    public static final int SET_CONTROL_LINE_BITS_DTR = 0x01 << 0; //bit0
    public static final int SET_CONTROL_LINE_BITS_RTS = 0x01 << 1; //bit1
   
    //private static final int USB_RECIP_INTERFACE = 0x01;
   
    //refer:
    //http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/drivers/usb/class/cdc-acm.h#L28
    //#define USB_RT_ACM              (USB_TYPE_CLASS | USB_RECIP_INTERFACE)
    private static final int USB_RT_FDTI = UsbConstants.USB_TYPE_CLASS | USB_RECIP_INTERFACE;
       
       
    //add explain by Crifan Li
    //following definition is from
    //libftdi1-1.0.tar\libftdi1-1.0\libftdi1-1.0\src\ftdi.h
    //which is downloaded from:
    private static final int  SIO_SET_DTR_MASK = 0x1;
    private static final int  SIO_SET_DTR_HIGH =( 1 | ( SIO_SET_DTR_MASK  << 8));
    private static final int  SIO_SET_DTR_LOW =( 0 | ( SIO_SET_DTR_MASK  << 8));
    private static final int  SIO_SET_RTS_MASK = 0x2;
    private static final int  SIO_SET_RTS_HIGH =( 2 | ( SIO_SET_RTS_MASK << 8 ));
    private static final int  SIO_SET_RTS_LOW =( 0 | ( SIO_SET_RTS_MASK << 8 ));
   
   
    // From ftdi.h
    /**
     * Reset the port.
     */
    private static final int SIO_RESET_REQUEST = 0;
   
    /**
     * Set the modem control register.
     */
    private static final int SIO_SET_MODEM_CTRL_REQUEST = 1;
       
    /**
     * Set flow control register.
     */
    private static final int SIO_SET_FLOW_CTRL_REQUEST = 2;
   
    /**
     * Set baud rate.
     */
    private static final int SIO_SET_BAUD_RATE_REQUEST = 3;
   
    /**
     * Set the data characteristics of the port.
     */
    private static final int SIO_SET_DATA_REQUEST = 4;
   
    private static final int SIO_RESET_SIO = 0;
   
    public static final int FTDI_DEVICE_OUT_REQTYPE =
            UsbConstants.USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT;
   
    public static final int FTDI_DEVICE_IN_REQTYPE =
            UsbConstants.USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN;
   
    /**
     * Length of the modem status header, transmitted with every read.
     */
    private static final int MODEM_STATUS_HEADER_LENGTH = 2;
   
    private final String TAG = FtdiSerialDriver.class.getSimpleName();
   
    private DeviceType mType;
   
    /**
     * FTDI chip types.
     */
    private static enum DeviceType {
        TYPE_BM, TYPE_AM, TYPE_2232C, TYPE_R, TYPE_2232H, TYPE_4232H;
    }
   
    private int mInterface = 0; /* INTERFACE_ANY */
   
    private int mMaxPacketSize = 64; // TODO(mikey): detect
   
    private int mBaudRate;
    private int mDataBits;
    private int mParity;
    private int mStopBits;
   
    /**
     * Due to http://b.android.com/28023 , we cannot use UsbRequest async reads
     * since it gives no indication of number of bytes read. Set this to
     * {@code true} on platforms where it is fixed.
     */
    private static final boolean ENABLE_ASYNC_READS = false;
   
    /**
     * Constructor.
     *
     * @param usbDevice the {@link UsbDevice} to use
     * @param usbConnection the {@link UsbDeviceConnection} to use
     * @throws UsbSerialRuntimeException if the given device is incompatible
     *             with this driver
     */
    public FtdiSerialDriver(UsbDevice usbDevice, UsbDeviceConnection usbConnection) {
        super(usbDevice, usbConnection);
        mType = null;
    }
   
    public void reset() throws IOException {
        int result = mConnection.controlTransfer(FTDI_DEVICE_OUT_REQTYPE, SIO_RESET_REQUEST,
                SIO_RESET_SIO, 0 /* index */, null, 0, USB_WRITE_TIMEOUT_MILLIS);
        if (result != 0) {
            throw new IOException("Reset failed: result=" + result);
        }
   
        // TODO(mikey): autodetect.
        mType = DeviceType.TYPE_R;
    }
   
    @Override
    public void open() throws IOException {
        boolean opened = false;
        try {
            for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
                if (mConnection.claimInterface(mDevice.getInterface(i), true)) {
                    Log.d(TAG, "claimInterface " + i + " SUCCESS");
                } else {
                    Log.d(TAG, "claimInterface " + i + " FAIL");
                }
            }
            reset();
            setParameters(DEFAULT_BAUD_RATE, DEFAULT_DATA_BITS, DEFAULT_STOP_BITS, DEFAULT_PARITY);
            opened = true;
        } finally {
            if (!opened) {
                close();
            }
        }
    }
   
    @Override
    public void close() {
        mConnection.close();
    }
   
    @Override
    public int read(byte[] dest, int timeoutMillis) throws IOException {
        final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(0);
   
        if (ENABLE_ASYNC_READS) {
            final int readAmt;
            synchronized (mReadBufferLock) {
                // mReadBuffer is only used for maximum read size.
                readAmt = Math.min(dest.length, mReadBuffer.length);
            }
   
            final UsbRequest request = new UsbRequest();
            request.initialize(mConnection, endpoint);
   
            final ByteBuffer buf = ByteBuffer.wrap(dest);
            if (!request.queue(buf, readAmt)) {
                throw new IOException("Error queueing request.");
            }
   
            final UsbRequest response = mConnection.requestWait();
            if (response == null) {
                throw new IOException("Null response");
            }
   
            final int payloadBytesRead = buf.position() - MODEM_STATUS_HEADER_LENGTH;
            if (payloadBytesRead > 0) {
                Log.d(TAG, HexDump.dumpHexString(dest, 0, Math.min(32, dest.length)));
                return payloadBytesRead;
            } else {
                return 0;
            }
        } else {
            final int totalBytesRead;
   
            synchronized (mReadBufferLock) {
                final int readAmt = Math.min(dest.length, mReadBuffer.length);
                totalBytesRead = mConnection.bulkTransfer(endpoint, mReadBuffer,
                        readAmt, timeoutMillis);
            }
   
            if (totalBytesRead < MODEM_STATUS_HEADER_LENGTH) {
                throw new IOException("Expected at least " + MODEM_STATUS_HEADER_LENGTH + " bytes");
            }
   
            final int payloadBytesRead = totalBytesRead - MODEM_STATUS_HEADER_LENGTH;
            if (payloadBytesRead > 0) {
                System.arraycopy(mReadBuffer, MODEM_STATUS_HEADER_LENGTH, dest, 0, payloadBytesRead);
            }
            return payloadBytesRead;
        }
    }
   
    @Override
    public int write(byte[] src, int timeoutMillis) throws IOException {
        final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(1);
        int offset = 0;
   
        while (offset < src.length) {
            final int writeLength;
            final int amtWritten;
   
            synchronized (mWriteBufferLock) {
                final byte[] writeBuffer;
   
                writeLength = Math.min(src.length - offset, mWriteBuffer.length);
                if (offset == 0) {
                    writeBuffer = src;
                } else {
                    // bulkTransfer does not support offsets, make a copy.
                    System.arraycopy(src, offset, mWriteBuffer, 0, writeLength);
                    writeBuffer = mWriteBuffer;
                }
   
                amtWritten = mConnection.bulkTransfer(endpoint, writeBuffer, writeLength,
                        timeoutMillis);
            }
   
            if (amtWritten <= 0) {
                throw new IOException("Error writing " + writeLength
                        + " bytes at offset " + offset + " length=" + src.length);
            }
   
            Log.d(TAG, "Wrote amtWritten=" + amtWritten + " attempted=" + writeLength);
            offset += amtWritten;
        }
        return offset;
    }
   
    @Override
    @Deprecated
    public int setBaudRate(int baudRate) throws IOException {
        long[] vals = convertBaudrate(baudRate);
        long actualBaudrate = vals[0];
        long index = vals[1];
        long value = vals[2];
        int result = mConnection.controlTransfer(FTDI_DEVICE_OUT_REQTYPE,
                SIO_SET_BAUD_RATE_REQUEST, (int) value, (int) index,
                null, 0, USB_WRITE_TIMEOUT_MILLIS);
        if (result != 0) {
            throw new IOException("Setting baudrate failed: result=" + result);
        }
        return (int) actualBaudrate;
    }
   
    @Override
    public void setParameters(int baudRate, int dataBits, int stopBits, int parity)
            throws IOException {
        mBaudRate = setBaudRate(baudRate);
   
        int config = dataBits;
   
        switch (parity) {
            case PARITY_NONE:
                config |= (0x00 << 8);
                break;
            case PARITY_ODD:
                config |= (0x01 << 8);
                break;
            case PARITY_EVEN:
                config |= (0x02 << 8);
                break;
            case PARITY_MARK:
                config |= (0x03 << 8);
                break;
            case PARITY_SPACE:
                config |= (0x04 << 8);
                break;
            default:
                throw new IllegalArgumentException("Unknown parity value: " + parity);
        }
   
        switch (stopBits) {
            case STOPBITS_1:
                config |= (0x00 << 11);
                break;
            case STOPBITS_1_5:
                config |= (0x01 << 11);
                break;
            case STOPBITS_2:
                config |= (0x02 << 11);
                break;
            default:
                throw new IllegalArgumentException("Unknown stopBits value: " + stopBits);
        }
   
        int result = mConnection.controlTransfer(FTDI_DEVICE_OUT_REQTYPE,
                SIO_SET_DATA_REQUEST, config, 0 /* index */,
                null, 0, USB_WRITE_TIMEOUT_MILLIS);
        if (result != 0) {
            throw new IOException("Setting parameters failed: result=" + result);
        }
   
        mParity = parity;
        mStopBits = stopBits;
        mDataBits = dataBits;
    }
   
    private long[] convertBaudrate(int baudrate) {
        // TODO(mikey): Braindead transcription of libfti method.  Clean up,
        // using more idiomatic Java where possible.
        int divisor = 24000000 / baudrate;
        int bestDivisor = 0;
        int bestBaud = 0;
        int bestBaudDiff = 0;
        int fracCode[] = {
                0, 3, 2, 4, 1, 5, 6, 7
        };
   
        for (int i = 0; i < 2; i++) {
            int tryDivisor = divisor + i;
            int baudEstimate;
            int baudDiff;
   
            if (tryDivisor <= 8) {
                // Round up to minimum supported divisor
                tryDivisor = 8;
            } else if (mType != DeviceType.TYPE_AM && tryDivisor < 12) {
                // BM doesn't support divisors 9 through 11 inclusive
                tryDivisor = 12;
            } else if (divisor < 16) {
                // AM doesn't support divisors 9 through 15 inclusive
                tryDivisor = 16;
            } else {
                if (mType == DeviceType.TYPE_AM) {
                    // TODO
                } else {
                    if (tryDivisor > 0x1FFFF) {
                        // Round down to maximum supported divisor value (for
                        // BM)
                        tryDivisor = 0x1FFFF;
                    }
                }
            }
   
            // Get estimated baud rate (to nearest integer)
            baudEstimate = (24000000 + (tryDivisor / 2)) / tryDivisor;
   
            // Get absolute difference from requested baud rate
            if (baudEstimate < baudrate) {
                baudDiff = baudrate - baudEstimate;
            } else {
                baudDiff = baudEstimate - baudrate;
            }
   
            if (i == 0 || baudDiff < bestBaudDiff) {
                // Closest to requested baud rate so far
                bestDivisor = tryDivisor;
                bestBaud = baudEstimate;
                bestBaudDiff = baudDiff;
                if (baudDiff == 0) {
                    // Spot on! No point trying
                    break;
                }
            }
        }
   
        // Encode the best divisor value
        long encodedDivisor = (bestDivisor >> 3) | (fracCode[bestDivisor & 7] << 14);
        // Deal with special cases for encoded value
        if (encodedDivisor == 1) {
            encodedDivisor = 0; // 3000000 baud
        } else if (encodedDivisor == 0x4001) {
            encodedDivisor = 1; // 2000000 baud (BM only)
        }
   
        // Split into "value" and "index" values
        long value = encodedDivisor & 0xFFFF;
        long index;
        if (mType == DeviceType.TYPE_2232C || mType == DeviceType.TYPE_2232H
                || mType == DeviceType.TYPE_4232H) {
            index = (encodedDivisor >> 8) & 0xffff;
            index &= 0xFF00;
            index |= 0 /* TODO mIndex */;
        } else {
            index = (encodedDivisor >> 16) & 0xffff;
        }
   
        // Return the nearest baud rate
        return new long[] {
                bestBaud, index, value
        };
    }
   
    @Override
    public boolean getCD() throws IOException {
        return false;
    }
   
    @Override
    public boolean getCTS() throws IOException {
        return false;
    }
   
    @Override
    public boolean getDSR() throws IOException {
        return false;
    }
   
    @Override
    public boolean getDTR() throws IOException {
        return mDtr;
        //return false;
    }
   
    @Override
    public void setDTR(boolean value) throws IOException {
        mDtr = value;
        setDtrRts();
    }
   
    @Override
    public boolean getRI() throws IOException {
        return false;
    }
   
    @Override
    public boolean getRTS() throws IOException {
        return mRts;
        //return false;
    }
       
    private void setDtrRts() {
        short usb_val = 0;
   
        if (mDtr)
        {
            usb_val = SIO_SET_DTR_HIGH;
        }
        else
        {
            usb_val = SIO_SET_DTR_LOW;
        }
   
        if (mRts)
        {
            usb_val |= SIO_SET_RTS_HIGH;
        }
        else
        {
            usb_val |= SIO_SET_RTS_LOW;
        }
           
        //sendFdtiControlMessage(SET_CONTROL_LINE_STATE, usb_val, null);
           
      //refer
      //(1) http://developer.android.com/intl/zh-CN/reference/android/hardware/usb/UsbDeviceConnection.html#controlTransfer%28int,%20int,%20int,%20int,%20byte[],%20int,%20int%29
      //(2) mConnection.controlTransfer in setParameters()
      //(3)
      //int ftdi_setdtr(struct ftdi_context *ftdi, int state) in
      //int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
      //in
      //libftdi1-1.0.tar\libftdi1-1.0\libftdi1-1.0\src\ftdi.c
      mConnection.controlTransfer(
              FTDI_DEVICE_OUT_REQTYPE,
              SIO_SET_MODEM_CTRL_REQUEST, /* Set the modem control register */
              usb_val, /* clear/set RTS/DTR */
              0,/* index */
              null, /* buffer is null */
              0, /* length is 0 for null buffer */
              USB_WRITE_TIMEOUT_MILLIS /* timeout in millisecond */
              );
    }
   
    @Override
    public void setRTS(boolean value) throws IOException {
        mRts = value;
        setDtrRts();
   
//        short usb_val = 0;
//        if (mRts)
//        {
//            usb_val = SIO_SET_RTS_HIGH;
//        }
//        else
//        {
//            usb_val = SIO_SET_RTS_LOW;
//        }
//       
//        mConnection.controlTransfer(
//                FTDI_DEVICE_OUT_REQTYPE,
//                SIO_SET_MODEM_CTRL_REQUEST, /* Set the modem control register */
//                usb_val, /* clear/set RTS/DTR */
//                0,/* index */
//                null, /* buffer is null */
//                0, /* length is 0 for null buffer */
//                USB_WRITE_TIMEOUT_MILLIS /* timeout in millisecond */
//                );
    }
   
    public static Map<Integer, int[]> getSupportedDevices() {
        final Map<Integer, int[]> supportedDevices = new LinkedHashMap<Integer, int[]>();
        supportedDevices.put(Integer.valueOf(UsbId.VENDOR_FTDI),
                new int[] {
                    UsbId.FTDI_FT232R,
                });
        return supportedDevices;
    }
   
}

一些解释说明:

1.下载驱动源码

usb-serial-for-android

主要是利用其:

UsbSerialLibrary

2.下载参考源码

从:

http://www.intra2net.com/en/developer/libftdi/

下载到所参考的源码:

http://www.intra2net.com/en/developer/libftdi/download/libftdi1-1.0.tar.bz2

3.供参考的一些页面:

https://github.com/mik3y/usb-serial-for-android

http://asf.atmel.com/docs/2.9.0/avr32.services.usb.cdc.example.evk1100.asfv1/html/cdc_8h_source.html

http://fenrir.naruoka.org/download/autopilot/external/TinyFeather/DSP/autopilot/peripheral/usb_cdc.cpp

http://www.lvr.com/usb_virtual_com_port.htm

http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/drivers/usb/class/cdc-acm.h#L28

转载请注明:在路上 » 【整理】移植FTDI的FT232R的USB转串口驱动到Android平台以支持RTS,DTR等功能以便支持HART硬件通讯

86 queries in 0.267 seconds, using 19.17MB memory