Serial Tutorial


#21

I would like to be able to pass the serial port number I want to use as an argument to a function. This would allow me to use the same function with two different devices connected to two different serial ports. See the example below to get the idea of what I want to do.

void foo(SerialPort portname){
portname.read();

}

I could call foo from my loop function using something like:

foo(Serial4);
foo(Serial1);

Does anyone know how to do this?


#22

If you are only interested in allowing hardware serial ports, which sounds like the case, use the class USARTSerial&. For example:

void foo(USARTSerial &portName) {
}

You can pass in Serial1, Serial2, … to foo.

However, if you want to be able to pass in Serial (USB serial) or a hardware serial port (Serial1, Serial4, …) then your options are a bit more limited.

You can use Stream which will allow you to read, write, print, etc… That’s usually the best choice. The issue is that there is no common base class that supports, for example, begin().

void foo(Stream &portName) {
}

#23

If you want to support all kinds Stream based interfaces (e.g. USBSerial, USARTSerial, ParticleSoftSerial, …) you could creat a base class that internally uses Stream and derive some templated classes from it that implement the hardware specific routines (e.g. xxx.begin()).

As an example for this, you could have a look at this library which I tried to implement as “hardware agnostic” as I could get it.
https://build.particle.io/libs/uCamIII/0.0.3/tab/uCamIII.h


#24

Thanks, this is helpful.


Function calls over USB without cloud connection
#25

Thank you. Looking at your example code gives me an idea of what to do.


#26

Is there documentation for the configuration using USB serial commands? Some of them are pretty self explanatory, but not all of them.

i - Prints the device ID (24 character hexadecimal string)
f - Firmware update (using ymodem)
x - Exit listening mode
s - Print system_module_info
v - System firmware version
L - Safe listen mode (does not run user code concurrently with listening mode)
w - Configure Wi-Fi
m - Print MAC Address for the Wi-Fi adapter


#27

As far as I know, that’s the extent of the documentation on it. Which ones do you need more info on?


#28

More documentation would lead me to believe that I’m not missing anything. More documentation around what the different fields in system_module_info are would be nice. It’s not critical by any means, but more documentation and less effort on the user’s end allows the user to spend more time on their application, rather than decoding undocumented Serial interfaces.


#29

In particular I am curious what the system_module_info result means.


#30

I have followed your tutorial. I am using putty on my windows, but the only the analogread value seems to be showing up? It’s all jumbled together so I can’t decipher my data. I tried to insert commas to separate the values, or use println(), but no luck. Any ideas?


#31

Have you tried particle serial monitor --follow() to catch the serial output?
If the data shows up with line breaks there you probably need to change some PuTTY settings.