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 ) ;
0 commit comments