Function can not send Buffer? + Serial problem

I am trying to send bytes on serial, not sure exactly what the problem is.

On of the problem is it seems function can not output a binary buffer only but complains of error.

when sending the complete msg that contains also other data also the buffer is sent, but can not find a way to separate the buffer so that only it would go to serial.

And then there is still the serial configuration. USB serial never has stop bits, but it only has options 1 or 2 stop bits?

This is the function, I would like to output the buffer like this, returning the complete msg works, just returning the buffer does not.

var buffer = Buffer.alloc(12);

var Camera = msg.payload.camera||1;
var Type = msg.payload.type||10;

var FocusH = ((msg.payload.focus >> 8) & 0xFF)||0;
var FocusL = (msg.payload.focus & 0xFF||0);
var LVZ_ID = 1;

var Counter = context.get('Counter')||0;
if (Counter >= 127) {Counter = 0;} else {Counter++;}
context.set('Counter',Counter);

var Checksum=Camera+Type+Counter+LVZ_ID+FocusH+FocusL;
var ChecksumH = (Checksum >> 8) & 0xFF;
var ChecksumL = Checksum & 0xFF;

buffer[0] = 0xFF;
buffer[1] = 0xFF;
buffer[2] = 0xFF;
buffer[3] = Camera;
buffer[4] = Type;
buffer[5] = Counter;
buffer[6] = LVZ_ID;
buffer[7] = FocusH;
buffer[8] = FocusL;
buffer[9] = 0;
buffer[10] = ChecksumH;
buffer[11] = ChecksumL;

msg.payload.out = buffer;
return msg;

The serial node expects its payload in msg.payload (pretty sure it says that in the built in help?)

Change this...

To this

msg.payload = buffer;

Note, if you need to keep the original Payload, just copy it to another property in the msg object before you overwrite msg.payload with the buffer.

Thanks, I figured that out finally my self too, but still was not working so build a test system to see the data on terminal. Everything looked fine until I realised low and high byte where on wrong order. Fixing that and now it works :slight_smile:

I am just begining with node RED and everything is still very new for me, coming from LabView and C, it is very strange how loosely variables are defined and that I can not route data to the icon output I want and everything comes in from one input.

It doesn't take too long to get to grips with.

Might be worth you spending an hour watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point & contains a few gold nuggets you might otherwise miss.

Ps, There is a contrib node that might be of interest - node-red-contrib-buffer-parser to help with reading and writing buffers.

Is there a way to select the serial port from dashboard? Now I am setting it on the serial port configuration , but would be needed to be be able to select the right serial port from dashboard from a list of available serial ports.

A config node cannot be dynamically changed unless it has been designed in such a way.

There are a couple of workarounds...
Use env vars for setting up (if the input can be typed into)
Or setup multiple serial ports and wire the nodes / redirect the flow based on user selection.

I'm sure (especially on Linux) you could do something at the OS level (like an alias)

But is seems the solution would be pretty simple if the Serial port nodes would provide out the available serial ports it already well detects. What would it require to make that data available?

If we use this with our camera control device, the customer should be easily able to select the port from user interface, or better I could make polling on available ports to find out if the device is connected, but would need the available ports and way to select the port other than from the node configuration.

And it rather should work the same on all platforms.

Multible serial ports could maybe work on Windoews as they are called COM5 etc. but on linux and macOS the port names do not have any predefined structure that I know of.

Someone to look at the source code, discuss with one of the leaders how they would approach the modifications & finally a pull request.

Note, I don't think it will be quite as simple as you think (you would need to have some kind of control message structure that can query ports, stop start ports & set settings of the port). Quite a restructuring of the current serial port nodes.

Another possibility is to allow the user to specify the serial port to use via the dashboard and use that input to write a file containing, for example
process.env.SERIAL_PORT = "/dev/ttyUSB0"
and then automatically restart node-red. In settings.js require that file so that when node-red is restarted it will make that environment variable available. In the serial port specify that the SERIAL_PORT env var is to be used.

Is there a way to get the list of available serial ports?

Ideally the SW would automatically go trough the available ports and find our device based on response from it.

it would be also ok, if the users selects from list of available ports like from the serial port configuration, but having that available on dashboard.

the user finding from the computer first on what serial port it is connected and then typing or copy/pasting that information would be rather undesirable.

I don't think that is a node-red issue is it? I don't know if it is even possible on Linux.

The Serial node has the list of available serial ports, they are visible on the serial port configuration. I hope there would be interface to get the list of available serial ports from the Serial port node, so the selection of serial port could be done on dashboard, not in the coding environment.

You could replicate that functionality in a function node.
You would need to add necessary module references to global in settings.js (not necessary in node-red v1.3 which will be released soon)

I am still very much lost with all the terminology but can pick key words and google, but I think the functionality I am requesting is quite obvious, and still wondering if I am just missing something.

Yes the functionality is available on the node configuration, just would want that configuration to be available on dashboard, and/or list of available ports available to be used in function node and some way to select the port to be used from function node.

What I mean is if you look at the source code behind that functionality on the serial node, you could duplicate that in a function node. Once you have a list or array of serial ports in a function, it is very easy to pass this to the dashboard.

However this code will probably need access to modules like OS or FS. A function node ordinarily, does not have access to these modules so you will need to modify settings.js to include any extra modules. See here

Hopefully that is clearer?

Ps, once v1.3 of node-red is released, modifying settings.js is no longer a requirement (v1.3 function node has new UI elements to specify access to additional modules)

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.