Serial - Can I Use Serial Component to Read from a USB Device?

Hi,

I have a USB device that outputs text messages. I connected it to Android phone using OTG adapter. Now, in my AI2 app, I'd like to connect to the device, read from it, and display received data (text).

I have this code:

When executed, the code fails on call to OpenSerial:

Any ideas how to fix this?

I know that the device works fine. (I can plug in into a USB port on a laptop and read data from it using python.)

Also, the Android phone I am using does support OTG.

Is there any other component or extension that would let me open a USB OTG device and read/write from/to it? (I searched but did not find anything else.)

Thanks.

All we have is

1 Like

Thanks. I have seen the pages about Arduino... BTW my device is FTDI-based and there seems to be some support for FTDI. Initially I used 115200 rate, then after reviewing various posts I dropped the rate to 9600 to no avail. I will try some more, try to figure out why phone setting about who controls the USB cannot be changed (strange!?), and also have a look at the Arduino extension but I now do not have much hope.

Did you initialize the baud rate before opening a connection?

This works to send text data from phone to device:

Can your device receive data (just to test)?

ArduinoInterface.aia (3.7 KB)

I tried this example but I get the same error.

You are building this into an apk, download/transfering to your phone, and installing, correct?

I see blocks in the background of your error which indicates you aren't seeing that error on your phone, unless I'm not understanding something.

Here is an .aia file for receiving data back from the device. I just tested this with an Arduino Uno and it works. My test phone is a Moto G Play XT1609 running Android 7.1.1. Haven't tested on other devices. I build the app to apk and then download and install onto phone for testing. Not using emulator or companion app.

ArduinoInterface_receive.aia (4.4 KB)

I added an initialize serial command to the initialize screen event to prevent an android "Error 3901: Serial was not initialized" popup. That block should probably be added to the last .aia i sent you too.

I think ideally the serial block should have an event handler for the buffer, i.e., "when buffer has data, do...". My aia checks the buffer once every 10 millisec for data. Probably not ideal, but I was rushing to get you a working example.

Here is my code running on the Arduino:

//variable for Serial input
int input = 0;
//Pins for LED
const int LED = 13;

// the setup function runs once
void setup() {
  //Start the serial monitor at 9600 baud
  Serial.begin(9600);
  //Declare the LEDs to be outputs
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
}

//runs over and over again
void loop() {
  //check if there's incoming data,
  if (Serial.available() > 0) {
    //if so, then read the incoming data.
    input = Serial.read();

    //if data, send the pin high
    if (input == '1') {
      digitalWrite(LED, HIGH);
      //send back data to phone
      Serial.println("4");
    }
    else if (input == '0') {
      digitalWrite(LED, LOW);
      Serial.println("5");
    }
  }

}

This program will send an Arduino pin HIGH/5V/on when "1" is received from serial line, and then send the number "4" back to the phone. It will send the pin LOW/0V/off when a "0" is received, and return "5".

Since you are maybe using some other device, there is also this thread, which further defines the default communication: Testing the next release of MIT App Inventor (nb183) - #6 by ewpatton

Looking at the physicaloid library sources, the defaults are 8 bits, 1 stop bit, no parity.

Also, there is an app in the play store called "Serial USB Terminal" by Kai Morich. With this app you can quickly and easily test your serial parameters from your phone to device to make sure it is working properly. For example, with the arduino connected running the above sketch, entering a "1" into this app returns a "4" on the terminal. That app allows you to fine tune your serial settings, including baud, data bits, parity, stop bits, and new line/carriage return conditions.

Hope this helps.

Completely agree

Thanks for your reception block, it has been really useful for me, I'm completely agree with you, if the serial block had an event handler then the reception would be implemented easier.

Maybe you can try my extension. It works for several serial adapters with different chips and for Arduino clones. Please report if you find it useful or if you have any problems. More information can be found here:

Greetings, I tried the example and it works very well but I have a problem modifying it, I do not want the data to be stacked one after the other I just need to update the value, perform this program but sometimes it shows well and sometimes it appears incomplete, I hope you can help me and thank you for the contribution. The program shows the time and date that the Arduino has.

Captura de pantalla 2020-10-27 133432 Captura de pantalla 2020-10-27 133453

I had a look at your code block.

First: BufferSize just returns the size, not the number of characters in the buffer. So the first statement will always be true if you have not reduced the buffer size (default=255).
ReadSerial returns all characters received since last read. This is not the same as a message that you send from the Arduino, since the timer tick call can be executed even in the middle of a message . If so you will get only part of the message in the ReadSerial and the remaining part in the next read. The serial communication is not packet oriented. If the timer period is long compared with the message repeat time from the Arduino, you can even get 2 messages in one read. Your complete message takes about 18mS at 9.6kBaud.

Since this seems to be a common problem I will have a look at implementing a message oriented ReadSerialLine that returns an empty string until a complete message has been received, ended by a NewLine character.

In addition to ReadSerialLine, you can also add a reading of a specific number of bytes as it is in the bluetooth component. It would be a good idea to make the block names and functionality similar to the bluetooth component.

It is ok but i need to read serial port and enter result in text1 as "1" if and only if a pulse is present , every time i click start button