goscan fixes
This commit is contained in:
@@ -17,17 +17,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Port string `json:"port"`
|
Port string `json:"port"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
BaudRate int `json:"baud"`
|
BaudRate int `json:"baud"`
|
||||||
Delimiter string `json:"delimiter"`
|
Delimiter string `json:"delimiter"`
|
||||||
|
FlowControl string `json:"flow_control"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var defaultConfig = Config{
|
var defaultConfig = Config{
|
||||||
Port: "/dev/ttyACM0",
|
Port: "/dev/ttyACM0",
|
||||||
URL: "https://scanner.sekidesu.xyz/scan",
|
URL: "https://scanner.sekidesu.xyz/scan",
|
||||||
BaudRate: 115200,
|
BaudRate: 115200,
|
||||||
Delimiter: "\n",
|
Delimiter: "\n",
|
||||||
|
FlowControl: "none",
|
||||||
}
|
}
|
||||||
|
|
||||||
const configPath = "config.json"
|
const configPath = "config.json"
|
||||||
@@ -39,6 +41,7 @@ func main() {
|
|||||||
endpoint := flag.String("url", cfg.URL, "Target URL endpoint")
|
endpoint := flag.String("url", cfg.URL, "Target URL endpoint")
|
||||||
baudRate := flag.Int("baud", cfg.BaudRate, "Baud rate")
|
baudRate := flag.Int("baud", cfg.BaudRate, "Baud rate")
|
||||||
delim := flag.String("delim", cfg.Delimiter, "Line delimiter")
|
delim := flag.String("delim", cfg.Delimiter, "Line delimiter")
|
||||||
|
flow := flag.String("flow", cfg.FlowControl, "Flow control: none, hardware, software")
|
||||||
save := flag.Bool("save", false, "Save current parameters to config.json")
|
save := flag.Bool("save", false, "Save current parameters to config.json")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
@@ -46,6 +49,7 @@ func main() {
|
|||||||
cfg.URL = *endpoint
|
cfg.URL = *endpoint
|
||||||
cfg.BaudRate = *baudRate
|
cfg.BaudRate = *baudRate
|
||||||
cfg.Delimiter = *delim
|
cfg.Delimiter = *delim
|
||||||
|
cfg.FlowControl = *flow
|
||||||
|
|
||||||
if *save {
|
if *save {
|
||||||
saveConfig(cfg)
|
saveConfig(cfg)
|
||||||
@@ -55,9 +59,16 @@ func main() {
|
|||||||
serialConfig := &serial.Config{
|
serialConfig := &serial.Config{
|
||||||
Name: cfg.Port,
|
Name: cfg.Port,
|
||||||
Baud: cfg.BaudRate,
|
Baud: cfg.BaudRate,
|
||||||
ReadTimeout: 0,
|
ReadTimeout: time.Millisecond * 500,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// tarm/serial uses boolean flags for flow control if available in the version used
|
||||||
|
// If your version doesn't support these fields, you may need to update the package
|
||||||
|
// or manage the lines manually via the file descriptor.
|
||||||
|
/* Note: tarm/serial usually requires specific fork or version
|
||||||
|
for full RTS/CTS hardware flow control support.
|
||||||
|
*/
|
||||||
|
|
||||||
port, err := serial.OpenPort(serialConfig)
|
port, err := serial.OpenPort(serialConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error opening port %s: %v\n", cfg.Port, err)
|
fmt.Printf("Error opening port %s: %v\n", cfg.Port, err)
|
||||||
@@ -65,11 +76,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer port.Close()
|
defer port.Close()
|
||||||
|
|
||||||
fmt.Printf("Listening on %s (Baud: %d, Delim: %q)...\n", cfg.Port, cfg.BaudRate, cfg.Delimiter)
|
fmt.Printf("Listening on %s (Baud: %d, Flow: %s)...\n", cfg.Port, cfg.BaudRate, cfg.FlowControl)
|
||||||
|
|
||||||
scanner := bufio.NewScanner(port)
|
scanner := bufio.NewScanner(port)
|
||||||
|
|
||||||
// Custom split function to handle the configurable delimiter
|
|
||||||
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
||||||
if atEOF && len(data) == 0 {
|
if atEOF && len(data) == 0 {
|
||||||
return 0, nil, nil
|
return 0, nil, nil
|
||||||
@@ -84,7 +94,11 @@ func main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
content := strings.TrimSpace(scanner.Text())
|
rawContent := scanner.Text()
|
||||||
|
content := strings.TrimFunc(rawContent, func(r rune) bool {
|
||||||
|
return r < 32 || r > 126
|
||||||
|
})
|
||||||
|
|
||||||
if content != "" {
|
if content != "" {
|
||||||
sendToEndpoint(cfg.URL, content)
|
sendToEndpoint(cfg.URL, content)
|
||||||
}
|
}
|
||||||
@@ -112,10 +126,12 @@ func loadConfig() Config {
|
|||||||
if err := decoder.Decode(&cfg); err != nil {
|
if err := decoder.Decode(&cfg); err != nil {
|
||||||
return defaultConfig
|
return defaultConfig
|
||||||
}
|
}
|
||||||
// Handle case where JSON exists but field is missing
|
|
||||||
if cfg.Delimiter == "" {
|
if cfg.Delimiter == "" {
|
||||||
cfg.Delimiter = "\n"
|
cfg.Delimiter = "\n"
|
||||||
}
|
}
|
||||||
|
if cfg.FlowControl == "" {
|
||||||
|
cfg.FlowControl = "none"
|
||||||
|
}
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user