Skip to content

Commit 20ecbac

Browse files
authored
Add files via upload
1 parent 92639dc commit 20ecbac

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

ListPorts.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var serialport = require("serialport");
2+
var SerialPort = serialport.SerialPort;
3+
4+
// list serial ports:
5+
serialport.list(function (err, ports) {
6+
ports.forEach(function(port) {
7+
console.log(port.comName);
8+
console.log(port.pnpId);
9+
console.log(port.manufacturer);
10+
});
11+
});

OpenPort.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var serialport = require('serialport');// include the library
2+
SerialPort = serialport.SerialPort; // make a local instance of it
3+
// get port name from the command line:
4+
portName = process.argv[2];
5+
6+
var myPort = new SerialPort(portName, {
7+
baudRate: 9600,
8+
// look for return and newline at the end of each data packet:
9+
parser: serialport.parsers.readline("\r\n")
10+
});
11+
12+
function showPortOpen() {
13+
console.log('port open. Data rate: ' + myPort.options.baudRate);
14+
}
15+
16+
function saveLatestData(data) {
17+
console.log('data received: ' + data);
18+
}
19+
20+
function showPortClose() {
21+
console.log('port closed.');
22+
}
23+
24+
function showError(error) {
25+
console.log('Serial port error: ' + error);
26+
}
27+
28+
myPort.on('open', showPortOpen);
29+
myPort.on('data', saveLatestData);
30+
myPort.on('close', showPortClose);
31+
myPort.on('error', showError);

WebServer.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// include the libraries
2+
var serialport = require('serialport');
3+
SerialPort = serialport.SerialPort;
4+
var express = require('express');
5+
var app = express();
6+
var server = require('http').createServer(app);
7+
var io = require('socket.io').listen(server);
8+
var ent = require('ent'); // Blocks HTML characters (security equivalent to htmlentities in PHP)
9+
var fs = require('fs'); //module for reading files
10+
11+
var latestData = 0; //stores latest serial data
12+
var spdataRec = false; //flag for Serial port data received
13+
14+
// Loading the page index.html
15+
app.use(express.static(__dirname + '/public'));
16+
app.get('/', function (req, res) {
17+
res.sendFile(__dirname + '/index.html');
18+
});
19+
20+
server.listen(3000);
21+
22+
io.on("connection", function(socket){
23+
console.log('A client connected to the server...');
24+
console.log('\n');
25+
26+
var interval = setInterval(function(){
27+
if(spdataRec == true){
28+
console.log(">>>Sending data to the client: ");
29+
console.log("Sent data: " + latestData);
30+
console.log('\n');
31+
socket.emit('recData', latestData);
32+
spdataRec = false;
33+
}
34+
}, 1000);
35+
36+
socket.on("disconnect", function(){
37+
console.log('A client disconnected from the server...');
38+
console.log('\n');
39+
clearInterval(interval);
40+
});
41+
42+
socket.on('wrData', function(data){
43+
console.log("<<<Receiving data from the client: ");
44+
console.log("Received data: " + data);
45+
console.log('\n');
46+
47+
//Write data to the serial port
48+
myPort.write(data, function(err, results) {
49+
console.log('err ' + err);
50+
console.log('results ' + results);
51+
console.log('\n');
52+
});
53+
});
54+
});
55+
56+
io.on('connection_failed', function(){
57+
console.log('Connection failed...');
58+
console.log('\n');
59+
});
60+
61+
//Configure the serial port
62+
var portName = process.argv[2]; //get port name from the command line
63+
var myPort = new SerialPort(portName, {
64+
baudRate: 9600,
65+
// look for return and newline at the end of each data packet:
66+
parser: serialport.parsers.readline("\n")
67+
});
68+
69+
//serial port event functions
70+
function showPortOpen() {
71+
console.log('Port open. Data rate: ' + myPort.options.baudRate);
72+
}
73+
function saveLatestData(data) {
74+
//console.log('Serial port data: ' + data);
75+
latestData = data;
76+
spdataRec = true;
77+
}
78+
function showPortClose() {
79+
console.log('Port closed.');
80+
}
81+
function showError(error) {
82+
console.log('Serial port error: ' + error);
83+
}
84+
85+
//serial port events
86+
myPort.on('open', showPortOpen);
87+
myPort.on('data', saveLatestData);
88+
myPort.on('close', showPortClose);
89+
myPort.on('error', showError);

index.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var app = require('express')();
2+
var http = require('http').Server(app);
3+
var io = require('socket.io')(http);
4+
5+
app.get('/', function(req, res){
6+
res.sendFile(__dirname + '/index.html');
7+
});
8+
9+
io.on('connection', function(socket){
10+
socket.on('chat message', function(msg){
11+
io.emit('chat message', msg);
12+
});
13+
});
14+
15+
http.listen(3000, function(){
16+
console.log('listening on *:3000');
17+
});

0 commit comments

Comments
 (0)