Exemple #1
0
void GetLinuxSerialPorts(JNIEnv* env,
                         std::vector<std::basic_string<char> >& ports)
{
    int pos;
    struct dirent **namelist;
    const char* sysdir = "/sys/class/tty/";
    // Scan through /sys/class/tty.
    // it contains all tty-devices in the system
    pos = scandir(sysdir, &namelist, NULL, NULL);
    if (pos < 0)
    {
        ReportError(env, "Failed to enumerate serial ports.");
    }
    while (pos--)
    {
        if (strcmp(namelist[pos]->d_name, "..") != 0
                && strcmp(namelist[pos]->d_name, ".") != 0)
        {
            // Construct full absolute file path
            string devicedir = sysdir;
            devicedir += namelist[pos]->d_name;
            // Register the device
            GetComPort(devicedir, ports);
        }
        free(namelist[pos]);
    }
    free(namelist);
}
Exemple #2
0
int _tmain(int argc, _TCHAR* argv[])
{
	std::string hexFile;

	int result = 0;
	
	if (argc == 1)
	{
		hexFile = "Drone.hex";
	}
	else if(argc==2)
	{
		std::wstring wHexFile = argv[1];
		std::string tmp(wHexFile.begin(), wHexFile.end()); //not portable
		hexFile = tmp;
	}
	else
	{
		std::cerr << "Usage: droneupdate [Drone.hex]" << std::endl;
		return -1;
	}
	
	/*
	if (CheckAvrdudeVersion(6, 1 ) )
	{

	}
	else
	{
		std::cerr << "ERROR: avrdude is too old" << std::endl;
		std::cerr << "  download newer version -- tested with 6.01 http://download.savannah.gnu.org/releases/avrdude/avrdude-6.0.1-mingw32.zip" << std::endl;
		return -1;
	}
	*/
	
	/*
#if USE_FTDI_API

#else
	int comPort;
	const int PORT_SCAN_LIMIT = 1000;
	bool status = GetComPort(&comPort,PORT_SCAN_LIMIT);
	if(!status)
	{
		std::cerr << "ERROR: GetComPort()" << std::endl;
		std::cerr << "ERROR: No Serial Port COMx found (where x is 0 to " << PORT_SCAN_LIMIT << " checked)" << std::endl;
		std::cerr << "ERROR: Check that Drone is plugged-in and powered-up (hold start until it turns on)." << std::endl;
		return 1;
	}
#endif
	*/

#if USE_FTDI_API
	//scan for comPort here
	LONG comPort = -1;
	FT_STATUS ftStatus;
	ftStatus = GetComPort(&comPort);
	if (ftStatus == FT_OK)
	{
	}
	else
	{
		std::cerr << "ERROR: GetComPort()" << std::endl;
		std::cerr << "ERROR: returning ftStatus" << std::endl;
		return ftStatus;
	}


	if (comPort != -1)
	{
#else
	const int PORT_SCAN_LIMIT = 1000;
	for (int comPort = 0; comPort < PORT_SCAN_LIMIT; comPort++)
	{

#endif
		std::ostringstream ossComPort;
		ossComPort << comPort;
		std::string strComPort(ossComPort.str());
		std::cout << "COM Port = " << strComPort << std::endl;

#if !(USE_FTDI_API)
		const char COM_PREFIX[] = "\\\\.\\";
		//wsprintf(szPort, "COM%d", nPort);
		std::string f = std::string(COM_PREFIX) + "COM" + strComPort;

		HANDLE m_hIDComDev = CreateFileA(f.c_str(),
			GENERIC_READ | GENERIC_WRITE,
			0,
			NULL,
			OPEN_EXISTING,
			FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
			NULL);
		if (m_hIDComDev == INVALID_HANDLE_VALUE)
		{
			DWORD err = GetLastError();
		}
		else
		{
			if (!CloseHandle(m_hIDComDev))
			{
				DWORD err = GetLastError();
			}
			else
			{
#endif

				std::string cmd = std::string(AVRDUDE_CMD) + " -P com" + strComPort + " -U flash:w:" + hexFile + " ";
				std::cout << cmd << std::endl;

				//result = 0; //DEBUG
				result = system(cmd.c_str());

#if !( USE_FTDI_API )

				if (0 == result)
				{

					break;
				}
			}
		}
	}
	if (result)
	{
		std::cerr << "  Attempted update with serial port COMx where x is 0 to "<< PORT_SCAN_LIMIT << std::endl;
		std::cerr << "  Check that Drone is plugged-in and powered-up (hold start until it turns on)." << std::endl;
		std::cerr << "ERROR: Drone firmware update FAILED" << std::endl;
	}
	else
	{
		std::cout << "OK: Drone firmware update PASSED" << std::endl;
	}


#else

	}
	else
	{
		std::cerr << "ERROR: comPort==-1" << std::endl;
		std::cerr << "ERROR: returning ftStatus" << std::endl;
		result = comPort;
	}

#endif

	return result;
}