Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'ReadLine')

311 Views Asked by At

When launching the app.js, there is an error written above, here is an example of the code

const SerialPort = require('serialport');
const Readline = SerialPort.parsers.ReadLine;
const port = new SerialPort('COM4', { baudRate: 9600 });
const parser = new Readline();

// Открытие COM порта и установка связи
port.open(function(err) {
  if (err) {
    return console.log('Ошибка открытия порта: ', err.message);
  }
  
  console.log('Порт открыт');
});

// Парсинг принятых данных
port.pipe(parser);
parser.on('data', function(data) {
  // Обработка полученных данных
  console.log('Получены данные: ', data);
});

// Управление сервоприводом
function controlServo(servoNumber, angle) {
  const command = `${servoNumber},${angle}`;
  port.write(command, function(err) {
    if (err) {
      return console.log('Ошибка записи: ', err.message);
    }
    
    console.log('Отправлена команда: ', command);
  });
}

Why does the code not run, but gives an error? I sat on most of the forums and did not find an answer. I have fully checked the instructions for using the SerialPort library and the ReadLine usage instructions

// Пример использования
controlServo(1, 90); // Управление сервоприводом 1: угол 90 градусов

I tried to load the ReadLine module directly, but eventually an error appeared in the console that serialport is not a constructor

There is also a site code from which data should be transmitted to the COM port

<!DOCTYPE html>
<html>
<head>
  <title>Управление сервоприводами</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
  <div class="container">
    <h2>Управление сервоприводами</h2>
    <form>
      <div class="form-group">
        <label for="servo1">Сервопривод 1:</label>
        <input type="number" class="form-control" id="servo1" placeholder="Угол">
      </div>
      <div class="form-group">
        <label for="servo2">Сервопривод 2:</label>
        <input type="number" class="form-control" id="servo2" placeholder="Угол">
      </div>
      <div class="form-group">
        <label for="servo3">Сервопривод 3:</label>
        <input type="number" class="form-control" id="servo3" placeholder="Угол">
      </div>
      <div class="form-group">
        <label for="servo4">Сервопривод 4:</label>
        <input type="number" class="form-control" id="servo4" placeholder="Угол">
      </div>
      <button type="button" class="btn btn-primary" onclick="recordActions()">Запись</button>
      <button type="button" class="btn btn-success" onclick="playbackActions()">Воспроизведение</button>
      <button type="button" class="btn btn-danger" onclick="clearActions()">Очистка</button>
    </form>
  </div>
  
  <script>
    function recordActions() {
      // Код для отправки команды записи на COM порт
      // Используйте значения из полей ввода для углов сервоприводов
    }
    
    function playbackActions() {
      // Код для отправки команды воспроизведения на COM порт
    }
    
    function clearActions() {
      // Код для отправки команды очистки на COM порт
    }
  </script>
</body>
</html>

here is the error code that the console outputs:

Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'ReadLine')
    at <anonymous> (file:///C:/Users/%D0%93%D0%B5%D0%BD%D0%B4%D0%B5%D0%BB%D1%8C%D1%84%D0%B8%D0%BD%D0%B8/Desktop/%D0%9D%D0%BE%D0%B2%D0%B0%D1%8F%20%D0%BF%D0%B0%D0%BF%D0%BA%D0%B0%20(7)/app.js:2:37)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at executeUserEntryPoint (node:internal/modules/run_main:83:12)
    at <anonymous> (node:internal/main/run_main_module:23:47)
0

There are 0 best solutions below