Serial had been deprecated - looking for replacment

Hi, its me again. Looks like most of my errors are all solved. I recreated the progam using a blinky template but

Serial pc(USBTX, USBRX);

is not longer valid. Can some one point me to the correct replacement. I just want t spit some serial data out to a terminal.

(NB I’m using MBED studio and LPC1114 - so its bare metal)

Thanks

Andrew

Hello,

The old Serial were splitted into two new APIs and do not contains printf any more without external help (library)

Hitchhiker’s Guide to Printf in Mbed 6 - Mbed OS - Arm Mbed OS support forum

BR, Jan

Hi Andrew,

Good to hear most of your errors are resolved! Regarding your question, Serial has indeed been deprecated in the mbed OS, and the recommended replacement is to use the BufferedSerial or UnbufferedSerial classes, depending on your needs.

Here’s an example of how you can replace your Serial usage with BufferedSerial:

cpp

Copy code

#include "mbed.h"

// Create a BufferedSerial object
BufferedSerial pc(USBTX, USBRX, 9600); // Specify TX, RX pins and baud rate

int main() {
    pc.write("Hello, World!\r\n", 15); // Example of sending data
    while (true) {
        // Your application logic here
    }
}

Key Notes:

  1. Why BufferedSerial?
  • It provides an efficient way to handle serial communication, particularly with its buffer for RX/TX, minimizing data loss.
  • Suitable for higher baud rates and interrupt-based handling.
  1. For Simpler Use:
    If buffering isn’t critical, you can use UnbufferedSerial in the same way, but you’ll need to handle data carefully since it lacks internal buffering.
  2. Documentation Reference
    You can find more details in the mbed official documentation.

Let me know if you face any issues or need further assistance. Happy coding!

Hello Syed, Thanks for your help. I’ve made the chnges to the code, like this

BufferedSerial pc(USBTX, USBRX, 9600); //this is the first statement in main()
.
.
.
.
pc.write(“\n vendor_id = %d pair_command = %d address = %d command = %d stop_bit = %d \r”, vendor_id, pair_command, address, command, stop_bit);

but I’m now getting this error

The compiler is spitting out ‘use of undeclared identifier’. Any ideas why this is happening?

Thanks
Andrew

Ahh,

looks like the BufferedSerial statement must come at the top of the program. Let me keep digging.

Andrew

Not out of the woods yet.

I’m getting ‘too many arguments’ error now. Just for reference, all I’m doing here is spitting out to Teleterm the codes and data from the remote control for debugging/dev purposes. Under normal operation, this is disabled (I am using mbed LPC1114).

Any pointers greatly appreciated.

Rgds

Andrew

Be so kind and open links to documentation what I posted above and look how it the write method looks like.

write (const void *buffer, size_t length)

There are just two parametr - a buffer of chars and its size. So there is not possible to do something similar like with printf function. So you need something like this - cplusplus.com/reference/cstdio/sprintf/

And code will look like this

 char buffer [50];
 int n, a=5, b=3;
 n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
 pc.write(&buffer, n);

But I believe the guy with the chatGPT will explain it better again :slight_smile:

BR, Jan

I think I’m starting to smell success. Thanks for everyones help!

Hello Andrew,

It looks like the error is occurring because the compiler can’t find the declaration for one or more of the variables (vendor_id, pair_command, address, command, or stop_bit) that you’re trying to use in the pc.write() statement.

Here are a few things to check:

  1. Variable Declaration: Ensure that the variables (vendor_id, pair_command, address, command, stop_bit) are declared before the pc.write() line and are in the correct scope. They should be defined and initialized properly before using them.For example:

cpp

Copy code

int vendor_id = 123;
int pair_command = 456;
int address = 789;
int command = 101112;
int stop_bit = 1;
  1. Include the Correct Header: Make sure that the header file for BufferedSerial is included properly at the top of your file. It should look something like this:

cpp

Copy code

#include "mbed.h"
  1. Check for Typo: Verify that the variable names match exactly between the declaration and the pc.write() call (case sensitivity matters).
  2. Format String: Your format string uses "%d" to print integers, but be sure that the values you’re passing are indeed of type int. If any of them are not integers, you’ll need to adjust the format specifier accordingly.

Once you’ve verified these points, the error should be resolved.

Let me know if you’re still having trouble!

Best,
Syed

Hello Andrew,

It looks like the error is occurring because the compiler can’t find the declaration for one or more of the variables (vendor_id, pair_command, address, command, or stop_bit) that you’re trying to use in the pc.write() statement.

Here are a few things to check:

  1. Variable Declaration: Ensure that the variables (vendor_id, pair_command, address, command, stop_bit) are declared before the pc.write() line and are in the correct scope. They should be defined and initialized properly before using them.
    For example:

cpp

Copy code

int vendor_id = 123;
int pair_command = 456;
int address = 789;
int command = 101112;
int stop_bit = 1;
  1. Include the Correct Header: Make sure that the header file for BufferedSerial is included properly at the top of your file. It should look something like this:

cpp

Copy code

#include "mbed.h"
  1. Check for Typo: Verify that the variable names match exactly between the declaration and the pc.write() call (case sensitivity matters).
  2. Format String: Your format string uses %d to print integers, but be sure that the values you’re passing are indeed of type int. If any of them are not integers, you’ll need to adjust the format specifier accordingly.

If you’re working on projects like this and need advanced web development or web solutions, feel free to explore how professional services can streamline your workflow.

Let me know if you’re still having trouble!

Best,
Syed