Help with setting up UART receive on spark core via USB [Solved]

Hi guys,

I have been looking through guides on this site and others. I am trying to send a Uart/serial value to my spark core and if it receives the data it will print saying it was right or wrong. This is just a simple test to make sure I will be having my uart working for a project.

    void setup() {
    Serial.begin(9600);
    Serial.println("SERIAL: BEGIN");
}

void loop() {
    if (Serial1.available() > 0)
  {
    int i = Serial1.read();
    if (i==1){
        Serial.println("I am number 1");
    }else{
        Serial.println("I am not number 1");
    }
  }
}

This is my code as of now. I am not even seeing the “SERIAL: BEGIN” in my putty.

Along with that I am unsure of how to send that int value over serial/usb.

Any help is much appreciated.

i can see a couple of things…

Are you using windows? you may need to do some magic to make the core wait for windows to connect. have a look at this

it looks like you are missing the Serial1.begin() call so the core can get that set up too… I’m a little surprised there wasn’t some fun hard to read and understand errors when compiling the code :slight_smile:

Okay I tried what you said to add and I got some of those “fun hard to read and understand errors”

Code is:

#include "application.h"

void setup() {
    Serial1.begin()
    Serial.begin(9600);
    
    while(!Serial.available()) SPARK_WLAN_Loop();
}

void loop() {
    Serial.println("SERIAL: BEGIN");
    delay(100);
    
    if (Serial1.available() > 0)
  {
    int i = Serial1.read();
    if (i==1){
        Serial.println("I am number 1");
    }else{
        Serial.println("I am not number 1");
    }
  }
}

Error is:

In file included from ../inc/spark_wiring.h:29:0,
from ../inc/application.h:29,
from the_user_app.cpp:1:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
^
the_user_app.cpp: In function 'void setup()':
the_user_app.cpp:4:19: error: no matching function for call to 'USARTSerial::begin()'
void loop();
^
the_user_app.cpp:4:19: note: candidates are:
In file included from ../inc/application.h:33:0,
from the_user_app.cpp:1:
../inc/spark_wiring_usartserial.h:83:10: note: void USARTSerial::begin(long unsigned int)
void begin(unsigned long);
^
../inc/spark_wiring_usartserial.h:83:10: note: candidate expects 1 argument, 0 provided
../inc/spark_wiring_usartserial.h:84:10: note: void USARTSerial::begin(long unsigned int, uint8_t)
void begin(unsigned long, uint8_t);
^
../inc/spark_wiring_usartserial.h:84:10: note: candidate expects 2 arguments, 0 provided
the_user_app.cpp:5:5: error: expected ';' before 'Serial'
#line 1
^
make: *** [the_user_app.o] Error 1

Flash unsuccessful.

now i’m confused… do you have a device hooked up to the TX and RX pins of the core and that’s what your tying to get in your terminal or are you wanting to read from the computer/USB serial connection and reply back on the same with the text?

With the errors they are pretty clear whats wrong, start at the bottom of the error list and work your way up, it even lets you know where to look, the_user_app.cpp is your code and 5:5 is the line and position (but its normally a line or 2 out just so you know). the other bits of code can be found on the spark github page if you need to reference them, but the error will tell you whats wrong (hint: what speed are you talking at)

Serial.whatever() is the USB serial connection
Serial1.whatever() is the TX and RX on the core
hence why i was confused… in your initial code the lines after the loop() are serial1 so will be reading the RX line of the core.

I am sorry, I only have the sparkcore. I am trying to send a value to it via putty/or similar. Once the sparkcore receives the value it goes in to an if statement that prints whether it was right or wrong.

Okay, now I have it printing to putty correctly. Do you know how to send a value to the uart over usb? I am stuck there now. Code is now:

#include "application.h"

void setup() {
    Serial.begin(9600);
}

void loop() {
    Serial.println("SERIAL: BEGIN");
    delay(100);
    
    int i = Serial.read();
    if (i==1){
        Serial.println("I am number 1");
    }else{
        Serial.println("I am not number 1");
  }
}

Hi @abarkle1

The ASCII character 1 on your keyboard is not the equal to 1 as you have in your program. Try checking if the value of i is equal to 49 instead since that is the ASCII code for “1”.

Serial.read() will return -1 I believe if there are no characters to read. You would be better off using Serial.available() before reading so you can see if there are characters to be read.

1 Like

does that mean its always saying “i am not number 1” :wink:
ahhh @bko beat me to it while i was testing code!

here is a little code to demonstrate

void setup() {
    Serial.begin(9600);
}

void loop() {
    if (Serial.available() > 0)
  {
    char c = Serial.read();
    int i = c;
        Serial.println(c);
        Serial.println(i);

  }
}

if your wanting to read numbers you will probably need a function called atoi (ascii to integer) its a standard c++ function and google will be helpful in finding how to use it

1 Like

Okay so I said this eariler but Idk if it wasnt seen. I do not know how to send something via serial I tried Serial.write(1); on spark core (the rx and tx pins are wired together) and received to my putty.

a weird scribble that looked like a broken character, followed by 255 and then Serial: Begin

ummm really confused now

what is the overall goal? what do you want to do with UART/Serial? will it be a serial device hooked up to the spark core, or the spark core hooked up to a computer using USB

Serial.read Serial.write and Serial.print are UART over USB
Serial1.read Serial1.write and Serial1.print are UART on the TX/RX

if its a device hooked up to the spark core then connect it to the TX and RX pins.

if its just the spark core to a computer then totally forget about the TX and RX pins

if you use putty, to open up a terminal window. whatever you type in the window will get sent to the core

If you do Serial.write(1); you will not get the character 1 in your PuTTY for the reason @bko has explained already.


I guess this is only to learn how things work, isn’t it?

If you want your Core send itself a byte via TX/RX, then you’d wire it as you said and do Serial1.write(b);
If you want to send it back to your PC, you’d do as already done Serial.write(b);.
If you want to send a numeric variable in human readable form, you should use Serial.print(num);.
When receiving multiple bytes, you should read all available bytes in a loop.

With all this and the info from previous posts you should be able to get it working.
If not, post your most recent code again and we’ll point out what might call for improvement.

1 Like

Thank you all for your help I see my mistakes and am getting close to the end I think now.

This new code has caused a slightly different issue now though:

#include "application.h"

void setup() {
    Serial1.begin(9600);
    
}

void loop() {
        Serial.println("SERIAL: BEGIN");
        delay(100);
        
        Serial1.write(1);
        delay(100);
        
        char c = Serial1.read();
        int i = c;
            Serial.println(c);
            delay(100);
            Serial.println(i);
           
}

So now that I have changed my code so that I want to have the sparkcore send itseld a value (via tx pin) and receieve on its rx pin.Then following your advice compare the char value vs the int value to see the difference.

My new issue is with using `Serial1.begin(9600); I now will get request time out errors to the core when trying to flash OR it will just never finish updating (and not show up in my device manager).

Again I really appreciated all of your help and sorry I was so confusing.`

Now you’re missing Serial.begin(115200);!

You need this in setup() in order to have the line Serial.println("SERIAL: BEGIN"); (and all the other Serial.* lines) working.

But your flashing problems won’t come from this code.

And my suggestion was not to test the difference between char and int, but between print and write.

1 Like

Thank you so much.

I have it all working as I need it and am extremely happy. I will leave my final code for anyone who comes along later looking for help.

#include "application.h"

void setup() {
    Serial.begin(9600);
    Serial1.begin(9600);
    
}

void loop() {
        Serial.println("SERIAL: BEGIN");
        delay(100);
        
        //Serial1.write("A");
        Serial1.write(1);
        delay(100);
        
        //char c = Serial1.read();
        int i = Serial1.read();
        
        Serial.println(i);
        
        
        Serial.println("Did I print?");
}
1 Like