Using Serial and SerialUSB at the Same Time

Hello,

I've got an application using Serial to read/write data to a Bluetooth modem. Currently, the bluetooth modem is connected to the pre-marked Rx/Tx pins on the Due. I am using the SerialUSB (Native USB Port) for debugging in the Serial monitor. This has been working great. Now, I want to issue some commands to the connected modem to change it's name. I cannot seem to figure out how to take commands in through the Serial Monitor using SerialUSB.read() and write them to the Serial, and be able to read it's response back to the Serial Monitor through SerialUSB.

This is currently what I am trying, and the device is reacting, but I just see a bunch of numbers printed to the screen. Is there a way for Serial to translate this to ASCII by default?

void setup()
{
    // Serial monitor
    SerialUSB.begin(9600);

    // Bluetooth modem
    Serial.begin(115200);
}

void loop()
{
    if (SerialUSB.available())
    {
        unsigned char out_msg = SerialUSB.read();
        Serial.write(out_msg);
    }

    if (Serial.available()) 
    {
        unsigned char in_msg = Serial.read();
        SerialUSB.print(in_msg);
    }
}

This is the device and guide I am using for communication: Device Setup Guide

They are using SoftwareSerial library, which I assumed that I could directly translate to Serial and SerialUSB and have it work the same, but apparently I am doing something wrong?

If I change Serial.write(out_msg) to Serial.print(out_msg) I get no response back to the Serial Monitor?

Thanks in advance for any help!

I'm not 100% sure on your hardware setup, however.

I would connect the Due TX that is currently connected to your Bluetooth RX and connect it to the Dues RX line.

This way, what every you send "out" of the TX is returned through the RX - this removes any complication with the bluetooth and you can be certain that your code is passing data correctly.

By the way, your code currently only reads 1 character every loop. So it essentially does this:

PC Send: "Hello"

Code: 

Read "h"
Send "h"

check Bluetooth Serial

Read "e"
Send "e"

check Bluetooth Serial

Etc...

If you want to take the whole "Hello" sentence, you need to use a "while" loop instead of just if statements.

Hope this helps.

Apart from that, I believe your code should act as you intend

Good Luck
JW

Turns out the data was transferring correctly. The problem was the type for reading SerialUSB line was an unsigned char so it was always printing the decimal values instead of the ASCII characters I was expecting.

JWScotSat:
By the way, your code currently only reads 1 character every loop. So it essentially does this:

PC Send: "Hello"

Code:

Read "h"
Send "h"

check Bluetooth Serial

Read "e"
Send "e"

check Bluetooth Serial

This does seem to be the best way of doing it on a Due, since the Arduino framework doesn't have a serial transmit buffer for the Due. You end up wasting ridiculous amounts of time sending strings from the Due but sending a single character at a time is buffered so your sketch sees almost zero time used in the Serial.write().