> > Port série

Port série

tags : port série communication qt c++

Le port série est un moyen simple de communiquer avec le monde extérieur depuis un PC. S'il est en voie de disparition sur les PC portables, on retrouve souvent un connecteur interne sur les cartes mères ou sur les mini PC.

Code Qt (toutes plates-formes)

Qt > 5.1

#include <QSerialPort>
#include <QSerialPortInfo>

void connect()
{
	foreach (QSerialPortInfo info, QSerialPortInfo::availablePorts())
	{
		if(info.manufacturer().contains("Microchip")) _port = new QSerialPort(info.portName());
	}
	if(_port!=NULL)
	{
		_port->open(QIODevice::ReadWrite);
		_port->setDataBits(QSerialPort::Data8);
		_port->setBaudRate(QSerialPort::Baud115200);
		_port->setParity(QSerialPort::NoParity);
		_port->setStopBits(QSerialPort::OneStop);
		_port->setFlowControl(QSerialPort::NoFlowControl);
	}
	else
	{
		qDebug()<<"unable to found interface driver";
	}
}

_port->write((char*)trame, trame_lenght);	// envoie trame_lenght octets de trame

Qt < 5.1 : QExtSerialPort

Code Unix

#ifndef __COM_H__
#define __COM_H__

#include <sys/select.h>

class Com
{
public:
	Com(const unsigned char num);
	~Com();

	int send(const char buff[], int size);
	int receive(char buff[], int size);
	int read_block(char buff[], int size);

	void waitfordata(void);

	int getnumbyte(void);
private:
	int fd;

	int max_fd;
	fd_set input;
};

#endif // __COM_H__
#include "com.h"

#include <stdio.h>     // Standard input/output definitions
#include <string.h>    // String function definitions
#include <unistd.h>    // UNIX standard function definitions
#include <fcntl.h>     // File control definitions
#include <errno.h>     // Error number definitions

#include <termios.h>   // POSIX terminal control definitions
#include <sys/ioctl.h> // Controle de ligne

#include <sys/time.h>

Com::Com(const unsigned char num)
{
	struct termios options;
	char port[]="/dev/ttyS ";	// attention diffère suivant la distribution unix
	port[9]=num+'0';

	fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
	if (fd == -1)
	{
		perror("Port ttyS indisponible");
	}

	tcgetattr(fd, &options);

	// reset options
	options.c_cflag = 0;
	options.c_lflag = 0;
	options.c_iflag = 0;
	options.c_oflag = 0;
	options.c_ispeed = 0;
	options.c_ospeed = 0;

	// set la vitesse
	cfsetispeed(&options, B115200);
	cfsetospeed(&options, B115200);

	// reste local, activation reception
	options.c_cflag |= (CLOCAL | CREAD);

	// 8 bits, 1 stop, 0 parity
	options.c_cflag &= ~PARENB;
	options.c_cflag &= ~CSTOPB;
	options.c_cflag &= ~CSIZE;
	options.c_cflag |= CS8;

	// disable flow control
	options.c_cflag &= ~CRTSCTS;

	//output options
	options.c_oflag = ONOCR;

	//output options
	options.c_iflag = (IGNPAR | IGNBRK);

	// mode raw
	options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

	// time out
	options.c_cc[VTIME] = 0;

	// mode non bloquant
	//fcntl(fd, F_SETFL, FNDELAY);
	fcntl(fd, F_SETFL, 0);

	// set les paramettres
	tcsetattr(fd, TCSANOW, &options);

	//init select
	max_fd = fd+1;
	FD_ZERO(&input);
	FD_SET(fd, &input);
}

Com::~Com()
{
	close(fd);
}

int Com::send(const char buff[], int size)
{
	return write(fd,buff,size);
}

int Com::receive(char buff[], int size)
{
	return read(fd,buff,size);
}

int Com::read_block(char buff[], int size)
{
	waitfordata();
	return read(fd,buff,size);
}

int Com::getnumbyte(void)
{
	int bytes;
	ioctl(fd, FIONREAD, &bytes);
	return bytes;
}

void Com::waitfordata(void)
{
	struct timeval timeout;
	timeout.tv_sec  = 0;
	timeout.tv_usec = 1;
	select(max_fd, &input, NULL, NULL, &timeout);
}