source: pacpussensors/trunk/Sick/SickSocket.cpp@ 132

Last change on this file since 132 was 99, checked in by nguyenhu, 9 years ago

compilation under linux with 0.2.X framework

File size: 2.7 KB
Line 
1/*********************************************************************
2// created: 2014/02/02
3// filename: SickSocket.cpp
4//
5// author: Cyril Fougeray
6// Copyright Heudiasyc UMR UTC/CNRS 6599
7//
8// version: $Id: $
9//
10// purpose: Management of the socket connection with Sick sensors
11*********************************************************************/
12
13#include "SickSocket.h"
14
15#include <QCoreApplication>
16#include <QDebug>
17
18
19#include "Pacpus/kernel/Log.h"
20
21namespace pacpus {
22
23DECLARE_STATIC_LOGGER("pacpus.base.SickSocket");
24
25
26SickSocket::SickSocket(AbstractSickSensor * parent)
27 : myParent(parent)
28{
29 socket = new QTcpSocket(this);
30 connect( socket, SIGNAL(connected()), this,SLOT(socketConnected()) );
31 connect( socket, SIGNAL(disconnected()),this, SLOT(socketConnectionClosed()) );
32 connect( socket, SIGNAL(readyRead()),this, SLOT(socketReadyRead()) );
33 connect( socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)) );
34}
35
36
37SickSocket::~SickSocket()
38{
39 delete socket;
40}
41
42
43void SickSocket::connectToServer(QString host, int port)
44{
45 qDebug("trying to connect to server");
46 socket->connectToHost(host,port);
47}
48
49
50int SickSocket::socketConnected()
51{
52 qDebug() << "we are connected to the server " << socket->peerName().toLatin1()
53 << " at the port " << socket->peerPort() << " via the socket " << socket->socketDescriptor();
54 emit configuration();
55
56 return 1;
57}
58
59//////////////////////////////////////////////////////////////////////////
60/// protected slot
61void SickSocket::socketConnectionClosed()
62{
63 qDebug("the connection was closed");
64}
65
66
67void SickSocket::socketReadyRead()
68{
69 SickFrame * frame = new SickFrame();
70 frame->time = road_time();
71 frame->size = socket->bytesAvailable();
72 frame->msg = new char[frame->size];
73
74 if (!frame->msg) {
75 LOG_FATAL("cannot allocate memory of size " << frame->size
76 << " - file: " << __FILE__
77 << " line: " << __LINE__
78 );
79 }
80
81 frame->size = socket->read(frame->msg, (qint64) frame->size);
82
83 SickFrameEvent *e = new SickFrameEvent;
84 e->frame = frame;
85 QCoreApplication::postEvent((QObject*)myParent, e);
86}
87
88
89void SickSocket::socketError(QAbstractSocket::SocketError e)
90{
91 qWarning("socket error number %d",e);
92}
93
94
95void SickSocket::sendToServer(QString data) //a adapter aux données binaires
96{
97 mutex.lock();
98 QTextStream os(socket);
99 os << data.toLatin1() << endl;//change in qt5
100 mutex.unlock();
101 qDebug() << "data sent to server: " << data.toLatin1() ; //a adapter aussi
102}
103
104} // namespace pacpus
Note: See TracBrowser for help on using the repository browser.