Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I mock serial port during development?

I am developing a node.js application for my Raspberry Pi which receives data from its serial port, but I don't directly develop the application on it, I use my main computer instead. So I have this code in my app.js :

var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var sp = new SerialPort("/dev/ttyACM0", {
    parser: serialport.parsers.readline("\n")
});

sp.on("data", function (rawData) {
...

This works well on the Rasperry Pi but I want to be able to run the application on my development computer first without having to comment every block of code about the serial port.

What is the best way to achieve this ? Is there a way to mock the serial port ?

like image 685
Harijoe Avatar asked Feb 27 '15 18:02

Harijoe


1 Answers

AFAIK, there aren't any libraries that do this natively right now. What I've done in the past is to use the node-serialport library's own test code as an example, eg: https://github.com/Manetos/node-serialport/blob/157e6f9df7989abd72d509c9827d13b2b10258de/test_mocks/linux-hardware.js

If you take a look at that file, they're mocking the serial port behavior for their own tests, you can simply copy what they're doing there and use it in your stuff, and you should be good to go.

Hope that helps!

like image 179
rdegges Avatar answered Sep 28 '22 14:09

rdegges