Trouble reading from Serial on Electron


#1

I have been working with a Particle Electron and have had trouble with sending and receiving data from the Serial pins (UARTS 1, 4, or 5). Is there anything that I have to do to the Electron itself in order to be able to send and receive data? Is there anything I should specifically now about sending and receiving data over the extra UARTs?


#2

@unseenshadow2, what kind of trouble are you having exactly?


#3

This tutorial by @rickkas7 might help:


#4

I am unsure as to whether I am actually sending messages to the UART device or not, but I have tried placing it on UART4 and USART1 with no data received. How I am checking this is by sending “ATL” to the UART device, to which it should respond with “OK” or “ERROR” and the chip should be watching for “AT” to determine what the baud rate is. I have yet to receive data from the device on the Electron, but we have had successful communications with the device on other chips.

Here is the code that I have been testing with:

#include "Serial2/Serial2.h"
#include "Serial4/Serial4.h"

#define SERIAL_RATE 1000
long lastSend = 0;
long lastCheck = SERIAL_RATE;

void setup()
{
    Serial.begin(57600);
    Serial1.begin(57600);
    Serial2.begin(57600);
    Serial4.begin(57600);
}

void loop() 
{
    // Send our data
    if (lastSend < millis())
    {
        // Send data
        Serial.println("Sending ATL");
        Serial1.print("ATL\r\n");
        Serial2.print("ATL\r\n");
        Serial4.print("ATL\r\n");
        Serial.println("ATL sent");

        // delay(SERIAL_RATE);
        // Serial1.flush();
        // Serial2.flush();
        // Serial4.flush();
        lastSend = millis() + SERIAL_RATE;
    }

    // Check if data has been recieved
    if (lastCheck < millis())
    {
        // Serial 4
        // Serial.println("Checking Serial4");
        // if (Serial4.available())
        // {
        //     String recieved = Serial4.readString();
        //     Serial.print("Read: \""); Serial.print(recieved); Serial.println("\"");
        // }
        // Serial.println("Done checking Serial4");

        // // Serial 2 (UART4)
        // Serial.println("Checking Serial2");
        // if (Serial2.available())
        // {
        //     String recieved = Serial2.readString();
        //     Serial.print("Read: \""); Serial.print(recieved); Serial.println("\"");
        // }
        // Serial.println("Done checking Serial2");

        // Serial
        Serial.println("Checking Serial");
        if (Serial.available())
        {
            String recieved = Serial.readString();
            Serial.print("Read: \""); Serial.print(recieved); Serial.println("\"");
        }
        Serial.println("Done checking Serial");

        // Serial 1
        Serial.println("Checking Serial1");
        if (Serial1.available())
        {
            String recieved = Serial1.readString();
            Serial.print("Read: \""); Serial.print(recieved); Serial.println("\"");
        }
        Serial.println("Done checking Serial1");
        Serial.println();

        lastCheck = millis() + SERIAL_RATE;
    }
}

#5

How have you connected Serial2? It’s not a trivial task which involves some HW modifications.
AFAIK the Electron only really supports Serial1, Serial4 and Serial5 (as stated in the tutorial above and the docs)
https://docs.particle.io/reference/firmware/electron/#serial

You may also try with Serial#.read() first, to see whether you get any response at all.
Also bridging RX to TX on the same port is a basic way to test whether the desired ports work at all.

Are you getting any of your debug print statements back of the device?
Is the Electron breathing cyan?

With this code

#include "Serial4/Serial4.h"
#include "Serial5/Serial5.h"

const uint32_t SERIAL_RATE = 5000;
uint32_t lastCheck;
uint32_t lastSend;

void setup()
{
    Serial.begin(57600);
    Serial1.begin(57600);
    Serial4.begin(57600);
    Serial5.begin(57600);
}

void loop() 
{
    // Send our data
    if (millis() - lastSend >= SERIAL_RATE)
    {
        lastSend = millis();
        // Send data
        Serial.println("Sending ATL");
        Serial1.print("1:ATL\r\n");
        Serial4.print("4:ATL\r\n");
        Serial5.print("5:ATL\r\n");
        Serial.println("ATL sent");
        delay(100);
    }

    // Check if data has been recieved
    if (millis() - lastCheck >= SERIAL_RATE)
    {
        lastCheck = millis();
        
        Serial.println("Checking ports");
        if (Serial1.available())
        {
            Serial.println("Received data on Serial1");
            while (Serial1.available())
              Serial.write((uint8_t)Serial1.read());
            Serial.println();
        }

        if (Serial4.available())
        {
            Serial.println("Received data on Serial4");
            while (Serial4.available())
              Serial.write((uint8_t)Serial4.read());
            Serial.println();
        }
        if (Serial5.available())
        {
            Serial.println("Received data on Serial5");
            while (Serial5.available())
              Serial.write((uint8_t)Serial5.read());
            Serial.println();
        }
        Serial.println("Done checking ports");
        Serial.println();
    }
}

and a jumper chain TX1 -> RX4/TX4 -> RX5/TX5 -> RX1, I get this result

Sending ATL
ATL sent
Checking ports
Received data on Serial1
5:ATL

Received data on Serial4
1:ATL

Received data on Serial5
4:ATL

Done checking ports

Also note the datatypes and syntax for the millis() timers - that’s the official way how it should be done, to avoid troubles with value overrun and type conversions.


#6

I haven’t made any hardware modifications to the device. I will also check the RX to TX connection to see if I get anything back and post what I get after I am done with the test.

NOTE: Working from a coworker’s computer at the moment


#7

So the test worked, I will be trying a different Serial device soon.