Go Down

Topic: Array entered not being read (Read 891 times) previous topic - next topic


Sep 18, 2018, 11:54 pm Last Edit: Sep 18, 2018, 11:57 pm by jim-yang
If I make {} my start and end markers, does that mean I have to type { {,2,3,4,5,6,}} into the Serial Monitor to set the End markers  as
Code: [Select]

byte startMarker = '{';
byte endMarker = '}'

Do I need to clear the array at the end of receiving? When I run this code, it gives me an odd display. Isn't the program just supposed to overwrite previous save instances?

input into the serial monitor: (A, 5, 6,7, 5, 3, 1, Z} [Enter]

Output:This just in ... 44 32 53 44 32 54 44

Input: (A, 4, 2, 6, 4, 2, 7, Z} [Enter]
Output: This just in ... 44 32 52 44 32 50 44

Code: [Select]
void recvBytesWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
byte startMarker = 'A';
byte endMarker = 'Z';
byte rb;
byte comp;

while (Serial.available() > 0 && newData == false)
rb = Serial.read();
if (recvInProgress == true)
if (rb != endMarker)
receivedBytes[ndx] = rb;
if (ndx >= numBytes)
ndx = numBytes - 1;
recvInProgress = false;
numReceived = ndx;  // save the number for use when printing
ndx = 0;
newData = true;

else if (rb == startMarker)
recvInProgress = true;

void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
for (byte n = 0; n < numReceived; n++) {
Serial.print(receivedBytes[n], DEC);
Serial.print(' ');
newData = false;


0x80 ,0x02 ,0x50 ,0x30 ,0x02 ,0x20 ,0x10 ,0x040
is not what you think it is. That is a stream of 47 bytes. The serial monitor is an ASCII terminal program dealing with characters. If, from within Arduino for example, you performed Serial.write(0x40) it would output the single byte.
I think I see what you mean now. If I type in the Serial Monitor: A [enter] 1 [enter] 1 [enter] 1 [enter] 1 [enter] 1 [enter] 1 [enter] 1 [enter] Z [enter]. The output results in the ASCIII DEC and the resultant output is [49 49 49 49 49 49 49]. If I enter 2, it becomes 50.

What I'm confused about now is, if what you described applies to all serial communication. I think this is the issue I've ran into. In my windows form is the whole array like I was initially.

If I use serial communication to send the whole array over, the stream will still be very large and give me issues compared to me sending 1 byte at a time right?


The serial link transmits (typically) 8-bit bytes. The link itself doesn't care what is in those bytes. It's up to the applications at either end to configure the stream such that what the transmitter sends is understood by the receiver. This is what is referred to as a protocol. As you are in control of both ends, you can devise any protocol you want. Bear in mind that hitting the <enter> key simply results in sending ASCII 10 (newline) or ASCII 13 (return) or some combination of the two. This does not have to be transmitted by you unless you choose. By convention, these codes are often used to signal the end of a transmission but can be replaced by any other 'marker' you wish. What is important is that both ends are on the same page (in terms of understanding each other).

What most often is done, as the characters are received one byte/character at a time, they are added to a receive buffer and tested at the same time to see if an end-of-line or end-of-text 'marker' has been received. If that's the case, as flag is raised signalling the buffer-handling segment of your code that it is now safe to extract the contents of the receive buffer and do some thing with it. You can use an array or a circular (ring) buffer to collect the incoming bytes.
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

Go Up