1import * as uart from 'uart' 2/* Uart's options are configured in app.json. 3{ 4 "version": "0.0.1", 5 "io": { 6 "serial": { 7 "type": "UART", 8 "port": 2, 9 "dataWidth":8, 10 "baudRate":115200, 11 "stopBits":0, 12 "flowControl":"disable", 13 "parity":"none" 14 } 15 }, 16 "debugLevel": "DEBUG" 17} 18*/ 19 20var msgbuf = 'this is amp uart test' 21 22function ab2str(buf) { 23 return String.fromCharCode.apply(null, new Uint8Array(buf)); 24} 25 26console.log('uart open') 27var serial = uart.open({ 28 id: 'serial', 29 //mode: 'poll', //just for read mode 30 success: function() { 31 console.log('open uart success') 32 }, 33 fail: function() { 34 console.log('open uart failed') 35 } 36}); 37console.log('uart write') 38 39serial.write(msgbuf); 40 41sleepMs(1000); 42 43console.log('uart read') 44var rCnt = 0; 45var rtrn = 0; 46 47var value = '' 48 49//just for read mode 50// while(1) 51// { 52// rtrn = serial.read() 53// if(0 != rtrn) 54// { 55// value += ab2str(rtrn); 56// rCnt++; 57// } 58 59// if(rCnt > 10) 60// { 61// break; 62// } 63 64// } 65 66// console.log('sensor value is ' + value) 67 68serial.on('data', function(data, len) { 69console.log('uart receive data len is : ' + len + ' data is: ' + ab2str(data)); 70}) 71 72//serial.close(); 73