jacobsa / go-serial

A Go library for dealing with serial ports.
Apache License 2.0
626 stars 120 forks source link

On OpenWRT : error opening port:%!(EXTRA *os.SyscallError=SYS_IOCTL: inappropriate ioctl for device) #37

Open captcha1 opened 6 years ago

captcha1 commented 6 years ago

Pretty sure this is a go-serial bug (due to TCSETS2 being hard coded ? ) :

On OpenWRT, I get

error opening port:%!(EXTRA *os.SyscallError=SYS_IOCTL: inappropriate ioctl for device)

... could be because OpenWRT uses musl (not glibc).

More details , doing "strace" on OpenWRT, shows :

ioctl(5, _IOC(_IOC_READ, 0x54, 0x2b, 0x2c), 0x9116c90) = -1 ENOTTY (Not a tty)

FYI, to cross compile for MIPS (Atheros Soc) on OpenWRT, do :

export GOARCH=mips export GOMIPS=softfloat # requires the recently released go1.10 to get the softfloat code generator

captcha1 commented 6 years ago

FYI , on a Atheros MIPS 24Kc machine running OpenWRT 17.01.2 , I get :

TCSETS2 = 0x8030542B BOTHER = 0x00001000 NCCS = 23

captcha1 commented 6 years ago

... though there seems to be 2 files named termbits.h with 2 different values for NCCS :

$ cd lede-sdk-17.01.4-ar71xx-generic_gcc-5.4.0_musl-1.1.16.Linux-x86_64/staging_dir/toolchain-mips_24kc_gcc-5.4.0_musl-1.1.16/include $ grep NCCS asm/ |grep define

asm-generic/termbits.h:#define NCCS 19 asm/termbits.h:#define NCCS 23

... so that looks like another source of bugs ...

captcha1 commented 6 years ago

I did a test, and the speed gets set correctly when NCCS=23

julioSab commented 6 years ago

Did you manage to make go-serial working with openwrt?

captcha1 commented 6 years ago

Yes ...

It's probably better to fix it by using "cgo", but a workaround is to hard code for OpenWRT :

TCSETS2 = 0x8030542B

BOTHER = 0x00001000

NCCS = 23

On Thu, Apr 5, 2018 at 1:40 PM, julioSab notifications@github.com wrote:

Did you manage to make go-serial working with openwrt?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jacobsa/go-serial/issues/37#issuecomment-379069513, or mute the thread https://github.com/notifications/unsubscribe-auth/AJ9ihDfLKuZLxojcCSMidJpuiXyUOALjks5tloEigaJpZM4SXQNk .

philipgreat commented 6 years ago

The following code works under mips and arm CPU, it should work on other linux too.

env GOOS=linux GOARCH=arm GOARM=5 go build -ldflags "-s -w" -o server-arm server.go env GOOS=linux GOARCH=mips go build -ldflags "-s -w" mem.go

PC linux is not tested yet. Referenced different headers made this works.

https://github.com/mojo-runtime/lib-linux/blob/6dbfa74d17beda9be9c6e3b595c76f8df3cbb077/c/struct-termios.h

#if defined(__mips__)
#  define NCCS 23
#elif defined(__sparc__)
#  define NCCS 17
#else
#  define NCCS 19
#endif

The change to the file serial/open_linux.go

Please reference https://github.com/philipgreat/go-serial/blob/master/serial/open_linux.go

func nccs() int {
    if runtime.GOARCH == "mips" {
        return 23
    }
    if runtime.GOARCH == "mipsle" {
        return 23
    }
    if runtime.GOARCH == "spark" {
        return 17
    }
    if runtime.GOARCH == "sparc64" {
        return 17
    }
    return 19

}

const (
    kTCSETS2 = unix.TCSETS2 //0x8030542B
    kBOTHER  = unix.BOTHER//0x00001000
    kNCCS    = nccs() 
)
chmorgan commented 5 years ago

@philipgreat plan to push a PR for this fix?

chmorgan commented 5 years ago

@philipgreat also your fork fails to build for me here on 1.11, reporting:

# github.com/philipgreat/go-serial/serial
../github.com/philipgreat/go-serial/serial/open_linux.go:62:2: const initializer nccs() is not a constant
philipgreat commented 5 years ago

The code was out of sync.

chmorgan commented 5 years ago

Hi @philipgreat what do you mean? I was just building against your package and the package was failing to build. It looks like go doesn’t allow a constant to be defined as a function, or maybe only if that function can be determined to return a constant.

philipgreat commented 5 years ago

I run a wrong test, and I am trying to find out a more dynamic way to address this issue.

chmorgan commented 5 years ago

It seems like the golang idiomatic way is to create different build files that defined the same per platform constant, or a function to return the value with a runtime check, but that wasn’t a constant.

On Mon, Jan 7, 2019 at 1:08 PM Philip Z notifications@github.com wrote:

I run a wrong test, and I am trying to find out a more dynamic way to address this issue.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jacobsa/go-serial/issues/37#issuecomment-452026905, or mute the thread https://github.com/notifications/unsubscribe-auth/ABJ-AOa6wlqbGdhXsTp8VMer8Z0Ss_6Hks5vA40ogaJpZM4SXQNk .

chmorgan commented 5 years ago

Anyone looking at this still? I ran into this bug again today and of course since January I forgot about the issue and was trying to debug it for a half hour...

chmorgan commented 5 years ago

Oh, last commit in this project is Jan 2018 so it looks dead. Anyone know of a supported serial library for go?

philipgreat commented 5 years ago

const ( kTCSETS2 = unix.TCSETS2 //0x8030542B kBOTHER = unix.BOTHER//0x00001000 kNCCS = 23 // 23 is the value fix for MIPS. most of the OpenWrt routers equipped with MIPS cpu. )

Please use 23 for MIPS cpu. The compile environment values are

env GOOS=linux GOARCH=mips GOMIPS=softfloat

To make this dynamic is not supported in golang with nccs(), another way is adding a new file just for linux and arm CPU.

chmorgan commented 5 years ago

@philipgreat don't get me wrong, I appreciate that you figured out the values. I'm looking for a maintained library however, one with a number of eyes on it, regular updates, pull requests handled etc. I may end up forking to do it myself or we could work together to do so. I see you have a MIPSLE only library, thoughts on expanding it to conditionally support the different patterns using compile time checks or cgo as others have suggested?

philipgreat commented 5 years ago

I initiated a PR for conditional compilation.

Philip Zhang 邮箱:philip_chang@163.com

Signature is customized by Netease Mail Master

On 08/05/2019 08:04, Chris Morgan wrote:

@philipgreat don't get me wrong, I appreciate that you figured out the values. I'm looking for a maintained library however, one with a number of eyes on it, regular updates, pull requests handled etc. I may end up forking to do it myself or we could work together to do so. I see you have a MIPSLE only library, thoughts on expanding it to conditionally support the different patterns using compile time checks or cgo as others have suggested?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.