Serial streams with EOL defined as CR only.

52 views
Skip to first unread message

Daniel Jankins

unread,
Apr 21, 2022, 12:27:25 PM4/21/22
to golang-nuts
Hi,

I have a serial port that is streaming data.  The data is terminated by a /r only.
I am trying to use scanner.Scan () to break up the lines.  Is there a way to change the scanner end of line character to /r only?

Thank you

config := &serial.Config {
Name:        lmt.ComPort,
Baud:        57600,
ReadTimeout: 30 * time.Second,
Size:        8,
}

stream, err = serial.OpenPort(config)
if err != nil {
fmt.Printf("ERROR - Can not open com port:%s\n", lmt.ComPort)
return
}

scanner := bufio.NewScanner(stream)

for scanner.Scan() {
bufStr := scanner.Text()
.
.
.
}

--
DanJ

Ian Lance Taylor

unread,
Apr 21, 2022, 12:46:40 PM4/21/22
to Daniel Jankins, golang-nuts
On Thu, Apr 21, 2022 at 9:27 AM Daniel Jankins <djan...@gmail.com> wrote:
>
> I have a serial port that is streaming data. The data is terminated by a /r only.
> I am trying to use scanner.Scan () to break up the lines. Is there a way to change the scanner end of line character to /r only?

See https://pkg.go.dev/bufio#Scanner.Split to change how the scanner
collects tokens.

Ian
Reply all
Reply to author
Forward
0 new messages