I am a beginner with Node-Red and I would like to read the data of a meter via modbus and then display it in float format
With Modbus-Read node I'm getting this data:
How can I to convert in a float number like 407.555
thanks in advance for the help
Update 06/11/2021: I'tryng to convert the array recived from modbus but the result is wrong.
I try tu use a function like this:
let pay = msg.payload;
const buf = Buffer.allocUnsafe(4);
buf.writeInt16BE(pay[0],2);
buf.writeInt16BE(pay[1],0);
msg.payload = buf.readFloatBE(0);
return msg;
In NodeRed if I add a debug I can read
0x1f85
0xb40
so is the same reading using a different modbus reader but the conversion in nodered with the function is 3.700156759202689e-32 and the value right is 2.18
Related
I'm using X-NUCLEO-NFC05A1 with STM32 NUCLEO-F401RE board to read an NFC-A (ISO14443A) tag. I couldn't find any function for reading the tag. Can anyone help me?
I tried the sample given by ST, I could find write function from there. But I couldn't find any reading function from there.
You can simply use rfalTransceiveBlockingTxRx() from rfal_rf.h provided by the RFAL library. That transceive mechanism applies to all RF technologies.
Since there is no generic commandset for interacting with NFC-A tags, the exact coding of the READ command will depend on your specific tag type. For instance, for a Type 2 tag, the READ command would consist of two bytes: 0x30 <BLOCK-ADDRESS-AS-SINGLE-BYTE>
For such a tag, you could, for instance, use something like this:
uint8_t bufferTx[2];
uint16_t lenTx;
uint8_t bufferRx[16];
uint16_t lenRxMax, lenRx;
ReturnCode status;
lenTx = 0;
bufferTx[lenTx++] = 0x30;
bufferTx[lenTx++] = 0; // TODO: change this to the read offset
lenRxMax = 16;
lenRx = 0;
status = rfalTransceiveBlockingTxRx(&bufferTx[0], lenTx, &bufferRx[0], lenRxMax, &lenRx, RFAL_TXRX_FLAGS_DEFAULT, rfalConvMsTo1fc(5));
// if status does not indicate error,
// you will now find the response in bufferRx,
// the actual response length is lenRx
I am trying to serialize a structure in C using XDR and send the serialized data to python over a tcp socket.
I tried using xdrmem_create() to create an XDR stream , call appropriate pack functions and pass the character array to a socket, to achieve this but I get an EOF error on the python side when I try to deserialize the stream .
I was able to achieve this operation successfully between
C server and C client, also between
python server and python client.
I get an error only when I use it with C and Python.
C snippet
#include<rpc/rpc.h>
....
xdrmem_create(&xdrs, arr, MAXLENGTH, XDR_ENCODE);
if(!xdr_person(&xdrs,&pkt)){
printf("ERROR");
};
.....
send(new_fd, arr, MAXLENGTH, 0)
Python snippet
import xdrlib
.....
data = s.recv(4)
unpacker = xdrlib.Unpacker(data)
message_size = unpacker.unpack_uint()
data = s.recv(message_size)
unpacker.reset(data)
person={}
person["name"] = unpacker.unpack_string().decode()
person["age"] = unpacker.unpack_int()
person["flag"] = unpacker.unpack_bool()
person["errEnum"] = unpacker.unpack_enum()
I wonder if there is a mismatch between the way data is serialized in C and Python.
Desired outcome: Send a struct of GPS data from C using a web server and have the data ( lat, long, etc.) display on a web page.
Using: Linux, libwebsockets (LWS) library for web server. C code is implemented as a standalone plugin in the LWSWS (web server) app.
Current understanding/work on project: Casting struct from plugin to a char array and trying to parse the array (using JSON) into the individual members of the original struct. Then display each variable on a web page.
I am new to Linux, LWS, HTML, JavaScript, and JSON. I can use JSON or Javascript to work with the variables, whichever makes more sense. I am not completely sure of my plan on either side (C/JS) of this part of the project.
What is the best way to transfer this information and convert for use on the web page?
Here is my struct:
struct per_session_data__gps_rcvr {
struct time_struct{
int month;
int day;
int year;
int hour;
int minute;
double second;
}time;
double latitude;
char lat_indicator;
double longitude;
char lon_indicator;
double heading;
int quality;
int satellites;
};
Sending struct out, cast to a char pointer:
// Write GPS data to GUI
n = lws_snprintf( (char *)p, sizeof(buf) - LWS_PRE, "%s", (char *)pss );
m = lws_write(wsi, p, n, LWS_WRITE_TEXT);
if (m < n) {
lwsl_err("ERROR %d writing to di socket\n", n);
return -1;
}
break;
The test code I am modifying appears to receive data in a variable called "msg". I am assuming I need to parse the data sent in the char array into chunks that correspond to the members of the original struct. Then I can display each piece of GPS data on the web page.
var socket_gps;
if (use_lws_meta)
socket_gps = lws_meta.new_ws("", "gps-rcvr-protocol");
else
socket_gps = new_ws(get_appropriate_ws_url(""), "gps-rcvr-protocol");
try {
socket_gps.onopen = function() {
document.getElementById("wsdi_statustd").style.backgroundColor = "#40ff40";
document.getElementById("wsdi_status").innerHTML =
" <b>websocket connection opened</b><br>" +
san(socket_gps.extensions);
}
socket_gps.onmessage =function got_packet(msg) {
document.getElementById(" **VARIABLE GOES HERE** ").textContent = msg.data + "\n";
}
socket_gps.onclose = function(){
document.getElementById("wsdi_statustd").style.backgroundColor = "#ff4040";
document.getElementById("wsdi_status").textContent = " websocket connection CLOSED ";
}
}
catch(exception) {
alert('<p>Error' + exception);
}
Please let me know if there is a better way to send the data out from C, and/or if I have correctly configured the data. In my research, it was mentioned that data could be sent as binary, but sending plain text messages and using JSON seemed like a better plan.
It looks like JSON can "stringify" info, but only if it's typed in or in JavaScript format already? I'm not sure. I have not seen any way to parse out a char array, a set number of characters at a time, and save those segments as variables on the HTML side. That is what I was hoping for.
Thank you for your suggestions!
Like this:
n = snprintf( (char *)p, sizeof(buf), "{\"NameofParam1\":\"%d\",\"NameofParam2\":\"%d\"}", param1, param2 );
That gives you your whole JSON string in one go (stored in buffer). Just use the appropriate flag (%s, %X, %c, %f, etc.) to capture your parameter.
I have recently started using Node-RED in a Raspberry Pi and my quest is to connect to a couple of I2C devices with the use of the node-red-contrib-i2c package.
One action which I am trying to do is to send an array of integers to the I2C device with the I2C output node but I always get the error
TypeError: String.isString is not a function
I have tried a couple of different syntax but without success. I have included an export of the flow below.
[{"id":"d2a2b073.e222c","type":"function","z":"e442e95f.6c2b98","name":"","func":"msg.address = 96;\nmsg.command = 48;\nmsg.payload = [85,85];\nreturn msg;","outputs":1,"noerr":0,"x":316,"y":243,"wires":[["7061e7e7.ac6f98","65101109.cf323"]]},{"id":"ce6f4bff.3bf998","type":"inject","z":"e442e95f.6c2b98","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":126,"y":244,"wires":[["d2a2b073.e222c"]]},{"id":"7061e7e7.ac6f98","type":"debug","z":"e442e95f.6c2b98","name":"","active":true,"console":"false","complete":"true","x":539,"y":263,"wires":[]},{"id":"65101109.cf323","type":"i2c out","z":"e442e95f.6c2b98","name":"","i2cdevice":"3d751c57.1120f4","address":"96","command":"","payload":"","count":"2","x":567,"y":223,"wires":[]},{"id":"3d751c57.1120f4","type":"i2c-device","z":"","device":"/dev/i2c-1","address":"100"}]
An example of the msg.payload sent:
msg.address = 96;
msg.command = 48;
msg.payload = [85,85];
return msg;
I am trying to retrieve the video format
Ex. my input video file is video.mpeg then I want format as mpeg
I used this function
double Format = cvGetCapturePrperty(capture,CV_CAP_PROP_FORMAT);
but it returns 0.000
Please help.
Thanks.
CV_CAP_PROP_FORMAT is not what you think. Check the documentation.
You are probably looking for:
double val = cvGetCaptureProperty( capture, CV_CAP_PROP_FOURCC );
char* fourcc = (char*) (&val);