示例#1
0
void NetworkManager::SerialSetting::fromMap(const QVariantMap &setting)
{
    if (setting.contains(QLatin1String(NM_SETTING_SERIAL_BAUD))) {
        setBaud(setting.value(QLatin1String(NM_SETTING_SERIAL_BAUD)).toUInt());
    }

    if (setting.contains(QLatin1String(NM_SETTING_SERIAL_BITS))) {
        setBits(setting.value(QLatin1String(NM_SETTING_SERIAL_BITS)).toUInt());
    }

    if (setting.contains(QLatin1String(NM_SETTING_SERIAL_PARITY))) {
        QChar character = setting.value(QLatin1String(NM_SETTING_SERIAL_PARITY)).toChar();

        if (character == 'n') {
            setParity(NoParity);
        } else if (character == 'E') {
            setParity(EvenParity);
        } else if (character == 'o') {
            setParity(OddParity);
        }
    }

    if (setting.contains(QLatin1String(NM_SETTING_SERIAL_STOPBITS))) {
        setStopbits(setting.value(QLatin1String(NM_SETTING_SERIAL_STOPBITS)).toUInt());
    }

    if (setting.contains(QLatin1String(NM_SETTING_SERIAL_SEND_DELAY))) {
        setSendDelay((Setting::SecretFlagType)setting.value(QLatin1String(NM_SETTING_SERIAL_SEND_DELAY)).toULongLong());
    }
}
示例#2
0
bool serial::setFormat (format_t fmt)
{
	bool r = true;

	switch(fmt) {
	case serial::Format7E1:
		r &= setDatabits(serial::DB7);
		r &= setStopbits(serial::SB1);
		r &= setParity(serial::ParityEven);
		break;
	case serial::Format7O1:
		r &= setDatabits(serial::DB7);
		r &= setStopbits(serial::SB1);
		r &= setParity(serial::ParityOdd);
		break;
	case serial::Format8N1:
	default:
		r &= setDatabits(serial::DB8);
		r &= setStopbits(serial::SB1);
		r &= setParity(serial::ParityNone);
		break;
	}

	return r;
}
示例#3
0
SerialPort::SerialPort(const string device)
	: m_fd(0), m_baudRate(0), m_wordSize(0), m_asyncTimeout(DEFAULT_ASYNC_TIMEOUT)
{
	if((m_fd = ::open(device.c_str(), O_RDWR | O_NOCTTY)) < 0)
		throw SysException();

	::bzero(&m_term, sizeof(m_term));

	::tcgetattr(m_fd, &m_termOld);

	setBaudRate(DEFAULT_BAUD_RATE);
	setWordSize(DEFAULT_WORD_SIZE);
	setParity(DEFAULT_PARITY);

	m_term.c_cflag |= CLOCAL | CREAD;
	m_term.c_iflag = IGNPAR;
	m_term.c_oflag = OPOST;
	m_term.c_lflag = 0;

	m_term.c_cc[VTIME] = 0;
	m_term.c_cc[VMIN] = 1;

	setAttr();

	if(::fcntl(m_fd, F_SETOWN, getpid()) == -1)
		throw SysException();
}
示例#4
0
static int openTouchDev( char const *devName ){
   int fd = -1 ;
   if( !isSerial(devName) ){
      fd = open(devName, O_RDONLY);
   }
   else {
      printf( "Serial touch screen\n" );
      char const *end = strchr( devName, ',' );
      if( 0 == end )
         end = devName + strlen(devName);
      unsigned nameLen = end-devName ;
      printf( "nameLen: %u, end %p\n", nameLen, end );
      char deviceName[512];
      if( nameLen < sizeof(deviceName) ){
         memcpy( deviceName, devName, nameLen );
         deviceName[nameLen] = '\0' ;
         unsigned baud = 9600 ;
         unsigned databits = 8 ;
         char parity = 'N' ;
         unsigned stop = 1 ;
         if( '\0' != *end ){
            end++ ;
            baud = 0 ; 
            while( isdigit(*end) ){
               baud *= 10 ;
               baud += ( *end-'0' );
               end++ ;
            }

            if( ',' == *end ){
               end++ ;
               databits = *end-'0' ;
               end++ ;
               if( ',' == *end ){
                  end++ ;
                  parity = *end++ ;
                  if( ',' == *end ){
                     stop = end[1] - '0';
                  }
               }
            }
         }
         fd = open( deviceName, O_RDWR );
         if( 0 < fd ){
            printf( "settings: %s,%u,%u,%c,%u\n", deviceName, baud, databits, parity, stop );
            setBaud( fd, baud );
            setRaw( fd );
            setDataBits( fd, databits );
            setStopBits( fd, stop );
            setParity( fd, parity );
         }
         else
            perror( deviceName );
      }
      else
         fprintf( stderr, "Invalid touch device name\n" );
   }

   return fd ;
}
/*!
\fn bool Posix_QextSerialPort::open(int=0)
Opens a serial port.  Note that this function does not specify which device to open.  If you need
to open a device by name, see Posix_QextSerialPort::open(const char*).  This function has no 
effect if the port associated with the class is already open.  The port is also configured to the 
current settings, as stored in the Settings structure.
*/
bool Posix_QextSerialPort::open(int) {
    LOCK_MUTEX();
    if (!portOpen) {

        /*open the port*/
        Posix_File->setName(portName);
        if (Posix_File->open(IO_Async|IO_Raw|IO_ReadWrite)) {
            portOpen=true;
        }

        /*configure port settings*/
        tcgetattr(Posix_File->handle(), &Posix_CommConfig);

        /*set up other port settings*/
        Posix_CommConfig.c_cflag|=CREAD|CLOCAL;
        Posix_CommConfig.c_lflag&=(~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG));
        Posix_CommConfig.c_iflag&=(~(INPCK|IGNPAR|PARMRK|ISTRIP|IXANY));
        Posix_CommConfig.c_oflag&=(~OPOST);
        Posix_CommConfig.c_cc[VMIN]=0;
        Posix_CommConfig.c_cc[VINTR] = _POSIX_VDISABLE; 
        Posix_CommConfig.c_cc[VQUIT] = _POSIX_VDISABLE; 
        Posix_CommConfig.c_cc[VSTART] = _POSIX_VDISABLE; 
        Posix_CommConfig.c_cc[VSTOP] = _POSIX_VDISABLE; 
        Posix_CommConfig.c_cc[VSUSP] = _POSIX_VDISABLE; 
        setBaudRate(Settings.BaudRate);
        setDataBits(Settings.DataBits);
        setStopBits(Settings.StopBits);
        setParity(Settings.Parity);
        setFlowControl(Settings.FlowControl);
        setTimeout(Posix_Copy_Timeout.tv_sec, Posix_Copy_Timeout.tv_usec);
        tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);
    }
    UNLOCK_MUTEX();
    return portOpen;
}
示例#6
0
bool QSerialPort::open(int block)
{
	if( block== 1) portDesc = ::open(portName.toAscii(),O_RDWR);
	else portDesc = ::open(portName.toAscii(),O_RDWR | O_NONBLOCK );
	if(portDesc == -1)
	{
		return false;
	}
	portOpen = true;

	tcgetattr(portDesc, &portConfig);
	
	portConfig.c_cflag|=(CLOCAL | CREAD);
	portConfig.c_lflag&=(~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG));
	portConfig.c_iflag&=(~(IGNBRK|BRKINT|PARMRK|ISTRIP |INLCR| IGNCR|ICRNL|IXON)); //  (~(INPCK|IGNPAR|PARMRK|ISTRIP|IXANY));
	portConfig.c_iflag&=~(ICRNL);

	portConfig.c_oflag&=(~OPOST);
	
	tcsetattr(portDesc, TCSANOW, &portConfig);

	setBaudRate(settings.baudRate);
	setDataBits(settings.dataBits);
	setFlowControl(settings.flowControl);
	setParity(settings.parity);
	setStopBits(settings.stopBits);
	setDtr();
	setRts();

	connect(&portFile, SIGNAL(readyRead()), this, SLOT(slotNotifierActivated()));

	return true;
}
示例#7
0
void
SerialPort::setSerial(int baud, int dataBits, int parity, int stopBits)
{
    setBaud(baud);
    setDataBits(dataBits);
    setParity(parity);
    setStopBits(stopBits);
}
/*!
\fn bool Win_QextSerialPort::open(OpenMode mode)
Opens a serial port.  Note that this function does not specify which device to open.  If you need
to open a device by name, see Win_QextSerialPort::open(const char*).  This function has no effect
if the port associated with the class is already open.  The port is also configured to the current
settings, as stored in the Settings structure.
*/
bool Win_QextSerialPort::open(OpenMode mode) {
	unsigned long confSize = sizeof(COMMCONFIG);
	Win_CommConfig.dwSize = confSize;
	DWORD dwFlagsAndAttributes = 0;
	if (queryMode() == QextSerialBase::EventDriven)
		dwFlagsAndAttributes += FILE_FLAG_OVERLAPPED;

    LOCK_MUTEX();
    if (mode == QIODevice::NotOpen)
        return isOpen();
    if (!isOpen()) {
        /*open the port*/
        Win_Handle=CreateFileA(port.toLatin1(), GENERIC_READ|GENERIC_WRITE,
                              FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, dwFlagsAndAttributes, NULL);
        if (Win_Handle!=INVALID_HANDLE_VALUE)
        {
            /*configure port settings*/
            GetCommConfig(Win_Handle, &Win_CommConfig, &confSize);
            GetCommState(Win_Handle, &(Win_CommConfig.dcb));

            /*set up parameters*/
            Win_CommConfig.dcb.fBinary=TRUE;
            Win_CommConfig.dcb.fInX=FALSE;
            Win_CommConfig.dcb.fOutX=FALSE;
            Win_CommConfig.dcb.fAbortOnError=FALSE;
            Win_CommConfig.dcb.fNull=FALSE;
            setBaudRate(Settings.BaudRate);
            setDataBits(Settings.DataBits);
            setStopBits(Settings.StopBits);
            setParity(Settings.Parity);
            setFlowControl(Settings.FlowControl);
            setTimeout(Settings.Timeout_Millisec);
            SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));

            //init event driven approach
			if (queryMode() == QextSerialBase::EventDriven) {
		        Win_CommTimeouts.ReadIntervalTimeout = MAXDWORD;
		        Win_CommTimeouts.ReadTotalTimeoutMultiplier = 0;
		        Win_CommTimeouts.ReadTotalTimeoutConstant = 0;
				Win_CommTimeouts.WriteTotalTimeoutMultiplier = 0;
				Win_CommTimeouts.WriteTotalTimeoutConstant = 0;
				SetCommTimeouts(Win_Handle, &Win_CommTimeouts);
            	if (!SetCommMask( Win_Handle, EV_TXEMPTY | EV_RXCHAR | EV_DSR)) {
            		qWarning("Failed to set Comm Mask. Error code: %ld", GetLastError());
					UNLOCK_MUTEX();
            		return false;
            	}
            	overlapThread->start();
            }
			QIODevice::open(mode);
        }
    } else {
		UNLOCK_MUTEX();
    	return false;
    }
    UNLOCK_MUTEX();
    return isOpen();
}
示例#9
0
文件: Serial.cpp 项目: thomaswihl/wos
void Serial::config(uint32_t speed, Serial::Parity parity, Serial::WordLength dataBits, Serial::StopBits stopBits, HardwareFlowControl hardwareFlow)
{
    disable(Device::Part::All);
    setSpeed(speed);
    setParity(parity);
    setWordLength(dataBits);
    setStopBits(stopBits);
    setHardwareFlowControl(hardwareFlow);
}
示例#10
0
/*! \brief SerialDevice::stopDevice Is the serial interface's implementation of setDefaults.
 * Sets the serial devices to the default values. (Rate=9600, Parity=None, Flow=None, Data=8, Stop=1)
 */
void SerialDevice::setDefaults()
{
    statusReady = false;
    setBaudRate(9600);
    setParity(0);
    setFlowControl(0);
    setDataBits(8);
    setStopBits(1);
}
示例#11
0
/*!
Sets the port settigns.
*/
void QextSerialPort::setPortSetting(const PortSettings& settings)
{
    setBaudRate(settings.BaudRate);
    setDataBits(settings.DataBits);
    setParity(settings.Parity);
    setStopBits(settings.StopBits);
    setFlowControl(settings.FlowControl);
    setTimeout(settings.Timeout_Millisec);
}
示例#12
0
/*!
		\fn Win_QextSerialPort::Win_QextSerialPort(const PortSettings& settings)
			Constructs a port with default name and specified settings.
*/
Win_QextSerialPort::Win_QextSerialPort(const PortSettings& settings) {
	Win_Handle=INVALID_HANDLE_VALUE;
	setBaudRate(settings.BaudRate);
	setDataBits(settings.DataBits);
	setStopBits(settings.StopBits);
	setParity(settings.Parity);
	setFlowControl(settings.FlowControl);
	setTimeout(settings.Timeout_Sec, settings.Timeout_Millisec);
}
示例#13
0
/*!
Opens the serial port associated to this class.
This function has no effect if the port associated with the class is already open.
The port is also configured to the current settings, as stored in the Settings structure.
*/
bool QextSerialPort::open(OpenMode mode)
{
    QMutexLocker lock(mutex);
    if (mode == QIODevice::NotOpen)
        return isOpen();
    if (!isOpen()) {
        qDebug() << "trying to open file" << port.toAscii();
        //note: linux 2.6.21 seems to ignore O_NDELAY flag
        if ((fd = ::open(port.toAscii() ,O_RDWR | O_NOCTTY | O_NDELAY)) != -1) {
            qDebug("file opened succesfully");

            setOpenMode(mode);              // Flag the port as opened
            tcgetattr(fd, &old_termios);    // Save the old termios
            Posix_CommConfig = old_termios; // Make a working copy


            /* the equivelent of cfmakeraw() to enable raw access */
#ifdef HAVE_CFMAKERAW
            cfmakeraw(&Posix_CommConfig);   // Enable raw access
#else
            Posix_CommConfig.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
                                    | INLCR | IGNCR | ICRNL | IXON);
            Posix_CommConfig.c_oflag &= ~OPOST;
            Posix_CommConfig.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
            Posix_CommConfig.c_cflag &= ~(CSIZE | PARENB);
            Posix_CommConfig.c_cflag |= CS8;
#endif

            /*set up other port settings*/
            Posix_CommConfig.c_cflag|=CREAD|CLOCAL;
            Posix_CommConfig.c_lflag&=(~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG));
            Posix_CommConfig.c_iflag&=(~(INPCK|IGNPAR|PARMRK|ISTRIP|ICRNL|IXANY));
            Posix_CommConfig.c_oflag&=(~OPOST);
            Posix_CommConfig.c_cc[VMIN]= 0;
#ifdef _POSIX_VDISABLE  // Is a disable character available on this system?
            // Some systems allow for per-device disable-characters, so get the
            //  proper value for the configured device
            const long vdisable = fpathconf(fd, _PC_VDISABLE);
            Posix_CommConfig.c_cc[VINTR] = vdisable;
            Posix_CommConfig.c_cc[VQUIT] = vdisable;
            Posix_CommConfig.c_cc[VSTART] = vdisable;
            Posix_CommConfig.c_cc[VSTOP] = vdisable;
            Posix_CommConfig.c_cc[VSUSP] = vdisable;
#endif //_POSIX_VDISABLE
            setBaudRate(Settings.BaudRate);
            setDataBits(Settings.DataBits);
            setParity(Settings.Parity);
            setStopBits(Settings.StopBits);
            setFlowControl(Settings.FlowControl);
            setTimeout(Settings.Timeout_Millisec);
            tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig);

            if (queryMode() == QextSerialPort::EventDriven) {
                readNotifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
                connect(readNotifier, SIGNAL(activated(int)), this, SIGNAL(readyRead()));
            }
示例#14
0
/*!
Opens a serial port.  Note that this function does not specify which device to open.  If you need
to open a device by name, see QextSerialPort::open(const char*).  This function has no effect
if the port associated with the class is already open.  The port is also configured to the current
settings, as stored in the Settings structure.
*/
bool QextSerialPort::open(OpenMode mode) {
    unsigned long confSize = sizeof(COMMCONFIG);
    Win_CommConfig.dwSize = confSize;
    DWORD dwFlagsAndAttributes = 0;
    if (queryMode() == QextSerialPort::EventDriven)
        dwFlagsAndAttributes += FILE_FLAG_OVERLAPPED;

    QMutexLocker lock(mutex);
    if (mode == QIODevice::NotOpen)
        return isOpen();
    if (!isOpen()) {
        /*open the port*/
        Win_Handle=CreateFileA(port.toAscii(), GENERIC_READ|GENERIC_WRITE,
                              0, NULL, OPEN_EXISTING, dwFlagsAndAttributes, NULL);
        if (Win_Handle!=INVALID_HANDLE_VALUE) {
            QIODevice::open(mode);
            /*configure port settings*/
            GetCommConfig(Win_Handle, &Win_CommConfig, &confSize);
            GetCommState(Win_Handle, &(Win_CommConfig.dcb));

            /*set up parameters*/
            Win_CommConfig.dcb.fBinary=TRUE;
            Win_CommConfig.dcb.fInX=FALSE;
            Win_CommConfig.dcb.fOutX=FALSE;
            Win_CommConfig.dcb.fAbortOnError=FALSE;
            Win_CommConfig.dcb.fNull=FALSE;
            setBaudRate(Settings.BaudRate);
            setDataBits(Settings.DataBits);
            setStopBits(Settings.StopBits);
            setParity(Settings.Parity);
            setFlowControl(Settings.FlowControl);
            setTimeout(Settings.Timeout_Millisec);
            SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));

            //init event driven approach
            if (queryMode() == QextSerialPort::EventDriven) {
                Win_CommTimeouts.ReadIntervalTimeout = MAXDWORD;
                Win_CommTimeouts.ReadTotalTimeoutMultiplier = 0;
                Win_CommTimeouts.ReadTotalTimeoutConstant = 0;
                Win_CommTimeouts.WriteTotalTimeoutMultiplier = 0;
                Win_CommTimeouts.WriteTotalTimeoutConstant = 0;
                SetCommTimeouts(Win_Handle, &Win_CommTimeouts);
                if (!SetCommMask( Win_Handle, EV_TXEMPTY | EV_RXCHAR | EV_DSR)) {
                    qWarning() << "failed to set Comm Mask. Error code:", GetLastError();
                    return false;
                }
                winEventNotifier = new QWinEventNotifier(overlap.hEvent, this);
                connect(winEventNotifier, SIGNAL(activated(HANDLE)), this, SLOT(onWinEvent(HANDLE)));
                WaitCommEvent(Win_Handle, &eventMask, &overlap);
            }
        }
    } else {
        return false;
    }
    return isOpen();
}
/*!
\fn Posix_QextSerialPort::Posix_QextSerialPort(const char* name, const PortSettings& settings)
Constructs a port with specified name and settings.
*/
Posix_QextSerialPort::Posix_QextSerialPort(const char* name, const PortSettings& settings)
                     :QextSerialBase(name) {
    construct();
    setBaudRate(settings.BaudRate);
    setDataBits(settings.DataBits);
    setStopBits(settings.StopBits);
    setParity(settings.Parity);
    setFlowControl(settings.FlowControl);
    setTimeout(settings.Timeout_Sec, settings.Timeout_Millisec);
}
示例#16
0
NetworkManager::SerialSetting::SerialSetting(const Ptr &other):
    Setting(other),
    d_ptr(new SerialSettingPrivate())
{
    setBaud(other->baud());
    setBits(other->bits());
    setParity(other->parity());
    setStopbits(other->stopbits());
    setSendDelay(other->sendDelay());
}
示例#17
0
/*!
 * \brief SerialDev::configPort - Parametri per configurare la porta seriale
 * \return true se riesce a configurare correttamente la porta seriale
 */
bool SerialDev::configPort (const QString &name)
{
    bool debugVal = m_debug;
    m_debug = true;
    setPortName(name);

    if (!open(QIODevice::ReadWrite)) {
        QString testo = QString("Can't open %1, error code %2")
                    .arg(portName()).arg(error());
        debug(testo);
        return false;
    }

    if (!setBaudRate(QSerialPort::Baud115200)) {
        QString testo = QString("Can't set rate 115200 baud to port %1, error code %2")
                     .arg(portName()).arg(error());
        debug(testo);
        return false;
    }

    if (!setDataBits(QSerialPort::Data8)) {
        QString testo = QString("Can't set 8 data bits to port %1, error code %2")
                     .arg(portName()).arg(error());
        debug(testo);
        return false;
    }

    if (!setParity(QSerialPort::NoParity)) {
        QString testo = QString("Can't set no patity to port %1, error code %2")
                     .arg(portName()).arg(error());
        debug(testo);
        return false;
    }

    if (!setStopBits(QSerialPort::OneStop)) {
        QString testo = QString("Can't set 1 stop bit to port %1, error code %2")
                     .arg(portName()).arg(error());
        debug(testo);
        return false;
    }

    if (!setFlowControl(QSerialPort::NoFlowControl)) {
        QString testo = QString("Can't set no flow control to port %1, error code %2")
                     .arg(portName()).arg(error());
        debug(testo);
        return false;
    }

    connect(this, SIGNAL(error(QSerialPort::SerialPortError)),
            this, SLOT(errorSlot(QSerialPort::SerialPortError)));
    connect(this, SIGNAL(readyRead()), this, SLOT(fromDeviceSlot()));

    m_debug = debugVal;
   return true;
}
/*!
\fn bool Posix_QextSerialPort::open(OpenMode mode)
Opens the serial port associated to this class.
This function has no effect if the port associated with the class is already open.
The port is also configured to the current settings, as stored in the Settings structure.
*/
bool Posix_QextSerialPort::open(OpenMode mode)
{
    LOCK_MUTEX();
    if (mode == QIODevice::NotOpen)
    {
        UNLOCK_MUTEX();
        return isOpen();
    }

    if (!isOpen())
    {
        /*open the port*/
        Posix_File->setFileName(port);
        QueueReceiveSignals = 10;
        if (Posix_File->open(QIODevice::ReadWrite | QIODevice::Unbuffered))
        {
            /*set open mode*/
            QIODevice::open(mode);

            /*configure port settings*/
            tcgetattr(Posix_File->handle(), &Posix_CommConfig);

            /*set up other port settings*/
            Posix_CommConfig.c_cflag|=CREAD|CLOCAL;
            Posix_CommConfig.c_lflag&=(~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG));
            Posix_CommConfig.c_iflag&=(~(INPCK|IGNPAR|IGNBRK|PARMRK|ISTRIP|ICRNL|IXANY));
            Posix_CommConfig.c_oflag&=(~OPOST);
            Posix_CommConfig.c_cc[VMIN]=0;
            Posix_CommConfig.c_cc[VINTR] = _POSIX_VDISABLE;
            Posix_CommConfig.c_cc[VQUIT] = _POSIX_VDISABLE;
            Posix_CommConfig.c_cc[VSTART] = _POSIX_VDISABLE;
            Posix_CommConfig.c_cc[VSTOP] = _POSIX_VDISABLE;
            Posix_CommConfig.c_cc[VSUSP] = _POSIX_VDISABLE;
            setBaudRate(Settings.BaudRate);
            setDataBits(Settings.DataBits);
            setParity(Settings.Parity);
            setStopBits(Settings.StopBits);
            setFlowControl(Settings.FlowControl);
            //setTimeout(Settings.Timeout_Sec, Settings.Timeout_Millisec);
            setTimeout(Settings.Timeout_Millisec);
            tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);

            handle = Posix_File->handle();
            readerThread->handle = handle;
            readerThread->shutdown = false;
            readerThread->start();
        }
        else
        {
            qDebug("Could not open File! Error code : %d", Posix_File->error());
        }
    }
    UNLOCK_MUTEX();
    return isOpen();
}
示例#19
0
			/*
			 * Initialize Uart HAL Peripheral
			 *
			 * Enables clocks, the UART peripheral (but neither TX nor RX)
			 * Sets baudrate and parity.
			 */
			static void
			initialize(uint32_t baudrate, Parity parity = Parity::Disabled)
			{
				enable();
				// DIRTY HACK: disable and reenable uart to be able to set
				//             baud rate as well as parity
				USART2->CR1 &= ~USART_CR1_UE;	// Uart Disable
				setBaudrate(baudrate);
				setParity(parity);
				USART2->CR1 |=  USART_CR1_UE;	// Uart Reenable
			}
示例#20
0
/*!
\fn Win_QextSerialPort::Win_QextSerialPort(const PortSettings& settings)
Constructs a port with default name and specified settings.
*/
Win_QextSerialPort::Win_QextSerialPort(const PortSettings& settings, QextSerialBase::QueryMode mode) {
    Win_Handle=INVALID_HANDLE_VALUE;
    setBaudRate(settings.BaudRate);
    setDataBits(settings.DataBits);
    setStopBits(settings.StopBits);
    setParity(settings.Parity);
    setFlowControl(settings.FlowControl);
    setTimeout(settings.Timeout_Millisec);
    setQueryMode(mode);
    init();
}
/*!
\fn Posix_QextSerialPort::Posix_QextSerialPort(const PortSettings& settings)
Constructs a port with default name and specified settings.
*/
Posix_QextSerialPort::Posix_QextSerialPort(const PortSettings& settings)
 : QextSerialBase()
{
    setBaudRate(settings.BaudRate);
    setDataBits(settings.DataBits);
    setParity(settings.Parity);
    setStopBits(settings.StopBits);
    setFlowControl(settings.FlowControl);

    Posix_File=new QFile();
    setTimeout(settings.Timeout_Sec, settings.Timeout_Millisec);
}
示例#22
0
void QextSerialPortPrivate::setPortSettings(const PortSettings &settings, bool update)
{
    setBaudRate(settings.BaudRate, false);
    setDataBits(settings.DataBits, false);
    setStopBits(settings.StopBits, false);
    setParity(settings.Parity, false);
    setFlowControl(settings.FlowControl, false);
    setTimeout(settings.Timeout_Millisec, false);
    settingsDirtyFlags = DFE_ALL;
    if (update && q_func()->isOpen())
        updatePortSettings();
}
/*!
\fn Posix_QextSerialPort::Posix_QextSerialPort(const PortSettings& settings)
Constructs a port with default name and specified settings.
*/
Posix_QextSerialPort::Posix_QextSerialPort(const PortSettings& settings, QextSerialBase::QueryMode mode)
    : QextSerialBase()
{
    setBaudRate(settings.BaudRate);
    setDataBits(settings.DataBits);
    setParity(settings.Parity);
    setStopBits(settings.StopBits);
    setFlowControl(settings.FlowControl);

    setTimeout(settings.Timeout_Millisec);
    setQueryMode(mode);
    init();
}
示例#24
0
/*!
Constructs a port with default name and specified settings.
*/
QextSerialPort::QextSerialPort(const PortSettings& settings, QextSerialPort::QueryMode mode)
    : QIODevice()
{
    construct();
    setBaudRate(settings.BaudRate);
    setDataBits(settings.DataBits);
    setParity(settings.Parity);
    setStopBits(settings.StopBits);
    setFlowControl(settings.FlowControl);
    setTimeout(settings.Timeout_Millisec);
    setQueryMode(mode);
    platformSpecificInit();
}
示例#25
0
/*!
\fn Posix_QextSerialPort::construct(void)
Common constructor function, called by all versions of 
Posix_QextSerialPort::Posix_QextSerialPort().  Sets up default port settings (115200 8N1 
Hardware flow control where supported, otherwise no flow control, and 500 ms timeout).
*/
void Posix_QextSerialPort::construct(void) {
    QextSerialBase::construct();
#ifdef NOQFILE
    m_fdFile=-1;
#else
    Posix_File=new QFile();
#endif
    setBaudRate(BAUD115200);
    setDataBits(DATA_8);
    setStopBits(STOP_1);
    setParity(PAR_NONE);
    setFlowControl(FLOW_HARDWARE);
    setTimeout(0, 500);
}
示例#26
0
int main()
{ 
	alt_putstr("Hello from Nios II!\n");
	//alt_putstr("Input the char you would like to send.\n");
	//alt_putchar(*parallel_in);
	//alt_putstr("\n");
	//int c = alt_getchar();
	//alt_putstr("\n");
	int i = 0;
	char hello[20] = "Hello, world! abc123";

	while (i < 20) {

		*transmit_enable = 0x0;
		*load = 0x0;
		//*parallel_out = c;
		*data_bus_output = (hello[i]);
		//getBits(*parallel_out);
		//alt_putstr("\n*parallel_out\n");
		//alt_putchar((*parallel_out >> 1));
		//alt_putchar(*parallel_out);
		*data_bus_output = setParity(*data_bus_output);
		//getBits(*parallel_out);
		//alt_putchar(getCharacter(*parallel_out));
		//alt_putstr("\ngetCharacter(*parallel_out)\n");
		//alt_putchar(getCharacter(*parallel_out));
		usleep(1000);
		*transmit_enable = 0x1;
		*load = 0x1;
		usleep(50);
		*load = 0x0;
		while (!*character_received) {
		}
		while (!*character_sent) {
		}
		*transmit_enable = 0x0;

		/*if (getParity(*parallel_in)) {
			alt_putstr("bad parity\n");
		} else {
			alt_putstr("good parity\n");
		}*/
		//getBits(*parallel_in);
		alt_putchar(getCharacter(*data_bus_input));
		//alt_putstr("\n");
		i++;
	}

	return 0;
}
示例#27
0
int main( int argc, char const * const argv[] )
{
   if( 2 <= argc )
   {
      char const *const deviceName = argv[1];
      serialSignal_t ss( deviceName, handler );
      if( ss.isOpen() )
      {
         unsigned baud = ( 2 < argc ) ? strtoul( argv[2], 0, 0 ) : 115200 ;
         int rval = setBaud( ss.getFd(), baud );
         if( 0 == rval )
            debugPrint( "set baud to %u\n", baud );
         else
            printf( "error setting baud to %u\n", baud );

         unsigned dataBits = ( 3 < argc ) ? strtoul( argv[3], 0, 0 ) : 8 ;
         rval = setDataBits( ss.getFd(), dataBits );
         if( 0 == rval )
            debugPrint( "set data bits to %u\n", dataBits );
         else
            printf( "error setting data bits to %u\n", dataBits );

         char parity = ( 4 < argc ) ? toupper( *argv[4] ) : 'N' ;
         rval = setParity( ss.getFd(), parity );
         if( 0 == rval )
            debugPrint( "set parity to %c\n", parity );
         else
            printf( "error setting parity to %c\n", parity );

         unsigned stopBits = ( ( 5 < argc ) && ( '2' == *argv[5] ) ) ? 2  : 1 ;
         rval = setStopBits( ss.getFd(), stopBits );
         if( 0 == rval )
            debugPrint( "set stop bits to %u\n", stopBits );
         else
            printf( "error setting stop bits to %u\n", stopBits );

         debugPrint( "device opened\n" );
         while( 1 ){
            pause();
         }
      }
      else
         perror( deviceName );
   }
   else
      fprintf( stderr, "Usage: serialSignal deviceName [baud=115200 [databits=8 [parity=N]]]\n" );

   return 0 ;
}
/*!
\fn bool Posix_QextSerialPort::open(OpenMode mode)
Opens the serial port associated to this class.
This function has no effect if the port associated with the class is already open.
The port is also configured to the current settings, as stored in the Settings structure.
*/
bool Posix_QextSerialPort::open(OpenMode mode)
{
    LOCK_MUTEX();
    if (mode == QIODevice::NotOpen)
        return isOpen();
    if (!isOpen()) {
        /*open the port*/
        qDebug("trying to open file");
        //note: linux 2.6.21 seems to ignore O_NDELAY flag
        if ((fd = ::open(port.toAscii() ,O_RDWR | O_NOCTTY | O_NDELAY)) != -1) {
            qDebug("file opened succesfully");

            setOpenMode(mode);			// Flag the port as opened
            tcgetattr(fd, &old_termios);	// Save the old termios
            Posix_CommConfig = old_termios;	// Make a working copy
            cfmakeraw(&Posix_CommConfig);	// Enable raw access

            /*set up other port settings*/
            Posix_CommConfig.c_cflag|=CREAD|CLOCAL;
            Posix_CommConfig.c_lflag&=(~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG));
            Posix_CommConfig.c_iflag&=(~(INPCK|IGNPAR|PARMRK|ISTRIP|ICRNL|IXANY));
            Posix_CommConfig.c_oflag&=(~OPOST);
            Posix_CommConfig.c_cc[VMIN]= 0;
#ifdef _POSIX_VDISABLE	// Is a disable character available on this system?
            // Some systems allow for per-device disable-characters, so get the
            //  proper value for the configured device
            const long vdisable = fpathconf(fd, _PC_VDISABLE);
            Posix_CommConfig.c_cc[VINTR] = vdisable;
            Posix_CommConfig.c_cc[VQUIT] = vdisable;
            Posix_CommConfig.c_cc[VSTART] = vdisable;
            Posix_CommConfig.c_cc[VSTOP] = vdisable;
            Posix_CommConfig.c_cc[VSUSP] = vdisable;
#endif //_POSIX_VDISABLE
            setBaudRate(Settings.BaudRate);
            setDataBits(Settings.DataBits);
            setParity(Settings.Parity);
            setStopBits(Settings.StopBits);
            setFlowControl(Settings.FlowControl);
            setTimeout(Settings.Timeout_Millisec);
            tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig);
        } else {
            qDebug("could not open file: %s", strerror(errno));
        }
    }
    UNLOCK_MUTEX();
    return isOpen();
}
示例#29
0
bool SerialPort::initPort(const uchar initialBaudrate,
                                  const ulong timeout)
{
    uchar baudrate = initialBaudrate % 7;
    if (baudrate == 0)                // Adapt baud
    {
      setBaudRate(BAUD2400);
      qDebug() << "Baudrate" << "2.4k";
    }
    else if (baudrate == 1)
    {
      setBaudRate(BAUD4800);
      qDebug() << "Baudrate" << "4.8k";
    }
    else if (baudrate == 2)
    {
      setBaudRate(BAUD9600);
      qDebug() << "Baudrate" << "9.6k";
    }
    else if (baudrate == 3)
    {
      setBaudRate(BAUD19200);
      qDebug() << "Baudrate" << "19.2k";
    }
    else if (baudrate == 4)
    {
      setBaudRate(BAUD38400);
      qDebug() << "Baudrate" << "38.4k";
    }
    else if (baudrate == 5)
    {
      setBaudRate(BAUD57600);
      qDebug() << "Baudrate" << "57.6k";
    }
    else if (baudrate == 6)
    {
      setBaudRate(BAUD115200);
      qDebug() << "Baudrate" << "115.2k";
    }
    setFlowControl(FLOW_OFF);
    setParity(PAR_NONE);
    setDataBits(DATA_8);
    setStopBits(STOP_1);
    setTimeout(timeout);
    return open(QIODevice::ReadWrite);
}
示例#30
0
/*!
 * \brief Rs232DevicePrivate::configPort - Parametri per configurare la porta seriale
 * \return true se riesce a configurare correttamente la porta seriale
 */
bool Rs232DevicePrivate::configPort ()
{
    if (!open(QIODevice::ReadWrite)) {
        QString testo = QString("Can't open %1, error code %2")
                    .arg(portName()).arg(error());
        debug(testo);
        return false;
    }

    if (!setBaudRate(QSerialPort::Baud115200)) {
        QString testo = QString("Can't set rate 115200 baud to port %1, error code %2")
                     .arg(portName()).arg(error());
        debug(testo);
        return false;
    }

    if (!setDataBits(QSerialPort::Data8)) {
        QString testo = QString("Can't set 8 data bits to port %1, error code %2")
                     .arg(portName()).arg(error());
        debug(testo);
        return false;
    }

    if (!setParity(QSerialPort::NoParity)) {
        QString testo = QString("Can't set no patity to port %1, error code %2")
                     .arg(portName()).arg(error());
        debug(testo);
        return false;
    }

    if (!setStopBits(QSerialPort::OneStop)) {
        QString testo = QString("Can't set 1 stop bit to port %1, error code %2")
                     .arg(portName()).arg(error());
        debug(testo);
        return false;
    }

    if (!setFlowControl(QSerialPort::NoFlowControl)) {
        QString testo = QString("Can't set no flow control to port %1, error code %2")
                     .arg(portName()).arg(error());
        debug(testo);
        return false;
    }

    return true;
}