~d-nelson/research-assistant/nextDNK

« back to all changes in this revision

Viewing changes to RAPlugins/COMPort/COMPortTelecom.cpp

  • Committer: Viktor Bursian
  • Date: 2013-06-06 13:42:39 UTC
  • Revision ID: vbursian@gmail.com-20130606134239-bx5ks8sg3y9oqckz
Tags: version_0.2.0
first usable version 0.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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"
 
13
#include "NetOps.h"
 
14
#include "GeneralGUI.h"
 
15
#include <QDebug>
 
16
namespace RA {
 
17
//------------------------------------------------------------------------------
 
18
 
 
19
DEFINE_CLASS_TAG(sCOMPortTelecom)
 
20
 
 
21
//---------------------------------------------------------- sCOMPortTelecom ---
 
22
 
 
23
void  sCOMPortTelecom::Preconfigure (sNodePtr  DeviceCfg)
 
24
{
 
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);
 
31
  };
 
32
  sTelecom::Preconfigure(DeviceCfg);
 
33
};
 
34
 
 
35
 
 
36
sCOMPortTelecom::~sCOMPortTelecom ()
 
37
{
 
38
  if( Port ){
 
39
    Port->close();
 
40
    delete Port;
 
41
    Port = NULL;
 
42
  };
 
43
};
 
44
 
 
45
 
 
46
sCOMPortTelecom::sCOMPortTelecom (sString   DevicetId
 
47
                                 ,sNodePtr  DeviceCfg)
 
48
    :sTelecom(DevicetId,DeviceCfg)
 
49
    ,Name("COM1")
 
50
    ,BaudRate(115200)
 
51
    ,Port(NULL)
 
52
    ,TerminalSymbols("\x0D\x0A")
 
53
{
 
54
  Preconfigure(DeviceConfig);
 
55
  if( DeviceSettings.IsCorrect() ){
 
56
    sTextNode::sPtr             S;
 
57
    sIntegerNode::sPtr          I;
 
58
 
 
59
    S=DeviceSettings->GetAttr("Name");  //берём указатель на значение атрибута
 
60
    if( S.IsCorrect() )     //проверяем, что значение есть и оно строковое
 
61
      Name = sString2QString( (sString)(*S) ); //преобразуем его в QString
 
62
 
 
63
    I=DeviceSettings->GetAttr("BaudRate");
 
64
    if( I.IsCorrect() )
 
65
      BaudRate = (int)(*I);
 
66
  };
 
67
  Port = new AbstractSerial();
 
68
  Port->setDeviceName(Name);
 
69
  InputString.clear();
 
70
  connect(Port, SIGNAL(readyRead()), this, SLOT(on_ReadyToRead()));
 
71
};
 
72
 
 
73
 
 
74
void  sCOMPortTelecom::Connect ()
 
75
{
 
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;
 
85
    }else{
 
86
      BaudRate=50;  BR=AbstractSerial::BaudRate50;
 
87
    };
 
88
    if (Port->open(QIODevice::ReadWrite | QIODevice::Unbuffered)) {
 
89
 
 
90
      if (!Port->setBaudRate(BR)) {
 
91
          qDebug() << "Set baud rate " <<  BaudRate << " error.";
 
92
      };
 
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);
 
103
 
 
104
      Port->setDtr(true);  // This is set to switch on
 
105
      Port->setRts(true);  // power supply to ML327
 
106
 
 
107
      Port->reset();
 
108
    }
 
109
    else {
 
110
      qDebug() << "Error opened serial device " << Port->deviceName();
 
111
      SetDriverState(Offline);
 
112
    }
 
113
 
 
114
  };
 
115
};
 
116
 
 
117
 
 
118
void  sCOMPortTelecom::Disconnect ()
 
119
{
 
120
  if( DriverState() != Offline ){
 
121
    Port->close();
 
122
    SetDriverState(Offline);
 
123
  };
 
124
};
 
125
 
 
126
 
 
127
bool  sCOMPortTelecom::SendMessage (const QByteArray &  Msg)
 
128
{
 
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;
 
134
 
 
135
    return (ba.size() == bw);
 
136
};
 
137
 
 
138
void sCOMPortTelecom::on_ReadyToRead ()
 
139
{
 
140
   long numBytes = Port->bytesAvailable();
 
141
   bool Fin = !(numBytes > 0);
 
142
   while (!Fin)
 
143
   {
 
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))
 
149
     {
 
150
         int Pos = InputString.indexOf(TerminalSymbols);
 
151
         QByteArray msg = InputString.left(Pos);
 
152
         InputString.remove(0, Pos + TerminalSymbols.size());
 
153
         emit GotMessage(msg);
 
154
     }
 
155
     numBytes = Port->bytesAvailable();
 
156
     Fin = !(numBytes > 0);
 
157
   }
 
158
};
 
159
 
 
160
//------------------------------------------------------------------------------
 
161
}; //namespace RA
 
162