I just made an example. It doesn't work.
I couldn't do it with the "official" serial library ( it gave EOF when trying to read ), so I used Erwins. Probably the it is interface setup issue, I will dig it later. I will provide the source to the rs232 if needed, but basically here are the important functions:
type Port struct {
options Options
file *os.File
}
// Open will open the named serial port device with the given options.
// Open returns an *rs232.Error which implements the built-in error interface
// but additionally allows access to specific error codes. See Error.
//
// See type Options for valid parameter ranges.
func Open(name string, opt Options) (port *Port, err error) {
// validate options
mrate, mformat, err := validateOptions(&opt)
if err != nil {
return nil, err
}
// open special device file
// O_NOCTTY: if the file is a tty, don't make it the controlling terminal.
file, err := os.OpenFile(
name, syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NONBLOCK, 0666)
if err != nil {
if os.IsNotExist(err) {
return nil, &Error{ERR_DEVICE, err.Error()}
} else if os.IsPermission(err) {
return nil, &Error{ERR_ACCESS, err.Error()}
} else {
return nil, &Error{ERR_PARAMS, err.Error()}
}
}
fd := file.Fd()
// timeout settings
vmin := uint8(0)
if opt.Timeout == 0 {
vmin = 1
}
// termios settings
err = setTermios(fd, mrate, mformat, vmin, opt.Timeout)
if err != nil {
file.Close()
return nil, err
}
// set device file to blocking mode
err = syscall.SetNonblock(int(fd), false)
if err != nil {
file.Close()
return nil, &Error{ERR_PARAMS, err.Error()}
}
return &Port{opt, file}, nil
}
func (p *Port) Close() error {
return p.file.Close()
}
func (p *Port) Read(b []byte) (n int, err error) {
return p.file.Read(b)
}