import bleComm from '../utils/bleComm.js';
import common from '../utils/common.js';
Page({
onUnload: function () {
bleComm.disConnect();
},
onHide: function () {
bleComm.disConnect();
},
onLoad: function () {
// wx.showLoading({
// title: '靠近连接',
// mask: true
// })
// bleComm.connectDevice().then(res => {
// wx.showToast({
// title: '蓝牙连接成功',
// icon: 'success',
// duration: 300
// })
// });
},
})
page{
background-color:#F0F0F0;
}
.wx_h{
padding-top: 10px;
margin-left: 4px;
font-size:19px;
text-align: center;
}
.wx_title{
padding-top: 10px;
padding-bottom: 10px;
margin-left: 4px;
font-size:16px;
}
.wx_sendtext{
margin-left: 4px;
margin-right: 4px;
margin-bottom: 15px;
font-size:14px;
height: 50px;
background-color:#FFFFFF;
color: #a050a1;
}
.weui-btn{
margin-left:2px;
margin-right:2px;
text-align: center;
text-decoration: none;
background-color: #8D1CB7;
color: #FFFFFF;
}
.other-button-hover{
background-color: #731496;
}
.wx_rectext{
margin-left: 4px;
margin-right: 4px;
font-size:14px;
background-color:#FFFFFF;
color: #a050a1;
}
page{
background-color:#F0F0F0;
}
.wx_h{
padding-top: 10px;
margin-left: 4px;
font-size:19px;
text-align: center;
}
.wx_title{
padding-top: 10px;
padding-bottom: 10px;
margin-left: 4px;
font-size:16px;
}
.wx_sendtext{
margin-left: 4px;
margin-right: 4px;
margin-bottom: 15px;
font-size:14px;
height: 50px;
background-color:#FFFFFF;
color: #a050a1;
}
.weui-btn{
margin-left:2px;
margin-right:2px;
text-align: center;
text-decoration: none;
background-color: #8D1CB7;
color: #FFFFFF;
}
.other-button-hover{
background-color: #731496;
}
.wx_rectext{
margin-left: 4px;
margin-right: 4px;
font-size:14px;
background-color:#FFFFFF;
color: #a050a1;
}
<view class="wx_h">
<text>串口调试工具</text>
</view>
<view class="section wx_title">
<text>发送数据</text>
</view>
<view class="btn-area">
<input class="wx_sendtext" placeholder="请输入..." bindinput="bindKeyInput" />
<button class="weui-btn" hover-class="other-button-hover" bindtap="sendData">数据发送</button>
</view>
<view class="section wx_title">
<text>接收数据</text>
</view>
<view class="btn-area">
<view class="wx_rectext">
<scroll-view scroll-y style="height: 350px;" bindscrolltoupper="upper" bindscrolltolower="lower" bindscroll="scroll" scroll-into-view="{{toView}}" scroll-top="{{scrollTop}}">
<text>{{motto}}</text>
</scroll-view>
</view>
</view>
<text>{{motto}}</text>
var extraLine = [];
data: {
motto: '',
msg: '',
},
onShow: function () {
wx.onBLECharacteristicValueChange((characteristic) => {
var news = common.arrayBufferToString(characteristic.value);
extraLine.push(news)
console.log(news);
this.setData({
motto: extraLine.join('\n'),
scrollTop: this.data.scrollTop + 30
})
})
},
bindKeyInput: function (result) {
this.setData({
msg: result.detail.value
});
},
sendData: function () {
bleComm.writeValue(this.data.msg);
}
import bleComm from '../utils/bleComm.js';
import common from '../utils/common.js';
var extraLine = [];
Page({
data: {
motto: '',
msg: '',
},
onUnload: function () {
bleComm.disConnect();
extraLine = [];
},
onHide: function () {
bleComm.disConnect();
extraLine = [];
},
onLoad: function () {
wx.showLoading({
title: '靠近连接',
mask: true
})
bleComm.connectDevice().then(res => {
wx.showToast({
title: '蓝牙连接成功',
icon: 'success',
duration: 300
})
});
},
onShow: function () {
wx.onBLECharacteristicValueChange((characteristic) => {
var news = common.arrayBufferToString(characteristic.value);
extraLine.push(news)
console.log(news);
this.setData({
motto: extraLine.join('\n'),
scrollTop: this.data.scrollTop + 30
})
})
},
bindKeyInput: function (result) {
this.setData({
msg: result.detail.value
});
},
sendData: function () {
bleComm.writeValue(this.data.msg);
}
})
#define my_Serial Serial //定义串口通讯为Serial
String msg = ""; //定义一个字符串
void setup(void) {
my_Serial.begin(57600);
}
void loop(void) {
/*---每收到一次信号,向通信另一端反馈一次---*/
if (my_Serial.available() > 0) //如果串口有数据输入
{
msg = my_Serial.readStringUntil('\n'); //获取换行符前所有的内容
my_Serial.println(msg); //向蓝牙通信的另一端发送数据
}
delay(1);
}
END
杨立斌
美科
解惑
创建时间:2017/12/22
30串口调试工具