Problems reading serial data

Hi, I am having difficulty with a serial reading program on the Electron.

I am using one device: Device-1 (not arduino) to send serial messages to the Electron: Device-2.

Here is my code:


    const int bSize = 10; 

        char Buffer[bSize];  // Serial buffer
            char Command[10];    // Arbitrary Value for command size
            int ByteCount;
    void SerialParser(void) {
     ByteCount = -1;
     ByteCount =  Serial1.readBytesUntil('\r',Buffer,bSize);  
       if (ByteCount  > 0) {
       memset(Buffer, 0, sizeof(Buffer));   // Clear contents of Buffer
    void setup() {
        Serial.begin(9600);  // For Serial Monitor
        Serial1.begin(9600);  // For Hardware RX / TX
    void loop() {
             if (ByteCount  > 0) {
              Serial.print("Data     : ");
              Serial.println(Command);    // Print Data on Monitor

On device-1 I am sending the string “Test Data” every 500ms. Below is an image of the Electron output on the serial monitor.

The reason why this is confusing me is that I loaded the exact same code (Except didn’t use “Serial” and “Serial1”, just used “Serial” since it once has one) and the output worked perfectly.

So I don’t know what is different that is going on, Obviously different processors, but would that effect this?

Anybody that could offer help would be great.

Is the baud rate on the device sending to the electron correct?

Also… you can write a simple serial passthrough firmware to send whatever received via Serial1 to Serial and see if the data is corrupted?

1 Like

From glancing at your code I’m not quite sure why it’s not working, but I wrote a modified version that works properly on both the Photon and Electron. One of the things about readBytesUntil is that it blocks the loop until the condition is met. In the simple case here, it shouldn’t be an issue, but as you add more things to your loop it can get unwieldy. Instead, I add data to the buffer and process it when a line is received. When doing this, however, it’s important to process all outstanding serial data, not just one byte per loop iteration. At higher data rates, especially on the Electron, the serial receive buffer will overflow if you don’t empty it fast enough.

#include "Particle.h"

const size_t SERIAL_RECV_BUF_SIZE = 100;

char serialRecvBuf[SERIAL_RECV_BUF_SIZE];
size_t serialRecvBufIndex = 0;

void serialHandler();
void commandHandler(char *cmd, char *opt);

void setup() {

void loop() {

void serialHandler() {
	while(true) {
		int ch =;
		if (ch < 0) {

		if (ch == '\r' || ch == '\n') {
			if (serialRecvBufIndex > 0) {
				// Buffer is not empty
				serialRecvBuf[serialRecvBufIndex] = 0;
				char *cmd = strtok(serialRecvBuf, ",");
				char *opt = strtok(NULL, ",");

				commandHandler(cmd, opt);

				serialRecvBufIndex = 0;
		else {
			if (serialRecvBufIndex < (SERIAL_RECV_BUF_SIZE - 1)) {
				serialRecvBuf[serialRecvBufIndex++] = (char) ch;

void commandHandler(char *cmd, char *opt) {
	Serial.printlnf("cmd=%s opt=%s", cmd, ((opt != NULL) ? opt : "NULL"));


Hi thanks for the suggestion. I wrote a pass through sketch and it revealed that there was a problem with my device that was sending data. I should have suspected that was the problem, but it worked fine when I read the data with my Arduino Uno using the same code. I am not sure why that is, but atleast I now know the problem.

I was able to fix the problem on my sending device and it is working fine now. Thanks!!!

Thank you for taking the time to modify and test my sketch!! I test yours and it works fine now that I fixed the external problem. My old sketch works fine now too, but I will incorporate your for the reasons you suggest.

Thanks again!!!