Giter Site home page Giter Site logo

gotmc / lxi Goto Github PK

View Code? Open in Web Editor NEW
14.0 3.0 4.0 1.22 MB

Go-based implementation of the LAN eXtensions for Instrumentation (LXI) standard

License: MIT License

Go 82.92% Makefile 6.68% Just 10.40%
lxi tcpip test-equipment test-automation ivi ethernet lan-extensions scpi scpi-commands scpi-instrument

lxi's People

Contributors

matthewrankin avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

lxi's Issues

make bufio.Reader a member of lxi.Device

Hello,

I would suggest to make the bufio.Reader instantiated in the Query method a member of the Device struct.

There are multiple potential problems with the existing code.

  1. The reader may read more data than the line, if possible. This data will be lost.
  2. The reader allocates buffers for buffering data and the reader object. This creates pressure on the GC.

By making the Reader a member of the Device struct, it becomes persistant as well as its buffers and the data it contains. Is it still possible to use the Read methods and even read bytes per bytes, but efficiently.

Change the Device into this:

type Device struct {
	conn net.Conn
	rdr *bufio.Reader
}

The NewDevice function becomes:

func NewDevice(address string) (*Device, error) {
	v, err := NewVisaResource(address)
	if err != nil {
		return nil, err
	}
	tcpAddress := fmt.Sprintf("%s:%d", v.hostAddress, v.port)
	c, err := net.Dial("tcp", tcpAddress)
	if err != nil {
		return nil, err
	}
	return &Device{conn: c, rdr: bufio.NewReader(c)}, nil
}

Note that the convention is to return a nil Device in case of error.

The Read function becomes:

// Read reads from the network connection into the given byte slice.
func (d *Device) Read(p []byte) (n int, err error) {
	return d.rdr.Read(p)
}

The Close function becomes:

// Close closes the underlying network connection.
func (d *Device) Close() error {
	d.rdr = nil
	c := d.conn
	d.conn = nil
	return c.Close()
}

And Query becomes:

func (d *Device) Query(cmd string) (string, error) {
	err := d.Command(cmd)
	if err != nil {
		return "", err
	}
	return d.rdr.ReadString('\n')
}

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.