go-serial icon indicating copy to clipboard operation
go-serial copied to clipboard

Invalid serial port error trying to open CH343G based serial port

Open nnnpa31 opened this issue 2 years ago • 10 comments

When does it happen This error is reported when I open /dev/ttyCH343USB0.

What module The device model is CH343G.

System and version information Ubuntu 22.04, Go 1.20

Code

package main
...
func main() {
    mode := &serial.Mode{
	BaudRate: 921600,
	DataBits: 8,
	StopBits: 1,
	Parity:   serial.NoParity,
    }
    port, err := serial.Open("/dev/ttyCH343USB0", mode)
    if err != nil {
        panic(err) // The error happen here.
    }
    defer port.Close()
}

What's return

panic: Invalid serial port

More infomation The same code and device can run on Windows, just replace /dev/ttyCH343USB0 with COM7.

I don't know what other information I need to provide about this. If you need it, please let me know and I will provide it as soon as possible.

Update: I can use the same settings to open it in the serial module of other languages.

nnnpa31 avatar Mar 01 '23 06:03 nnnpa31

Check this to see if it helps: https://github.com/bugst/go-serial/blob/master/serial_linux.go#L12

xray-bit avatar Mar 20 '23 06:03 xray-bit

On Windows 11 (10.0.22621 Build 22621) i have the same error, but it works on ARM64 (Raspberry Pi 3B). I have a CH341 USB adapter.

mateo08c avatar May 06 '23 21:05 mateo08c

I have the same issue after updating to Windows 11. The same code did run on Windows 10 just fine.

Heckelbert avatar May 08 '23 08:05 Heckelbert

Up!

mateo08c avatar Dec 13 '23 20:12 mateo08c

Did it work normally with other terminal emulators? like putty? Is there an easy/cheap way to get one of those CH343G boards to try?

cmaglie avatar Feb 16 '24 12:02 cmaglie

Did it work normally with other terminal emulators? like putty? Is there an easy/cheap way to get one of those CH343G boards to try?

Yes it works perfectly with putty.

mateo08c avatar Feb 16 '24 12:02 mateo08c

@mateo08c @nnnpa31 @xray-bit @Heckelbert I've updated the library with better error reporting, could you retry with the latest v1.6.2 and paste the error here? At least we could understand where the InvalidSerialPort error is raised...

cmaglie avatar Feb 16 '24 13:02 cmaglie

@mateo08c @nnnpa31 @xray-bit @Heckelbert I've updated the library with better error reporting, could you retry with the latest v1.6.2 and paste the error here? At least we could understand where the InvalidSerialPort error is raised...

It works! However, even with a defer and a call close function it still won't reopen for me afterward.

package main

import (
	"fmt"
	"go.bug.st/serial"
	"go.bug.st/serial/enumerator"
	"log"
)

func main() {
	ports, err := enumerator.GetDetailedPortsList()
	if err != nil {
		log.Fatal(err)
	}
	if len(ports) == 0 {
		fmt.Println("No serial ports found!")
		return
	}
	for _, port := range ports {
		fmt.Printf("Found port: %s\n", port.Name)
		if port.IsUSB {
			fmt.Printf("   USB ID     %s:%s\n", port.VID, port.PID)
			fmt.Printf("   USB serial %s\n", port.SerialNumber)
		}
	}

	open, err := serial.Open("COM11", &serial.Mode{
		BaudRate: 9600,
		DataBits: 8,
		Parity:   serial.NoParity,
		StopBits: serial.OneStopBit,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	defer open.Close()

	buf := make([]byte, 1024)
	for {
		n, err := open.Read(buf)
		if err != nil {
			fmt.Println(err)
			return
		}

		fmt.Printf("Read %d bytes: %s\n", n, string(buf[:n]))
	}
}

First run:

Read 1 bytes:
Read 1 bytes:
Read 1 bytes:
Read 31 bytes: test, 32768k bytes found.
INFO
Read 1 bytes: :
Read 31 bytes:  Self tests complete.
INFO: Do
Read 1 bytes: w
Read 27 bytes: nloading switch software.
Read 1 bytes: E
Read 20 bytes: PROM download (Y) ?
Read 1 bytes: �
Read 31 bytes: ���♠Initial download successful
Read 1 bytes: .
Read 2 bytes: 
Read 1 bytes: F
Read 31 bytes: O: Initialising Flash File Syst
Read 1 bytes: e
Read 3 bytes: m. 
Read 1 bytes:  
Read 2 bytes: 
...etc

Second run:

Found port: COM5
Found port: COM3
Found port: COM4
Found port: COM8
Found port: COM10
Found port: COM6
Found port: COM11
   USB ID     1A86:7523
   USB serial
Invalid serial port

mateo08c avatar Feb 17 '24 12:02 mateo08c