patterngoMinor
Utility that decodes and logs UDP packets
Viewed 0 times
decodeslogspacketsudputilitythatand
Problem
I have written the following utility, as my first non-tutorial program in Go.
The purpose of the utility is
Since I am planning further functionality (setting duty cycle of a brushless motor controller via serial), which will be logged to the same file, I have separated the file writing from the UDP packet logging via a channel.
Since this is a utility that serves a single purpose, error handling is minimal.
I am hoping for feedback regarding the general style and idiomaticy of the code and the folder structure (separating loadcell-related files into a separate sub-package, in the loadcell sub-directory).
Specifically, I would appreciate your thoughts on:
The code is hosted here, and for longevity of this question, I repeat the files below. I have left out import statements for brevity.
./main.go
```
package main
import (
...
"github.com/mikehamer/ati-torque-force-logger/loadcell"
)
func main() {
// Parse flags
loadCellAddress := flag.String("address", "192.168.1.200:49152", "The address of the loadcell")
logFileName := flag.String("logfile", fmt.Sprintf("%s_loadcell.log", time.Now().Format("2006-01-02_15-04-05")), "The name of the logfile")
flag.Parse()
//open CSV log
logfile, err := os.Create(*logFileName)
if err != nil {
log.Fatal(
The purpose of the utility is
- to connect to a torque/force sensor (aka load-cell) via UDP;
- to send an initialization command; and
- to record the resulting stream of measurements.
Since I am planning further functionality (setting duty cycle of a brushless motor controller via serial), which will be logged to the same file, I have separated the file writing from the UDP packet logging via a channel.
Since this is a utility that serves a single purpose, error handling is minimal.
I am hoping for feedback regarding the general style and idiomaticy of the code and the folder structure (separating loadcell-related files into a separate sub-package, in the loadcell sub-directory).
Specifically, I would appreciate your thoughts on:
- A possible bug in
go version go1.4.2 darwin/amd64, in loadcell.go (see comment before the finalforloop)
- The
FromNetworkBytesfunction in loadCellPacket.go. I really wanted to make a class method, e.g.packet := loadCellPacket.FromNetworkBytes(b), but as far as I found, the Go options are either:packet := loadCellPacketFromNetworkBytes(b)(a simple function), orvar packet loadCellBytes; packet.FromNetworkBytes(b). I opted for the later, since I can reuse the packet variable, but I would appreciate feedback.
The code is hosted here, and for longevity of this question, I repeat the files below. I have left out import statements for brevity.
./main.go
```
package main
import (
...
"github.com/mikehamer/ati-torque-force-logger/loadcell"
)
func main() {
// Parse flags
loadCellAddress := flag.String("address", "192.168.1.200:49152", "The address of the loadcell")
logFileName := flag.String("logfile", fmt.Sprintf("%s_loadcell.log", time.Now().Format("2006-01-02_15-04-05")), "The name of the logfile")
flag.Parse()
//open CSV log
logfile, err := os.Create(*logFileName)
if err != nil {
log.Fatal(
Solution
Regarding the first part of it:
From reading the golang docs, it appears
may work because a specific allocation and initialization of 36 []byte type is occuring, at least enough to hold something if a UDP packet arrives to be read
see Documentation: builtin
while
in this case you've got buf as a pointer to byte but neither a new nor make has been called on it
One caveat, you are using 1.4.x and the docs seem to be up to current 1.9.x. I didn't see an easy way to check the specs on the net package or Connection/UDP usage, as to whether anything has changed...
From reading the golang docs, it appears
buf := make([]byte, 36) //BUG? This causes ReadFromUDP to block = GOODmay work because a specific allocation and initialization of 36 []byte type is occuring, at least enough to hold something if a UDP packet arrives to be read
see Documentation: builtin
while
var buf []byte // While this causes ReadFromUDP to continuously return 0,nil,nilin this case you've got buf as a pointer to byte but neither a new nor make has been called on it
One caveat, you are using 1.4.x and the docs seem to be up to current 1.9.x. I didn't see an easy way to check the specs on the net package or Connection/UDP usage, as to whether anything has changed...
Code Snippets
var buf []byte // While this causes ReadFromUDP to continuously return 0,nil,nilContext
StackExchange Code Review Q#87117, answer score: 2
Revisions (0)
No revisions yet.