distributed / sers Goto Github PK
View Code? Open in Web Editor NEWSerial port access for the Go programming language.
License: MIT License
Serial port access for the Go programming language.
License: MIT License
Hey,
could you provide a minimalistic example how to use sers?
Thanks
Tobias
$ GOOS=linux GOARCH=arm GOARM=7 go build -o=vaisala.bin .
# github.com/soypat/cereal
../../../go/pkg/mod/github.com/soypat/[email protected]/cereal.go:160:18: undefined: sers.Open
Hi,
I am trying to use this with WIndows, at the moment it is failing to set the baud rate properly, it is staying at 9600.
I saw you did some work on the Linux side to allow custom baud rates already.
package main
import (
"encoding/hex"
"fmt"
"log"
"time"
"github.com/distributed/sers"
)
func main() {
portname := "COM3"
rb, err := readFirstBytesFromPort(portname)
if err != nil {
log.Fatal(err)
}
fmt.Printf("got %d bytes from %s:\n%s", len(rb), portname,
hex.Dump(rb))
}
func readFirstBytesFromPort(fn string) ([]byte, error) {
sp, err := sers.Open(fn)
if err != nil {
return nil, err
}
defer sp.Close()
err = sp.SetMode(10400, 8, sers.N, 1, sers.NO_HANDSHAKE)
if err != nil {
return nil, err
}
// setting:
// minread = 0: minimal buffering on read, return characters as early as possible
// timeout = 1.0: time out if after 1.0 seconds nothing is received
err = sp.SetReadParams(0, 1.0)
if err != nil {
return nil, err
}
mode, err := sp.GetMode()
fmt.Println(mode)
sp.SetBreak(false)
time.Sleep(2000 * time.Millisecond)
sp.SetBreak(true)
time.Sleep(25 * time.Millisecond)
sp.SetBreak(false)
time.Sleep(25 * time.Millisecond)
sp.Write([]byte {0x81})
time.Sleep(3 * time.Millisecond)
sp.Write([]byte {0x13})
time.Sleep(3 * time.Millisecond)
sp.Write([]byte {0xF7})
time.Sleep(3 * time.Millisecond)
sp.Write([]byte {0x81})
time.Sleep(3 * time.Millisecond)
sp.Write([]byte {0x0C})
time.Sleep(1000 * time.Millisecond)
var rb [128]byte
n, err := sp.Read(rb[:])
if rb[0] == 0x00 {
fmt.Println("Looks like we got our break echo")
}
if n <= 6 {
fmt.Println("Looks like ECU didn't respond")
}
return rb[:n], err
}
output:
9600,8n1,none
...
expected:
10400,8n1,none
Tested with raspberry Pi and my desktop:
Modify ReadClose verification program to be as follows
package main
import (
"flag"
"fmt"
"io"
"log"
"time"
"github.com/distributed/sers"
)
func main() {
err := Main()
if err != nil {
log.Fatal(err)
}
}
func readpart(r io.ReadCloser) error {
go func() {
time.Sleep(50 * time.Second)
fmt.Printf("============================> close now\n")
err := r.Close()
fmt.Printf("close err %v\n", err)
}()
_, err := r.Read(make([]byte, 128))
if err != nil {
return err
}
return nil
}
func Main() error {
flag.Parse()
if len(flag.Args()) < 1 {
return fmt.Errorf("please provide a serial file name")
}
fn := flag.Args()[0]
f, err := sers.Open(fn)
if err != nil {
return err
}
err = f.SetReadParams(0, 1)
if err != nil {
return err
}
return readpart(f)
}
Program now blocks for 50 seconds even though timeout is 1 second.
I am available to help resolving this issue but am unfamiliar with this repo and CGo.
Hello,
Would it be possible to add a function like this to set and clear break on the serial line ?
Thank you very much for sharing your work.
int setbreak(int fd,bool on) {
if (on) {
return ioctl(fd, TIOCSBRK, NULL);
} else {
return ioctl(fd, TIOCCBRK, NULL);
}
}
Hey Michael,
thanks for the swift response on the example!
I've set up sers and it seems to work - however the baud rate is fixed at 38400. Whatever baudrate I specify (e.g. 9600, 19200.... or a custom one 69500) it remains at 38400 bit/s.
err = mySerialPort.SetMode(19200, 8, sers.N, 1, sers.NO_HANDSHAKE)
I'm writing the code on a Raspberry Pi / Raspbian (Debian Jessie) and I'm using the UART which is located on the GPIO pins.
In order to verify the mentioned behaviour, I write the same character in a endless loop to the UART while monitoring it on my Oscilloscope (which is able to decode RS232 on the fly).
Writing a small test program in Python verified also that an arbitrary baudrate can be set on the Raspi's UART.
I would very much appreciate if you could give me a hint or maybe fix this. Your library is very valuable, since it seems to be the only on in Golang to support arbitrary baudrates.
Thanks!
I've switched https://github.com/SMerrony/DasherG to use this package for serial I/O and it's working well - thanks.
It would be useful to be able to query the current state of an active serial connection. Eg. a GetMode() func.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.