【发布时间】:2017-11-04 13:01:14
【问题描述】:
有了这个函数,我得到了空返回
帮我写出正确的函数
$scope.result= sendread("AT+GMR") //get value datas.toString() or Not OK
console.log($scope.result) // undefined
function sendread(input){
port.write(input+'\r\n')
port.drain(() => {
port.once('data',(datas) =>{
if (datas.toString().match(/"OK"/g)) {
return 'Not OK'
}
return datas.toString() //652a09gg.Q2406B 1489876 060706 17:19 OK
})
})
}
谢谢
编辑
完整代码作为评论中的请求
<script src="http://localhost/angular.min.js.js"></script>
<html ng-app="myApp" ng-controller="srpcrtl">
result : {{result}}
<br>
com {{com}}
<script>
'use strict';
var app = angular.module('myApp', []);
app.controller('srpcrtl', function($scope) {
var portName = "COM1"
const serialPort = require('serialport');
const port = new serialPort(portName, {
baudRate: 115200,
dataBits: 8,
parity: 'none',
//parser: serialPort.parsers.readline('\r\n')
})
$scope.com = port.path
console.log('port is now open');
port.on('open', function() {
$scope.result = sendread("AT+GMR") //get value datas.toString() or Not OK
console.log($scope.result)
function sendread(input) {
port.write(input + '\r\n')
port.drain(() => {
port.once('data', (datas) => {
if (datas.toString().match(/"OK"/g)) {
return 'Not OK'
}
console.log(datas.toString())
return datas.toString()
})
})
}
})
})
</script>
</html>
更多细节
$scope.result on html print empty ,$scope.result on console print undefined ,console.log(datas.toString()) print correct result "652a09gg.Q2406B 1489876 060706 17:19"
【问题讨论】:
-
检查您的控制台是否有错误
-
@UsmanRana 控制台没有错误
-
这是你的完整代码吗?
-
刚刚用完整代码编辑过
标签: javascript at-command node-serialport angular1.6