14 #include "charinterface.h"
17 #include "xnodeconnector.h"
24 #include "dummyport.h"
27 #define SNPRINT_BUF_SIZE 128
31 XCustomCharInterface::XCustomCharInterface(
const char *name,
bool runtime,
const shared_ptr<XDriver> &driver) :
36 XCustomCharInterface::setEOS(
const char *str) {
44 bool addednull =
false;
45 if(
buffer().back() !=
'\0') {
55 ret = vsscanf( &
buffer()[0], fmt, ap);
64 XCustomCharInterface::toDouble()
const throw (XConvError &) {
66 int ret = this->
scanf(
"%lf", &x);
68 throw XConvError(__FILE__, __LINE__);
72 XCustomCharInterface::toInt()
const throw (XConvError &) {
74 int ret = this->
scanf(
"%d", &x);
76 throw XConvError(__FILE__, __LINE__);
80 XCustomCharInterface::toUInt()
const throw (XConvError &) {
82 int ret = this->
scanf(
"%u", &x);
84 throw XConvError(__FILE__, __LINE__);
89 XCustomCharInterface::toStr()
const {
94 return QString( &
buffer()[0]).simplified();
97 XCustomCharInterface::query(
const XString &str)
throw (XCommError &) {
104 int buf_size = SNPRINT_BUF_SIZE;
105 std::vector<char> buf;
107 buf.resize(buf_size);
112 ret = vsnprintf(&buf[0], buf_size, fmt, ap);
116 if(ret < 0)
throw XConvError(__FILE__, __LINE__);
117 if(ret < buf_size)
break;
126 XCustomCharInterface::query(
const char *str)
throw (XCommError &) {
134 int buf_size = SNPRINT_BUF_SIZE;
135 std::vector<char> buf;
137 buf.resize(buf_size);
142 ret = vsnprintf(&buf[0], buf_size, fmt, ap);
146 if(ret < 0)
throw XConvError(__FILE__, __LINE__);
147 if(ret < buf_size)
break;
152 this->query(&buf[0]);
156 XCharInterface::XCharInterface(
const char *name,
bool runtime,
const shared_ptr<XDriver> &driver) :
159 m_bGPIBUseSerialPollOnWrite(true),
160 m_bGPIBUseSerialPollOnRead(true),
161 m_gpibWaitBeforeWrite(1),
162 m_gpibWaitBeforeRead(2),
163 m_gpibWaitBeforeSPoll(1),
165 m_serialBaudRate(9600),
167 m_serialParity(PARITY_NONE),
168 m_serial7Bits(false),
169 m_serialFlushBeforeWrite(true),
170 m_serialHasEchoBack(false),
172 m_script_query(create<
XStringNode>(
"Query", true)) {
176 tr[ *
device()].add(
"GPIB");
179 tr[ *
device()].add(
"SERIAL");
182 tr[ *
device()].add(
"TCP/IP");
184 tr[ *
device()].add(
"DUMMY");
186 m_lsnOnSendRequested = tr[ *m_script_send].onValueChanged().connectWeakly(
187 shared_from_this(), &XCharInterface::onSendRequested);
188 m_lsnOnQueryRequested = tr[ *m_script_query].onValueChanged().connectWeakly(
189 shared_from_this(), &XCharInterface::onQueryRequested);
194 XCharInterface::open() throw (XInterfaceError &) {
198 shared_ptr<XPort>
port;
200 if(shot[ *
device()].to_str() ==
"GPIB") {
202 port->setEOS(eos().c_str());
206 if(shot[ *
device()].to_str() ==
"SERIAL") {
208 const char *seos = eos().length() ? eos().c_str() : serialEOS().c_str();
213 if(shot[ *
device()].to_str() ==
"TCP/IP") {
215 port->setEOS(eos().c_str());
218 if(shot[ *
device()].to_str() ==
"DUMMY") {
220 port->setEOS(eos().c_str());
224 throw XOpenInterfaceError(__FILE__, __LINE__);
236 XCharInterface::send(
const XString &str)
throw (XCommError &) {
237 this->send(str.c_str());
241 XCharInterface::send(
const char *str)
throw (XCommError &) {
244 dbgPrint(driver()->getLabel() +
" Sending:\"" + dumpCString(str) +
"\"");
247 catch (XCommError &e) {
248 e.print(driver()->getLabel() + i18n(
" SendError, because "));
253 XCharInterface::write(
const char *sendbuf,
int size)
throw (XCommError &) {
256 dbgPrint(driver()->getLabel() + formatString(
" Sending %d bytes", size));
257 m_xport->write(sendbuf, size);
259 catch (XCommError &e) {
260 e.print(driver()->getLabel() + i18n(
" SendError, because "));
265 XCharInterface::receive() throw (XCommError &) {
268 dbgPrint(driver()->
getLabel() +
" Receiving...");
271 dbgPrint(driver()->
getLabel() +
" Received;\"" +
272 dumpCString((
const char*)&
buffer()[0]) +
"\"");
274 catch (XCommError &e) {
275 e.print(driver()->
getLabel() + i18n(
" ReceiveError, because "));
280 XCharInterface::receive(
unsigned int length)
throw (XCommError &) {
283 dbgPrint(driver()->getLabel() + QString(
" Receiving %1 bytes...").arg(length));
284 m_xport->receive(length);
285 dbgPrint(driver()->getLabel() + QString(
" %1 bytes Received.").arg(buffer().size()));
287 catch (XCommError &e) {
288 e.print(driver()->getLabel() + i18n(
" ReceiveError, because "));
296 throw XInterfaceError(i18n(
"Port is not opened."), __FILE__, __LINE__);
303 throw XInterfaceError(i18n(
"Port is not opened."), __FILE__, __LINE__);
304 this->query(shot[ *m_script_query].to_str());
307 tr.unmark(m_lsnOnQueryRequested);