Skip to content

Commit 9750fe2

Browse files
authored
Merge pull request #91 from imguoguo/fix-baudrate
fix(desktop): custom baud rate bug
2 parents 0016db4 + b9e4006 commit 9750fe2

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

desktop/src/renderer/src/components/device-modal/serial-port.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const SerialPort = ({ setMsg }: SerialPortProps): ReactElement => {
4141
useEffect(() => {
4242
const savedBaudRate = storage.getBaudRate()
4343
setBaudRate(savedBaudRate)
44-
44+
4545
getSerialPorts(true)
4646

4747
const rmListener = window.electron.ipcRenderer.on(IpcEvents.OPEN_SERIAL_PORT_RSP, (_, err) => {
@@ -68,18 +68,24 @@ export const SerialPort = ({ setMsg }: SerialPortProps): ReactElement => {
6868
if (autoOpen) {
6969
const port = storage.getSerialPort()
7070
if (port && serialPorts.includes(port)) {
71-
await selectSerialPort(port)
71+
const savedBaudRate = storage.getBaudRate()
72+
await selectSerialPort(port, savedBaudRate)
7273
}
7374
}
7475
}
7576

76-
async function selectSerialPort(port: string): Promise<void> {
77+
async function selectSerialPort(port: string, customBaudRate?: number): Promise<void> {
7778
if (serialPortState === 'connecting') return
7879
setSerialPortState('connecting')
7980
setIsFailed(false)
8081
setMsg('')
8182

82-
const success = await window.electron.ipcRenderer.invoke(IpcEvents.OPEN_SERIAL_PORT, port, baudRate)
83+
let rate = baudRate
84+
if (customBaudRate && !baudRateOptions.some(option => option.value === customBaudRate)) {
85+
rate = customBaudRate
86+
}
87+
88+
const success = await window.electron.ipcRenderer.invoke(IpcEvents.OPEN_SERIAL_PORT, port, rate)
8389

8490
if (success) {
8591
setSerialPort(port)
@@ -99,12 +105,12 @@ export const SerialPort = ({ setMsg }: SerialPortProps): ReactElement => {
99105
async function handleBaudRateChange(newBaudRate: number): Promise<void> {
100106
setBaudRate(newBaudRate)
101107
storage.setBaudRate(newBaudRate)
102-
108+
103109
if (serialPort && serialPortState === 'connected') {
104-
const currentPort = serialPort
110+
const currentPort = serialPort
105111
await closeSerialPort()
106112
setTimeout(() => {
107-
selectSerialPort(currentPort)
113+
selectSerialPort(currentPort, newBaudRate)
108114
}, 200)
109115
}
110116
}
@@ -118,7 +124,7 @@ export const SerialPort = ({ setMsg }: SerialPortProps): ReactElement => {
118124
loading={serialPortState === 'connecting'}
119125
status={isFailed ? 'error' : undefined}
120126
placeholder={t('modal.selectSerial')}
121-
onChange={selectSerialPort}
127+
onChange={(serialPort) => selectSerialPort(serialPort)}
122128
onClick={() => getSerialPorts(false)}
123129
/>
124130
<Select

desktop/src/renderer/src/components/menu/serial-port/index.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ export const SerialPort = (): ReactElement => {
4949
setSerialPorts(ports)
5050
}
5151

52-
async function openSerialPort(port: string): Promise<void> {
52+
async function openSerialPort(port: string, customBaudRate?: number): Promise<void> {
5353
if (connectingPort) return
5454
setConnectingPort(port)
5555

56-
const success = await window.electron.ipcRenderer.invoke(IpcEvents.OPEN_SERIAL_PORT, port, baudRate)
56+
const rate = customBaudRate ?? baudRate
57+
const success = await window.electron.ipcRenderer.invoke(IpcEvents.OPEN_SERIAL_PORT, port, rate)
5758
if (success) {
5859
setSerialPort(port)
5960
storage.setSerialPort(port)
@@ -69,12 +70,12 @@ export const SerialPort = (): ReactElement => {
6970
async function handleBaudRateChange(newBaudRate: number): Promise<void> {
7071
setBaudRate(newBaudRate)
7172
storage.setBaudRate(newBaudRate)
72-
73+
7374
if (serialPort) {
74-
const currentPort = serialPort
75+
const currentPort = serialPort
7576
await closeSerialPort()
7677
setTimeout(() => {
77-
openSerialPort(currentPort)
78+
openSerialPort(currentPort, newBaudRate)
7879
}, 200)
7980
}
8081
}

0 commit comments

Comments
 (0)