-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtxt2nano.go
104 lines (94 loc) · 2.39 KB
/
txt2nano.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright (C) 2017 - Alexey V. Voronin @ FoxyLab
* Email: support@foxylab.com
* Website: https://acdc.foxylab.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package main
import (
"log"
"io/ioutil"
"fmt"
"github.com/jacobsa/go-serial/serial"
"time"
"os"
)
func main() {
var length uint32
const buf_size = 256
var i uint32
filename := os.Args[1]
fmt.Printf("File: %s\n", filename)
data, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
length = uint32(len(data))
fmt.Printf("File size: %d\n", length)
time.Sleep(2000 * time.Millisecond);
//port parameters
options := serial.OpenOptions{
PortName: "COM33",
BaudRate: 9600,
DataBits: 8,
StopBits: 1,
MinimumReadSize: 1,
}
//port open
comport, err := serial.Open(options)
if err != nil {
log.Fatalf("serial.Open: %v", err)
}
defer comport.Close()
//flush buffer
buf := make([]byte, buf_size)
comport.Read(buf)
//transmit buffer
b:=make([]byte, 1)
for i = 0; i < length; i++ {
//char send
b[0] = data[i]
n, err := comport.Write(b)
//20 ms delay
time.Sleep(20 * time.Millisecond);
//char #
fmt.Println(string(b[0]))
if err != nil {
log.Fatalf("port.Write: %d", n)
}
if i<(length-1) {
fmt.Println("ACK wait");
//ACK wait
for {
buf := make([]byte, 1)
n,err = comport.Read(buf)
if err == nil {
if n > 0 {
if buf[0] == 0x06 {
fmt.Println("ACK received");
break
}
}
}
}
}
}
//EOF sent
fmt.Println("EOF");
b[0] = 0x1A;//CTRL-Z
comport.Write(b);
fmt.Print("O.K.")
}