gotmc / lxi Goto Github PK
View Code? Open in Web Editor NEWGo-based implementation of the LAN eXtensions for Instrumentation (LXI) standard
License: MIT License
Go-based implementation of the LAN eXtensions for Instrumentation (LXI) standard
License: MIT License
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.
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')
}
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.