Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-28-2012, 07:40 PM   PM User | #1
dingledow
New to the CF scene

 
Join Date: Dec 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
dingledow is an unknown quantity at this point
Question Log value into console that is placed in HTML

Hi

I am using NodeJS to create a server that brings in serial data. The serial data is successfully showing in the HTML; however, I would like to create a 'var' that can hold the data. Please can someone help?

Here is my index.html page:

Code:
<!DOCTYPE html>
<html>
	<head>
	<script src="/socket.io/socket.io.js"></script>
	
	<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
	
	<script>
	
		
		
		// open a connection to the serial server:
		var socket = io.connect('http://localhost:8080');

		 // when you get a serialdata event, do this:
		socket.on('serialEvent', function (data) {
			// look for the textDisplay element in the HTML below:
			var element = document.getElementById('IDTag');
			// set the stuff inside the element's HTML tags to
			// whatever the 'value' property of the received data is:
			element.innerHTML = data.value;
			
			});

  </script>

	</head>
	<body>
	And the latest is:
	<div id="IDTag">The RFID Tag ID will show here.</div>	
	</body>
</html>
and here is my SerialServer.js doc:

Code:
/*
	serialServer.js
	a node.js app to read serial strings and send them to webSocket clients
	requires:
		* node.js (http://nodejs.org/)
		* express.js (http://expressjs.com/)
		* socket.io (http://socket.io/#how-to-use)
		* serialport.js (https://github.com/voodootikigod/node-serialport)
		
	based on the core examples for socket.io and serialport.js
		
	created 21 Aug 2012
	modified 14 Oct 2012
	by Tom Igoe
	
	Patches and improvements suggested by Steve Klise, Lia Martinez, and Will Jennings

*/


var serialport = require("serialport"),				// include the serialport library
	SerialPort  = serialport.SerialPort,			// make a local instance of serial
	app = require('express')(),						// start Express framework
  	server = require('http').createServer(app),		// start an HTTP server
  	io = require('socket.io').listen(server);		// filter the server using socket.io

var serialData = {};								// object to hold what goes out to the client

server.listen(8080);								// listen for incoming requests on the server

console.log("Listening for new clients on port 8080");

// open the serial port. Change the name to the name of your port, just like in Processing and Arduino:
var myPort = new SerialPort("/dev/tty.usbmodemfa131", { 
	// look for return and newline at the end of each data packet:
	parser: serialport.parsers.readline("\r\n") 
});
  
// respond to web GET requests with the index.html page:
app.get('/', function (request, response) {
  response.sendfile(__dirname + '/index.html');
});

// listen for new socket.io connections:
io.sockets.on('connection', function (socket) {
	// if there's a socket client, listen for new serial data:  
	myPort.on('data', function (data) {
		// set the value property of scores to the serial string:
		serialData.value = data;
		// for debugging, you should see this in Terminal:
		console.log(data);
		// send a serial event to the web client with the data:
		socket.emit('serialEvent', serialData);
	});
});
Thanks in advance!
dingledow is offline   Reply With Quote
Old 12-29-2012, 04:09 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
I don't know anything about nodeJS, but this is the bit that shows your data in the html:
Code:
element.innerHTML = data.value;
so I guess at that point if you had made a globally-acessible array like this:
Code:
var mydata=[];
then below the innerHTML line you can push the data onto the array:
Code:
mydata.push(data.value)
or initialize a string:
Code:
var mydata="";
and then add the data to the string:
Code:
mydata+=data.value;
or if you want the var to be overwritten each time data is received (to mirror what is shown in the html)
Code:
var mydata;
and then inside socket.on:
Code:
mydata=data.value;
depends what you want to do, really
xelawho is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:25 PM.


Advertisement
Log in to turn off these ads.