Skip to content

Commit

Permalink
finishing up
Browse files Browse the repository at this point in the history
  • Loading branch information
amandaghassaei committed Feb 16, 2017
1 parent 992583d commit 033a78c
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 10 deletions.
9 changes: 8 additions & 1 deletion README.md
Expand Up @@ -19,4 +19,11 @@ node dependencies:
<a href="https://www.npmjs.com/package/serialport">serialport</a><br/>
<a href="https://www.npmjs.com/package/socket.io">socket-io</a>

Simple demo of standalone node server with web client <a href="https://github.com/amandaghassaei/NodeSerialPortBoilerplate">here</a>.
Simple demo of standalone node server with web client <a href="https://github.com/amandaghassaei/NodeSerialPortBoilerplate">here</a>.


**Attribution:**

Door sound effects from <a href="https://www.freesoundeffects.com/">freesoundeffects.com</a><br/>
Bike Horn sound effect from <a href="http://soundbible.com/1446-Bike-Horn.html">soundbible.com</a><br/>
Swoosh sound effect from <a href="http://soundbible.com/706-Swoosh-3.html">soundbible.com</a>
Binary file added SerialDemo.sb2
Binary file not shown.
1 change: 0 additions & 1 deletion arduino/SerialRepeat/SerialRepeat.ino
Expand Up @@ -11,7 +11,6 @@ void loop() {
Serial.print("received: ");
Serial.println(Serial.readString());
}
// delay(1);
}


Expand Down
18 changes: 10 additions & 8 deletions nodeServer.js
Expand Up @@ -17,17 +17,15 @@ var allPorts = [];
io.on('connection', function(socket){

refreshAvailablePorts(function(_allPorts, _portName, _baudRate){
currentPort = changePort(_portName, _baudRate);
changePort(_portName, _baudRate);
});

socket.on('initPort', function(data){
refreshAvailablePorts(function(){
var _portName = data.portName || portName;
var _baudRate = data.baudRate || baudRate;
if (!checkThatPortExists(_portName)) return;
currentPort = changePort(_portName, _baudRate);
portName = _portName;
baudRate = _baudRate;
changePort(_portName, _baudRate);
});
});

Expand Down Expand Up @@ -114,13 +112,14 @@ io.on('connection', function(socket){
return port;
}

function disconnectPort(){
function disconnectPort(callback){
if (currentPort && currentPort.isOpen()){
var oldBaud = baudRate;
var oldName = portName;
console.log("disconnecting port " + oldName + " at " + oldBaud);
currentPort.on('close', function(){
io.emit("portDisconnected", {baudRate:oldBaud, portName:oldName});
if (callback) callback();
});
currentPort.close(function(error){
if (error) {
Expand All @@ -129,7 +128,7 @@ io.on('connection', function(socket){
}

});
}
} else if (callback) callback();
}

function changePort(_portName, _baudRate){
Expand All @@ -139,8 +138,11 @@ io.on('connection', function(socket){
return null;
}
if (!_baudRate) _baudRate = baudRate;
disconnectPort();
return initPort(_portName, _baudRate);
disconnectPort(function(){
currentPort = initPort(_portName, _baudRate);
portName = _portName;
baudRate = _baudRate;
});
}

function onPortOpen(name, baud){
Expand Down
Binary file added sound effects/bike_horn.wav
Binary file not shown.
Binary file added sound effects/close_door_1.wav
Binary file not shown.
Binary file added sound effects/open_door_1.wav
Binary file not shown.
Binary file added sound effects/swoosh.wav
Binary file not shown.

0 comments on commit 033a78c

Please sign in to comment.