fix: 串口获取档位
This commit is contained in:
		
							parent
							
								
									6ede5e69bb
								
							
						
					
					
						commit
						ee0b96125c
					
				| @ -3,6 +3,9 @@ import HiSerialSDK from '@ohos.hiserialsdk'; | |||||||
| import { SerialPortTag } from '../config'; | import { SerialPortTag } from '../config'; | ||||||
| 
 | 
 | ||||||
| // 打开串口工具 | // 打开串口工具 | ||||||
|  | /* | ||||||
|  |   * @param serialPort 串口名称 | ||||||
|  |  */ | ||||||
| export const OpenSerialPort = (serialPort: string) => { | export const OpenSerialPort = (serialPort: string) => { | ||||||
|   return new Promise<number>((resolve, reject) => { |   return new Promise<number>((resolve, reject) => { | ||||||
|     testNapi.SerialOpenAsync(serialPort, (value: number) => { |     testNapi.SerialOpenAsync(serialPort, (value: number) => { | ||||||
| @ -17,6 +20,11 @@ export const OpenSerialPort = (serialPort: string) => { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| //初始化串口数据 | //初始化串口数据 | ||||||
|  | /* | ||||||
|  |   * @param fd 串口文件描述符 | ||||||
|  |   * @param speed 波特率 | ||||||
|  |   * @returns Promise<boolean> 成功返回true,失败返回false | ||||||
|  |  */ | ||||||
| export const InitSerialPortData = | export const InitSerialPortData = | ||||||
|   async (fd: number, speed: number) => { |   async (fd: number, speed: number) => { | ||||||
|     return new Promise<boolean>((resolve, reject) => { |     return new Promise<boolean>((resolve, reject) => { | ||||||
| @ -35,6 +43,11 @@ export const InitSerialPortData = | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
| //发送数据 | //发送数据 | ||||||
|  | /* | ||||||
|  |   * @param fd 串口文件描述符 | ||||||
|  |   * @param data 要发送的数据数组 | ||||||
|  |   * @returns Promise<number> 成功返回发送的字节数,失败返回-1 | ||||||
|  |  */ | ||||||
| export const SendSerialPortData = (fd: number, data: number[]) => { | export const SendSerialPortData = (fd: number, data: number[]) => { | ||||||
|   console.log(SerialPortTag, "wzj-----发送数据") |   console.log(SerialPortTag, "wzj-----发送数据") | ||||||
|   return new Promise<number>((resolve, reject) => { |   return new Promise<number>((resolve, reject) => { | ||||||
| @ -49,6 +62,11 @@ export const SendSerialPortData = (fd: number, data: number[]) => { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // 接受数据 | // 接受数据 | ||||||
|  | /* | ||||||
|  |   * @param fd 串口文件描述符 | ||||||
|  |   * @param timeout 超时时间,单位毫秒 | ||||||
|  |   * @returns Promise<HiSerialSDK.receiveInfo> 成功返回接收到的数据,失败返回-1 | ||||||
|  |  */ | ||||||
| export const ReceiveSerialPortData = (fd: number, timeout: number,) => { | export const ReceiveSerialPortData = (fd: number, timeout: number,) => { | ||||||
|   console.log(SerialPortTag, "wzj-----接受数据") |   console.log(SerialPortTag, "wzj-----接受数据") | ||||||
|   return new Promise<HiSerialSDK.receiveInfo>((resolve) => { |   return new Promise<HiSerialSDK.receiveInfo>((resolve) => { | ||||||
|  | |||||||
							
								
								
									
										72
									
								
								entry/src/main/ets/utils/business/SerialPortService.ets
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								entry/src/main/ets/utils/business/SerialPortService.ets
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,72 @@ | |||||||
|  | // 串口业务逻辑 | ||||||
|  | 
 | ||||||
|  | import { | ||||||
|  |   CancelReceiveSerialPortData, | ||||||
|  |   InitSerialPortData, | ||||||
|  |   OpenSerialPort, | ||||||
|  |   ReceiveSerialPortDataBySelf | ||||||
|  | } from '../SerialPort'; | ||||||
|  | import Prompt from '@system.prompt'; | ||||||
|  | import { SerialPortTag } from '../../config'; | ||||||
|  | 
 | ||||||
|  | class serialPortService { | ||||||
|  |   private fd: number = -1 | ||||||
|  |   private serialPort: string = "/dev/ttyS3" | ||||||
|  |   // 波特率 | ||||||
|  |   private baudRate: string = "115200" | ||||||
|  |   private events: Array<Function> = [] | ||||||
|  |   // 尝试次数 | ||||||
|  |   private tryCount: number = 0 | ||||||
|  | 
 | ||||||
|  |   async init() { | ||||||
|  |     const res = await OpenSerialPort(this.serialPort) | ||||||
|  |     if (res === -1) { | ||||||
|  |       Prompt.showToast({ | ||||||
|  |         message: "串口打开失败" | ||||||
|  |       }) | ||||||
|  |       //   失败了重试,最大次数为5次 | ||||||
|  |       this.tryCount++; | ||||||
|  |       if (this.tryCount < 5) { | ||||||
|  |         console.log(SerialPortTag, "串口打开失败,尝试第", this.tryCount, "次重试") | ||||||
|  |         setTimeout(() => { | ||||||
|  |           this.init(); | ||||||
|  |         }, 1000); | ||||||
|  |         return; | ||||||
|  |       } else { | ||||||
|  |         console.error(SerialPortTag, "串口打开失败,重试次数已达上限") | ||||||
|  |         return; | ||||||
|  |       } | ||||||
|  |     } else { | ||||||
|  |       this.fd = res; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   async canInit() { | ||||||
|  |     if (this.fd !== -1) { | ||||||
|  |       await InitSerialPortData(this.fd, Number(this.baudRate)) | ||||||
|  |       ReceiveSerialPortDataBySelf(this.fd, (res1: number, res2: number, res3: number[]) => { | ||||||
|  |         this.events.forEach((callback) => { | ||||||
|  |           callback(res3); | ||||||
|  |         }); | ||||||
|  |       }) | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   onMsg(callback: Function) { | ||||||
|  |     if (this.events.indexOf(callback) === -1) { | ||||||
|  |       this.events.push(callback); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   async closed() { | ||||||
|  |     const res = await CancelReceiveSerialPortData(this.fd); | ||||||
|  |     this.events = []; | ||||||
|  |     if (res) { | ||||||
|  |       console.log(SerialPortTag, "串口关闭成功") | ||||||
|  |     } else { | ||||||
|  |       console.error(SerialPortTag, "串口关闭失败") | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export const SerialPortService = new serialPortService(); | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user