void NfcPeerToPeer::initClientSocket()
{
    if (!m_useConnectionLess && !m_connectClientSocket) {
        // If using only one socket for connection-oriented, do not initialize
        // a client (second) socket here. The server will already wait for incoming
        // connections.
        return;
    }

    if (m_nfcClientSocket) {
        delete m_nfcClientSocket;
    }

    qDebug() << "Creating new client socket";
    // The NFC client socket (in case of connectionless the only one)
    m_nfcClientSocket = new QLlcpSocket(this);
    connect(m_nfcClientSocket, SIGNAL(readyRead()), this, SLOT(readTextClient()));
    connect(m_nfcClientSocket, SIGNAL(disconnected()), this, SLOT(clientSocketDisconnected()));
    connect(m_nfcClientSocket, SIGNAL(error(QLlcpSocket::SocketError)), this, SLOT(clientSocketError(QLlcpSocket::SocketError)));
    connect(m_nfcClientSocket, SIGNAL(stateChanged(QLlcpSocket::SocketState)), this, SLOT(clientSocketStateChanged(QLlcpSocket::SocketState)));
    //connect(nfcClientSocket, SIGNAL(bytesWritten(qint64)))

    if (m_useConnectionLess) {
        m_nfcClientSocket->bind(m_nfcPort);
    } else {
        qDebug() << "Connecting client socket to service";
        #if defined(MEEGO_EDITION_HARMATTAN)
            // On Harmattan, already connect the client socket to the URI.
            // On Symbian, the connection can also be opened when the target is found.
            m_nfcClientSocket->connectToService(0, m_nfcUri);
        #endif
    }
    qDebug() << "NfcPeerToPeer::initClientSocket() finished";
}
Exemple #2
0
/**
*函数介绍:初始化客户端
*输入参数:无
*返回值:无
*/
void Network::initClient()
{
	//写入系统日志
	//Global::systemLog->append(QString(tr("初始化网络客户端:%1.....")).arg(clientName), clientName + tr("客户端初始化."), SystemLog::LOG);


	//新建一个客户端socket
	clientSocket = new TcpSocket();
	//初始化它的永久描述符,在断开后,未删除前仍有效
	clientSocket->setSocketDescriptorEternal(clientSocket->socketDescriptor());
	//信号槽:跟踪clientSocket状态
	connect(clientSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(getClientSocketState(QAbstractSocket::SocketState)));
	//信号槽: 客户端接收远程计算机数据
	connect(clientSocket, SIGNAL(haveReadDataSignal(QByteArray, int)), this, SLOT(clientReceiveData(QByteArray, int)));
	//信号槽: 客户端连接服务器成功
	connect(clientSocket, SIGNAL(connectedSignal(int)), this, SLOT(clientSocketConnectSuccess(int)));
	//信号槽: 客户端关闭与服务器连接
	connect(clientSocket, SIGNAL(disconnectedBeforeDeleteSignal(int)), this, SLOT(clientSocketDisconnect(int)));
	//信号槽: 客户端socket出现错误
	connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(clientSocketError(QAbstractSocket::SocketError)));

	connect(clientSocket, SIGNAL(disconnected()), this, SLOT(disconnectSlot()) );

	//写入系统日志
	//Global::systemLog->append(QString(tr("初始化网络客户端:%1结束.")).arg(clientName), clientName + tr("客户端初始化结束."), SystemLog::LOG);
}
//--------------------------------------------------------------------------------------
// Mwars::Mwars()
// the Constructor
//--------------------------------------------------------------------------------------
Mwars::Mwars(QWidget *parent) : QMainWindow(parent), ui(new Ui::mwarsForm)  {

    ui->setupUi(this);  // Ui:mwarsForm ui

    //*********** two timers
    QTimer *keepServerAliveTimer = new QTimer(this);  // instantiate timer
    QTimer *testTimer = new QTimer(this);


    // a. TCP stream to scanner server
    // clientSocket: client socket sends stream to scanner. clientSocket already exists
    // Q_SIGNALS signals emitted by socket  unimplemented: void hostFound() void connected()
    connect( &clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
            this, SLOT(clientSocketError(QAbstractSocket::SocketError)) );
    connect( &clientSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
            this, SLOT(clientSocketState(QAbstractSocket::SocketState)) );

    connect( &clientSocket, SIGNAL(readyRead()),this, SLOT(OnReadyRead()) );
    connect( &clientSocket, SIGNAL(disconnected()), this, SLOT(disconnectedz()) );

    // b. UDP stream from opencv / camera
    listenSocket = new QUdpSocket ( this );
    listenSocket->bind(9988 );
    connect( listenSocket, SIGNAL(readyRead()),this, SLOT(OnReadyRead()) );

    //signals from within code
    connect( this, SIGNAL(connectScanner()), this, SLOT(connectToScanner()));  // Constructor
    connect( keepServerAliveTimer, SIGNAL(timeout()), this, SLOT(keepServerAlive()));

//    connect( testTimer, SIGNAL(timeout()), this, SLOT(testSignal()));

    // signals from ui
    connect( ui->xSlider, SIGNAL(valueChanged(int)), this, SLOT(sendScanCmd()));
    connect( ui->ySlider, SIGNAL(valueChanged(int)), this, SLOT(sendScanCmd()));
    connect( ui->spinboxXCamTrim, SIGNAL(valueChanged(int)), this, SLOT(sendScanCmd()));
    connect( ui->spinboxYCamTrim, SIGNAL(valueChanged(int)), this, SLOT(sendScanCmd()));

    //connect( ui->pbLaserToggle, SIGNAL(clicked()), this, SLOT(laserToggle()));
    connect( ui->pushButtonLaserDisableToggle, SIGNAL(clicked()), this, SLOT(laserDisableToggle()));
    connect( ui->pushButtonScannerToggle, SIGNAL(clicked()), this, SLOT(scannerToggle()));
    connect( ui->pushButtonConnect, SIGNAL(clicked()), this, SLOT(connectToScanner()));
    connect( ui->radioButtonVerboseOn, SIGNAL(clicked()), this, SLOT(verboseOn()));
    connect( ui->radioButtonVerboseOff, SIGNAL(clicked()), this, SLOT(verboseOff()));
    connect( ui->pushButtonResetScanner, SIGNAL(clicked()), this, SLOT(resetScanner()));
    connect( ui->pushButtonCameraDataToggle, SIGNAL(clicked()), this, SLOT(cameraDataToggle()));
    connect( ui->pushButtonTestDataToggle, SIGNAL(clicked()), this, SLOT(testDataToggle()));
    //connect( ui->pbAlignBS, SIGNAL(clicked()), this, SLOT(alignBoresight()));

    connect( ui->pbCalibrateToggle, SIGNAL(clicked()), this, SLOT(calibrateToggle()));
    connect( ui->pbCalSetULHC, SIGNAL(clicked()), this, SLOT(calSetULHC()));
    connect( ui->pbCalSetLRHC, SIGNAL(clicked()), this, SLOT(calSetLRHC()));
    connect( ui->pbCalFullFrame, SIGNAL(clicked()), this, SLOT(calFullFrame()));
    connect( ui->pbCalHalfFrame, SIGNAL(clicked()), this, SLOT(calHalfFrame()));

    connect( ui->spinboxXCamTrim, SIGNAL(valueChanged(int)), this, SLOT(xCamTrim()));
    connect( ui->spinboxYCamTrim, SIGNAL(valueChanged(int)), this, SLOT(yCamTrim()));
    connect( ui->pbCamTrimOffset, SIGNAL(clicked()), this, SLOT(camTrimOffset()));

    connect( ui->sbXCamOffset, SIGNAL(valueChanged(int)), this, SLOT(xCamOffset()));
    connect( ui->sbYCamOffset, SIGNAL(valueChanged(int)), this, SLOT(yCamOffset()));
    connect( ui->pbCamOffset, SIGNAL(clicked()), this, SLOT(camScanOffset()));

    connect( ui->pbCalLaserBsight, SIGNAL(clicked()), this, SLOT(alignBoresight()));
    //connect( this, SIGNAL (testSignal()), this, SLOT (testSignal()));


    // temporary stuff to test parallax offset
    g_xLenWallFOV_mm = (1310 * 2);  // measured width, x axis, of FOV at target distance = range (mm)
    g_yLenWallFOV_mm = (975 * 2);
    g_xSF_pixelTomm =  (float)g_xLenWallFOV_mm / CAMERA_X_PIXELS;
    g_ySF_pixelTomm =  (float)g_yLenWallFOV_mm / CAMERA_Y_PIXELS;
    range_mm         = 2750;          // measured range cam to target (mm)


    //overwritten when cam file is read
    g_xScanFOVOrigin_su = DAC_X_MIN;		// scanner origin corresponding to x_camFOVOrigin (scan units)
    g_xScanFOVMax_su    = DAC_X_MAX;		// scanner max x corresponding to x_camFOVMax (scan units)

    // read camera to scanner calibration file. This reads the calibration parameters
    // and sets main slider and spin box values
    readXMLFile();

    ui->spinboxXCamTrim->setRange(-5000, +5000);
    ui->spinboxYCamTrim->setRange(-5000, +5000);
    ui->radioButtonVerboseOff->setChecked(true);

    clearStateWidets();				// clear Server State widget text

#ifdef FORCE_ENABLE_WIDGETS
    enableWidgets();
#else
     disableWidgets();				// control widgets get enabled when connected
#endif

    verbose = false;                // verbose OFF
    laserOnFlag = false;
    testDataEnableFlag = false;     // set by TestData button to turn on test data stream
    cameraDataEnableFlag = false;   // set by Camera Data button to turn on OpenCVcamera data stream
    laserDisableFlag = false;       // set by Laser On/Off button
    scannerOnFlag = false;          // set by Scanner On/Off button
    tcpConnected = false;           // tcp connected
    serverReady = false;
    calibrateFlag = false;

    setWindowTitle(tr("MWARS SCANNER CONSOLE"));



    // =================== start scannerserver comms ===============================
    //  a) UDP stream of co-ords and laser command from OpenCV Project mwars
    //  b) TCP connection to server in the scanner unit

    // 1. connect to the TCP server in the laser scanner unit
    emit connectScanner();              //runs connectToScanner


    // ==================== timers ==================================
    // start the keep alivetimer. Used to get response to update Console
    keepServerAliveTimer->start(1000);  //1000 = 1 sec

    //start the test signal timer for testSignal()
    // 1000 = 1 sec, 1 = 1mS
    //testTimer->start(100);             // 5 Hz
    //testTimer->start(10);              // 50 Hz
    //testTimer->start(20);              // 25 Hz
    // testTimer->start(60);              //  8 Hz
    // testTimer->start(50);              //  10 Hz

    sendScanCmd();     // init scanner, slew to slider values

    rectangleIsRunning = false;
}