github.com/bluenviron/gomavlib/v2@v2.2.1-0.20240308101627-2c07e3da629c/examples/router/main.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/bluenviron/gomavlib/v2"
     7  )
     8  
     9  // this example shows how to:
    10  // 1) create a node which communicates with a serial endpoint
    11  // 2) print incoming frames
    12  // 3) route received frames to every other channel
    13  
    14  func main() {
    15  	// create a node which communicates with a serial endpoint
    16  	// - communicates with multiple endpoints
    17  	// -
    18  	// - writes messages with given system id
    19  	node, err := gomavlib.NewNode(gomavlib.NodeConf{
    20  		Endpoints: []gomavlib.EndpointConf{
    21  			gomavlib.EndpointSerial{
    22  				Device: "/dev/ttyUSB0",
    23  				Baud:   57600,
    24  			},
    25  			gomavlib.EndpointUDPClient{"1.2.3.4:5900"},
    26  		},
    27  		Dialect:     nil,         // do not use a dialect and do not attempt to decode messages (in a router it is preferable)
    28  		OutVersion:  gomavlib.V2, // change to V1 if you're unable to communicate with the target
    29  		OutSystemID: 10,
    30  	})
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  	defer node.Close()
    35  
    36  	// print incoming frames
    37  	for evt := range node.Events() {
    38  		if frm, ok := evt.(*gomavlib.EventFrame); ok {
    39  			log.Printf("received: id=%d, %+v\n", frm.Message().GetID(), frm.Message())
    40  
    41  			// route frame to every other channel
    42  			node.WriteFrameExcept(frm.Channel, frm.Frame)
    43  		}
    44  	}
    45  }