Skip to content

pauleyj/gobee

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gobee - A Go XBee Library

Build Status Coverage Status Go Report Card codebeat badge

gobee, a library for enabling support of XBee series 2 and series 3 low power radios to your Go project.


Usage

Implement the XBeeTransmitter and XBeeReceiver interfaces, instantiate an XBee, and start communicating, almost... It is up to the gobee user to configure and own the serial port the XBee is connected to and marshal data to/from it to gobee.

XBeeTransmitter

type XBeeTransmitter interface {
	Transmit([]byte) (int, error)
}

gobee uses the XBeeTransmitter interface to request a byte slice be sent to the serial communications port the XBee is connected to.

XBeeReceiver

type XBeeReceiver interface {
	Receive(rx.RxFrame) error
}

gobee uses the XBeeReceiver interface to report received API frames.

XBee

This the XBee widget used to communicate with the physical XBee.

...
transmitter := &Transmitter{...}	// your XBeeTransmitter
receiver    := &Receiver{...}		// your XBeeReceiver
xbee        := gobee.New(transmitter, receiver)
...

Transmitting a Data Frame

When sending a frame, construct frame using the appropriate frame builder and hand it to gobee to generate an API frame. gobee will then call the supplied Transmitters Transmit to send the bytes to the UART the XBee is connected to.

at := tx.NewATBuilder().
		ID(1).
		Command([2]byte{'A','O'}).
		Parameter(nil).
		Build()
_, err := xbee.TX(at)
...

Sending API Frame to the UART

When a frame is transmitted, gobee forms an appropriate API packet (see Transmitting a Frame) and sends it to your XBeeTransmitter for writing to the serial UART the XBee is connected to.

...
func (tx *Transmitter) Transmit(buffer []byte) (n int, err error) {

	i, err := port.Write(buffer)
	if err != nil {
		fmt.Printf("Failed to write buffer to xbee comms: %v\n", err)
	}

	return i, err
}
...

Receiving Data from the UART

When data is received from the serial UART the XBee is connected to, send it to gobee.

...
n, err := port.Read(buffer)
...
for i := 0; i < n; i++ {
	err = xbee.RX(buffer[i])
	...
}
...

Receiving Data Frames from gobee

Your implemented XBeeReceiver will get called when completed API frames are received. gobee validates the received API frame and reports the data frame via the XBeeReceiver interface.

func (r *Receiver) Receive(f rx.RxFrame) error {
	switch f.(type) {
	case *rx.ZB:
		// do something with received ZB frame
	...
	}

	return nil
}

License

gobee is licensed under the MIT License. See the LICENSE for more information.