EspiaSerialLine.cpp 2.94 KB
Newer Older
1
/***************************************************************//**
2
3
4
5
6
7
8
 * @file   EspiaSerialLine.cpp
 * @brief  This file contains the Espia::SerialLine class implementation
 *
 * @author A.Kirov, A.Homs
 * @date   03/06/2009
 *******************************************************************/

kirov's avatar
kirov committed
9
#include "EspiaSerialLine.h"
kirov's avatar
kirov committed
10
#include "Espia.h"
kirov's avatar
kirov committed
11

12
using namespace lima::Espia;
kirov's avatar
kirov committed
13
14
using namespace std;

kirov's avatar
kirov committed
15
#define CHECK_CALL(ret)		ESPIA_CHECK_CALL(ret)
kirov's avatar
kirov committed
16

kirov's avatar
kirov committed
17

18
19
20
21
22
SerialLine::SerialLine(Dev& edev, const string& line_term, 
		       double timeout, int block_size, 
		       double block_delay) 
	: HwSerialLine(line_term, timeout, block_size, block_delay),
	  m_dev(edev)
kirov's avatar
kirov committed
23
{
24
	DEB_CONSTRUCTOR();
25
	DEB_PARAM() << DEB_VAR1(edev.getDevNb());
26
27
28

	if (edev.isMeta()) {
		DEB_ERROR() << "Specified Dev is a meta-device";
29
		throw LIMA_HW_EXC(NotSupported, "Can't create a "
30
		                          "SerialLine for a meta-device");
31
32
33
34
35
	}

	ostringstream os;
	os << "Serial#" << edev.getDevNb();
	DEB_SET_OBJ_NAME(os.str());
kirov's avatar
kirov committed
36
37
38
}


39
SerialLine::~SerialLine()
kirov's avatar
kirov committed
40
{
41
	DEB_DESTRUCTOR();
kirov's avatar
kirov committed
42
43
44
}


45
46
47
48
49
50
Dev& SerialLine::getDev()
{
	return m_dev;
}


51
void SerialLine::write(const string& buffer, bool no_wait)
kirov's avatar
kirov committed
52
{
53
	DEB_MEMBER_FUNCT();
54
	DEB_PARAM() << DEB_VAR2(buffer, no_wait);
55

56
	unsigned long len = buffer.size();
kirov's avatar
kirov committed
57
58
59
60
	char *ptr = len ? (char *) buffer.data() : NULL;
	int block_size; getBlockSize(block_size);
	double block_delay; getBlockDelay(block_delay);

61
62
63
	DEB_TRACE() << "Calling espia_ser_write";
	CHECK_CALL(espia_ser_write(m_dev, ptr, &len, block_size, 
				   Sec2USec(block_delay), !no_wait));
kirov's avatar
kirov committed
64
65
66
}


67
void SerialLine::read(string& buffer, int max_len, double timeout)
kirov's avatar
kirov committed
68
{
69
	DEB_MEMBER_FUNCT();
70
	DEB_PARAM() << DEB_VAR2(max_len, timeout);
71

kirov's avatar
kirov committed
72
73
74
	buffer.resize(max_len);
	char *ptr = max_len ? (char *) buffer.data() : NULL;
	unsigned long ret_len = max_len;
75
	unsigned long tmout = Sec2USec(checkDefTimeout(timeout));
kirov's avatar
kirov committed
76

77
78
	DEB_TRACE() << "Calling espia_ser_read";
	CHECK_CALL(espia_ser_read(m_dev, ptr, &ret_len, tmout));
kirov's avatar
kirov committed
79
	buffer.resize(ret_len);
80

81
	DEB_RETURN() << DEB_VAR1(buffer);
kirov's avatar
kirov committed
82
83
84
}


85
86
void SerialLine::readStr(string& buffer, int max_len, 
			 const string& term, double timeout)
kirov's avatar
kirov committed
87
{
88
	DEB_MEMBER_FUNCT();
89
	DEB_PARAM() << DEB_VAR3(max_len, term, timeout);
90

kirov's avatar
kirov committed
91
92
93
94
	buffer.resize(max_len);
	char *ptr = max_len ? (char *) buffer.data() : NULL;
	char *term_ptr = (char *) term.data();
	unsigned long ret_len = max_len;
95
	unsigned long tmout = Sec2USec(checkDefTimeout(timeout));
kirov's avatar
kirov committed
96

97
98
99
	DEB_TRACE() << "Calling espia_ser_read_str";
	CHECK_CALL(espia_ser_read_str(m_dev, ptr, &ret_len, term_ptr,
				      term.size(), tmout));
kirov's avatar
kirov committed
100
	buffer.resize(ret_len);
101

102
	DEB_RETURN() << DEB_VAR1(buffer);
kirov's avatar
kirov committed
103
104
105
}


106
void SerialLine::flush()
kirov's avatar
kirov committed
107
{
108
109
	DEB_MEMBER_FUNCT();
	DEB_TRACE() << "Calling espia_ser_flush";
kirov's avatar
kirov committed
110
	CHECK_CALL(espia_ser_flush(m_dev));
kirov's avatar
kirov committed
111
112
113
}


114
void SerialLine::getNbAvailBytes(int& avail_bytes)
kirov's avatar
kirov committed
115
{
116
117
	DEB_MEMBER_FUNCT();
	DEB_TRACE() << "Calling espia_ser_read";
kirov's avatar
kirov committed
118
119
120
	unsigned long ret_bytes = 0;
	CHECK_CALL(espia_ser_read(m_dev, NULL, &ret_bytes, 0));
	avail_bytes = ret_bytes;
121
	DEB_RETURN() << DEB_VAR1(avail_bytes);
kirov's avatar
kirov committed
122
}