Commit 680801120af1dd55e59ae8e0ef87bb3c84369312
1 parent
a1b37d0f37
Exists in
master
LS산전 시리얼 테스트 프로그램 추가
Showing
1 changed file
with
73 additions
and
0 deletions
Show diff stats
go/src/lsis/serial.go
@@ -0,0 +1,73 @@ | @@ -0,0 +1,73 @@ | ||
1 | +package main | ||
2 | + | ||
3 | +import ( | ||
4 | + "log" | ||
5 | + "os" | ||
6 | + | ||
7 | + "github.com/tarm/serial" | ||
8 | +) | ||
9 | + | ||
10 | +func main() { | ||
11 | + w() | ||
12 | + r() | ||
13 | +} | ||
14 | + | ||
15 | +func r() { | ||
16 | + p, err := serial.OpenPort(&serial.Config{ | ||
17 | + Name: "/dev/ttyUSB1", | ||
18 | + Baud: 230400, | ||
19 | + }) | ||
20 | + if err != nil { | ||
21 | + log.Fatal(err) | ||
22 | + } | ||
23 | + | ||
24 | + b := make([]byte, 16) | ||
25 | + t := 0 | ||
26 | + for { | ||
27 | + n, err := p.Read(b) | ||
28 | + if err != nil { | ||
29 | + log.Fatal(err) | ||
30 | + } | ||
31 | + | ||
32 | + t += n | ||
33 | + log.Print(t) | ||
34 | + if t >= 1024*1024 { | ||
35 | + break | ||
36 | + } | ||
37 | + } | ||
38 | +} | ||
39 | + | ||
40 | +func w() { | ||
41 | + f, err := os.OpenFile("/dev/zero", os.O_RDONLY, 666) | ||
42 | + if err != nil { | ||
43 | + log.Fatal(err) | ||
44 | + } | ||
45 | + | ||
46 | + p, err := serial.OpenPort(&serial.Config{ | ||
47 | + Name: "/dev/ttyUSB1", | ||
48 | + Baud: 230400, | ||
49 | + }) | ||
50 | + if err != nil { | ||
51 | + log.Fatal(err) | ||
52 | + } | ||
53 | + | ||
54 | + b := make([]byte, 16) | ||
55 | + t := 0 | ||
56 | + for { | ||
57 | + n, err := f.Read(b) | ||
58 | + if err != nil { | ||
59 | + log.Fatal(err) | ||
60 | + } | ||
61 | + | ||
62 | + _, err = p.Write(b[:n]) | ||
63 | + if err != nil { | ||
64 | + log.Fatal(err) | ||
65 | + } | ||
66 | + | ||
67 | + t += n | ||
68 | + log.Print(t) | ||
69 | + if t >= 1024*1024+1024 { | ||
70 | + break | ||
71 | + } | ||
72 | + } | ||
73 | +} |