Original: Android Project Combat (45): USB to Serial communication (Ch34xuartdriver)
Requirements: USB interface to plug in a hardware, from the hardware to obtain data
For example : mobile phone USB plug-in hardware a,a through Bluetooth communication to obtain equipment A, B data, as a transit point (can do some data processing) to the data (device A, b generated) to the mobile phone program.
Device A can also be a sensor itself, generating data to be transmitted to the mobile phone program itself.
applies to: The program needs some sensor data, but the phone itself is not supported (or not available).
There is a problem with the Bluetooth connection (the manufacturer's Bluetooth underlying changes cause instability, can not connect multiple)
cons : Some phones do not support OTG, that is, unable to obtain data for USB interface connection hardware
---------------------------------------------------------Split Line--------------------------------------------------------------
This article takes ch34x chip as an example.
Official: http://www.wch.cn/download/CH341SER_ANDROID_ZIP.html
The official demo is the Eclipse Project, and you'll need to create a demo of Android Studio yourself
First, create an Android studio project
B. Place the Ch34xuartdriver.jar file in the directory: App-to-libs file
Right-click Jar Package
III. Create a new XML folder under the Res file, and tell the Device_filter.xml in the official demo to copy it in.
and add code to the Androidmanifest.xml file:
In an activity, the role is to indicate when the user inserts the device, will be prompted whether to open the program, and transferred to the specified activity ( This step is not necessary to operate, if you do not want the device to insert the pop-up prompt to open a program, do not do this step )
<intent-filter> <action android:name= "android.hardware.usb.action.USB_DEVICE_ATTACHED"/ > </intent-filter> android:resource= "@xml/device_filter"
Iv. methods provided by some JAR packages
//turn on the device Public voidOpendevice (Android.hardware.usb.UsbDevice usbdevice) {/*compiled code*/ } //turn off the device Public voidCloseDevice () {/*compiled code*/ } //determine if the system supports USB HOST Public BooleanUsbfeaturesupported () {/*compiled code*/ } Public intResumeusblist () {/*compiled code*/ } Public intResumeusbpermission () {/*compiled code*/ } //determine if the device is connected (plugged into a USB port) Public BooleanIsConnected () {/*compiled code*/ } protectedAndroid.hardware.usb.UsbDevice Getusbdevice () {/*compiled code*/ } //initialization of the serial device Public BooleanUartinit () {/*compiled code*/ } //Configure serial port baud rate, function description can refer to programming manual Public BooleanSetconfig (intIbyteBbyteB1,byteB2,byteB3) {/*compiled code*/ } //Read serial data Public intReadData (byte[] Bytes,inti) {/*compiled code*/ } //Write serial data Public intWriteData (byte[] Bytes,inti) {/*compiled code*/ } //Write serial data Public intWriteData (byte[] Bytes,intIintI1) {/*compiled code*/}
V. Some of the pits encountered in practice (emphasis)
1, hardware engineer on the USB serial hardware data transmission processing, the length of this data can not be arbitrarily specified
The length of the pro-test data is 32 correct, and the program read () method accepts normal, i.e. 16, 32, 64 .... , if the length is 28, 34 this, then the program read () method reads the data is not normal.
2, write () The parameter of the method is byte[] array, that is, if the interface input is a string, you need to convert the string to byte[] array.
The method in the official demo is incorrect and is provided correctly as follows
/*** Convert string to byte[] array *@paramarg * String object to convert *@returnconverted Byte[] array*/ Private byte[] toByteArray2 (String Arg) {if(Arg! =NULL) { /*1. First remove the string ", and then convert the string to a char array*/ Char[] NewArray =New Char[1000]; Char[] Array =Arg.tochararray (); intLength = 0; for(inti = 0; i < Array.Length; i++) { if(Array[i]! = ") {Newarray[length]=Array[i]; Length++; }} Newarray[length]= 0x0D; Newarray[length+ 1] = 0x0A; Length+ = 2; byte[] ByteArray =New byte[length]; for(inti = 0; i < length; i++) {Bytearray[i]= (byte) Newarray[i]; } returnByteArray; } return New byte[] {}; }
convert string to byte[] array
Similarly, there are several methods that you might use in your project practice:
Public byte [] subbytes (byteintint count) { bytenewbyte [Count]; 0, count); return BS; }
intercept a part of the byte[] array
/*** Convert byte[] Array to string type *@paramarg * byte[] array required for conversion *@paramlength * required to convert array lengths *@returnstring formation after conversion*/ PrivateString tohexstring (byte[] Arg,intlength) {String result=NewString (); if(Arg! =NULL) { for(inti = 0; i < length; i++) { if(i==length-1) {result=result+(integer.tohexstring (arg[i)< 0? Arg[i] + 256:arg[i]). Length () = = 1? "0" + integer.tohexstring (Arg[i] < 0 Arg[i] + 256: Arg[i]): integer.tohexstring (Arg[i]< 0? Arg[i] + 256: Arg[i])) + ""; }Else{result=result+(integer.tohexstring (arg[i)< 0? Arg[i] + 256:arg[i]). Length () = = 1? "0" + integer.tohexstring (Arg[i] < 0 Arg[i] + 256: Arg[i]): integer.tohexstring (Arg[i]< 0? Arg[i] + 256: Arg[i])) + " "; } } returnresult; } return""; }
Convert byte[] array to String type
3, about the USB port plug operation Monitoring, write dead in the jar package, if you want to customize, you need to modify the jar package source code
4, about the third step of the operation is not necessary, according to the needs of the decision whether to add
5. Not all phones support USB serial communication (OTG feature not supported)
6. The process is to turn on the device and configure the device, if you modify the configuration parameters, you can directly configure the device, do not need to do close--> Open > Config
Vi. Android Studio Demo Link
---------------------------------------------------------Split Line--------------------------------------------------------------
Bluetooth communication, USB serial communication, unity and Android communication issues, welcome to join the right QQ Group Consulting.
Android Project Combat (45): USB to Serial communication (Ch34xuartdriver)