fei.wang
8 天以前 ae7b22322555448d95fd56f505bafa325c167a26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
 
import SerialPortFinder from 'android_serialport_api.SerialPortFinder';
import SerialHelper from 'com.shmily.serialport.SerialHelper';
import ComBean from 'com.shmily.serialport.bean.ComBean';
import ByteUtil from 'com.shmily.serialport.utils.ByteUtil';
import StandardCharsets from 'java.nio.charset.StandardCharsets';
import { IConnectOptions, IDataType } from '../interface';
import Toast from 'android.widget.Toast';
 
 
class MySerialHelper extends SerialHelper {
  private outerSerial : RS232Serial;
  constructor(opt : IConnectOptions, outerSerial : RS232Serial) {
    super(opt.port, opt.baudRate.toInt());
    this.setBaudRate(opt.baudRate.toInt());
    this.setDataBits(opt.dataBits.toInt());
    this.setStopBits(opt.stopBits.toInt());
    this.setParity(opt.parity.toInt());
    this.setFlowCon(opt.flowCon.toInt());
    this.outerSerial = outerSerial;
  }
  override onDataReceived(comBean : ComBean) : void {
    if (this.outerSerial.globalMsgCallback != null) {
      if (this.outerSerial.mDataType == 'ASCII') {
        this.outerSerial.globalMsgCallback?.invoke(new String(comBean.bRec, StandardCharsets.UTF_8));
      } else {
        this.outerSerial.globalMsgCallback?.invoke(ByteUtil.Bytes2Hex(comBean.bRec));
      }
    }
  }
}
 
export function checkHasIntegration() : boolean {
  try {
    Class.forName("com.shmily.serialport.SerialHelper");
    return true;
  } catch (e : Exception) {
    return false;
  }
}
 
export class RS232Serial {
  serialHelper : MySerialHelper | null = null;
  mDataType : IDataType = 'ASCII';
  globalMsgCallback : ((data : string) => void) | null = null;
  constructor() { }
  getDeviceList(): string[] {
    const serialPortFinder = new SerialPortFinder();
    try {
      const ports = serialPortFinder.getAllDevicesPath();
      if (ports.size > 0) {
        return Array.fromNative(ports);
      }
      return [];
    } catch (e) {
      Toast.makeText(UTSAndroid.getUniActivity(), e.message, Toast.LENGTH_LONG).show();
      return [];
    }
  }
  isOpen() : boolean {
    if (this.serialHelper == null) {
      return false;
    }
    return this.serialHelper!.isOpen();
  }
  open(opt : IConnectOptions) : boolean {
    try {
      this.serialHelper = new MySerialHelper(opt, this);
      this.serialHelper!.open();
      return true;
    } catch (e) {
      console.error(e)
      return false;
    }
  }
  sendHex(hexStr : string) : void {
    if (this.serialHelper == null) {
      console.error('Serial port is not open');
      return;
    }
    if (this.isOpen()) {
      const hexPure = hexStr.trim().replace(/\s/g, '');
      if (hexPure != '') {
        this.serialHelper!.sendHex(hexPure);
      } else {
        console.error('Hex is empty or null');
      }
    } else {
      console.error('Serial port is not open');
    }
  }
  sendText(text : string) : void {
    if (this.serialHelper == null) {
      console.error('Serial port is not open');
      return;
    }
    if (this.isOpen()) {
      if (text.trim() != '') {
        this.serialHelper!.sendTxt(text);
      } else {
        console.error('Text is empty or null');
      }
    } else {
      console.error('Serial port is not open');
    }
  }
  @UTSJS.keepAlive
  subscribe(dataType : IDataType, callback : (data : string) => void) : void {
    this.mDataType = dataType;
    this.globalMsgCallback = callback;
  }
  unsubscribe() : void {
    this.globalMsgCallback = null;
  }
  close(): boolean {
    if (!this.isOpen()) {
      console.error('Serial port is not open');
      return false;
    }
    this.serialHelper!.close();
    return true;
  }
}