FrelonSerialLine.h 4.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//###########################################################################
// This file is part of LImA, a Library for Image Acquisition
//
// Copyright (C) : 2009-2011
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This 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 software 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, see <http://www.gnu.org/licenses/>.
//###########################################################################
22
23
24
25
#ifndef FRELONSERIALLINE_H
#define FRELONSERIALLINE_H

#include "Frelon.h"
26
#include "ThreadUtils.h"
27
28
29
30
31
32
33
34
35

namespace lima
{

namespace Frelon
{

class SerialLine : public HwSerialLine
{
36
	DEB_CLASS_NAMESPC(DebModCameraCom, "SerialLine", "Frelon");
37

38
39
40
41
42
 public:
	enum MsgPart {
		MsgSync, MsgCmd, MsgVal, MsgReq, MsgTerm, 
	};
	
43
44
45
46
47
48
49
50
	enum AnsPart {
		AnsResp, AnsWarn, AnsErr,
	};
	
	enum {
		MaxReadLen = 10000,
	};

ahoms's avatar
ahoms committed
51
52
	typedef std::map<MsgPart, std::string> MsgPartStrMapType;

53
	static const double TimeoutSingle, TimeoutNormal, TimeoutMultiLine, 
54
55
56
			    TimeoutReset;
	
	SerialLine(Espia::SerialLine& espia_ser_line);
57
58
	virtual ~SerialLine();

59
60
	Espia::SerialLine& getEspiaSerialLine();

61
62
	virtual void write(const std::string& buffer, 
			   bool no_wait = false);
63
64
	virtual void read(std::string& buffer, 
			  int max_len = MaxReadLen, 
65
			  double timeout = TimeoutDefault);
66
67
	virtual void readStr(std::string& buffer, int max_len, 
			     const std::string& term, 
68
			     double timeout = TimeoutDefault);
69
70
	virtual void readLine(std::string& buffer, 
			      int max_len = MaxReadLen, 
71
			      double timeout = TimeoutDefault);
72
73
	virtual void readSingleLine(std::string& buffer, 
				    int max_len = MaxReadLen, 
74
				    double timeout = TimeoutDefault);
75
76
	virtual void readMultiLine(std::string& buffer, 
				   int max_len = MaxReadLen);
77
	
78
79
	virtual void flush();

80
	virtual void getNbAvailBytes(int &avail);
81
	
82
83
84
	virtual void setTimeout(double timeout);
	virtual void getTimeout(double& timeout) const;

85
	void splitMsg(const std::string& msg, 
ahoms's avatar
ahoms committed
86
		      MsgPartStrMapType& msg_parts) const;
87
88
89
90
	void decodeFmtResp(const std::string& ans, std::string& fmt_resp);

	void sendFmtCmd(const std::string& cmd, std::string& resp);

91
92
93
	void writeRegister(Reg reg, int  val);
	void readRegister (Reg reg, int& val);

94
	int getLastWarning();
95

96
97
98
99
	void clearCache();
	void setCacheActive(bool  cache_act);
	void getCacheActive(bool& cache_act);

100
 private:
101
102
103
	enum RegOp {
		None, DoCmd, ReadReg, WriteReg, DoReset, MultiRead
	};
104
	friend std::ostream& operator <<(std::ostream& os, RegOp op);
105

106
	typedef std::map<Reg, int> RegValMapType;
107
108
109
110
111
112
113
114
115

	AutoMutex lock(int mode);

	virtual void writeCmd(const std::string& buffer, 
			      bool no_wait = false);
	virtual void readResp(std::string& buffer, 
			      int max_len = MaxReadLen, 
			      double timeout = TimeoutDefault);

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
	class ReadRespCleanup {
	public:
		ReadRespCleanup(SerialLine& ser_line)
			: m_ser_line(ser_line)
		{}

		~ReadRespCleanup()
		{ m_ser_line.readRespCleanup(); }

	private:
		SerialLine& m_ser_line;
	};
	friend class RespCleanup;

	void readRespCleanup();

132
	bool isRegCacheable(Reg reg);
133
	bool getRegCacheVal(Reg reg, int& val);
134

135
136
	double getRegSleepTime(Reg reg);
 
137
	Espia::SerialLine& m_espia_ser_line;
138
	Cond m_cond;
139
	int m_last_warn;
140

141
	RegValMapType m_reg_cache;
142
143
144
145
146
147
	bool m_cache_act;
	RegOp m_curr_op;
	Reg m_curr_reg;
	bool m_curr_cache;
	std::string m_curr_resp;
	std::string m_curr_fmt_resp;
148
149
};

150
151
152
std::ostream& operator <<(std::ostream& os, SerialLine::RegOp op);


153
154
155
156
157
inline AutoMutex SerialLine::lock(int mode)
{
	return AutoMutex(m_cond.mutex(), mode);
}

158
159
160
161
162
163
164
165
166

} // namespace Frelon



} // namespace lima


#endif // FRELONSERIALLINE_H