📜 ⬆️ ⬇️

Serial, UART and Android, as a link with microcontrollers

image

Our subjects:


Tablet on Android 4.1.1 “DNS AirTab M101w” and the following devices:
1. Tiva C Series TM4C123G LaunchPad Board
2. Stellaris LaunchPad EK-LM4F120XL board
3. Arduino MEGA 2560 board
4. MSP430 LaunchPad, M430G2553

And now we will in turn connect all these boards via USB OTG to the tablet and try to establish connection with them without resorting to root access.

Test Subject 1. Tiva C Series TM4C123G LaunchPad Board


The board has a micro-usb connector “DEBUG”, and an RGB LED, and we will play with them.
The following sketch was filled with the Energia IDE:
char data = ' '; //    boolean rs, gs, bs = false; //   void setup() { //  pinMode(RED_LED, OUTPUT); pinMode(GREEN_LED, OUTPUT); pinMode(BLUE_LED, OUTPUT); Serial.begin(9600); // Serial    9600 } void loop() { if (Serial.available()){ //    data = Serial.read(); //  switch (data){ //  case '1': rs = !rs; break; case '2': gs = !gs; break; case '3': bs = !bs; break; } //  digitalWrite(RED_LED, rs); digitalWrite(GREEN_LED, gs); digitalWrite(BLUE_LED, bs); //    Serial.print("RGB="); Serial.print(rs); Serial.print(gs); Serial.println(bs); } } 


In my case, when you connect to the tablet in the / dev / usb / directory, the file tty1-1: 1.0 appears. Let's try to register the following in the terminal emulator:
 $ echo 2 > /dev/tty1-1:1.0 $ read s < /dev/tty1-1:1.0 $ echo $s RGB=010 

')
And then, unexpectedly for me, the green light comes on and I saw the output line. Why unexpected? Because I neglected the pre-setting of the connection, jabbed a finger at the sky, but for how successful! We will not dwell on this. But I will say that in different versions of the kernel, the boards will be determined differently, and you cannot think of a single command line without crutches (yes, linux is ordinary) .

Having played a little with the colors, sending different numbers to the so-called COM port started searching for a universal Android solution. There is a lot on Google Play for the “Serial UART” request, and most applications work as they should with a Texas Instruments toy. But my attempts to find that open source compatible thread ended in almost nothing. For Arduino found a cool library, but more on that below ...

Test 2: Stellaris LaunchPad EK-LM4F120XL Board


As I understand it, this is the previous version of our first test subject and with the android behaves the same way. But the current Energia 0101E0010 at the moment when trying to fill the sketch does not see this board (Windows 8).
 No ICDI device with USB VID:PID 1cbe:00fd found! Failed! 


Therefore, I simply turned on the output of the final binary path in the console and loaded it manually using the LM Flash Programmer.

Test 3: Arduino MEGA 2560 Board


Slightly change the sketch and we will flash only one LED.
Fill with Arduino IDE
 #define LED 13 char data = ' '; boolean ls = false; void setup() { pinMode(LED, OUTPUT); Serial.begin(9600); } void loop() { if (Serial.available()){ data = Serial.read(); switch (data){ case '1': ls = !ls; break; } digitalWrite(LED, ls); Serial.print("LED="); Serial.println(ls); } } 


We connect to the tablet, again we see in the / dev / usb / directory the tty1-1: 1.0 file, we repeat the experiment:
 $ echo 1 > /dev/tty1-1:1.0 

and here I find out that the LED blinked 2 times with a period of about 100 ms and went out. Okay, try the following:
 $ read s < /dev/tty1-1:1.0 

And then the terminal froze in anticipation ... Ok Ctrl + C
 $ echo $s 

It is logical that there is nothing in the output.

But it was only an introduction, there is one wonderful project Physicaloid Library . To begin, open the library as an ordinary android project in Eclipse. Now we will create a new project and in its properties in paragraph android, Library click Add ... and select Physicaloid Library. Now, what if you press the button to send the string “1” to arduin:
 public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: mPhysicaloid = new Physicaloid(this); if(mPhysicaloid.open()) { byte[] buf = "1".getBytes(); mPhysicaloid.write(buf, buf.length); mPhysicaloid.close(); } break; } 

I don’t need to tell how to read the data, because there’s everything in the library documentation.

Test 4: MSP430 LaunchPad, M430G2553.


This board did not want to work with my hardware at all. Just not detected by the system, and USB Device info just hangs when it is updated.

Total:


I would like such a library to work with Tiva C Series boards. Himself remake, while the skill is not enough.

Source: https://habr.com/ru/post/444110/


All Articles