简体   繁体   English

Beaglebone Black串行端口JAVASCRIPT

[英]Beaglebone Black Serial port JAVASCRIPT

I am having trouble outputting to an LCD display. 我无法输出到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));

And this script: 这个脚本:

'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;

The error is as follows 错误如下

 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)

I am assuming I need a serial port code because it is called upon. 我假设我需要一个串行端口代码,因为它被调用了。 I am unsure how to do this. 我不确定该怎么做。 The beaglebone black is a microprocessor that I am attempting to make output to an LCD display. beaglebone黑色是我正在尝试输出到LCD显示器的微处理器。

It seems the module serialport is missing. 似乎缺少模块串行端口。 Try to install it running 尝试安装它运行

npm install serialport

You can also run npm install in your app directory, it will automatically install all dependencies in a folder named node_modules 您还可以在应用程序目录中运行npm install ,它将自动将所有依赖项安装在名为node_modules的文件夹中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM