点击按钮调用功能

时间:2018-09-23 21:51:11

标签: javascript button event-handling serial-port

单击按钮时,我试图写入串行端口“ F”。我正在尝试构建一个Web界面,因此当单击此按钮时,它将运行一个功能。

我可以在代码中调用该函数,它将起作用,但是我似乎无法使其在代码之外起作用

index.html文件位于子文件夹“ public”中

// serial port initialization:
var serialport = require("serialport");
    serialPort = serialport;
    portName = "/dev/ttyACM0";
    portConfig = {
    baudRate: 9600,
    };

var foward = new Buffer(1);
    foward.writeUInt8(0x46,0); // 0x46 is F in ASCII

var backward = new Buffer(1);
    backward.writeUInt8(0x42,0); // 0x42 is B in ASCII

var left = new Buffer(1);
    left.writeUInt8(0x4C,0); // 0x4C is L in ASCII

var right = new Buffer(1);
    right.writeUInt8(0x52,0); // 0x52 is F in ASCII

var stop = new Buffer(1);
    stop.writeUInt8(0x53,0); // 0x53 is F in ASCII

//Opening Port
var port = new serialPort(portName, portConfig);
port.on('open', openPort); // calling openPort function also

    function openPort() {
        port.write('Success', function(err) {
            if (err) {
            return console.log('Error on write: ', err.message);
            }
            console.log('Open is opened');
        })

    //Foward command
    function sendForward() {
         // convert the value to an ASCII string before sending it:
         port.write(foward, function(err) {
            if (err) {
            return console.log('Error on write: ', err.message);
            }
            console.log('foward');
        });
    }

html代码

<html>
<body>

<h2>Button</h2>
<button type="button" onclick="sendForward()">Click Me!</button>

</body>
</html>

0 个答案:

没有答案
相关问题