Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions desktop/src/renderer/src/components/device-modal/serial-port.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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)
Expand All @@ -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)
}
}
Expand All @@ -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
Expand Down
11 changes: 6 additions & 5 deletions desktop/src/renderer/src/components/menu/serial-port/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}
Expand Down