Serial Tutorial

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?

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) {
}
1 Like

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

2 Likes

Thanks, this is helpful.

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

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

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

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.

In particular I am curious what the system_module_info result means.

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?

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.

Hi @rickkas7 (and others), how can I set my Electron into DFU mode with Linux?

I tried with

stty -F /dev/ttyACM0 14400

but I got "invalid argument “14400”’

14400 is not an accepted baud rate with some/most Linux distros.
However, when you have CLI installed particle usb dfu will do the job.

Oh wow! Unfortunately I can't run usb dfu because I get:

! The particle-usb dependency is missing or invalid.
! Please reinstall: https://docs.particle.io/tutorials/developer-tools/cli/#installing

Tried to

After the reinstalling I get the error when I run

/home/myUser/bin/particle usb dfu

Even if particle usb prints:

epar@epar-laptop:~/bin$ ./particle usb
Control USB devices
Usage: particle usb <command>
Help:  particle help usb <command>

Commands:
  list             List the devices connected to the host computer
  start-listening  Put a device into the listening mode
  stop-listening   Make a device exit the listening mode
  safe-mode        Put a device into the safe mode
  dfu              Put a device into the DFU mode
  reset            Reset a device
  configure        Update the system USB configuration

Global Options:
  -v, --verbose  Increases how much logging to display                                                                                 [count]
  -q, --quiet    Decreases how much logging to display

You might need to delete the ~/.particle/node_modules directory as described here:

1 Like

Yes, deleting that folder solved the problem. I suggest you to add it into the warning along with the url for reinstalling :wink:

I think this is a Serial issue, but to be honest I am not sure. I am sending data over a Mesh network from edge devices to my gateway, and then writing that data over the USB port.

Serial.write(data);

Off the USB port, I’m getting all sorts of extra data & errors I think are from the Serial port? Does anyone know where this extra metadata is coming from (and how to stop it/ parse it out and only send my simple dictionary over in Serial.write(data); ?

Here is the output I’m seeing -

0000059261 [wiring] TRACE: parse packet 95
{"time":946768410,"device”:”xxxx”,”count":0,"version":"0.1.4"}0000064393 [wiring] TRACE: parse packet 98
{"time":946768420,"device":"xxxx","count":1200,"version":"0.1.4"}0000069259 [wiring] TRACE: parse packet 95
{"time":946768420,"device":"xxxx","count":0,"version":"0.1.4"}0000074395 [wiring] TRACE: parse packet 98
{"time":946768430,"device":"xxxx","count":1200,"version":"0.1.4"}0000079259 [wiring] TRACE: parse packet 95
{"time":946768430,"device":"xxxx","count":0,"version":"0.1.4"}0000081752 [comm.protocol] TRACE: Reply recieved: type=2, code=0
0000081754 [comm.protocol] INFO: message id 99 complete with code 0.00
0000081756 [comm.protocol] INFO: rcv'd message type=13
0000084394 [wiring] TRACE: parse packet 98
{"time":946768440,"device":"xxxx","count":1199,"version":"0.1.4"}

All these lines starting with a milliseconds timestamp and [....] category label come from the logging commands embedded in the device OS.
You obviously have a SerialLogHandler declared but haven’t limited the scope as shown in the reference docs
https://docs.particle.io/reference/device-os/firmware/photon/#logging-categories

If you want to get rid of all the logging output and only want to see your Serial.write() output you can just remove/comment the declaration of the SerialLogHandler entirely.

3 Likes

Nailed it. Thanks @ScruffR!

2 Likes

Hi,

I have 9 parameters reading by the electron remotely, now i am thinking to use LCD to locally monitor them. i found LCD that can accept RS232, so is it possible to to get all these 9 parameters from particle electron to the LCD using RS232 communication?

thank you