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

[Question] Is There a Way to Send Break Signal

Open KiddoV opened this issue 3 years ago • 10 comments

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!

KiddoV avatar Jan 23 '22 00:01 KiddoV

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.

kroppt avatar Apr 26 '22 18:04 kroppt

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

KiddoV avatar Apr 27 '22 02:04 KiddoV

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})

kroppt avatar Apr 27 '22 15:04 kroppt

Thanks! I will try that.

KiddoV avatar Apr 27 '22 15:04 KiddoV

@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.

KiddoV avatar May 06 '22 12:05 KiddoV

No, the type windowsPort is unexported.

kroppt avatar May 06 '22 19:05 kroppt

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

KiddoV avatar May 06 '22 21:05 KiddoV

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

kroppt avatar May 09 '22 15:05 kroppt

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+.

schmidtw avatar Sep 19 '22 02:09 schmidtw

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

KiddoV avatar Sep 19 '22 11:09 KiddoV

Looks like in this pull request: #137

supaplextor avatar Oct 28 '22 08:10 supaplextor

Sadly it doesn't support Windows 🥲

KiddoV avatar Oct 29 '22 12:10 KiddoV

@KiddoV please give #145 a try.

cmaglie avatar Nov 23 '22 15:11 cmaglie

Fixed by #145

cmaglie avatar Jan 02 '23 12:01 cmaglie