Giter Site home page Giter Site logo

sers's People

Contributors

distributed avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

sers's Issues

Example

Hey,

could you provide a minimalistic example how to use sers?

Thanks
Tobias

Custom baud rates do not work in Windows

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

Read blocks indefinitely on linux even with timeout

Tested with raspberry Pi and my desktop:

Steps to reproduce

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.

Set and clear break

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

Baudrate can not be changed under Linux

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.