bugst / go-serial

A cross-platform serial library for go-lang.
BSD 3-Clause "New" or "Revised" License
657 stars 199 forks source link

[Question] Is There a Way to Send Break Signal #131

Closed KiddoV closed 1 year ago

KiddoV commented 2 years ago

Thank you for the great package. I would like to send a break signal to the port. How do I accomplish this? It like when using TeraTerm you send the port a break signal. Thank you!

kroppt commented 2 years ago

According to the manual, break signals are just two bytes, a 255 byte followed by a 243 byte.

This macro sends IAC BREAK(255 243) to the host system on the TELNET console.

So, you should be able to replicate a break signal by sending those bytes.

KiddoV commented 2 years ago

@kroppt thanks for the info. Can you give some code example on how to send those bytes? I appreciate it!

kroppt commented 2 years ago

See https://pkg.go.dev/go.bug.st/serial#section-documentation for all the setup.

Then, when you are writing to the serial port, send the two bytes:

n, err := port.Write([]byte{255, 243})
KiddoV commented 2 years ago

Thanks! I will try that.

KiddoV commented 2 years ago

@kroppt Is there anyway that I can get the syscall.Handle without modify the lib? I want to try to set comm break like this: https://github.com/ricorx7/go-serial/blob/be02e536a880/syscall_windows.go#L65

func SetCommBreak(handle syscall.Handle) (err error) {
    r1, _, e1 := syscall.Syscall(procSetCommBreak.Addr(), 1, uintptr(handle), 0, 0)
    if r1 == 0 {
        if e1 != 0 {
            err = error(e1)
        } else {
            err = syscall.EINVAL
        }
    }
    return
}

Not sure how to get the Handle from outside.

kroppt commented 2 years ago

No, the type windowsPort is unexported.

KiddoV commented 2 years ago

Do you have any suggestion of how I can use the SetCommBreak from syscall.NewLazyDLL with my serial.Port instance?

kroppt commented 2 years ago

No. That would be the same as the code snippet you posted.

schmidtw commented 2 years ago

I don't have a way to put a scope on the output, but the suggested solution of sending 255,243 doesn't appear to work in all cases. I have an FTDI serial chip on a linux system. When I send a break using the snippet below, I'm able to see the expected results, but when I send 255,243 I don't.

unix.Syscall(unix.SYS_IOCTL, mySerialPortFile.Fd(), uintptr(unix.TCSBRKP), uintptr(0))

In my use case I need to reset a DS2480 chip & one way to do this is to send a break for 2ms+.

KiddoV commented 2 years ago

@schmidtw interesting, I will give it a try. Also sending 255, 243 doesn't work for me as well.

supaplextor commented 1 year ago

Looks like in this pull request: #137

KiddoV commented 1 year ago

Sadly it doesn't support Windows 🥲

cmaglie commented 1 year ago

@KiddoV please give #145 a try.

cmaglie commented 1 year ago

Fixed by #145