Reputation: 37
I've check such kind of issue. but i don't find any. if you found it. just let me know. I just getting started write javascript through node.js, and serialport. cand someone explain me why this error appear?
/Applications/MAMP/htdocs/homeautomation/server.js:42
var sp = new serialPort(portName, {
^
TypeError: undefined is not a function
at Object.<anonymous> (/Applications/MAMP/htdocs/homeautomation/server.js:42:10)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
this is my starting code
/*
* dependencies
*/
var express = require('express'),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
serialPort = require('serialport').serialPort;
server.listen(3000);
console.log('listen on port 3000')
/*
* Express
*/
var app = express();
// serve static files from index
app.configure(function(){
app.use(express.static(__dirname + '/'));
});
// respon to web GET request on index.html
app.get('/', function (req, res){
res.sendfile(__dirname + '/index.html');
});
/*
* Serial Port Setup
*/
var portName = '/dev/tty.usb.serial-A501JUTF';
//var portName = '/dev/tty.usbmodem1421';
var readData = ''; //Array to hold the values read from the port
var sp = new serialPort(portName, {
baudRate : 9600,
dataBits : 8,
parity : 'none',
stopBits: 1,
flowControl : false,
});
any help will be appreciated.
Upvotes: 2
Views: 5408
Reputation: 3
Try this. It works perfectly.
var SerialPort = require('serialport');
var serialPort = SerialPort.serialPort;
var sp = new SerialPort("/dev/ttyACM0", {
});
Upvotes: 0
Reputation: 17048
Your code is correct, except that you used the serialPort
object of the require('serialport')
library, when it's in fact SerialPort
that you need to use, hence the undefined is not a function
error that you encountered.
var SerialPort = require("serialport")
console.log(SerialPort.serialPort); // undefined
console.log(SerialPort.SerialPort); // { [Function: SerialPort, ... }
See the documentation for a sample usage.
Upvotes: 2