Skip to content

not clear buffer splitting when reading #129

@thedemoncat

Description

@thedemoncat

HI!!!

arduino App (platformio - esp8266)

#include <Arduino.h>
#include <ESP8266WiFi.h>


void setup() {
  // initialize serial:
  Serial.begin(9600);

}

void loop() {
  Serial.print("Hello world");
  delay(10000);   
}

golang app

package main

import "fmt"
import "log"
import "github.com/tarm/serial"

func main() {
	config := &serial.Config{
		Name: "COM3",
		Baud: 9600,
	}

	stream, err := serial.OpenPort(config)
	if err != nil {
		log.Fatal(err)
	}

	buf := make([]byte, 1024)

	for {
		n, err := stream.Read(buf)
		if err != nil {
			log.Fatal(err)
		}
		s := string(buf[:n])
		if s == "" {
			continue
		}
		fmt.Println(s)
	}
}

in the console I get this result:

API server listening at: 127.0.0.1:57847
H
ello world
H
ello world

Why is the first character read separately in the stream?

How to read a line correctly?

Thank you in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions