Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the twentytwenty domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in C:\inetpub\wwwroot\blogs\wp-includes\functions.php on line 6121
nodejs – Club TechKnowHow!
Categories
C++ Installation & Configuration Programming Qemu Tools & Platforms

Serial Port for emulated esp32 using QEMU(?)

Previous posts walk through how to run code on an esp32 emulator using QEMU and how to create virtualized sensors for esp32 emulator. This post explains how you can set this up in a way that external applications such as c++/python/nodejs apps can read data from the esp32 emulator.

Building IoT controllers and dashboards requires the ability of the app to be able to read data from the SerialPort. This becomes tricky when using emulators – not only you will need the emulator to process data like the real hardware, you will also need to make sure the emulator and software are able to integrate and communicate.

When working with hardware you would print data over the serial port using something like Serial.println(data); and read it in JS using something like

const { SerialPort } = require('serialport');

const port = new SerialPort({
  path: '/dev/ttyUSB0', 
  baudRate: 9600,        
});

port.on('open', () => {
  console.log('Serial port opened');
});

port.on('data', (data) => {
  console.log('Received:', data.toString());
});

port.on('error', (err) => {
  console.error('Serial port error:', err.message);
});

Now how would we do this with an emulator running on QEMU?

Modify QEMU command – Update your QEMU command to expose the serial port via TCP. This makes QEMU listen on TCP port 1234 for serial connections.

qemu-system-xtensa -nographic -machine esp32 \
    -drive file=flash_image.bin,if=mtd,format=raw \
    -serial tcp::1234,server,nowait

Create a Node.js script to read from the TCP port:

const net = require('net');

const client = net.createConnection({ port: 1234 }, () => {
    console.log('Connected to ESP32 emulator');
});

client.on('data', (data) => {
    console.log(data.toString());
});

client.on('error', (err) => {
    console.error('Connection error:', err);
});

client.on('close', () => {
    console.log('Connection closed');
});

now you should be able to run node reader/read.js to get the data being printed on the serial port.

 


Code to this project can be found on this GitHub repo. This is part 3 of a project of me trying to develop a tool which will make it much easier for developing softwares which need to be integrated with embedded devices.