Ejemplo n.º 1
0
/**
 * @brief MainWindow::~MainWindow
 */
MainWindow::~MainWindow()
{
    if (fConnected) {
        HandleSerialConnection();
    }
    delete ui;
}
Ejemplo n.º 2
0
/**
 * @brief MainWindow::MainWindow
 * @param parent
 */
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    fConnected(false),
    bytesRequired(0),
    boardStatus(0)
{
    ui->setupUi(this);
    cbDevList = new QComboBox(ui->mainToolBar);
    cbDevList->setMinimumWidth(250);
    cbDevList->setEnabled(false);
    ui->mainToolBar->insertWidget(ui->actionHandleConnection, cbDevList);
    ui->mainToolBar->insertSeparator(ui->actionHandleConnection);
    lbI2CErrors = new QLabel(ui->statusBar);
    lbI2CErrors->setText("I2C Errors: 0");
    ui->statusBar->insertPermanentWidget(0, lbI2CErrors);

    serial = new QSerialPort(this);

    connect(ui->actionHandleConnection, SIGNAL(triggered()), this,
            SLOT(HandleSerialConnection()));
    connect(ui->actionRead, SIGNAL(triggered()), this, SLOT(HandleReadSettings()));
    connect(ui->actionSet, SIGNAL(triggered()), this, SLOT(HandleApplySettings()));
    connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(HandleSaveSettings()));
    connect(&timer, SIGNAL(timeout()), this, SLOT(ProcessTimeout()));
    connect(serial, SIGNAL(readyRead()), this, SLOT(ReadSerialData()));
    connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this,
            SLOT(HandleSerialError(QSerialPort::SerialPortError)));
    connect(ui->pushSensor1AccCalibrate, SIGNAL(clicked()), this, SLOT(HandleAccCalibrate()));
    connect(ui->pushSensor1GyroCalibrate, SIGNAL(clicked()), this, SLOT(HandleGyroCalibrate()));

    FillPortsInfo();

    for(int i = 0; i < 4; i++) {
        ui->comboPitchCommand->addItem(OutputCommands[i].item_name, OutputCommands[i].item_id);
        ui->comboRollCommand->addItem(OutputCommands[i].item_name, OutputCommands[i].item_id);
        ui->comboYawCommand->addItem(OutputCommands[i].item_name, OutputCommands[i].item_id);
    }
    ui->comboPitchCommand->setCurrentIndex(3);
    ui->comboRollCommand->setCurrentIndex(3);
    ui->comboYawCommand->setCurrentIndex(3);

    for(int i = 0; i < 5; i++) {
        ui->comboPitchDeadTime->addItem(OutputDeadTime[i].item_name, OutputDeadTime[i].item_id);
        ui->comboRollDeadTime->addItem(OutputDeadTime[i].item_name, OutputDeadTime[i].item_id);
        ui->comboYawDeadTime->addItem(OutputDeadTime[i].item_name, OutputDeadTime[i].item_id);
    }
    ui->comboPitchDeadTime->setCurrentIndex(4);
    ui->comboRollDeadTime->setCurrentIndex(4);
    ui->comboYawDeadTime->setCurrentIndex(4);

    for(int i = 0; i < 6; i++) {
        ui->comboInputChannelPitch->addItem(InputChannel[i].item_name, InputChannel[i].item_id);
        ui->comboInputChannelRoll->addItem(InputChannel[i].item_name, InputChannel[i].item_id);
        ui->comboInputChannelYaw->addItem(InputChannel[i].item_name, InputChannel[i].item_id);
    }
    ui->comboInputChannelPitch->setCurrentIndex(5);
    ui->comboInputChannelRoll->setCurrentIndex(5);
    ui->comboInputChannelYaw->setCurrentIndex(5);

    for(int i = 0; i < 3; i++) {
        ui->comboInputModePitch->addItem(InputMode[i].item_name, InputMode[i].item_id);
        ui->comboInputModeRoll->addItem(InputMode[i].item_name, InputMode[i].item_id);
        ui->comboInputModeYaw->addItem(InputMode[i].item_name, InputMode[i].item_id);
    }
    ui->comboInputModePitch->setCurrentIndex(0);
    ui->comboInputModeRoll->setCurrentIndex(0);
    ui->comboInputModeYaw->setCurrentIndex(0);

    for(int i = 0; i < 6; i++) {
        ui->comboSensor1AxisTOP->addItem(SensorAxis[i].item_name, SensorAxis[i].item_id);
        ui->comboSensor1AxisRIGHT->addItem(SensorAxis[i].item_name, SensorAxis[i].item_id);
    }
    ui->comboSensor1AxisTOP->setCurrentIndex(2);
    ui->comboSensor1AxisRIGHT->setCurrentIndex(3);

    ui->comboData->addItem(PlotData[0].item_name, PlotData[0].item_id);
    ui->comboData->setCurrentIndex(0);

    ui->plotData->addGraph();
     /* line color red for first graph. */
    ui->plotData->graph(0)->setPen(QPen(Qt::red));
    ui->plotData->addGraph();
    /* line color green for second graph. */
    ui->plotData->graph(1)->setPen(QPen(Qt::green));
    ui->plotData->addGraph();
    /* line color blue for third graph. */
    ui->plotData->graph(2)->setPen(QPen(Qt::blue));

    ui->plotData->xAxis->setTickLabelType(QCPAxis::ltDateTime);
    ui->plotData->xAxis->setDateTimeFormat("hh:mm:ss");
    ui->plotData->xAxis->setAutoTickStep(false);
    ui->plotData->xAxis->setTickStep(2);
    ui->plotData->axisRect()->setupFullAxesBox();

    ui->plotData->yAxis->setLabel("Attitude, deg");

    /* make left and bottom axes transfer their ranges to right and top axes: */
    connect(ui->plotData->xAxis, SIGNAL(rangeChanged(QCPRange)),
            ui->plotData->xAxis2, SLOT(setRange(QCPRange)));
    connect(ui->plotData->yAxis, SIGNAL(rangeChanged(QCPRange)),
            ui->plotData->yAxis2, SLOT(setRange(QCPRange)));

    connect(ui->checkDataX, SIGNAL(clicked()), this, SLOT(HandleDataXClicked()));
    connect(ui->checkDataY, SIGNAL(clicked()), this, SLOT(HandleDataYClicked()));
    connect(ui->checkDataZ, SIGNAL(clicked()), this, SLOT(HandleDataZClicked()));
}
Ejemplo n.º 3
0
int listen_com(char* portName, int baudRate, int stopBits, int parity, int byteSize, int timeout) {

	// adapted from: http://stackoverflow.com/questions/6947413/how-to-open-read-and-write-from-serial-port-in-c

	struct termios tty;
	int err = 0;

	int fd = open(portName, O_RDWR | O_NOCTTY | O_SYNC);
	if (fd < 0)
	{
		printf("error %d opening %s: %s", errno, portName, strerror (errno));
		err = OPDI_DEVICE_ERROR;
		goto FINISH;
	}

        memset (&tty, 0, sizeof tty);
        if (tcgetattr(fd, &tty) != 0)
        {
                printf("error %d from tcgetattr", errno);
		err = OPDI_DEVICE_ERROR;
		goto FINISH;
        }

	if (baudRate != -1)
	{
		// TODO calculate call constant from baud rate
		cfsetospeed(&tty, B9600);
		cfsetispeed(&tty, B9600);
	}

        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
        // disable IGNBRK for mismatched speed tests; otherwise receive break
        // as \000 chars
        tty.c_iflag &= ~IGNBRK;         // ignore break signal
        tty.c_lflag = 0;                // no signaling chars, no echo,
                                        // no canonical processing
        tty.c_oflag = 0;                // no remapping, no delays
        tty.c_cc[VMIN]  = (timeout < 1 ? 1 : 0);	// block if no timeout specified
        tty.c_cc[VTIME] = (timeout < 1 ? 0 : (timeout / 100));	// read timeout

        tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

        tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                        // enable reading
	if (parity == 0)
	        tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
	else
	if (parity == 1) {
	        tty.c_cflag |= PARENB | PARODD;		// parity odd
	} else
	if (parity == 2) {
	        tty.c_cflag &= ~(PARODD);		// parity even
		tty.c_cflag |= PARENB;
	}

	if (stopBits == 2) {
        	tty.c_cflag |= CSTOPB;
	} else 
	if (stopBits == 1) {
        	tty.c_cflag &= ~CSTOPB;
	}
	
	if (byteSize == 5) {
		tty.c_cflag &= ~CSIZE;
		tty.c_cflag |= CS5;
	} else
	if (byteSize == 6) {
		tty.c_cflag &= ~CSIZE;
		tty.c_cflag |= CS6;
	} else
	if (byteSize == 7) {
		tty.c_cflag &= ~CSIZE;
		tty.c_cflag |= CS7;
	} else
	if (byteSize == 8) {
		tty.c_cflag &= ~CSIZE;
		tty.c_cflag |= CS8;
	}

        tty.c_cflag &= ~CRTSCTS;

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
        {
                printf("error %d from tcsetattr", errno);
		err = OPDI_DEVICE_ERROR;
		goto FINISH;
        }

	// wait for connections

	char inputData;
	int bytesRead;

	while (true) {
		printf("listening for a connection on serial port %s\n", portName);
        
		while (true) {
			// try to read a byte
			if ((bytesRead = read(fd, &inputData, 1)) >= 0) {
				if (bytesRead > 0) {
					printf("Connection attempt on serial port\n");

					err = HandleSerialConnection(inputData, fd);
			
					fprintf(stderr, "Result: %d\n", err);
				}
			}
			else {
				// timeouts are expected here
				// TODO
			}
		}
	}

FINISH:

	// cleanup
	if (fd > 0) {
		close(fd);
	}

	return err;
}