串口设备(SerialPort)

# 获取串口列表 getPorts

● 接口说明
获取设备的串口列表
● Typescript 类型参考

type ResBody = {
    resultCode: number, 
    resultMessage: string  
    data: string[]
}

zwexplorer.SerialPort.getPorts(
    data?: {}, 
    callback?: (res: ResBody)=>void
): Promise<ResBody>
● 返回数据
参数 类型 必然存在 说明
res.resultCode Number 状态码,0 为正常,其它为异常 [状态码参考]
res.data Array 接口返回的详细数据, 例: ["/dev/ttyS0","/dev/ttyS1"] 或 ["COM1","COM2"]
res.resultMessage String 接口消息
● 接口示例
zwexplorer.SerialPort.getPorts({},(res)=>{
  // 返回数据
})

# 打开串口 openPort

● 接口说明
打开串口
● Typescript 类型参考
type ReqBody = {
    port: string,
    baud: number}

type ResBody = {
    resultCode: number, 
    resultMessage: string
}

zwexplorer.SerialPort.openPort(
    data?: ReqBody, 
    callback?: (res: ResBody)=>void
): Promise<ResBody>
● 参数说明
参数 类型 必填 说明
data.port Number 串口号
data.baud Number 波特率(建议值9600)
● 返回数据
参数 类型 必然存在 说明
res.resultCode Number 状态码,0 为正常,其它为异常 [状态码参考]
res.data Object 接口返回的详细数据
res.resultMessage String 接口消息
● 接口示例
zwexplorer.SerialPort.openPort({},(res)=>{
  // 返回数据
})

# 关闭串口 closePort

● 接口说明
关闭串口
● Typescript 类型参考
type ReqBody = {
    port: string,
    baud: number}

type ResBody = {
    resultCode: number, 
    resultMessage: string
}

zwexplorer.SerialPort.closePort(
    data?: ReqBody, 
    callback?: (res: ResBody)=>void
): Promise<ResBody>
● 参数说明
参数 类型 必填 说明
data.port Number 串口号
data.baud Number 波特率(建议值9600)
● 返回数据
参数 类型 必然存在 说明
res.resultCode Number 状态码,0 为正常,其它为异常 [状态码参考]
res.data Object 接口返回的详细数据
res.resultMessage String 接口消息
● 接口示例
zwexplorer.SerialPort.closePort({},(res)=>{
  // 返回数据
})

# 向串口发送数据 sendData

● 接口说明
向指定的串口发送数据  
● Typescript 类型参考
type ReqBody = {
    port: string,
    data: string}

type ResBody = {
    resultCode: number, 
    resultMessage: string
}

zwexplorer.SerialPort.sendData(
    data?: ReqBody, 
    callback?: (res: ResBody)=>void
): Promise<ResBody>
● 参数说明
参数 类型 必填 说明
data.port Number 串口号
data.data Number 需要发送到串口设备的字符串数据
● 返回数据
参数 类型 必然存在 说明
res.resultCode Number 状态码,0 为正常,其它为异常 [状态码参考]
res.data Object 接口返回的详细数据
res.resultMessage String 接口消息
● 接口示例
zwexplorer.SerialPort.sendData({},(res)=>{
  // 返回数据
})

# 监听串口数据 onData

● 接口说明
监听串口数据
● Typescript 类型参考
type ReqBody = {
    port: string}

type ResBody = {
    resultCode: number, 
    resultMessage: string,
    data?: Arrar<string>
}

zwexplorer.SerialPort.onData(
    data: ReqBody, 
    callback?: (res: ResBody)=>void
): Promise<ResBody>
● 参数说明
参数 类型 必填 说明
data.port Number 串口号
● 返回数据
参数 类型 必然存在 说明
res.resultCode Number 状态码,0 为正常,其它为异常 [状态码参考]
res.data Array 串口号发送的消息
res.resultMessage String 接口消息
● 接口示例
zwexplorer.SerialPort.onData(data,(res)=>{
    // 返回数据
})
● 串口设备对接指南
// 监听串口设备的信息
zwexplorer.SerialPort.openPort({ port: '/dev/ttyS0', baud: 9600 }, (res) => {
  if (res.resultCode === 0) {
    // 已打开
    zwexplorer.SerialPort.onData({}, (res) => {
      // 监听到串口有信息发送过来
      zwexplorer.SerialPort.closePort({ port: '/dev/ttyS0', baud: 9600 }, (res) => {
        // 已关闭
      });
    });
  }
});
// 向串口设备发送信息
zwexplorer.SerialPort.openPort({ port: '/dev/ttyS0', baud: 9600 }, (res) => {
  if (res.resultCode === 0) {
    // 已打开
    zwexplorer.SerialPort.sendData({ port: '/dev/ttyS0' , data: 'doSomething' }, (res) => {
      // 监听到串口有信息发送过来
      zwexplorer.SerialPort.closePort({ port: '/dev/ttyS0', baud: 9600 }, (res) => {
        // 已关闭
      });
    });
  }
});