5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

goでwindowsでRS232C その2

Last updated at Posted at 2015-06-22

ターミナルのようなやつ。

term.PNG

サンプルコード

package main
import (
	"fmt"
	"github.com/ohisama/serial"
	"github.com/ohisama/key"
	"time"
)
func main() {
	device := "COM5"
	baud := 9600
	fmt.Println("open", device, "at", baud)
	port, err := serial.Open(device, baud)
	if err != nil {
		fmt.Println("open failed:", err)
		return
	}
	defer port.Close()
	fmt.Println("ready")
	buf := make([]byte, 100)
	c := make([]byte, 1)
	for {
		time.Sleep(200 * time.Millisecond)
		c[0] = key.Scan()
		if c[0] > 0 {
			n, err := port.Write(c)
			if err != nil {
				fmt.Println("serial write error:", err)
			}
			n, err = port.Read(buf)
			if err != nil {
				fmt.Println("serial read error:", err)
				break
			}
			fmt.Printf(string(buf[:n]))
		}
	}
}

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?