有沒有專業(yè)收費做網(wǎng)站優(yōu)化的數(shù)字化營銷
一、需求描述:
??????? rk3568開發(fā)板運行OpenHarmony4.0,通過開發(fā)板上的uart串口與電腦進行通訊,相互收發(fā)字符串。
二、案例展示
????????1、開發(fā)環(huán)境:
????????(1)rk3568開發(fā)板
????????(2)系統(tǒng):OpenHarmony
??????? (3)電腦:Windows11筆記本,串口調試助手
????????(4)Deveco Studio:evEco Studio 4.0 Release (4.0.0.600)
2、下載并運行開發(fā)案例
????????下載開發(fā)案例,使用Deveco Studio打開運行編譯、下載應用到rk3568開發(fā)板。開發(fā)板運行demo界面如下圖所示。(.hap應用文件,見附件)
????????(1)點擊波特率選擇按鈕,選擇相應的波特率;點擊地址輸入框,輸入使用的串口的設備地址,這里以uart5(設備地址/dev/ttyS5)為例,選擇波特率9600。???
????????(2)將開發(fā)板通過串口轉USB的轉接線,將開發(fā)板與筆記本連接起來,電腦打開一個串口調試助手,如下圖所示。
????????(3)開發(fā)板點擊“開啟按鈕”打開串口,然后點擊“發(fā)送”按鈕,想電腦通過串口發(fā)送輸入框的字符串,電腦運行的串口調試助手接信息,并回顯接收到的字符串;同理電腦通過串口調試助手想開發(fā)板發(fā)送字符串,開發(fā)板接收信息,并在回顯框中回顯接收到的字符串,如下圖所示。
????????(4)測試效果
OpenHarmony串口通訊展示
????????注意:運行demo,應確保開發(fā)板的串口節(jié)點已被引出可用,且讀寫權限已被允許
三、應用開發(fā)流程
?????? 該應用的開發(fā),使用NAPI方式來編寫使用串口的NAPI函數(shù),通過這些函數(shù),來對串口進行設置,打開,發(fā)送和接收數(shù)據(jù)(NAPI使用,詳見NAPI篇)。在應用界面編寫中,引用NAPI函數(shù)進行邏輯構建,完成應用開發(fā)。
?????????1、應用界面編寫(Index.ets)
import BQNapi from 'libentry.so';//引入NAPI
import promptAction from '@ohos.promptAction';const TAG = "uartToComputer"@Entry
@Component
struct Index {@State isStart: boolean = false;private dateTime: Date = new Date();private scroller: Scroller = new Scroller()@State receiveMessage: string = '';@State sendMessage: string = 'https://www.bearkey.net/';@State currentUart: string = '';private UartPort: string = '/dev/ttyS5'private UartBand: string[] = ['9600', '19200', '38400', '57600', '115200']private UartBand_N: number[] = [9600, 19200, 38400, 57600, 115200]@State currentUartBandIndex: number = 0@State bandRate: number = 0;private fd: number = -1;private setIntervalID: number = -1;aboutToAppear() {// this.fd = BQNapi.open_port(this.currentUart, 115200);// console.log(TAG, `打開串口${this.currentUart},ret=${this.fd}`)this.setIntervalID = setInterval(() => {//判斷是否有串口開啟if (this.fd > 0) {//獲取開啟狀態(tài)this.isStart = true//獲取波特率this.bandRate = BQNapi.getBandRate(this.fd)let temp: string = BQNapi.series_receive_data(this.fd);if (temp === "-1") {console.log(TAG, '未接收到數(shù)據(jù)或接收失敗');} else {this.dateTime = new Date();let year: number = this.dateTime.getFullYear(); //當前年let month = this.dateTime.getMonth() + 1;let date = this.dateTime.getDate();let hours = this.dateTime.getHours();let minutes = this.dateTime.getMinutes();let seconds = this.dateTime.getSeconds();let curryDateTime: string = year + '-' + month + '-' + date + ' ' + hours + ':' + minutes + ':' + seconds;temp = curryDateTime + '\n' + temp;this.receiveMessage = temp + this.receiveMessagethis.scroller.scrollTo({ xOffset: 0, yOffset: 0 })}} else {this.isStart = falsethis.bandRate = 0;this.currentUart = '-'}}, 300)}aboutToDisappear() {console.log(TAG, `退出應用`)clearInterval(this.setIntervalID);if (this.fd > 0) {let e: number = BQNapi.close_port(this.fd);console.log(TAG, `關閉串口${this.currentUart},ret=${e}`)if (e == 0) {console.log(TAG, `關閉成功`)} else {console.log(TAG, `關閉失敗`)}}}build() {Column() {Row() {Text('回顯框').fontSize(25).size({ width: '50%', height: 30 })Button('清空窗口').fontSize(30).size({ width: 160, height: 40 }).onClick(() => {this.receiveMessage = '';});}.justifyContent(FlexAlign.SpaceAround).margin({ top: 10, }).size({ width: '100%', height: 50 })Scroll(this.scroller) {Column() {Text(this.receiveMessage).fontSize(20).size({ width: '100%' }).constraintSize({ minHeight: 30 }).margin({ top: 10, bottom: 10 }).padding(10).textAlign(TextAlign.Start)}.size({ width: '100%' }).constraintSize({ minHeight: 300 }).justifyContent(FlexAlign.Start)}.size({ width: '100%', height: 150 }).border({ width: 1, style: BorderStyle.Solid, radius: 10 })Row() {Text('輸入框').fontSize(25).size({ width: '100%', height: 40 })}.justifyContent(FlexAlign.SpaceAround).margin({ top: 10, }).size({ width: '100%', height: 50 })TextInput({ placeholder: "請輸入需要發(fā)送的內容...", text: this.sendMessage }).fontSize(25).size({ width: '100%', height: 50 }).border({ width: 1, style: BorderStyle.Solid, radius: 10 }).margin({ bottom: 10 }).onChange((value) => {this.sendMessage = value;})Row() {Button('發(fā)送').fontSize(30).enabled(this.isStart).size({ width: 120, height: 40 }).onClick(() => {let a: number = BQNapi.series_send_data(this.sendMessage, this.fd)if (a === 0) {console.log(TAG, "發(fā)送成功!");} else {console.log(TAG, "發(fā)送失敗!");}})Button('清空').fontSize(30).enabled(this.isStart).size({ width: 120, height: 40 }).onClick(() => {this.sendMessage = '';})}.justifyContent(FlexAlign.SpaceAround).margin({ top: 10, }).size({ width: '100%', height: 50 })Row() {Text('當前串口信息').fontSize(25).size({ width: '100%', height: 30 })}.justifyContent(FlexAlign.SpaceAround).margin({ top: 10, }).size({ width: '100%', height: 50 })Column() {Row() {Text('波特率:').fontSize(25).size({ width: '40%', height: 30 }).textAlign(TextAlign.Center)Text(this.bandRate.toString()).fontSize(28).size({ width: '60%', height: 30 }).textAlign(TextAlign.Center)}.justifyContent(FlexAlign.SpaceAround).size({ width: '100%', height: 40 })Row() {Text('當前串口:').fontSize(25).size({ width: '40%', height: 30 }).textAlign(TextAlign.Center)Text(this.currentUart).fontSize(28).size({ width: '60%', height: 30 }).textAlign(TextAlign.Center)}.justifyContent(FlexAlign.SpaceAround).size({ width: '100%', height: 40 })}.size({ width: '100%', height: 100 }).justifyContent(FlexAlign.SpaceAround).border({ width: 1, style: BorderStyle.Solid, radius: 10 })Row() {Button(this.UartBand[this.currentUartBandIndex]).fontSize(25).size({ width: '30%', height: 45 }).onClick(() => {this.showPickerDialogForUartBand()})TextInput({ placeholder: "請輸入串口地址", text: this.UartPort }).fontSize(25).border({ width: 1, style: BorderStyle.Solid, radius: 10 }).size({ width: '60%', height: 45 }).onChange((value) => {this.UartPort = value;})}.justifyContent(FlexAlign.SpaceAround).margin({ top: 10, }).size({ width: '100%', height: 50 })Row() {Button("開啟").fontSize(35).size({ width: '30%', height: 45 }).backgroundColor(this.isStart ? Color.Green : Color.Blue).onClick(() => {if (this.isStart) {promptAction.showToast({message: "請先停止當前串口",duration: 500})} else {this.fd = BQNapi.open_port(this.UartPort, this.UartBand_N[this.currentUartBandIndex]);if (this.fd > 0) {console.log(TAG, `打開串口${this.currentUart},ret=${this.fd}`)this.currentUart = this.UartPort;promptAction.showToast({message: "開啟成功",duration: 500})} else {promptAction.showToast({message: "開啟失敗",duration: 500})}}})Button('停止').fontSize(35).size({ width: '30%', height: 45 }).backgroundColor(this.isStart ? Color.Red : Color.Blue).onClick(() => {if (this.isStart) {let e: number = BQNapi.close_port(this.fd);console.log(TAG, `關閉串口${this.currentUart},ret=${e}`)if (e == 0) {promptAction.showToast({message: "關閉成功",duration: 500})this.fd = -1;this.currentUart = '...'console.log(TAG, `關閉成功`)} else {console.log(TAG, `關閉失敗`)promptAction.showToast({message: "關閉失敗",duration: 500})}} else {promptAction.showToast({message: "未啟用串口",duration: 500})}})}.justifyContent(FlexAlign.SpaceAround).margin({ top: 20, }).size({ width: '100%', height: 50 })}.size({ width: '100%', height: '100%' }).padding(10).justifyContent(FlexAlign.Start).alignItems(HorizontalAlign.Center)}showPickerDialogForUartBand() {TextPickerDialog.show({range: this.UartBand,selected: this.currentUartBandIndex,disappearTextStyle: { color: Color.Red, font: { size: 15, weight: FontWeight.Lighter } },textStyle: { color: Color.Black, font: { size: 20, weight: FontWeight.Normal } },selectedTextStyle: { color: Color.Blue, font: { size: 30, weight: FontWeight.Bolder } },onAccept: (value: TextPickerResult) => {let temp = value.value as stringthis.currentUartBandIndex = value.index as number},onCancel: () => {console.info(TAG, "TextPickerDialog:onCancel()")},onChange: (value: TextPickerResult) => {console.info(TAG, "TextPickerDialog:onChange()" + JSON.stringify(value))}})}
}
????????2、NAPI函數(shù)引出聲明(index.d.ts)
export const open_port: (port_address: string, band_rate: number) => number; //打開串口,打開成功返回描述符(int),失敗-1export const close_port: (serial_port: number) => number; //關閉串口,關閉成功返回0,失敗則返回-1.export const series_send_data: (tx_data: string, serial_port: number) => number; //發(fā)送數(shù)據(jù)export const series_receive_data: (serial_port: number) => string; //接收數(shù)據(jù),成功返回接收字符串,失敗返回-1export const getBandRate: (serial_port: number) => number; //查詢波特率,成功返回波特率,失敗返回-1
????????3、完整應用工程源碼,私信
聲明:非本人允許,嚴禁轉載,演示開發(fā)板為廈門貝啟科技的BQ3568HM產(chǎn)品
(1)首頁-貝啟科技官方企業(yè)店-淘寶網(wǎng)
(2)廈門貝啟科技有限公司-Bearkey-官網(wǎng)