||
一、str2str播发TCP server,然后Node.js接收TCP client
str2str -in serial://ttyACM1:115200:8:n:1 -out tcpsvr://:8002
const aliyunIot = require('aliyun-iot-device-sdk'); const deviceConfig = require('./device_id_password.json'); const device = aliyunIot.device(deviceConfig); var net = require('net'); // 指定连接的tcp server ip,端口 var options = { host : '192.168.31.109', port : 8002 } var tcp_client = net.Socket(); // 连接 tcp server tcp_client.connect(options,function(){ console.log('connected to Server'); tcp_client.write('I am tcp_client of node!'); }) device.on('connect', () => { console.log('Connect successfully!'); console.log('Post properties every 5 seconds...'); var gga; // 接收数据 tcp_client.on('data',function(data){ gga = data.toString(); }) setInterval(() => { const params = { GGA: gga }; console.log(`Post properties: ${JSON.stringify(params)}`); device.postProps(params); }, 5000); device.serve('property/set', (data) => { console.log('Received a message: ', JSON.stringify(data)); }); }); device.on('error', err => { console.error(err); });
二、直接串口接收
const aliyunIot = require('aliyun-iot-device-sdk'); const deviceConfig = require('./device_id_password.json'); const device = aliyunIot.device(deviceConfig); const SerialPort = require('serialport') const port = new SerialPort("/dev/ttyACM0", { baudRate:115200, dataBits:8, parity:'none', stopBits:1, flowControl:false, autoOpen: false // this is the openImmediately flag [default is true] }); device.on('connect', () => { console.log('Connect successfully!'); console.log('Post properties every 5 seconds...'); var gga; port.open(function (error) { if ( error ) { console.log('failed to open: '+error); } else { console.log('open'); port.on('data', function(data) { gga = data.toString(); }); } }); setInterval(() => { const params = { GGA: gga }) }; console.log(`Post properties: ${JSON.stringify(params)}`); device.postProps(params); }, 5000); device.serve('property/set', (data) => { console.log('Received a message: ', JSON.stringify(data)); }); }); device.on('error', err => { console.error(err); });
GPS Quality indicator:
0: Fix not valid
1: GPS fix
2: Differential GPS fix, OmniSTAR VBS
4: Real-Time Kinematic, fixed integers
5: Real-Time Kinematic, float integers, OmniSTAR XP/HP or Location RTK
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2025-1-9 23:58
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社