forked from psi46/psi46test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prober.cpp
112 lines (86 loc) · 1.77 KB
/
prober.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
/* -------------------------------------------------------------
*
* file: prober.cpp
*
* description: connection to the Suess prober via RS232
*
* author: Beat Meier
* modified: 13.6.2016
*
* rev:
*
* -------------------------------------------------------------
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "rs232.h"
#include "prober.h"
const char defAnswer[] = " no response";
CProber::CProber()
{
rs232 = -1;
readback[0] = 0;
result = 0;
message = defAnswer;
}
bool CProber::Open (int portNr)
{
if (IsOpen()) return false;
rs232 = rs232_open(portNr, 9600, 'N', 8, 1, 0);
return rs232 >= 0;
}
void CProber::Close ()
{
if (!IsOpen()) return;
rs232_exit(rs232);
rs232 = -1;
return;
}
void CProber::Clear()
{
if (!IsOpen()) return;
rs232_clearRx(rs232);
readback[0] = 0;
}
void CProber::Read(int ms)
{
int cnt;
result = -1;
message = defAnswer;
cnt = rs232_gets(rs232, readback, sizeof(readback)-1, "\n", ms);
if (cnt>=2) readback[cnt-2] = 0; else { readback[0] = 0; return; }
message = strchr(readback, ':');
if (message == 0) { message = defAnswer; result = -1; return; }
message++;
while (*message == ' ') message++;
if (sscanf(readback, "%i", &result) != 1)
{ message = defAnswer; result = -1; return; }
}
int CProber::SendCmd(const char *fmt, ...)
{
if (!IsOpen()) return -2;
va_list ap;
char cmd[256];
Clear();
va_start(ap,fmt);
#ifdef _WIN32
_vsnprintf(cmd, 255, fmt, ap);
#else
vsnprintf(cmd, 255, fmt, ap);
#endif
va_end(ap);
rs232_puts(rs232,cmd);
rs232_puts(rs232,"\r\n");
Read();
if (result != 0) PrintErrorMsg();
return GetRsp();
}
void CProber::PrintErrorMsg()
{
if (result < 0)
printf(" RSP no response\n");
else
printf(" RSP %s\n", readback);
}