-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReceiveData.c
115 lines (103 loc) · 1.95 KB
/
ReceiveData.c
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
#include"Kuroko.h"
#include"App.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<process.h>
static void ReceiveData(char *FileName ,SerialPort SP)
{
SP.Hcom = SerialPortInit(SP);
CheckComHandle(SP.Hcom);
/*准备存储文件*/
FILE *pSave;
if (strlen(FileName) > MAX_PATH)
{
AErr = FileNameTooLong;
return;
}
pSave = fopen(FileName, "w");
if (pSave == NULL)
{
AErr = FileOpenFail;
return;
}
/*传输数据*/
HANDLE Wait;
Wait = (HANDLE)_beginthreadex(NULL, 0, ExitMonitor, NULL, 0, NULL);
/*传输数据起始符,由上位机和下位机统一*/
const char *DataStart = "tsD";
/*传输数据终止符,由上位机和下位机统一*/
const char *DataEnd = "deD";
/*段结束符,由上位机和下位机统一*/
const char SectionEnd = '#';
char SEbuf[4];
memset(SEbuf, 0, sizeof(char)* 4);
while (1)
{
if (!ReMessege(SP.Hcom, &SEbuf[0]))
{
if (Err == Err_PullOut)
{
return;
}
}
else
{
if (STRCMP(SEbuf, == , DataStart))
break;
SEbuf[2] = SEbuf[1];
SEbuf[1] = SEbuf[0];
}
}
printf("检测到数据起始符,开始保存数据至文件\n");
memset(SEbuf, 0, sizeof(char)* 3);
while (1)
{
if (ReMessege(SP.Hcom, &SEbuf[0]))
{
fprintf(pSave, "%c", SEbuf[0]);
if (SEbuf[0] == SectionEnd&&fExit == 1)
{
printf("同时检测到段终止符和退出\n");
break;
}
if (STRCMP(SEbuf, == , DataEnd))
{
printf("检测到数据终止符\n");
break;
}
SEbuf[2] = SEbuf[1];
SEbuf[1] = SEbuf[0];
}
else
{
if (Err == Err_PullOut)
{
return;
}
}
}
CloseHandle(Wait);
CloseHandle(SP.Hcom);
fclose(pSave);
fExit = 0;
}
void RDforCMD(int argc, char **argv)
{
if (argc < 5)
{
AErr = FewParameter;
return;
}
SerialPort SP = {
.ComName = { 'C', 'O', 'M' }
};
if (strlen(argv[2])>3)
{
AErr = SerialPortNameErr;
return;
}
sprintf(&SP.ComName[3], argv[2]);
DealStdStr(argv[3], &SP);
ReceiveData(argv[4], SP);
}