How to read data from micro USB port using Serial.read()

I have a Photon with external power and am trying to read data from a serial to USB cable + OTG adapter (Serial1 is not available as it is being used for another purpose).

I have experience reading data with an RS232 TLL using Serial1.readStringUntil(), but I’m struggling to get any kind of reading. Is this not possible, or am I doing something wrong? My code below, I am still new to C so I admit I don’t really know what I’m doing when it comes to char.

Thank you!

String string;
String usbstring;
String usbstring2;

void setup(){
    Serial.begin(9600); // micro usb port
    Serial1.begin(9600, SERIAL_7E1); // TTL
}

void loop(){
    
    if (Serial.available() > 0) {                 
        char inchar = (char)Serial.read();         
        usbstring += inchar;
        Particle.publish("char",String(inchar));
        if (inchar == '\n') {                     
            Particle.publish("fullstring", usbstring);   
        }
        
    usbstring2 = Serial.readStringUntil('\r');
    Particle.publish("string2",usbstring2);        
    
    }   
}

What device are you trying to communicate with?
The Photon is not capable to act as a USB Serial host.

However, the Photon also features a secondary UART port Serial2 and you can also have a software serial port via ParticleSoftSerial.

1 Like

Thanks @ScruffR. I am trying to send data from an Android app into a Photon in a way that a) does not use Serial1 b) does not consume data operations and c) is near real-time (so preferably a hard-wired connection vs. WiFi-based).

Argon would be cleaner and enable me to interface using BLE, but it can’t do 7E1 which is required for my use case.

ParticleSoftSerial look promising, I’ll give it a try tomorrow. Thank you!

In what way would you need 7E1 with BLE? With BLE (and USB Serial for that matter) there is no such distinction as the "raw data" will always be packaged in a report block without start-, stop- or parity bit.
You can also use WiFi over TCP or UDP without using any data operations and shouldn't add too much latency either (can you define "nead real-time"?).

Are you writing that Android app or are you using an already written app?

1 Like

Sorry, I meant I would use BLE to communicate with my custom Android app, but it’s the peripheral device connected via Serial1 that requires 7E1 (which disqualifies the Argon).

Thanks for also suggesting that, I was looking at the Photon to Photon over TCP/IP examples yesterday, but given that the device will be shipped to a customer site (and given my own technical limitations), it makes me a little nervous that I’d be able to effectively troubleshoot or update if something goes wrong or changes.

When I was experimenting with using 2 Photons and publish and subscribe, I was experiencing about a 2.5-3 second latency. I think between 1-2 seconds would be ideal.

ParticleSoftSerial is working, thank you so much @ScruffR! I’ve now got Serial1 sending/receiving using TX/RX and 7E1, and SoftSerial also receiving from a separate peripheral on 8N1.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.