Serial Port Testing

Local Server File: save as Serial_Port_Server.js

////////////

/*
Author: Prashant Patil
Date Created: 02/23/2019
Version: 1.0
Copyright: MIT

The Serial_Port_Server.js file along with the Serial_Port_Client can be used to debug the serial port devices.

*/

/ Serial Port Interface **/
var SerialPort = require(‘serialport’);

// Create Serial port and disable “auto open port during creation”
var port = new SerialPort(‘COM3’, {
autoOpen: false,
baudRate: 19200,
dataBits: 8,
stopBits: 1,
parity: ‘none’,
rtscts: false,
xon: true,
xoff: true,
xany: false });

var serial_terminator = ‘\n’
// open serial port
port.open();

/*
Serial Port Event managment
Functions below execute whenever corresponding event occures. For example,
port open, port close, whenever data is available at serial port.
*/

// port open event
port.on(‘open’, function() {
console.log(‘Serial port opened succesfully!’);
// flush serial port
port.flush(function() {

console.log('port flushed.');

});

});

// port close event
port.on(‘close’, function() {
console.log(‘Serial port closed succesfully!’);
});

// port error event if can’t open port
port.on(‘error’, function(err) {
return console.log(‘Error opening port: ‘, err.message);
});

// Read data that is available but keep the stream from entering “flowing mode”
port.on(‘readable’, function () {
//console.log(‘Data:’, port.read());
//console.log(‘Data:’, port.read.setEncoding()());
port.setEncoding(‘ascii’)
console.log(‘Recieved Character:’, port.read());
});

/** Creating WebScoket Server to serve Serial_Port_Client.html **/
const WebSocket = require(‘ws’);

// Create WebSocket Server.
const wsServer = new WebSocket.Server({ port: 1234 });

/* When created WebSocket server detects connection with a clint, it create a socket “socket” that
is now use to communicate with this clinet */
wsServer.on(‘connection’, function(socket){

console.log(‘Websocket Opened! Serial_Port_Server.js server has made connection with Serial_Port_Client.html.’);

/* event callback when the server recieves message from client with socket “socket” */
socket.on(‘message’, async function incoming(cmd_from_client) {

//console.log('Message from Serial_Port_Clent.html: %s', cmd_from_client);

// send the recieved cmd to serial port
port.write(cmd_from_client.concat(serial_terminator), function(err) {

  console.log('Send to Serial port:\n%s', cmd_from_client);
  if (err) {
    return console.log('Error on writing to serial port: ', err.message);
  }
  console.log('Message Sent!\n');
});

});

/*

Web-socket event managment

*/

// executing when connecting to clinet, for example, when browser is refreshed.
socket.on(‘error’, function(err) {

console.log('Error connecting to Serial_Port_Client.html:', err);

});

// executing when connecting to clinet is closed.
socket.on(‘close’, function(close_message) {

console.log('connection to Serial_Port_Client.html is closed:', close_message);

});

});

/////////////////////

HTML File

//////////////////

////////////////////

Getting Started With Python and Spider

Python is high-level language with many available libraries. This makes python an ideal choice for scientific computation. Other advantages of python includes

  1. Libraries (such as tkinter) for easily creating GUI (graphical user interface).
  2. Ability to easily convert python code to executable file using pyInstaller. Thus, enabling independent execution of code without installing python interpreter.
  3. Availability of pySerial for projects requiring hardware interface.

There are many IDE (Integrated Design Environment) that are available for using python. I personally prefer using Spyder (The Scientific Python Development Environment) as it has MATLAB like interpreter, variable explorer, integrated plotting options. Moreover, Fusion 360 uses Sypder IDE for creating add-on/scripts.

Getting Started with Python

A good resource to getting started with python is anaconda documentation. Simply install anaconda and it will install Spyder and other IDE along with it.  Immediately after installing anacoda update it by executing these commands in anaconda prompt. First, open the anaconda prompt by going to start – anaconda – Anacoda prompt. Next, executing the following commands:

conda update anaconda
conda update spyder

To install python packages, execute the appropriate installation commands in anaconda prompt. For example, to install pySerial package, type the following commands

conda install -c anaconda pyserial

Similarly, to install tkinter package, execute the following command

conda install -c anaconda tk

Using Serial Port in Python

Design a site like this with WordPress.com
Get started