Beaglebone黑色串口JAVASCRIPT

时间:2014-02-04 15:43:18

标签: javascript

我无法输出到LCD显示屏。

var usblcd = require('./usblcdmo');
var express = require('express');
var app = express();

var fs = require('fs');
var indexhtml = fs.readFileSync('./index.html');

app.get('^/$', function (req, res) {
   res.end(indexhtml, {'Content-Type': 'text/html'});
});

app.get(/^\/clearscreen$/, function(req, res) {
    console.log('clearscreen');
    usblcd.clearscreen();
    res.end(indexhtml, {'Content-Type': 'text/html'});
});

app.get(/^\/backlight\/(\d)$/, function(req, res) {
    var option = req.params[0];
    console.log('backlight ' + option);
    usblcd.backlight(option);
    res.end(indexhtml, {'Content-Type': 'text/html'});
});

app.get(/^\/blockcursor\/(\d+)$/, function(req, res) {
    var option = req.params[0];
    console.log('blockcursor ' + option);
    usblcd.backlight(option);
    res.end(indexhtml, {'Content-Type': 'text/html'});
});

app.get(/^\/contrast\/(\d+)$/, function(req, res) {
    var option = req.params[0];
    console.log('contrast ' + option);
    usblcd.contrast(option);
    res.end(indexhtml, {'Content-Type': 'text/html'});
});

app.get(/^\/brightness\/(\d+)$/, function(req, res) {
    var option = req.params[0];
    console.log('brightness ' + option);
    usblcd.brightness(option);
    res.end(indexhtml, {'Content-Type': 'text/html'});
});

app.get(/^\/backlightRGB\/(\d+)\/(\d+)\/(\d+)$/, function(req, res) {
    var red = req.params[0];
    var green = req.params[1];
    var blue = req.params[2];
    console.log('backlightRGB ' + red + ' ' + green + ' ' + blue);
    usblcd.backlightRGB(red, green, blue);
    res.end(indexhtml, {'Content-Type': 'text/html'});
});

app.get(/^\/show\/(.*)$/, function(req, res) {
    var option = req.params[0];
    console.log('show ' + option);
    usblcd.show(option);
    res.end(indexhtml, {'Content-Type': 'text/html'});
});

app.get('^/quote1$', function(req, res) {
    usblcd.clearscreen();
    usblcd.show('node.js, use the force');
    res.end(indexhtml, {'Content-Type': 'text/html'});
    });

app.get('^/quote2$', function(req, res) {
    usblcd.clearscreen();
    usblcd.show('node.js,  phone home');
    res.end(indexhtml, {'Content-Type': 'text/html'});
    });

app.get('^/quote3$', function(req, res) {
    usblcd.clearscreen();
    usblcd.show('node.js, make my day');
    res.end(indexhtml, {'Content-Type': 'text/html'});
    });

app.get('^/quote4$', function(req, res) {
    usblcd.clearscreen();
    usblcd.show('Houston, we havea node.js');
    res.end(indexhtml, {'Content-Type': 'text/html'});
    });

app.get('^/quote5$', function(req, res) {
    usblcd.clearscreen();
    usblcd.show('node.js, you are our only hope');
    res.end(indexhtml, {'Content-Type': 'text/html'});
    });

app.listen(4000);
console.log('Listening on port 4000');

usblcd.clearscreen();
usblcd.backlight(true);
usblcd.brightness(255);
usblcd.contrast(200);
usblcd.autoscroll(true);
usblcd.backlightRGB(0,255,0);
var now = new Date();
usblcd.show(now.toDateString() + ' ' + now.toTimeString().slice(0, 8));

这个脚本:

'use strict';

var SerialPort = require("serialport").SerialPort;
var serialPort = new SerialPort("/dev/ttyACM0", { baudrate: 57600 });
var Open = false;
var SaveBuffer = new Buffer(1024);
var SaveLen = 0;

function sp_write(data) {
    if (Open) {
        serialPort.write(data, function(err, results) {
            console.log('err ' + err + ' results ' + results);
        });
    }
    else {
        if (Buffer.isBuffer(data)) {
            data.copy(SaveBuffer, SaveLen);
            SaveLen += data.length;
        }
        else {
            new Buffer(data).copy(SaveBuffer, SaveLen);
            SaveLen += data.length;
        }
        //console.log('SaveLen ' + SaveLen);
    }
}

function show(data) {
    console.log('show ' + data);
    sp_write(data);
}

serialPort.on("open", function() {
    Open = true;
    serialPort.on("data", function (data) {
        console.log('serialPort data');
    });
    serialPort.on("close", function (data) {
        console.log('serialPort close');
    });
    //console.log('SaveBuffer ' + SaveBuffer.slice(0, SaveLen));
    console.log('open, sending buffered data ' + SaveLen);
    serialPort.write(SaveBuffer.slice(0, SaveLen), function(err, results) {
        console.log('err ' + err + ' results ' + results);
    });
    SaveLen = 0;
});

function backlight(data) {
    console.log('backlight ' + data);
    if (data === true || data === 1) {
        sp_write([0xFE, 0x42, 0x00]);
    }
    else {
        sp_write([0xFE, 0x46]);
    }
}

function brightness(bright) {
    console.log('brightness ' + bright);
    sp_write([0xFE, 0x99, bright]);
}

function contrast(contr) {
    console.log('contrast ' + contr);
    sp_write([0xFE, 0x50, contr]);
}

function clearscreen() {
    console.log('clearscreen');
    sp_write([0xFE, 0x58]);
}

function gotoxy(row,col) {
    console.log('gotoxy ' + row + ' ' + col);
    sp_write([0xFE, 0x47, row, col]);
}

function binarychoice(featurename, onCode, offCode, data) {
    console.log(featurename + ' ' + data);
    if (data === true || data === 1) {
        sp_write([0xFE, onCode]);
    }
    else {
        sp_write([0xFE, offCode]);
    }
}

function autoscroll(data) {
    binarychoice('autoscroll', 0x51, 0x52, data);
}

function underlinecursor(data) {
    binarychoice('underlinecursor', 0x4A, 0x4B, data);
}

function blockcursor(data) {
    binarychoice('blockcursor', 0x53, 0x54, data);
}

function backlightRGB(red, green, blue) {
    console.log('backlightRGB ' + red + ' ' + green + ' ' + blue);
    sp_write([0xFE, 0xD0, red, green, blue]);
}

exports.show = show;
exports.backlight = backlight;
exports.brightness = brightness;
exports.contrast = contrast;
exports.clearscreen = clearscreen;
exports.gotoxy = gotoxy;
exports.autoscroll = autoscroll;
exports.underlinecursor = underlinecursor;
exports.blockcursor = blockcursor;
exports.backlightRGB = backlightRGB;

错误如下

 Error: Cannot find module 'serialport'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object. (/var/lib/stickshift/52e570dee0b8cdcdb7000005/app-root/data/752908/testlcd/usblcdmo.js:3:18)
    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 Module.require (module.js:364:17)

我假设我需要一个串口代码,因为它被调用了。我不确定该怎么做。 beaglebone black是一个微处理器,我试图输出到LCD显示器。

1 个答案:

答案 0 :(得分:1)

似乎缺少模块序列端口。尝试安装它运行

npm install serialport

您还可以在app目录中运行npm install,它会自动将所有依赖项安装在名为node_modules

的文件夹中