Merge pull request #91 from imguoguo/fix-baudrate

fix(desktop): custom baud rate bug
This commit is contained in:
wenjie
2025-12-16 16:01:55 +08:00
committed by GitHub
2 changed files with 20 additions and 13 deletions

View File

@@ -41,7 +41,7 @@ export const SerialPort = ({ setMsg }: SerialPortProps): ReactElement => {
useEffect(() => {
const savedBaudRate = storage.getBaudRate()
setBaudRate(savedBaudRate)
getSerialPorts(true)
const rmListener = window.electron.ipcRenderer.on(IpcEvents.OPEN_SERIAL_PORT_RSP, (_, err) => {
@@ -68,18 +68,24 @@ export const SerialPort = ({ setMsg }: SerialPortProps): ReactElement => {
if (autoOpen) {
const port = storage.getSerialPort()
if (port && serialPorts.includes(port)) {
await selectSerialPort(port)
const savedBaudRate = storage.getBaudRate()
await selectSerialPort(port, savedBaudRate)
}
}
}
async function selectSerialPort(port: string): Promise<void> {
async function selectSerialPort(port: string, customBaudRate?: number): Promise<void> {
if (serialPortState === 'connecting') return
setSerialPortState('connecting')
setIsFailed(false)
setMsg('')
const success = await window.electron.ipcRenderer.invoke(IpcEvents.OPEN_SERIAL_PORT, port, baudRate)
let rate = baudRate
if (customBaudRate && !baudRateOptions.some(option => option.value === customBaudRate)) {
rate = customBaudRate
}
const success = await window.electron.ipcRenderer.invoke(IpcEvents.OPEN_SERIAL_PORT, port, rate)
if (success) {
setSerialPort(port)
@@ -99,12 +105,12 @@ export const SerialPort = ({ setMsg }: SerialPortProps): ReactElement => {
async function handleBaudRateChange(newBaudRate: number): Promise<void> {
setBaudRate(newBaudRate)
storage.setBaudRate(newBaudRate)
if (serialPort && serialPortState === 'connected') {
const currentPort = serialPort
const currentPort = serialPort
await closeSerialPort()
setTimeout(() => {
selectSerialPort(currentPort)
selectSerialPort(currentPort, newBaudRate)
}, 200)
}
}
@@ -118,7 +124,7 @@ export const SerialPort = ({ setMsg }: SerialPortProps): ReactElement => {
loading={serialPortState === 'connecting'}
status={isFailed ? 'error' : undefined}
placeholder={t('modal.selectSerial')}
onChange={selectSerialPort}
onChange={(serialPort) => selectSerialPort(serialPort)}
onClick={() => getSerialPorts(false)}
/>
<Select

View File

@@ -49,11 +49,12 @@ export const SerialPort = (): ReactElement => {
setSerialPorts(ports)
}
async function openSerialPort(port: string): Promise<void> {
async function openSerialPort(port: string, customBaudRate?: number): Promise<void> {
if (connectingPort) return
setConnectingPort(port)
const success = await window.electron.ipcRenderer.invoke(IpcEvents.OPEN_SERIAL_PORT, port, baudRate)
const rate = customBaudRate ?? baudRate
const success = await window.electron.ipcRenderer.invoke(IpcEvents.OPEN_SERIAL_PORT, port, rate)
if (success) {
setSerialPort(port)
storage.setSerialPort(port)
@@ -69,12 +70,12 @@ export const SerialPort = (): ReactElement => {
async function handleBaudRateChange(newBaudRate: number): Promise<void> {
setBaudRate(newBaudRate)
storage.setBaudRate(newBaudRate)
if (serialPort) {
const currentPort = serialPort
const currentPort = serialPort
await closeSerialPort()
setTimeout(() => {
openSerialPort(currentPort)
openSerialPort(currentPort, newBaudRate)
}, 200)
}
}