-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrecsend.cpp
131 lines (99 loc) · 1.9 KB
/
srecsend.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
#define _CRT_SECURE_NO_DEPRECATE
// srecsend.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include < stdio.h >
#include < stdlib.h >
#include "CSerial.h"
CSerial com;
int main(int argc, char* argv[])
{
int i;
int newLineCount;
int rv;
wchar_t comPortWs[256];
char buf[512];
FILE* in;
uint8_t rb;
int retransmission;
int fatalError;
int baudRate;
printf("scecsend B20231117 -#qUBECk#-\n");
if (argc < 4)
{
printf("usage: srecsend file.rec comPort baudrate\n\nExample: srecsend main.rec com1 500000\n\n");
return 20;
}
swprintf( comPortWs, 256, L"%hs", argv[2] );
baudRate = atoi( argv[3] );
if (baudRate == 0)
{
baudRate = 500000;
}
rv = com.open(comPortWs, baudRate);
if (rv)
{
printf("ERROR: can't open com port\n");
return 21;
}
in = fopen(argv[1], "r");
if (!in)
{
printf("ERROR: can't open input file\n");
com.close();
return 22;
}
fatalError = 0;
newLineCount = 0;
while (!feof(in))
{
fgets(buf, sizeof(buf) - 1, in);
retransmission = 0;
if (!feof(in))
{
do
{
//send line via uart
com.write( (uint8_t*)buf, strlen( buf ) );
if( ! ( ( strlen(buf) >= 2 ) && ( ( buf[1] == '9' ) || ( buf[1] == '8' ) || ( buf[1] == '7' ) ) ) )
{
if (!com.readByte(&rb))
{
printf( "%c", rb );
//fflush( stdout );
newLineCount++;
if( ( newLineCount % 80 == 0 ) )
{
printf( "\n" );
fflush( stdout );
}
if (rb == 'r')
{
retransmission = 1;
}
else if (rb == '!')
{
fatalError = 1;
retransmission = 0;
}
}
else
{
printf("\nNo response\n");
fatalError = 1;
break;
}
}
} while (retransmission);
}
if (fatalError)
{
printf("\nFatal error reported\n");
fclose(in);
com.close();
return 23;
}
}
fclose(in);
com.close();
return 0;
}