1
////////////////////////////////////////////////////////////////////////////////
2
/*! @file COMPortTelecom.cpp Short message service via serial port.
3
- Part of SMC_COM - Research Assistant Plugin package.
4
- Uses RAGUI - Research Assistant Graphical User Interface.
5
- Uses QtGui v.4.6 - http://qt.nokia.com/
6
- Uses RANet - Research Assistant Net Library (based on ANSI C++).
7
- Copyright(C) 2010, Viktor E. Bursian, St.Petersburg, Russia.
8
Viktor.Bursian@mail.ioffe.ru
9
- Copyright(C) 2010, Dmitry K. Nelson, St.Petersburg, Russia.
10
D.Nelson@mail.ioffe.ru
11
*///////////////////////////////////////////////////////////////////////////////
12
#include "COMPortTelecom.h"
14
#include "GeneralGUI.h"
17
//------------------------------------------------------------------------------
19
DEFINE_CLASS_TAG(sCOMPortTelecom)
21
//---------------------------------------------------------- sCOMPortTelecom ---
23
void sCOMPortTelecom::Preconfigure (sNodePtr DeviceCfg)
25
sCluster::sPtr DeviceSettings;
26
DeviceCfg |= "Settings";
27
DeviceSettings = DeviceCfg->GetAttr("Settings");
28
if( DeviceSettings.IsCorrect() ){
29
DeviceSettings |= attr("Name","COM1");
30
DeviceSettings |= attr("BaudRate",115200);
32
sTelecom::Preconfigure(DeviceCfg);
36
sCOMPortTelecom::~sCOMPortTelecom ()
46
sCOMPortTelecom::sCOMPortTelecom (sString DevicetId
48
:sTelecom(DevicetId,DeviceCfg)
52
,TerminalSymbols("\x0D\x0A")
54
Preconfigure(DeviceConfig);
55
if( DeviceSettings.IsCorrect() ){
59
S=DeviceSettings->GetAttr("Name"); //берём указатель на значение атрибута
60
if( S.IsCorrect() ) //проверяем, что значение есть и оно строковое
61
Name = sString2QString( (sString)(*S) ); //преобразуем его в QString
63
I=DeviceSettings->GetAttr("BaudRate");
67
Port = new AbstractSerial();
68
Port->setDeviceName(Name);
70
connect(Port, SIGNAL(readyRead()), this, SLOT(on_ReadyToRead()));
74
void sCOMPortTelecom::Connect ()
76
if( DriverState() == Offline ){
77
SetDriverState(Warming);
78
AbstractSerial::BaudRate BR;
79
if( BaudRate >= 115200 ){
80
BaudRate=115200; BR=AbstractSerial::BaudRate115200;
81
}else if( BaudRate >= 9600 ){
82
BaudRate=9600; BR=AbstractSerial::BaudRate9600;
83
}else if( BaudRate >= 1200 ){
84
BaudRate=1200; BR=AbstractSerial::BaudRate1200;
86
BaudRate=50; BR=AbstractSerial::BaudRate50;
88
if (Port->open(QIODevice::ReadWrite | QIODevice::Unbuffered)) {
90
if (!Port->setBaudRate(BR)) {
91
qDebug() << "Set baud rate " << BaudRate << " error.";
93
qDebug() << "Serial device " << Port->deviceName() << " open in " << Port->openMode();
94
qDebug() << "= Default parameters =";
95
qDebug() << "Device name : " << Port->deviceName();
96
qDebug() << "Baud rate : " << Port->baudRate();
97
qDebug() << "Data bits : " << Port->dataBits();
98
qDebug() << "Parity : " << Port->parity();
99
qDebug() << "Stop bits : " << Port->stopBits();
100
qDebug() << "Flow : " << Port->flowControl();
101
qDebug() << "Char timeout, msec : " << Port->charIntervalTimeout();
102
SetDriverState(Standby);
104
Port->setDtr(true); // This is set to switch on
105
Port->setRts(true); // power supply to ML327
110
qDebug() << "Error opened serial device " << Port->deviceName();
111
SetDriverState(Offline);
118
void sCOMPortTelecom::Disconnect ()
120
if( DriverState() != Offline ){
122
SetDriverState(Offline);
127
bool sCOMPortTelecom::SendMessage (const QByteArray & Msg)
129
QByteArray ba = Msg + TerminalSymbols; //byte array to write
130
long bw = 0; //bytes really written
131
bw = Port->write(ba);
132
// qDebug(" Bytes written - No Val - ");
133
// qDebug() << bw << ba;
135
return (ba.size() == bw);
138
void sCOMPortTelecom::on_ReadyToRead ()
140
long numBytes = Port->bytesAvailable();
141
bool Fin = !(numBytes > 0);
144
QByteArray ba = Port->read(numBytes);
145
// qDebug(" Bytes read - No Val - ");
146
// qDebug() << numBytes << ba;
147
InputString.append(ba);
148
while (InputString.contains(TerminalSymbols))
150
int Pos = InputString.indexOf(TerminalSymbols);
151
QByteArray msg = InputString.left(Pos);
152
InputString.remove(0, Pos + TerminalSymbols.size());
153
emit GotMessage(msg);
155
numBytes = Port->bytesAvailable();
156
Fin = !(numBytes > 0);
160
//------------------------------------------------------------------------------