62 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			62 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import buffer from '@ohos.buffer';
 | 
						|
import testNapi from '@ohos.hiserialsdk'
 | 
						|
 | 
						|
const TAG = '[TcpDemo.TcpClient]'
 | 
						|
import prompt from '@ohos.prompt'
 | 
						|
/**
 | 
						|
 * devPath
 | 
						|
 *j36--ttyS5
 | 
						|
 * j37--ttyS7
 | 
						|
 * j38--ttyS9
 | 
						|
 */
 | 
						|
 | 
						|
export default class SerialPortClient {
 | 
						|
  private devPath: string = ''
 | 
						|
  private len: any = ''
 | 
						|
  private fd: number = -1
 | 
						|
  private speed: number = 115200 //波特率
 | 
						|
  private databits: number = 8 //数据位
 | 
						|
  private flow_ctrl:number=0 //流控制
 | 
						|
  private stopbits:number=1 //停止位
 | 
						|
  private parity:number=0x4e //停止位
 | 
						|
 | 
						|
  constructor(devPath?:string) {
 | 
						|
    this.devPath = devPath || '/dev/ttyS5'
 | 
						|
 | 
						|
  }
 | 
						|
 | 
						|
  openSerial() {
 | 
						|
    this.fd = testNapi.SerialOpen(this.devPath);
 | 
						|
  }
 | 
						|
 | 
						|
  SerialSet(param?) {
 | 
						|
    this.speed = param?.speed || 115200
 | 
						|
    this.flow_ctrl=param?.flow_ctrl||0
 | 
						|
    this.databits = param?.databits||8
 | 
						|
    this.stopbits=param?.stopbits||1
 | 
						|
    this.parity=param?.parity || 0x4e
 | 
						|
    testNapi.SerialSet(this.fd, this.speed , this.flow_ctrl, this.databits, this.stopbits,this.parity);
 | 
						|
  }
 | 
						|
 | 
						|
  SerialSend(data?) {
 | 
						|
    const sendArr = data || [0x61, 0xAA, 0x0A, 0X15, 0X00] //档位查询
 | 
						|
    testNapi.SerialSend(this.fd, sendArr);
 | 
						|
  }
 | 
						|
 | 
						|
  SerialRecv(timeout?: number) {
 | 
						|
    let revTestInfo = testNapi.SerialRecv(this.fd, timeout);
 | 
						|
 | 
						|
    //let revTestInfo = testNapi.SerialRecv(this.fd, timeout, 6);
 | 
						|
    const message = revTestInfo?.recevedBuf?.toString()
 | 
						|
    console.log('revTestInfo.recevedBuf.toString()',revTestInfo.recevedBuf)
 | 
						|
    console.log('revTestInfo.recevedBuf.toString()',revTestInfo.recevedBuf.toString())
 | 
						|
    this.len = revTestInfo.recevedLen
 | 
						|
    return {
 | 
						|
      msg: message, recevedLen: this.len
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  SerialClose(callback) {
 | 
						|
    testNapi.SerialClose(this.fd);
 | 
						|
  }
 | 
						|
} |