Пример #1
0
int ProviderRs232::writeBytes(const qint64 size, const uint8_t * data)
{
	if (! _blockedForDelay)
	{
		if (!_rs232Port.isOpen())
		{
			return tryOpen(5000) ? 0 : -1;
		}

		if (_frameDropCounter > 5)
		{
			Debug(_log, "%d frames dropped", _frameDropCounter);
		}
		_frameDropCounter = 0;
		_blockedForDelay = true;
		_bytesToWrite = size;
		qint64 bytesWritten = _rs232Port.write(reinterpret_cast<const char*>(data), size);
		if (bytesWritten == -1 || bytesWritten != size)
		{
			Warning(_log,"failed writing data");
			QTimer::singleShot(500, this, SLOT(unblockAfterDelay()));
			return -1;
		}
		QTimer::singleShot(5000, this, SLOT(unblockAfterDelay()));
	}
	else
	{
		_frameDropCounter++;
	}

	return 0;
}
Пример #2
0
bool ProviderRs232::tryOpen(const int delayAfterConnect_ms)
{
	if (_deviceName.isEmpty() || _rs232Port.portName().isEmpty())
	{
		if ( _enableAutoDeviceName )
		{
			_deviceName = findSerialDevice();
			if ( _deviceName.isEmpty() )
			{
				return false;
			}
		}
		Info(_log, "Opening UART: %s", _deviceName.toLocal8Bit().constData());
		_rs232Port.setPortName(_deviceName);
	}

	if ( ! _rs232Port.isOpen() )
	{
		_frameDropCounter = 0;
		if (QFile::exists(_deviceName))
		{
			if ( _preOpenDelayTimeOut > QDateTime::currentMSecsSinceEpoch() )
			{
				return false;
			}
			if ( ! _rs232Port.open(QIODevice::ReadWrite) )
			{
				if ( _stateChanged )
				{
					Error(_log, "Unable to open RS232 device (%s)", _deviceName.toLocal8Bit().constData());
					_stateChanged = false;
				}
				return false;
			}
			Debug(_log, "Setting baud rate to %d", _baudRate_Hz);
			_rs232Port.setBaudRate(_baudRate_Hz);
			_stateChanged = true;
			_preOpenDelayTimeOut = 0;
		}
		else
		{
			_preOpenDelayTimeOut = QDateTime::currentMSecsSinceEpoch() + _preOpenDelay;
			return false;
		}
	}

	if (delayAfterConnect_ms > 0)
	{
		_blockedForDelay = true;
		QTimer::singleShot(delayAfterConnect_ms, this, SLOT(unblockAfterDelay()));
		Debug(_log, "Device blocked for %d ms", delayAfterConnect_ms);
	}

	return _rs232Port.isOpen();
}
Пример #3
0
int LedRs232Device::open()
{
	try
	{
		std::cout << "Opening UART: " << _deviceName << std::endl;
		_rs232Port.setPort(_deviceName);
		_rs232Port.setBaudrate(_baudRate_Hz);
		_rs232Port.open();

		if (_delayAfterConnect_ms > 0)
		{
			_blockedForDelay = true;
			QTimer::singleShot(_delayAfterConnect_ms, this, SLOT(unblockAfterDelay()));
			std::cout << "Device blocked for " << _delayAfterConnect_ms << " ms" << std::endl;
		}
	}
	catch (const std::exception& e)
	{
		std::cerr << "Unable to open RS232 device (" << e.what() << ")" << std::endl;
		return -1;
	}

	return 0;
}
Пример #4
0
int LedRs232Device::writeBytes(const unsigned size, const uint8_t * data)
{
	if (_blockedForDelay)
	{
		return 0;
	}

	if (!_rs232Port.isOpen())
	{
		// try to reopen
		int status = open();
		if(status == -1){
			// Try again in 3 seconds
			int seconds = 3000;
			_blockedForDelay = true;
			QTimer::singleShot(seconds, this, SLOT(unblockAfterDelay()));
			std::cout << "Device blocked for " << seconds << " ms" << std::endl;
		}
		return status;
	}

//	for (int i = 0; i < 20; ++i)
//		std::cout << std::hex << (int)data[i] << " ";
//	std::cout << std::endl;

	try
	{
		_rs232Port.flushOutput();
		_rs232Port.write(data, size);
		_rs232Port.flush();
	}
	catch (const serial::SerialException & serialExc)
	{
		// TODO[TvdZ]: Maybe we should limit the frequency of this error report somehow
		std::cerr << "Serial exception caught while writing to device: " << serialExc.what() << std::endl;
		std::cout << "Attempting to re-open the device." << std::endl;

		// First make sure the device is properly closed
		try
		{
			_rs232Port.close();
		}
		catch (const std::exception & e) {}

		// Attempt to open the device and write the data
		try
		{
			_rs232Port.open();
			_rs232Port.write(data, size);
			_rs232Port.flush();
		}
		catch (const std::exception & e)
		{
			// We failed again, this not good, do nothing maybe in the next loop we have more success
		}
	}
	catch (const std::exception& e)
	{
		std::cerr << "Unable to write to RS232 device (" << e.what() << ")" << std::endl;
		return -1;
	}

	return 0;
}