I was looking at Robin2's Serial input basics and trying to run the same tests he did, but I can't seem to get this code working. I made some slight modifications, but I don't think the modifications I made would affect it. If I uncomment my disptest function, it prints every element in the array(0-7) properly. In the Uno TX side, when writing the serial HEX is used in the Serial.print section, but shouldn't the monitor already know that due to the 0x prefix?
His test uses an Uno and a Mega to communicate, whereas I'm only using a UNO. I'm also using Visual Studio instead of the Arduino IDE.
I created an array to make sure I was displaying the contents within the array properly and it works fine.
Are my assumptions correct?
- There should not be an issue with me using a Uno instead of a Mega to receive the data using the code from the example.
Since the RX is the Mega, but I am using the Uno instead. In the getting started section, the instructions both reference using the Arduino IDE and in both sections they use the same example, so I don't have to worry about the code not working because I'm using different devices right. I saw in a previous post that someone had issues because they were using Python 3 and the code was written for Python 2.7. Since they were using they are using two different devices to communicate, the code on one device was incorrect affected the other. Since I'm using only 1 device and the Serial Monitor, there shouldn't be an issue.
- The Uno didn't run through the setup correctly.
I upload the code onto the Uno, and open the serial monitor. All that is displayed is Opening port and Port Open. Since it hasn't printed "Arduino is ready", this should mean that the setup function hasn't actually successfully ran yet? The only thing that happened is the serial communication is established via the 9600 baud rate.
- Since the setup isn't finished, the data being entered is not being read.
I open the Serial monitor and enter my array into the Serial and press enter {0x80 ,0x02 ,0x50 ,0x30 ,0x02 ,0x20 ,0x10 ,0x040}. The RX pin Blinks every time I press enter, but no output occurs. The Uno is receiving data from the serial monitor, but because of 2 and "Arduino is ready" isn't being printed it's not actually reading the data but just acknowledging it received data?
- 2,3 are incorrect. Since the disptest function is displaying the values in the array of each element, and VS said the upload process has finished, everything is loaded properly.
If I uncomment the disptest function, the Serial Monitor output constantly shows me the values of the test array. which means that the setup did run successfully. Since the function is constantly printing the values of the test array, the loop is running perfectly fine. If I enter numbers into the Serial Monitor and press enter the RX lights up, but since it's not being read properly, the data isn't being stored correctly.
-
I'm not entering the array properly.
Currently I'm trying to enter the array as {0x80 ,0x02 ,0x50 ,0x30 ,0x02 ,0x20 ,0x10 ,0x040}. This might not be the right way to enter the array? It's how the data is written in the code so I assumed it should be right. I've tried using [] and <> and (). -
Edit and nothing happens.
I modified the code so that it only displays my test array if the newData is false, but by default it is and therefore will be displaying, but even if I enter data, newData is not changing to true so this means that recBytesWithStartEndMarkers is not working properly. The RX is still lighting up when I enter numbers into the Serial Monitor.
void loop()
{
recvBytesWithStartEndMarkers();
showNewData();
disparraycond();
}
void disparraycond()
{
while (newData == false)
{
disptest();
}
}
I looked at the recvBytesWithStartEndMarkers function, and I think it should work fine, but I'm not sure why it wouldn't.
const byte numBytes = 8;
byte test[numBytes] = { 1,2,3,4,5,6,7,8};
byte receivedBytes[numBytes]; //Entered into the Serial Monitor: {0x80 ,0x02 ,0x50 ,0x30 ,0x02 ,0x20 ,0x10 ,0x040}
byte numReceived = 0;
boolean newData = false;
// The setup() function runs once each time the micro-controller starts
void setup()
{
Serial.begin(9600);
Serial.println("<Arduino is ready>");
}
// Add the main program code into the continuous loop() function
void loop()
{
recvBytesWithStartEndMarkers();
showNewData();
//disptest();
}
void disptest()
{
for (byte i = 0; i <= 7; i++)
{
Serial.print("Byte: "); Serial.println(i);
Serial.print("Value: "); Serial.println(receivedBytes[i], DEC);
}
}
void recvBytesWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
byte startMarker = 0x80;
byte endMarker = 0x40;
byte rb;
while (Serial.available() > 0 && newData == false) {
rb = Serial.read();
if (recvInProgress == true)
{
if (rb != endMarker)
{
receivedBytes[ndx] = rb;
ndx++;
if (ndx >= numBytes)
{
ndx = numBytes - 1;
}
}
else {
receivedBytes[ndx] = '\0'; // terminate the string
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], HEX);
Serial.print(' ');
}
Serial.println();
showGroupsOfBytes();
newData = false;
}
}
void showGroupsOfBytes() {
for (byte n = 0; n < numReceived; n++) {
Serial.print(receivedBytes[n], HEX);
Serial.print(' ');
if ((n + 1) % 5 == 0) {
Serial.println();
}
}
Serial.println();
}