UART sometimes missing first few characters on ATmega328P












2












$begingroup$


I wrote some code for the ATmega328P, (Packaged in a module that clones the Arduino Nano).



Normally when I send UART data from my computer to these micros, it is received just fine. However I have started a new project and I am getting intermittent characters missing from the beginning of a message.



The code I wrote thus far reads the incoming data and prints it back to the computer. I am trying to send "<A2>". Something like 80% of the messages are received and printed back fine. The rest are missing characters. Oddly, they are at the beginning of the message, giving me "A2>" or "2>"



I am using a baud of 115200, though 9600 and even 600 appear to give the same ratio of bad messages. I tried swapping the micro and there was no change. I'm now inclined to blame the code. I'm only sending messages once every 5 seconds or so.



The micro is being powered from the same USB cable going to the laptop that the data is sent over.



Below are oscilloscope/logic analyzer captures of two signals, one that worked fine and another that was missing characters. The probe is placed right at the "RX" pin and grounded right at "GND".



Signal received as "<A2>" (correct):
Scope capture (good)



Signal received as only "2>" (incorrect):
Scope capture (Not good?)



Those two signals look awfully close to me!



The purpose of my this code is to create a sort of protocol for received messages that processes them one at a time, even if multiples are sent all at once ("<A1><A2><A3>...") or if they are sent in only partial pieces ("<A" and then "2>").



Here's the code:



const int pinBuzzer = A5;      // the number of pin for the buzzer

unsigned long T = 0; // "T" value


//max input size expected for a single incoming msg
#define INPUT_SIZE 32
char serialCommandstr[INPUT_SIZE + 1];
char serialCommand_singleChar; //single char for itterating through incoming WX
byte serialCommandindex = 0; //index of RX buffer for itterating
bool serialCommand_msgPending = false; // if msg exists in buffer without termination ('>');
bool serialCommand_ready = false; // if terminated msg ready (a '>' is received)


void setup()
{
delay(500);
Serial.begin(115200);
Serial.println("### RESET ###");

} // END Setup()


void loop()
{
Serial.println("Waiting for input in the form of "<XY>"");
Serial.println("X=[A/B], Y=[1~8]");


serialCommand_ready = false;
while (serialCommand_ready == false)
{
serialPreParse();
}
if (serialCommandstr[1] == 'A')
{
T = (serialCommandstr[2] - 1) - char('0');
Serial.print("A, ");
}

Serial.print("T=");
Serial.print(T);
Serial.print("us...");

//Beep to indicate waveform start
tone(pinBuzzer, 1319, 50);
delay(51);

}

void serialPreParse()
{
if (Serial.available() && serialCommand_msgPending == false)
{
// NEW incoming message (buffer previously empty)

//reinitialize
serialCommandstr[0] = '';
serialCommand_singleChar = '';
serialCommandindex = 0;
serialCommand_msgPending = true; // if msg received without termination
serialCommand_ready = false; // if terminated msg ready

}

while (Serial.available())
{
if (serialCommandindex == INPUT_SIZE)
{
//maximum size of C string reached
break;
}
serialCommand_singleChar = Serial.read();
//strcat(serialCommand_singleChar, serialCommandstr);
serialCommandstr[serialCommandindex] = serialCommand_singleChar;
serialCommandindex++;
serialCommandstr[serialCommandindex] = ''; //null-termination

if (serialCommand_singleChar == '>')
{
//End of single msg.
serialCommand_ready = true;
serialCommand_msgPending = false;
break;
}

}


if (serialCommand_ready == true)
{
if (serialCommandstr[0] != '<')
{
Serial.println("nIncomplete command!");
}

Serial.println();
Serial.print("RX:"");
Serial.write(serialCommandstr);
Serial.println(""");

}


}









share|improve this question











$endgroup$








  • 1




    $begingroup$
    What is your 328Ps clock source, and at what frequency?
    $endgroup$
    – marcelm
    1 hour ago
















2












$begingroup$


I wrote some code for the ATmega328P, (Packaged in a module that clones the Arduino Nano).



Normally when I send UART data from my computer to these micros, it is received just fine. However I have started a new project and I am getting intermittent characters missing from the beginning of a message.



The code I wrote thus far reads the incoming data and prints it back to the computer. I am trying to send "<A2>". Something like 80% of the messages are received and printed back fine. The rest are missing characters. Oddly, they are at the beginning of the message, giving me "A2>" or "2>"



I am using a baud of 115200, though 9600 and even 600 appear to give the same ratio of bad messages. I tried swapping the micro and there was no change. I'm now inclined to blame the code. I'm only sending messages once every 5 seconds or so.



The micro is being powered from the same USB cable going to the laptop that the data is sent over.



Below are oscilloscope/logic analyzer captures of two signals, one that worked fine and another that was missing characters. The probe is placed right at the "RX" pin and grounded right at "GND".



Signal received as "<A2>" (correct):
Scope capture (good)



Signal received as only "2>" (incorrect):
Scope capture (Not good?)



Those two signals look awfully close to me!



The purpose of my this code is to create a sort of protocol for received messages that processes them one at a time, even if multiples are sent all at once ("<A1><A2><A3>...") or if they are sent in only partial pieces ("<A" and then "2>").



Here's the code:



const int pinBuzzer = A5;      // the number of pin for the buzzer

unsigned long T = 0; // "T" value


//max input size expected for a single incoming msg
#define INPUT_SIZE 32
char serialCommandstr[INPUT_SIZE + 1];
char serialCommand_singleChar; //single char for itterating through incoming WX
byte serialCommandindex = 0; //index of RX buffer for itterating
bool serialCommand_msgPending = false; // if msg exists in buffer without termination ('>');
bool serialCommand_ready = false; // if terminated msg ready (a '>' is received)


void setup()
{
delay(500);
Serial.begin(115200);
Serial.println("### RESET ###");

} // END Setup()


void loop()
{
Serial.println("Waiting for input in the form of "<XY>"");
Serial.println("X=[A/B], Y=[1~8]");


serialCommand_ready = false;
while (serialCommand_ready == false)
{
serialPreParse();
}
if (serialCommandstr[1] == 'A')
{
T = (serialCommandstr[2] - 1) - char('0');
Serial.print("A, ");
}

Serial.print("T=");
Serial.print(T);
Serial.print("us...");

//Beep to indicate waveform start
tone(pinBuzzer, 1319, 50);
delay(51);

}

void serialPreParse()
{
if (Serial.available() && serialCommand_msgPending == false)
{
// NEW incoming message (buffer previously empty)

//reinitialize
serialCommandstr[0] = '';
serialCommand_singleChar = '';
serialCommandindex = 0;
serialCommand_msgPending = true; // if msg received without termination
serialCommand_ready = false; // if terminated msg ready

}

while (Serial.available())
{
if (serialCommandindex == INPUT_SIZE)
{
//maximum size of C string reached
break;
}
serialCommand_singleChar = Serial.read();
//strcat(serialCommand_singleChar, serialCommandstr);
serialCommandstr[serialCommandindex] = serialCommand_singleChar;
serialCommandindex++;
serialCommandstr[serialCommandindex] = ''; //null-termination

if (serialCommand_singleChar == '>')
{
//End of single msg.
serialCommand_ready = true;
serialCommand_msgPending = false;
break;
}

}


if (serialCommand_ready == true)
{
if (serialCommandstr[0] != '<')
{
Serial.println("nIncomplete command!");
}

Serial.println();
Serial.print("RX:"");
Serial.write(serialCommandstr);
Serial.println(""");

}


}









share|improve this question











$endgroup$








  • 1




    $begingroup$
    What is your 328Ps clock source, and at what frequency?
    $endgroup$
    – marcelm
    1 hour ago














2












2








2





$begingroup$


I wrote some code for the ATmega328P, (Packaged in a module that clones the Arduino Nano).



Normally when I send UART data from my computer to these micros, it is received just fine. However I have started a new project and I am getting intermittent characters missing from the beginning of a message.



The code I wrote thus far reads the incoming data and prints it back to the computer. I am trying to send "<A2>". Something like 80% of the messages are received and printed back fine. The rest are missing characters. Oddly, they are at the beginning of the message, giving me "A2>" or "2>"



I am using a baud of 115200, though 9600 and even 600 appear to give the same ratio of bad messages. I tried swapping the micro and there was no change. I'm now inclined to blame the code. I'm only sending messages once every 5 seconds or so.



The micro is being powered from the same USB cable going to the laptop that the data is sent over.



Below are oscilloscope/logic analyzer captures of two signals, one that worked fine and another that was missing characters. The probe is placed right at the "RX" pin and grounded right at "GND".



Signal received as "<A2>" (correct):
Scope capture (good)



Signal received as only "2>" (incorrect):
Scope capture (Not good?)



Those two signals look awfully close to me!



The purpose of my this code is to create a sort of protocol for received messages that processes them one at a time, even if multiples are sent all at once ("<A1><A2><A3>...") or if they are sent in only partial pieces ("<A" and then "2>").



Here's the code:



const int pinBuzzer = A5;      // the number of pin for the buzzer

unsigned long T = 0; // "T" value


//max input size expected for a single incoming msg
#define INPUT_SIZE 32
char serialCommandstr[INPUT_SIZE + 1];
char serialCommand_singleChar; //single char for itterating through incoming WX
byte serialCommandindex = 0; //index of RX buffer for itterating
bool serialCommand_msgPending = false; // if msg exists in buffer without termination ('>');
bool serialCommand_ready = false; // if terminated msg ready (a '>' is received)


void setup()
{
delay(500);
Serial.begin(115200);
Serial.println("### RESET ###");

} // END Setup()


void loop()
{
Serial.println("Waiting for input in the form of "<XY>"");
Serial.println("X=[A/B], Y=[1~8]");


serialCommand_ready = false;
while (serialCommand_ready == false)
{
serialPreParse();
}
if (serialCommandstr[1] == 'A')
{
T = (serialCommandstr[2] - 1) - char('0');
Serial.print("A, ");
}

Serial.print("T=");
Serial.print(T);
Serial.print("us...");

//Beep to indicate waveform start
tone(pinBuzzer, 1319, 50);
delay(51);

}

void serialPreParse()
{
if (Serial.available() && serialCommand_msgPending == false)
{
// NEW incoming message (buffer previously empty)

//reinitialize
serialCommandstr[0] = '';
serialCommand_singleChar = '';
serialCommandindex = 0;
serialCommand_msgPending = true; // if msg received without termination
serialCommand_ready = false; // if terminated msg ready

}

while (Serial.available())
{
if (serialCommandindex == INPUT_SIZE)
{
//maximum size of C string reached
break;
}
serialCommand_singleChar = Serial.read();
//strcat(serialCommand_singleChar, serialCommandstr);
serialCommandstr[serialCommandindex] = serialCommand_singleChar;
serialCommandindex++;
serialCommandstr[serialCommandindex] = ''; //null-termination

if (serialCommand_singleChar == '>')
{
//End of single msg.
serialCommand_ready = true;
serialCommand_msgPending = false;
break;
}

}


if (serialCommand_ready == true)
{
if (serialCommandstr[0] != '<')
{
Serial.println("nIncomplete command!");
}

Serial.println();
Serial.print("RX:"");
Serial.write(serialCommandstr);
Serial.println(""");

}


}









share|improve this question











$endgroup$




I wrote some code for the ATmega328P, (Packaged in a module that clones the Arduino Nano).



Normally when I send UART data from my computer to these micros, it is received just fine. However I have started a new project and I am getting intermittent characters missing from the beginning of a message.



The code I wrote thus far reads the incoming data and prints it back to the computer. I am trying to send "<A2>". Something like 80% of the messages are received and printed back fine. The rest are missing characters. Oddly, they are at the beginning of the message, giving me "A2>" or "2>"



I am using a baud of 115200, though 9600 and even 600 appear to give the same ratio of bad messages. I tried swapping the micro and there was no change. I'm now inclined to blame the code. I'm only sending messages once every 5 seconds or so.



The micro is being powered from the same USB cable going to the laptop that the data is sent over.



Below are oscilloscope/logic analyzer captures of two signals, one that worked fine and another that was missing characters. The probe is placed right at the "RX" pin and grounded right at "GND".



Signal received as "<A2>" (correct):
Scope capture (good)



Signal received as only "2>" (incorrect):
Scope capture (Not good?)



Those two signals look awfully close to me!



The purpose of my this code is to create a sort of protocol for received messages that processes them one at a time, even if multiples are sent all at once ("<A1><A2><A3>...") or if they are sent in only partial pieces ("<A" and then "2>").



Here's the code:



const int pinBuzzer = A5;      // the number of pin for the buzzer

unsigned long T = 0; // "T" value


//max input size expected for a single incoming msg
#define INPUT_SIZE 32
char serialCommandstr[INPUT_SIZE + 1];
char serialCommand_singleChar; //single char for itterating through incoming WX
byte serialCommandindex = 0; //index of RX buffer for itterating
bool serialCommand_msgPending = false; // if msg exists in buffer without termination ('>');
bool serialCommand_ready = false; // if terminated msg ready (a '>' is received)


void setup()
{
delay(500);
Serial.begin(115200);
Serial.println("### RESET ###");

} // END Setup()


void loop()
{
Serial.println("Waiting for input in the form of "<XY>"");
Serial.println("X=[A/B], Y=[1~8]");


serialCommand_ready = false;
while (serialCommand_ready == false)
{
serialPreParse();
}
if (serialCommandstr[1] == 'A')
{
T = (serialCommandstr[2] - 1) - char('0');
Serial.print("A, ");
}

Serial.print("T=");
Serial.print(T);
Serial.print("us...");

//Beep to indicate waveform start
tone(pinBuzzer, 1319, 50);
delay(51);

}

void serialPreParse()
{
if (Serial.available() && serialCommand_msgPending == false)
{
// NEW incoming message (buffer previously empty)

//reinitialize
serialCommandstr[0] = '';
serialCommand_singleChar = '';
serialCommandindex = 0;
serialCommand_msgPending = true; // if msg received without termination
serialCommand_ready = false; // if terminated msg ready

}

while (Serial.available())
{
if (serialCommandindex == INPUT_SIZE)
{
//maximum size of C string reached
break;
}
serialCommand_singleChar = Serial.read();
//strcat(serialCommand_singleChar, serialCommandstr);
serialCommandstr[serialCommandindex] = serialCommand_singleChar;
serialCommandindex++;
serialCommandstr[serialCommandindex] = ''; //null-termination

if (serialCommand_singleChar == '>')
{
//End of single msg.
serialCommand_ready = true;
serialCommand_msgPending = false;
break;
}

}


if (serialCommand_ready == true)
{
if (serialCommandstr[0] != '<')
{
Serial.println("nIncomplete command!");
}

Serial.println();
Serial.print("RX:"");
Serial.write(serialCommandstr);
Serial.println(""");

}


}






microcontroller atmega serial uart c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago







Bort

















asked 1 hour ago









BortBort

3,29511639




3,29511639








  • 1




    $begingroup$
    What is your 328Ps clock source, and at what frequency?
    $endgroup$
    – marcelm
    1 hour ago














  • 1




    $begingroup$
    What is your 328Ps clock source, and at what frequency?
    $endgroup$
    – marcelm
    1 hour ago








1




1




$begingroup$
What is your 328Ps clock source, and at what frequency?
$endgroup$
– marcelm
1 hour ago




$begingroup$
What is your 328Ps clock source, and at what frequency?
$endgroup$
– marcelm
1 hour ago










1 Answer
1






active

oldest

votes


















4












$begingroup$

What jumps out to me here is the mark (low signal) levels in your UART signal. Marks should be close to zero, but the oscilloscope output you've included looks like it's only dropping to ~1.6V, especially on the first few characters. This might not be low enough to be reliably detected by your MCU.



What hardware are you using to generate UART output from the computer? Make sure it shares a ground with the computer. If this doesn't help, it may be faulty and need to be replaced.






share|improve this answer









$endgroup$









  • 2




    $begingroup$
    I think you may be onto something. The mark levels appear to drop lower over the course of he 4-character message, making the last one more likely to be recognized than the 1st.
    $endgroup$
    – brhans
    1 hour ago











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
return StackExchange.using("schematics", function () {
StackExchange.schematics.init();
});
}, "cicuitlab");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "135"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2felectronics.stackexchange.com%2fquestions%2f418565%2fuart-sometimes-missing-first-few-characters-on-atmega328p%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4












$begingroup$

What jumps out to me here is the mark (low signal) levels in your UART signal. Marks should be close to zero, but the oscilloscope output you've included looks like it's only dropping to ~1.6V, especially on the first few characters. This might not be low enough to be reliably detected by your MCU.



What hardware are you using to generate UART output from the computer? Make sure it shares a ground with the computer. If this doesn't help, it may be faulty and need to be replaced.






share|improve this answer









$endgroup$









  • 2




    $begingroup$
    I think you may be onto something. The mark levels appear to drop lower over the course of he 4-character message, making the last one more likely to be recognized than the 1st.
    $endgroup$
    – brhans
    1 hour ago
















4












$begingroup$

What jumps out to me here is the mark (low signal) levels in your UART signal. Marks should be close to zero, but the oscilloscope output you've included looks like it's only dropping to ~1.6V, especially on the first few characters. This might not be low enough to be reliably detected by your MCU.



What hardware are you using to generate UART output from the computer? Make sure it shares a ground with the computer. If this doesn't help, it may be faulty and need to be replaced.






share|improve this answer









$endgroup$









  • 2




    $begingroup$
    I think you may be onto something. The mark levels appear to drop lower over the course of he 4-character message, making the last one more likely to be recognized than the 1st.
    $endgroup$
    – brhans
    1 hour ago














4












4








4





$begingroup$

What jumps out to me here is the mark (low signal) levels in your UART signal. Marks should be close to zero, but the oscilloscope output you've included looks like it's only dropping to ~1.6V, especially on the first few characters. This might not be low enough to be reliably detected by your MCU.



What hardware are you using to generate UART output from the computer? Make sure it shares a ground with the computer. If this doesn't help, it may be faulty and need to be replaced.






share|improve this answer









$endgroup$



What jumps out to me here is the mark (low signal) levels in your UART signal. Marks should be close to zero, but the oscilloscope output you've included looks like it's only dropping to ~1.6V, especially on the first few characters. This might not be low enough to be reliably detected by your MCU.



What hardware are you using to generate UART output from the computer? Make sure it shares a ground with the computer. If this doesn't help, it may be faulty and need to be replaced.







share|improve this answer












share|improve this answer



share|improve this answer










answered 1 hour ago









duskwuffduskwuff

16.8k32650




16.8k32650








  • 2




    $begingroup$
    I think you may be onto something. The mark levels appear to drop lower over the course of he 4-character message, making the last one more likely to be recognized than the 1st.
    $endgroup$
    – brhans
    1 hour ago














  • 2




    $begingroup$
    I think you may be onto something. The mark levels appear to drop lower over the course of he 4-character message, making the last one more likely to be recognized than the 1st.
    $endgroup$
    – brhans
    1 hour ago








2




2




$begingroup$
I think you may be onto something. The mark levels appear to drop lower over the course of he 4-character message, making the last one more likely to be recognized than the 1st.
$endgroup$
– brhans
1 hour ago




$begingroup$
I think you may be onto something. The mark levels appear to drop lower over the course of he 4-character message, making the last one more likely to be recognized than the 1st.
$endgroup$
– brhans
1 hour ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Electrical Engineering Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2felectronics.stackexchange.com%2fquestions%2f418565%2fuart-sometimes-missing-first-few-characters-on-atmega328p%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

الفوسفات في المغرب

Four equal circles intersect: What is the area of the small shaded portion and its height

جامعة ليفربول