UdpServer::UdpServer(QObject *parent) :
    QObject(parent)
{
    mUdpSocket = new QUdpSocket(this);
    mSerialPort = new SerialPort(this);
    mTimer = new QTimer(this);
    mTimer->setInterval(10);
    mTimer->start();

    mPortPath = "/dev/ttyO2";
    mUdpPort = 27800;
    mBaudrate = 115200;
    mSendArray.clear();

    mPayloadLength = 0;
    mRxDataPtr = 0;
    mCrcLow = 0;
    mCrcHigh = 0;

    connect(mUdpSocket, SIGNAL(readyRead()),
            this, SLOT(readPendingDatagrams()));
    connect(mSerialPort, SIGNAL(serial_port_error(int)), this, SLOT(serialPortError(int)));
    connect(mSerialPort, SIGNAL(serial_data_available()), this, SLOT(serialDataAvilable()));
    connect(mTimer, SIGNAL(timeout()), this, SLOT(timerSlot()));
}
示例#2
0
void SerialPort::run()
{
    unsigned char buffer[1024];
    int res = 0;
    int failed_reads = 0;
    fd_set set;
    timespec timeout;
    
    while (false == mAbort)
    {
        FD_ZERO(&set); /* clear the set */
        FD_SET(mFd, &set); /* add our file descriptor to the set */
        timeout.tv_sec = 0;
        timeout.tv_nsec = 10000000;
        res = pselect(mFd + 1, &set, NULL, NULL, &timeout, NULL);
        
        if(res < 0)
        {
            qWarning() << "Select failed in read thread";
        } else if(res == 0)
        {
            // Timeout
        } else
        {
            res = read(mFd, buffer, 1024);
            if (res > 0)
            {
                failed_reads = 0;
                
                QMutexLocker locker(&mMutex);
                for (int i = 0;i < res;i++)
                {
                    if (mCaptureBytes > 0)
                    {
                        if (mCaptureBuffer != 0)
                        {
                            mCaptureBuffer[mCaptureWrite] = buffer[i];
                            mCaptureWrite++;
                        }
                        mCaptureBytes--;
                        if (mCaptureBytes == 0)
                        {
                            mCondition.wakeOne();
                        }
                    } else
                    {
                        mReadBuffer[mBufferWrite] = buffer[i];
                        mBufferWrite++;
                        //qDebug() << (quint8)buffer[i];
                        
                        if (mBufferWrite == mBufferSize)
                        {
                            mBufferWrite = 0;
                        }
                        Q_EMIT serial_data_available();
                    }
                }
            } else
            {
                if (res < 0) {
                    qCritical().nospace() << "Reading failed. MSG: " << strerror(errno);
                } else {
                    qCritical().nospace() << "Reading serial port returned 0";
                }
                
                failed_reads++;
                
                if (failed_reads > 3) {
                    QMutexLocker locker(&mMutex);
                    if (mIsOpen) {
                        close(mFd);
                        mIsOpen = false;
                    }
                    mAbort = true;
                    qCritical().nospace() << "Too many consecutive failed reads. Closing port.";
                    Q_EMIT serial_port_error(res);
                    return;
                }
            }
        }
    }
}
示例#3
0
void ta(int pi)
{
   int h, b, e;
   char *TEXT;
   char text[2048];

   printf("Serial link tests.\n");

   /* this test needs RXD and TXD to be connected */

   h = serial_open(pi, "/dev/ttyAMA0", 57600, 0);

   CHECK(10, 1, h, 0, 0, "serial open");

   time_sleep(0.1); /* allow time for transmission */

   b = serial_read(pi, h, text, sizeof(text)); /* flush buffer */

   b = serial_data_available(pi, h);
   CHECK(10, 2, b, 0, 0, "serial data available");

   TEXT = "\
To be, or not to be, that is the question-\
Whether 'tis Nobler in the mind to suffer\
The Slings and Arrows of outrageous Fortune,\
Or to take Arms against a Sea of troubles,\
";
   e = serial_write(pi, h, TEXT, strlen(TEXT));
   CHECK(10, 3, e, 0, 0, "serial write");

   e = serial_write_byte(pi, h, 0xAA);
   e = serial_write_byte(pi, h, 0x55);
   e = serial_write_byte(pi, h, 0x00);
   e = serial_write_byte(pi, h, 0xFF);

   CHECK(10, 4, e, 0, 0, "serial write byte");

   time_sleep(0.1); /* allow time for transmission */

   b = serial_data_available(pi, h);
   CHECK(10, 5, b, strlen(TEXT)+4, 0, "serial data available");

   b = serial_read(pi, h, text, strlen(TEXT));
   CHECK(10, 6, b, strlen(TEXT), 0, "serial read");
   if (b >= 0) text[b] = 0;
   CHECK(10, 7, strcmp(TEXT, text), 0, 0, "serial read");

   b = serial_read_byte(pi, h);
   CHECK(10, 8, b, 0xAA, 0, "serial read byte");

   b = serial_read_byte(pi, h);
   CHECK(10, 9, b, 0x55, 0, "serial read byte");

   b = serial_read_byte(pi, h);
   CHECK(10, 10, b, 0x00, 0, "serial read byte");

   b = serial_read_byte(pi, h);
   CHECK(10, 11, b, 0xFF, 0, "serial read byte");

   b = serial_data_available(pi, h);
   CHECK(10, 12, b, 0, 0, "serial data availabe");

   e = serial_close(pi, h);
   CHECK(10, 13, e, 0, 0, "serial close");
}
示例#4
0
Logger::Logger(QObject *parent) :
    QObject(parent)
{
    mPort = new SerialPort(this);
    mPacketInterface = new PacketInterface(this);

    mValueFile = new QFile("Data/BLDC_Values");
    mPrintFile = new QFile("Data/BLDC_Print");

    mValueFile->open(QIODevice::WriteOnly | QIODevice::Text);
    mPrintFile->open(QIODevice::WriteOnly | QIODevice::Text);

    mValueStream = new QTextStream(mValueFile);
    mPrintStream = new QTextStream(mPrintFile);

    mPort->openPort("/dev/ttyACM0");

    // Video
    mVidW = 1280;
    mVidH = 720;
    mVidFps = 25.0;
    mFAudioSamp = 44100;

    mFrameGrabber = new FrameGrabber(mVidW, mVidH, mVidFps, 0, this);
    mFrameGrabber->start(QThread::InheritPriority);
    mPlotter = new FramePlotter(this);
    mPlotter->start(QThread::InheritPriority);

    mCoder = new VideoCoder(mVidW, mVidH, mVidFps, "Data/v_video.avi", this);
    mCoder->start(QThread::InheritPriority);

    // Audio recording
    mTimer = 0;
    mAudio = 0;

    if (QAudioDeviceInfo::availableDevices(QAudio::AudioInput).size() > 0) {
        mAudioFile.setFileName("Data/v_audio.raw");
        mAudioFile.open(QIODevice::WriteOnly | QIODevice::Truncate);

        QAudioFormat format;
        // Set up the desired format, for example:
        format.setSampleRate(mFAudioSamp);
        format.setChannelCount(1);
        format.setSampleSize(8);
        format.setCodec("audio/pcm");
        format.setByteOrder(QAudioFormat::LittleEndian);
        format.setSampleType(QAudioFormat::UnSignedInt);

        QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
        if (!info.isFormatSupported(format)) {
            qWarning() << "Default format not supported, trying to use the nearest.";
            format = info.nearestFormat(format);
        }

        mAudio = new QAudioInput(format, this);
        mAudio->setNotifyInterval(1000 / mVidFps);
        mAudio->start(&mAudioFile);
    } else {
        mTimer = new QTimer(this);
        mTimer->setInterval(1000 / mVidFps);
        mTimer->start();
    }

    mConsoleReader = new ConsoleReader(this);

    connect(mConsoleReader, SIGNAL(textReceived(QString)),
            this, SLOT(consoleLineReceived(QString)));

    connect(mPort, SIGNAL(serial_data_available()),
            this, SLOT(serialDataAvailable()));

    if (mTimer != 0) {
        connect(mTimer, SIGNAL(timeout()), this, SLOT(timerSlot()));
    }

    if (mAudio != 0) {
        connect(mAudio, SIGNAL(notify()),
                this, SLOT(audioNotify()));

        // Lower the volume to avoid clipping. This seems to be passed to
        // pulseaudio.
        mAudio->setVolume(0.1);
    }

    connect(mPacketInterface, SIGNAL(dataToSend(QByteArray&)),
            this, SLOT(packetDataToSend(QByteArray&)));
    connect(mPacketInterface, SIGNAL(valuesReceived(PacketInterface::MC_VALUES)),
            this, SLOT(mcValuesReceived(PacketInterface::MC_VALUES)));
    connect(mPacketInterface, SIGNAL(printReceived(QString)),
            this, SLOT(printReceived(QString)));
    connect(mPacketInterface, SIGNAL(samplesReceived(QByteArray)),
            this, SLOT(samplesReceived(QByteArray)));
    connect(mPacketInterface, SIGNAL(rotorPosReceived(double)),
            this, SLOT(rotorPosReceived(double)));
    connect(mPacketInterface, SIGNAL(experimentSamplesReceived(QVector<double>)),
            this, SLOT(experimentSamplesReceived(QVector<double>)));

    connect(mPlotter, SIGNAL(frameReady(QImage)),
            mCoder, SLOT(setNextFrame(QImage)));
}