I’ve written some code to read data from a lidar and translate it to something readable which has worked. Now i wan’t to have 1 second worth of data every 5 seconds so i can round it out to a more reasonable number. This needs to be repeated infinitly
I’ve tried using setinterval and return combined with calling the funtion again but these result in an error within hex.replace. Does somebody know a fix for this? I only started coding a few weeks ago so it’s mainly cobbled together code from sites like this.
I unfortunatly can’t upgrade my node version because everything at my company runs on it and they don’t have the time and manpower to write new code for everything
The specific error is: the error shown is TypeError: Cannot read property ‘replace’ of undefined at Timeout.hexTRANSLATE [as _onTimeout] (/home/user/Desktop/lidar/Testfile.js:51:27) at listOnTimeout (internal/timers.js:549:17) at processTimers (internal/timers.js:492:7. and is shown after setInterval is completed
// Hello
// JS programma
console.log("Hello world");
console.log("Start Measurement");
const SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');
//Declare Serialport as a constant
//Create a new parser line where the data will be processed
const port = new SerialPort('/dev/ttyUSB0', { baudRate: 115200 });
const parser = port.pipe(new Readline({ delimiter: 'n' }));
//Find the lidar and establish the right baud rate.
//Create a variable in the new parser line
port.on('open', () => {
console.log('Serial port opened');
port.write("sfsdfn");
});
//Read out the socket and tell me if the lidar is there, connect to lidar via serial
port.on('error', (err) => {
console.error('Serial port error:', err.message);
});
//Read out socket and tell me if there are any errors, display any error messages
port.on('close', () => {
console.log('Serial port closed');
});
//Read out socket and tell me if its closed
parser.on('data', (line) => {
console.log('Received data:', line);
// Process the data received from the lidar here
const asciiString = hexTRANSLATE(line);
//Create the string where the readeble data needs to go
});
function hexTRANSLATE(hex) {
hexWithoutSpaces = hex.replace(/s/g, ''); // Remove spaces
// A funtion is created called hexTRANSLATE aling with the viables hexWithoutSpaces and sb (store buffer)
// hexWithoutSpaces is the same as hex.replace which removes empty spaces by following. The string has to start with/ because that's mandatory,s means string and /g makes it global so all instances will be effected.
//This basically says remove all spaces containeing a '' (empty space)
for (let i = 0; i < hexWithoutSpaces.length; i += 2) {
let str = hexWithoutSpaces.substring(i, i + 2);
let ch = String.fromCharCode(parseInt(str, 16));
}
}
setInterval(hexTRANSLATE,5000)
2
Answers
I'm going to take another look at the problem, the entire hexTRANSLATE function wasn't doing anything and something alse is translating the hex. Thanks for all the responses. I'm probbaly never picking up code after this again.
It looks like you are using Node.js to read data from a lidar device and translate it from hex to ASCII. I noticed a few issues with your code that might be causing the error.
First, you are calling the hexTRANSLATE function with setInterval, but you are not passing any argument to it. The function expects a hex string as an input, but it is getting undefined instead. That’s why the hex.replace method is throwing an error.
Second, you are not returning anything from the hexTRANSLATE function. You are just looping through the hex string and converting each pair of characters to ASCII, but you are not storing or printing the result.
Third, you are not using any logic to limit the data to one second worth every five seconds. You are just calling the hexTRANSLATE function every five seconds, regardless of how much data you have received from the lidar device.
To fix these issues, you could try something like this:
This code will store the incoming data in a buffer variable, and then translate and print it every five seconds. It will also clear the buffer after each translation so that only one second worth of data is processed at a time