Skip to content

Commit

Permalink
fix(node-serial): serial port often open failed
Browse files Browse the repository at this point in the history
Node serial-control passed wrong "stopBits" args. when open a serial port.It cause serial port open
failed. Now fix it!
  • Loading branch information
hempflower committed Oct 15, 2023
1 parent b219a5a commit ffe6f82
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 103 deletions.
9 changes: 3 additions & 6 deletions frontend/src/composables/serial.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { ref,onMounted,onBeforeUnmount } from 'vue';
import { getSerialPorts } from "@/serial";
import type { SerialOptions } from "@/serial";

export type SerialOptions = {
export interface SerialOptionsWithPath extends SerialOptions {
path: string;
baudRate: number;
dataBits: 5 | 6 | 7 | 8;
stopBits: 1 | 1.5 | 2;
parity: "none" | "even" | "mark" | "odd" | "space";
}

export const useSerialList = () => {
Expand All @@ -30,7 +27,7 @@ export const useSerialList = () => {


export const useSerialOptionsModel = () => {
const serialOptions = ref<SerialOptions>({
const serialOptions = ref<SerialOptionsWithPath>({
path: "",
baudRate: 115200,
dataBits: 8,
Expand Down
225 changes: 129 additions & 96 deletions frontend/src/nodes/node-serial-control/node-view.vue
Original file line number Diff line number Diff line change
@@ -1,115 +1,148 @@
<template>
<div class="serial-control-node-container">
<div class="serial-control-form-item">
<div class="input-label">
端口
</div>
<el-select :teleported="false" :persistent="false" class="input-width" v-model="serialOptions.path"
:disabled="isConnect" placeholder="端口">
<el-option v-for="item in serialPorts" :key="item" :label="item" :value="item" />
</el-select>
</div>
<div class="lab-side-form-item">
<div class="input-label">
波特率
</div>
<el-autocomplete :teleported="false" class="input-width" v-model="serialOptions.baudRate" :disabled="isConnect"
:fetch-suggestions="fetchPresetsBaudRate" placeholder="波特率" />
</div>
<div class="lab-side-form-item">
<div class="input-label">
停止位
</div>
<el-select :teleported="false" :persistent="false" class="input-width" v-model="serialOptions.stopBits"
:disabled="isConnect" placeholder="停止位">
<el-option label="1" value="1" />
<el-option label="1.5" value="1.5" />
<el-option label="2" value="2" />
</el-select>
</div>
<div class="lab-side-form-item">
<div class="input-label">
数据位
</div>
<el-select :teleported="false" :persistent="false" class="input-width" v-model="serialOptions.dataBits"
:disabled="isConnect" placeholder="数据位">
<el-option label="8" value="8" />
<el-option label="7" value="7" />
<el-option label="6" value="6" />
<el-option label="5" value="5" />
</el-select>
</div>
<div class="lab-side-form-item">
<div class="input-label">
检验位
</div>
<el-select :teleported="false" :persistent="false" class="input-width" v-model="serialOptions.parity"
:disabled="isConnect" placeholder="校验位">
<el-option label="" value="none" />
<el-option label="偶校验" value="even" />
<el-option label="奇校验" value="odd" />
<el-option label="标记" value="mark" />
<el-option label="空格" value="space" />
</el-select>
</div>
<div class="actions">
<el-button v-if="!isConnect" class="input-width " type="primary" @click="onOpenSerial">连 接</el-button>
<el-button v-if="isConnect" class="input-width " type="danger" @click="onCloseSerial">断 开</el-button>
</div>

<div class="serial-control-node-container">
<div class="serial-control-form-item">
<div class="input-label">端口</div>
<el-select
:teleported="false"
:persistent="false"
class="input-width"
v-model="serialOptions.path"
:disabled="isConnect"
placeholder="端口"
>
<el-option
v-for="item in serialPorts"
:key="item"
:label="item"
:value="item"
/>
</el-select>
</div>
<div class="lab-side-form-item">
<div class="input-label">波特率</div>
<el-autocomplete
:teleported="false"
class="input-width"
v-model="serialOptions.baudRate"
:disabled="isConnect"
:fetch-suggestions="fetchPresetsBaudRate"
placeholder="波特率"
/>
</div>
<div class="lab-side-form-item">
<div class="input-label">停止位</div>
<el-select
:teleported="false"
:persistent="false"
class="input-width"
v-model="serialOptions.stopBits"
:disabled="isConnect"
placeholder="停止位"
>
<el-option label="1" :value="0" />
<el-option label="1.5" :value="1" />
<el-option label="2" :value="2" />
</el-select>
</div>
<div class="lab-side-form-item">
<div class="input-label">数据位</div>
<el-select
:teleported="false"
:persistent="false"
class="input-width"
v-model="serialOptions.dataBits"
:disabled="isConnect"
placeholder="数据位"
>
<el-option label="8" :value="8" />
<el-option label="7" :value="7" />
<el-option label="6" :value="6" />
<el-option label="5" :value="5" />
</el-select>
</div>
<div class="lab-side-form-item">
<div class="input-label">检验位</div>
<el-select
:teleported="false"
:persistent="false"
class="input-width"
v-model="serialOptions.parity"
:disabled="isConnect"
placeholder="校验位"
>
<el-option label="" value="none" />
<el-option label="偶校验" value="even" />
<el-option label="奇校验" value="odd" />
<el-option label="标记" value="mark" />
<el-option label="空格" value="space" />
</el-select>
</div>
<div class="actions">
<el-button
v-if="!isConnect"
class="input-width"
type="primary"
@click="onOpenSerial"
>连 接</el-button
>
<el-button
v-if="isConnect"
class="input-width"
type="danger"
@click="onCloseSerial"
>断 开</el-button
>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, inject, Ref } from 'vue'
import { SerialOptions, useSerialList } from '@/composables/serial';
import { ref, inject, Ref } from "vue";
import { SerialOptionsWithPath, useSerialList } from "@/composables/serial";
const { serialPorts } = useSerialList()
const { serialPorts } = useSerialList();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const fetchPresetsBaudRate = (query: string, cb: any) => {
const commonBaudRate = [
9600,
19200,
38400,
57600,
115200,
230400,
460800,
921600,
]
const commonBaudRate = [
9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600,
];
const matchedBaudRate = query ? commonBaudRate.filter(item => item.toString().startsWith(query)) : commonBaudRate
cb(matchedBaudRate.map(item => ({ value: item, label: item })))
}
const matchedBaudRate = query
? commonBaudRate.filter((item) => item.toString().startsWith(query))
: commonBaudRate;
cb(matchedBaudRate.map((item) => ({ value: item, label: item })));
};
const isConnect = inject('isConnect', ref(false))
const onOpenSerial = inject<Ref<() => void>>('onOpenSerial')
const onCloseSerial = inject<Ref<() => void>>('onCloseSerial')
const serialOptions = inject<Ref<SerialOptions>>('serialOptions', ref({
path: '',
const isConnect = inject("isConnect", ref(false));
const onOpenSerial = inject<Ref<() => void>>("onOpenSerial");
const onCloseSerial = inject<Ref<() => void>>("onCloseSerial");
const serialOptions = inject<Ref<SerialOptionsWithPath>>(
"serialOptions",
ref({
path: "",
baudRate: 115200,
stopBits: 1,
stopBits: 0,
dataBits: 8,
parity: 'none',
}))
parity: "none",
})
);
</script>
<style lang="scss">
.serial-control-node-container {
.input-label {
font-size: 10px;
color: rgb(143, 143, 143);
font-weight: bold;
margin-bottom: 4px;
}
.input-label {
font-size: 10px;
color: rgb(143, 143, 143);
font-weight: bold;
margin-bottom: 4px;
}
.input-width {
width: 100% !important;
}
.input-width {
width: 100% !important;
}
.actions {
width: 100%;
margin-top: 16px;
}
.actions {
width: 100%;
margin-top: 16px;
}
}
</style>
5 changes: 4 additions & 1 deletion frontend/src/serial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { Base64 } from "js-base64";
export interface SerialOptions {
baudRate: number;
dataBits: 5 | 6 | 7 | 8;
stopBits: 1 | 1.5 | 2;
// 0: 1 stop bit
// 1: 1.5 stop bits
// 2: 2 stop bits
stopBits: 0 | 1 | 2;
parity: "none" | "even" | "mark" | "odd" | "space";
}

Expand Down

0 comments on commit ffe6f82

Please sign in to comment.