Created
November 3, 2015 06:43
-
-
Save soundanalogous/a13339b945b1c13ee067 to your computer and use it in GitHub Desktop.
example of connecting to a serial device using BreakoutJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset=utf-8 /> | |
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> | |
<title>Serial</title> | |
<!-- The following (socket.io.js) is only required when using the node_server --> | |
<script src="../../socket.io/socket.io.js"></script> | |
<script src="../../dist/Breakout.min.js"></script> | |
<script src="../libs/jquery.min.js"></script> | |
<script type="text/javascript"> | |
$(document).ready(function() { | |
// Declare these variables so you don't have | |
// to type the full namespace | |
var IOBoard = BO.IOBoard; | |
var IOBoardEvent = BO.IOBoardEvent; | |
var Serial = BO.Serial; | |
var SerialEvent = BO.SerialEvent; | |
// Set to true to print debug messages to console | |
BO.enableDebugging = true; | |
var arduino = new IOBoard("localhost", 8887); | |
var serial; | |
var incomingString = ""; | |
// Listen for the IOBoard READY event which indicates the IOBoard | |
// is ready to send and receive data | |
arduino.addEventListener(IOBoardEvent.READY, onReady); | |
function onReady(event) { | |
// Remove the event listener because it is no longer needed | |
arduino.removeEventListener(IOBoardEvent.READY, onReady); | |
// HW_SERIAL1 is RX1 and TX1 on the board | |
// board, portID, baud | |
serial = new Serial(arduino, Serial.HW_SERIAL1, 9600); | |
serial.addEventListener(SerialEvent.DATA, onSerialData); | |
serial.startReading(); | |
// example writing to the serial port | |
// setInterval(function () { | |
// // write a message to a console attached to the test board once a second | |
// var msg = "hello!"; | |
// var msgArray = []; | |
// for (var i = 0, len = msg.length; i < len; i++) { | |
// msgArray.push(msg[i].charCodeAt(0)); | |
// } | |
// serial.write(msgArray); | |
// }, 1000); | |
} | |
function onSerialData(event) { | |
var data = event.data; | |
console.log("serial: " + data); | |
} | |
}); | |
</script> | |
</head> | |
<body> | |
<h2>Serial Test</h2> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment