forked from MartinUbl/SREC-UART-Loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
175 lines (143 loc) · 3.96 KB
/
main.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#ifdef _WIN32
#include <windows.h>
#else
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#endif
#ifndef _WIN32
using HANDLE = int;
constexpr int INVALID_HANDLE_VALUE = -1;
#endif
#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char** argv)
{
if (argc != 3)
{
std::cerr << "Usage: uart_flasher.exe <filename> <port identifier>" << std::endl;
return 3;
}
std::string filename = argv[1];
HANDLE hComm;
#ifdef _WIN32
hComm = CreateFileA(argv[2], GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
#elif __APPLE__
hComm = open(argv[2], O_RDWR | O_NONBLOCK);
#else
hComm = open(argv[2], O_RDWR);
#endif
if (hComm <= INVALID_HANDLE_VALUE)
{
std::cerr << "Error in opening serial port" << std::endl;
return 1;
}
#ifdef _WIN32
DCB serialParams;
GetCommState(hComm, &serialParams);
serialParams.BaudRate = CBR_115200;
serialParams.ByteSize = 8;
serialParams.StopBits = ONESTOPBIT;
serialParams.Parity = NOPARITY;
SetCommState(hComm, &serialParams);
#else
struct termios tty;
if (tcgetattr(hComm, &tty) != 0)
{
std::cerr << "Error in configuring serial port (1)" << std::endl;
return 2;
}
tty.c_cflag &= ~PARENB;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~CRTSCTS;
tty.c_cflag |= CREAD | CLOCAL;
tty.c_lflag &= ~ICANON;
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL);
tty.c_oflag &= ~OPOST;
tty.c_oflag &= ~ONLCR;
tty.c_cc[VTIME] = 10;
tty.c_cc[VMIN] = 0;
cfsetispeed(&tty, B115200);
cfsetospeed(&tty, B115200);
if (tcsetattr(hComm, TCSANOW, &tty) != 0)
{
std::cerr << "Error in configuring serial port (2)" << std::endl;
return 2;
}
#endif
std::cout << "Opening serial port successful" << std::endl;
unsigned long l;
std::string ln;
{
std::ifstream ifile(argv[1]);
if (!ifile.is_open())
{
std::cerr << "Error in opening input file" << std::endl;
#ifdef _WIN32
CloseHandle(hComm);
#else
close(hComm);
#endif
return 1;
}
std::string str((std::istreambuf_iterator<char>(ifile)),
std::istreambuf_iterator<char>());
ifile.seekg(0, std::ios::beg);
int linecnt = 0;
int linecounter = 0;
int lineinc = 0;
for (int j = 0; j < str.size(); j++)
{
if (str[j] == '\n')
linecnt++;
}
std::cout << "Line count: " << linecnt << std::endl;
std::cout << "Uploading..." << std::endl;
while (std::getline(ifile, ln))
{
linecounter++;
#ifdef _WIN32
WriteFile(hComm, ln.c_str(), static_cast<DWORD>(ln.length()), &l, NULL);
#else
write(hComm, ln.c_str(), static_cast<int>(ln.length()));
#endif
if (lineinc < ((100 * linecounter) / linecnt))
{
lineinc = ((100 * linecounter) / linecnt);
std::cout << "Progress: " << lineinc << " %" << std::endl;
}
// Sleep(10);
// sleep(1);
struct timespec ts;
ts.tv_sec = 100 / 1000;
ts.tv_nsec = (100 % 1000) * 1000000;
nanosleep(&ts, NULL);
}
}
std::cout << "Launching..." << std::endl;
ln = "G";
#ifdef _WIN32
WriteFile(hComm, ln.c_str(), static_cast<DWORD>(ln.length()), &l, NULL);
#else
write(hComm, ln.c_str(), static_cast<int>(ln.length()));
#endif
std::cout << "Done. Switching to listener mode." << std::endl << std::endl;
char c;
#ifdef _WIN32
while (ReadFile(hComm, &c, 1, &l, NULL))
#else
while (read(hComm, &c, 1) == 1)
#endif
std::cout << c;
#ifdef _WIN32
CloseHandle(hComm);
#else
close(hComm);
#endif
return 0;
}