diff --git a/entry/src/main/ets/common/utils/SerialPortClient.ts b/entry/src/main/ets/common/utils/SerialPortClient.ts new file mode 100644 index 00000000..04027989 --- /dev/null +++ b/entry/src/main/ets/common/utils/SerialPortClient.ts @@ -0,0 +1,62 @@ +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); + } +} \ No newline at end of file diff --git a/entry/src/main/ets/common/utils/http.ets b/entry/src/main/ets/common/utils/http.ets new file mode 100644 index 00000000..12c5f178 --- /dev/null +++ b/entry/src/main/ets/common/utils/http.ets @@ -0,0 +1,96 @@ +import Prompt from '@system.prompt'; +import http from '@ohos.net.http'; + +enum RequestMethod { + OPTIONS = "OPTIONS", + GET = "GET", + HEAD = "HEAD", + POST = "POST", + PUT = "PUT", + DELETE = "DELETE", + TRACE = "TRACE", + CONNECT = "CONNECT" +} + +const BaseUrl = "" + +interface Response { + code: number, + data: any, + msg: string +} + +export async function request( + url: string, + config: { + method: string; + params?: any; + data?: any + + [key: string]: any + } +): Promise { + const httpRequest = http.createHttp(); + return new Promise((resolve, reject) => { + console.info("start request url: ", url, "request params: ", JSON.stringify(config.params || config.data || {})) + httpRequest.request(BaseUrl + url, + { + method: config.method as RequestMethod, + header: { + ...config.header, + }, + extraData: { + ...(config.params || {}), + ...(config.data || {}) + }, + expectDataType: http.HttpDataType.OBJECT, + connectTimeout: 60000, //超时时间 + }).then(async (data) => { + let result = data.result as Response + if (result.code === 1) { + console.info("success request url: ", url) + console.info(JSON.stringify(result)) + resolve(data.result as T) + } else if (result.code === 401) { + // console.error("401 request", url) + // let user = await PreferenceUtils.getUserModel() + // if (user?.isLogin) { + // userLoginUsingPost({ + // account: user.account, + // psd: user.password, + // }).then(res => { + // let newUser: UserModel = new UserModel(res.data) + // newUser.account = user.account + // newUser.password = user.password + // newUser.isLogin = true + // PreferenceUtils.writeUserData(newUser) + // request(url, config) + // }).catch(() => { + // PreferenceUtils.writeUserData(new UserModel()) + // if (router.getState().name !== "Login") { + // router.pushUrl({ url: "pages/Login" }) + // } + // }) + // } else { + // PreferenceUtils.writeUserData(new UserModel()) + // if (router.getState().name !== "Login") { + // router.pushUrl({ url: "pages/Login" }) + // } + // } + } else { + console.error("error request url: ", url) + console.error(result.msg) + reject(result.msg) + Prompt.showToast({ + message: result.msg, + duration: 2000, + }) + } + }).catch((err) => { + console.error("error request url: ", BaseUrl + url) + console.error(err) + reject(err) + }) + + }); +} \ No newline at end of file