OpenMarine

Full Version: AI to write python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
No excuse for messy code anymore ?

https://www.youtube.com/watch?v=jRAAaDll34Q

does node red as well, this in www.perplexity.ai

Code:
can you write and display node red code to check that the checksum of an nmea0183 sentence is correct and fix if it is wrong
PERPLEXITY
Here's the Node-RED code to check that the checksum of an NMEA0183 sentence is correct and fix it if it is wrong:
Drag and drop an inject node from the palette onto the workspace.
Double-click the inject node to open its configuration panel.
Set the Payload field to your NMEA0183 sentence here.
Drag and drop a function node from the palette onto the workspace.
Connect the inject node to the function node.
Double-click the function node to open its editor.
Copy and paste the following code into the editor:
javascript
var sentence = msg.payload;
var parts = sentence.split('*');
var checksum = parts[1];
var data = parts[0].substring(1);
var expected = 0;
for (var i = 0; i < data.length; i++) {
  expected ^= data.charCodeAt(i);
}
if (checksum == expected.toString(16).toUpperCase()) {
  return msg;
} else {
  var fixed = sentence.substring(0, sentence.length - 3) + expected.toString(16).toUpperCase();
  msg.payload = fixed;
  return msg;
}

Click Done to close the editor.
Deploy the flow.
This flow takes an NMEA0183 sentence as input and checks its checksum. If the checksum is correct, the flow returns the original message. If the checksum is incorrect, the flow fixes the checksum and returns the corrected message. The corrected message is sent to the next node in the flow.
Note that this flow assumes that the NMEA0183 sentence is in the format $SOMETHING*CHECKSUM\r\n. If your sentence is in a different format, you may need to modify the code accordingly.

Does this not just blow you away ??

Code:
please explain what this node red function does, check for errors  and suggest any changes to make it better "

if(msg.payload = undefined ||msg.payload.messageType == 4 || msg.payload.messageType == 20 || msg.payload.messageType == 21){
//    msg.payload = "type B ";
//    node.warn(msg.payload.messageType);
    return
}
//node.warn(msg.payload.messageType);
return msg;"

PERPLEXITY

The Node-RED function checks if the msg.payload is undefined or if it has a messageType of 4, 20, or 21. If any of these conditions are true, the function does nothing and returns. If none of these conditions are true, the function returns the original msg object.
There is an error in the function code. The if statement uses the assignment operator = instead of the comparison operator == to check if msg.payload is undefined. This means that the if statement always assigns undefined to msg.payload and the condition is always true. To fix this error, replace = with == in the if statement.
Here's the updated function code:
javascript
if (msg.payload == undefined || msg.payload.messageType == 4 || msg.payload.messageType == 20 || msg.payload.messageType == 21) {
    // Do nothing and return
    return;
}

// Return the original msg object
return msg;

To make the function better, you could add comments to explain what the function does and how it works. You could also use an array to store the messageType values to check instead of using multiple || operators. Here's the updated function code with these changes:
javascript
// Check if the msg.payload is undefined or has a messageType of 4, 20, or 21
if (msg.payload == undefined || [4, 20, 21].includes(msg.payload.messageType)) {
    // Do nothing and return
    return;
}

// Return the original msg object
return msg;

Note that the switch statement suggested in the previous answer is not necessary for this function because there are only a few messageType values to check.