예제 #1
0
/**
 * Conditionaly write "New Line"
 * @param context
 * @return number of characters written
 */
static size_t writeNewLine(scpi_t * context) {
    if (context->output_count > 0) {
        size_t len;
#ifndef SCPI_LINE_ENDING
#error no termination character defined
#endif
        len = writeData(context, SCPI_LINE_ENDING, strlen(SCPI_LINE_ENDING));
        flushData(context);
        return len;
    } else {
        return 0;
    }
}
//------------------------------------------------------------------------------
tFirmwareRet firmwarestore_flushData(tFirmwareStoreHandle pHandle_p)
{
    tFirmwareRet ret = kFwReturnOk;

    if (pHandle_p != NULL)
    {
        ret = flushData(pHandle_p);
    }
    else
    {
        ret = kFwReturnInvalidInstance;
    }

    return ret;
}
//! \brief Close the serial communication port.
XsResultValue SerialInterface::closeLive(void)
{
#ifdef LOG_RX_TX
	rx_log.close();
	tx_log.close();
#endif
	if (!isOpen())
		return m_lastResult = XRV_NOPORTOPEN;

	m_lastResult = XRV_OK;
#ifdef _WIN32
	if (::FlushFileBuffers(m_handle))
	{
		// read all data before closing the handle, a Flush is not enough for FTDI devices unfortunately
		// we first need to set the COMM timeouts to instantly return when no more data is available
		COMMTIMEOUTS cto;
		if (::GetCommTimeouts(m_handle,&cto))
		{
			cto.ReadIntervalTimeout = MAXDWORD;
			cto.ReadTotalTimeoutConstant = 0;
			cto.ReadTotalTimeoutMultiplier = 0;
			if (::SetCommTimeouts(m_handle,&cto))
			{
				char buffer[1024];
				DWORD length;
				do {
					if (!::ReadFile(m_handle, buffer, 1024, &length, NULL))
						break;
				} while (length > 0);
			}
			else
				m_lastResult = XRV_ERROR;
		}
		else
			m_lastResult = XRV_ERROR;
	}
	if (!::CloseHandle(m_handle))
		m_lastResult = XRV_ERROR;
	m_handle = INVALID_HANDLE_VALUE;
#else
	flushData();
	::close(m_handle);
	m_handle = -1;
#endif
	m_endTime = 0;

	return m_lastResult;
}
//------------------------------------------------------------------------------
tFirmwareRet firmwarestore_destroy (tFirmwareStoreHandle pHandle_p)
{
    tFirmwareRet ret = kFwReturnOk;

    if (pHandle_p == NULL)
    {
        ret = kFwReturnInvalidInstance;
    }

    ret = flushData(pHandle_p);

    if (ret == kFwReturnOk)
    {
        free (pHandle_p);
    }

    return ret;
}
예제 #5
0
void QToDoContent::ok_clicked()
{
    switch( m_type )
    {
    case AllTask:
    {
        QToDoData::ref().setToDo(m_taskList);
        QMessageBox::information(this,LOCAL("提示"),LOCAL("已选择"),0);
        m_taskList.clear();
        break;
    }
    case ToDo:
    {
        QToDoData::ref().setDone(m_taskList);
        flushData(QToDoData::ref().getToDo());
        QMessageBox::information(this,LOCAL("提示"),LOCAL("已处理"),0);
        m_taskList.clear();
        break;
    }
    }
}
예제 #6
0
void stk500service::setAddress(quint32 address) {
    // Make sure communication is alright
    checkConnection();

    // Compile the address change message
    char command[5];
    command[0] = 'a';
    memcpy(command+1, &address, 4);

    // Attempt to switch address two times
    for (int i = 0; i < 2; i++) {
        if (writeVerify(command, sizeof(command))) {
            return;
        } else {
            flushData();
        }
    }

    // Error
    QString addressText = QString("0x") + QString::number(address, 16);
    QString errorMessage = QString("Failed to switch to address %1").arg(addressText);
    throw ProtocolException(errorMessage);
}
예제 #7
0
/*! \brief Open a communication channel to the given USB port name. */
XsResultValue UsbInterface::open(const XsPortInfo &portInfo, uint32_t, uint32_t)
{
	d->m_endTime = 0;

#ifdef USE_WINUSB
	JLDEBUG(gJournal, "Open usb port " << portInfo.portName().toStdString());
#else
	JLDEBUG(gJournal, "Open usb port " << portInfo.usbBus() << ":" << portInfo.usbAddress());
#endif

	if (isOpen())
	{
		JLALERT(gJournal, "Port " << portInfo.portName().toStdString() << " already open");
		return (d->m_lastResult = XRV_ALREADYOPEN);
	}

#ifdef USE_WINUSB
	d->m_deviceHandle = CreateFileA(portInfo.portName().c_str(),
		GENERIC_WRITE | GENERIC_READ,
		FILE_SHARE_WRITE | FILE_SHARE_READ,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
		NULL);

	if (d->m_deviceHandle == INVALID_HANDLE_VALUE)
	{
		d->m_deviceHandle = NULL;
		return (d->m_lastResult = XRV_PORTNOTFOUND);
	}

	BOOL result = FALSE;
	UCHAR speed = 0;
	ULONG length = 0;
	USB_INTERFACE_DESCRIPTOR interfaceDescriptor = {0,0,0,0,0,0,0,0,0};
	WINUSB_PIPE_INFORMATION pipeInfo;

	result = d->m_winUsb.Initialize(d->m_deviceHandle, &d->m_usbHandle[0]);
	if (result)
	{
		result = d->m_winUsb.GetAssociatedInterface(d->m_usbHandle[0],0,&d->m_usbHandle[1]);
	}
	else
	{
#ifdef XSENS_DEBUG
		DWORD err = GetLastError();
		assert(result);
#endif
		return (d->m_lastResult = XRV_ERROR);
	}

	for (int k = 0; k<2;k++)
	{
		if(result)
		{
			assert(d->m_usbHandle[k] != 0);
			length = sizeof(UCHAR);
			result = d->m_winUsb.QueryDeviceInformation(d->m_usbHandle[k],
				DEVICE_SPEED,
				&length,
				&speed);
		}

		if(result)
		{
			d->m_deviceSpeed = speed;
			result = d->m_winUsb.QueryInterfaceSettings(d->m_usbHandle[k],
				0,
				&interfaceDescriptor);
		}
		if(result)
		{
			for(int i=0;i<interfaceDescriptor.bNumEndpoints;i++)
			{
				result = d->m_winUsb.QueryPipe(d->m_usbHandle[k],
					0,
					(UCHAR) i,
					&pipeInfo);

				if(pipeInfo.PipeType == UsbdPipeTypeBulk &&
					USB_ENDPOINT_DIRECTION_IN(pipeInfo.PipeId))
				{
					d->m_bulkInPipe = pipeInfo.PipeId;
					d->m_bulkInPipePacketSize =
						pipeInfo.MaximumPacketSize;
				}
				else if(pipeInfo.PipeType == UsbdPipeTypeBulk &&
					USB_ENDPOINT_DIRECTION_OUT(pipeInfo.PipeId))
				{
					d->m_bulkOutPipe = pipeInfo.PipeId;
				}
				else if(pipeInfo.PipeType == UsbdPipeTypeInterrupt)
				{
					d->m_interruptPipe = pipeInfo.PipeId;
				}
				else
				{
					result = FALSE;
					break;
				}
			}
		}
	}

	setTimeout(0);	//lint !e534
	flushData();	//lint !e534

	sprintf(d->m_portname, "%s", portInfo.portName().c_str());

//	d->m_offset = 0;
	::ResetEvent(&d->m_quitEvent);	//lint !e534
	d->m_threadHandle = xsStartThread(usbReadThreadFunc, d, &d->m_threadId);
	if (d->m_threadHandle == XSENS_INVALID_THREAD)
	{
#ifdef XSENS_DEBUG
		assert(0);
#endif
		return (d->m_lastResult = XRV_ERROR);
	}

#else // !USE_WINUSB
	libusb_device **deviceList;
	ssize_t listLength = UsbInterfacePrivate::getContextManager().m_libUsb.get_device_list(UsbInterfacePrivate::getContextManager().m_usbContext, &deviceList);
	if (listLength < 0)
		return d->m_lastResult = d->libusbErrorToXrv((int)listLength);

	// "USBxxx:yyy"
	uint8_t bus = XsPortInfo_usbBus(&portInfo);
	uint8_t address = XsPortInfo_usbAddress(&portInfo);

	XsResultValue xrv = XRV_OK;
	int result;
	libusb_device *device = NULL;
	for (int i = 0; i < listLength && device == NULL; ++i) {
		libusb_device *dev = deviceList[i];
		if (UsbInterfacePrivate::getContextManager().m_libUsb.get_bus_number(dev) != bus || UsbInterfacePrivate::getContextManager().m_libUsb.get_device_address(dev) != address)
			continue;

		libusb_device_descriptor desc;
		result = UsbInterfacePrivate::getContextManager().m_libUsb.get_device_descriptor(dev, &desc);
		if (result != LIBUSB_SUCCESS)
			break;

		libusb_config_descriptor *configDesc;
		result = UsbInterfacePrivate::getContextManager().m_libUsb.get_active_config_descriptor(dev, &configDesc);
		if (result != LIBUSB_SUCCESS)
			break;

		d->m_interface = -1;
		d->m_interfaceCount = configDesc->bNumInterfaces;
		// find the bulk transfer endpoints
		for (uint8_t ifCount = 0; ifCount < configDesc->bNumInterfaces && d->m_interface == -1; ++ifCount) {
			for (uint8_t altsettingCount = 0; altsettingCount < configDesc->interface[ifCount].num_altsetting; altsettingCount++) {
				const libusb_endpoint_descriptor *endpoints = configDesc->interface[ifCount].altsetting[altsettingCount].endpoint;
				int inEndpoint = -1, outEndpoint = -1;
				for (uint8_t i = 0; i < configDesc->interface[ifCount].altsetting[altsettingCount].bNumEndpoints; i++) {
					if ((endpoints[i].bmAttributes&LIBUSB_TRANSFER_TYPE_MASK) != LIBUSB_TRANSFER_TYPE_BULK)
						continue;

					switch (endpoints[i].bEndpointAddress&LIBUSB_ENDPOINT_DIR_MASK) {
					case LIBUSB_ENDPOINT_IN:
						inEndpoint = endpoints[i].bEndpointAddress&LIBUSB_ENDPOINT_ADDRESS_MASK;
						break;

					case LIBUSB_ENDPOINT_OUT:
						outEndpoint = endpoints[i].bEndpointAddress&LIBUSB_ENDPOINT_ADDRESS_MASK;
						break;
					}

				}

				if (outEndpoint == -1 || inEndpoint == -1)
					continue;

				d->m_interface = ifCount;
				d->m_dataOutEndPoint = outEndpoint;
				d->m_dataInEndPoint = inEndpoint;
			}
		}
		if (d->m_interface == -1) {
			xrv = XRV_INPUTCANNOTBEOPENED;
			break;
		}

		UsbInterfacePrivate::getContextManager().m_libUsb.free_config_descriptor(configDesc);
		UsbInterfacePrivate::getContextManager().m_libUsb.ref_device(dev);
		device = dev;
		result = LIBUSB_SUCCESS;
	}

	UsbInterfacePrivate::getContextManager().m_libUsb.free_device_list(deviceList, 1);
	if (result != LIBUSB_SUCCESS) {
		UsbInterfacePrivate::getContextManager().m_libUsb.unref_device(device);
		return d->m_lastResult = d->libusbErrorToXrv(result);
	}

	if (xrv != XRV_OK) {
		UsbInterfacePrivate::getContextManager().m_libUsb.unref_device(device);
		return d->m_lastResult = xrv;
	}

	libusb_device_handle *handle;
	result = UsbInterfacePrivate::getContextManager().m_libUsb.open(device, &handle);
	if (result != LIBUSB_SUCCESS) {
		UsbInterfacePrivate::getContextManager().m_libUsb.unref_device(device);
		return d->m_lastResult = d->libusbErrorToXrv(result);
	}

	// be rude and claim all interfaces
	for (int i = 0; i < d->m_interfaceCount; i++) {
		result = UsbInterfacePrivate::getContextManager().m_libUsb.kernel_driver_active(handle, i);
		if (result > 0)
			result = UsbInterfacePrivate::getContextManager().m_libUsb.detach_kernel_driver(handle, i);
		if (result == LIBUSB_SUCCESS)
			result = UsbInterfacePrivate::getContextManager().m_libUsb.claim_interface(handle, i);
		if (result != LIBUSB_SUCCESS) {
			for (int j = 0; j < i; j++) {
				while (result != LIBUSB_SUCCESS) {
					result = UsbInterfacePrivate::getContextManager().m_libUsb.release_interface(handle, j);
					UsbInterfacePrivate::getContextManager().m_libUsb.attach_kernel_driver(handle, j);
				}
			}

			UsbInterfacePrivate::getContextManager().m_libUsb.close(handle);
			UsbInterfacePrivate::getContextManager().m_libUsb.unref_device(device);
			return d->m_lastResult = d->libusbErrorToXrv(result);
		}
	}

	d->m_deviceHandle = handle;
	sprintf(d->m_portname, "%s", portInfo.portName().c_str());

	flushData();

#endif // !USE_WINUSB
	JLDEBUG(gJournal, "USB Port opened");
	return (d->m_lastResult = XRV_OK);
}
예제 #8
0
/*! \brief Close the USB communication port.
	\returns XRV_OK if the port was closed successfully
	\note Linux:\n
	If a kernel driver was detached when communication with the device started,
	attach it again. No guarantee is given that udev will pick up on it though.
*/
XsResultValue UsbInterface::closeUsb(void)
{
	//lint --e{534}
#ifdef LOG_RX_TX
	if (d->rx_log != NULL)
		fclose(d->rx_log);
	if (d->tx_log != NULL)
		fclose(d->tx_log);
	d->rx_log = NULL;
	d->tx_log = NULL;
#endif
	if (!isOpen())
		return d->m_lastResult = XRV_NOPORTOPEN;

	d->m_lastResult = XRV_OK;
#ifdef USE_WINUSB
	if (d->m_threadHandle != INVALID_HANDLE_VALUE)
	{
		while (d->m_running)
		{
			::SetEvent(d->m_quitEvent);
			XsTime::msleep(10);
		}
		::CloseHandle(d->m_threadHandle);
	}

	flushData();
	if(d->m_usbHandle[0]) {
		d->m_winUsb.Free(d->m_usbHandle[0]);
		d->m_usbHandle[0] = NULL;
	}
	if(d->m_usbHandle[1]) {
		d->m_winUsb.Free(d->m_usbHandle[1]);
		d->m_usbHandle[1] = NULL;
	}
	if (d->m_deviceHandle) {
		CloseHandle(d->m_deviceHandle);
		d->m_deviceHandle = NULL;
	}
#else
	flushData();
	libusb_device *dev = UsbInterfacePrivate::getContextManager().m_libUsb.get_device(d->m_deviceHandle);
	for (int i = 0; i < d->m_interfaceCount; i++) {
		int result = LIBUSB_ERROR_OTHER;
		while (result != LIBUSB_SUCCESS) {
			result = UsbInterfacePrivate::getContextManager().m_libUsb.release_interface(d->m_deviceHandle, i);
			if (result == LIBUSB_SUCCESS) {
				UsbInterfacePrivate::getContextManager().m_libUsb.attach_kernel_driver(d->m_deviceHandle, i);
			}
		}
	}

	UsbInterfacePrivate::getContextManager().m_libUsb.close(d->m_deviceHandle);
	d->m_deviceHandle = NULL;

	UsbInterfacePrivate::getContextManager().m_libUsb.unref_device(dev);
	d->m_interface = -1;
	d->m_dataInEndPoint = -1;
	d->m_dataOutEndPoint = -1;
#endif
	d->m_endTime = 0;

	return d->m_lastResult;
}
예제 #9
0
LRESULT CGridDlg::OnMsgNewImage(WPARAM wp, LPARAM lp) {
  cellCountFromSize();
  flushData();
  calculate();
  return 0;
}