void RfidListener::waitForSerialData()
{
	Serial serial = Serial();
	while (serial.Init(_port) == 0)
		Sleep(200);

	serial.Write("auth");
	serial.Read();
	if (serial.DataRead.compare("0") == 0)
	{
		serial.Write("init");
		serial.Read();
		if (serial.DataRead.compare("ok") == 0)
			serial.Write("ok");
	}

	while (!_quit)	// Ends thread on end
	{
		serial.Write("check");
		serial.Read();
		if (serial.DataRead.compare("1") == 0)
		{
			serial.Write("get");
			serial.Read();

			list<Credential>::iterator it = _credentials.begin();
			for (int i = 0; i < _credentials.size(); i++)
			{
				advance(it, 0);
				if (serial.DataRead.compare(it->id) == 0)
				{
					ZeroMemory(_username, sizeof(_username));
					ZeroMemory(_password, sizeof(_password));

					wcscpy_s(_username, 1024, it->username);
					wcscpy_s(_password, 1024, it->password);
					_connected = TRUE;
					_pProvider->OnConnectStatusChanged();
					break;
				}
			}
			if (_connected)
				break;
		}
		Sleep(1000); // Wait so as no to flood the serial port
	}
}
int main(int argc, char* argv[])
{
	printf("[i] Start... \n");

	Serial serial;
#if defined(WIN32)
	serial.open(SERIAL_PORT_NAME, SERIAL_PORT_RATE, true);
#elif defined(LINUX)
	serial.open(SERIAL_PORT_NAME, SERIAL_PORT_RATE);
#endif

	if(!serial.connected())
	{
		printf("[!] Cant open port!\n");
		return -1;
	}

	char c = 'y';
	int res = 12;

	char buf[BUF_SIZE1];
	memset(buf, 0, BUF_SIZE1);

	while(true)
	{

		serial.write( &c, 1 );
#if 0
		if(res = serial.available()){
			if( res = serial.Read(buf, res) ){
				printf("%d %s\n", res, buf);
			}
		}
#else
		if(serial.waitInput(1000)==0)
			printf("[i] timeout!\n");
		else
		{
			if(res = serial.available())
			{
				res = serial.read(buf, res);
				printf("%d %s\n", res, buf);
			}
		}
#endif
	}
	serial.close();

	printf("[i] End.\n");
	return 0;
}
Beispiel #3
0
void loop()
{
	int len;
	if(serial.available())
	{
		len = serial.available();
	
		serial.Read(buffer, len, 0);

		printf("%s\n",buffer);
	}
	printf("This is setup\n");
	sleep(1);

}
Beispiel #4
0
int main()
{
    Serial s;
    if(s.Open("/dev/ttySAC1",19200))
    {
        cout<<"port open\n";
        byte packet[]= {0xAA,0xBB,0x03,0x01,0x01,0x03};
        for(int i=0; i<sizeof(packet); i++)
        {
            s.WriteByte((char)packet[i]);
        }

        byte packet_read[1];

        int attempt=100;
        bool flag=false;
        while(attempt--)
        {
            if(s.Read(packet_read,1)>0)
            {
                for(int i=0; i<sizeof(packet_read); i++)
                    printf("%x\n",packet_read[i]);
                flag=true;
            }

        }
        if(!flag)
            cout<<"nothing to read...\n";
        cout<<"closing port...\n";
        s.Close();
    }
    else
    {
        cout<<"port could not open\n";
    }
    return 0;
}
Beispiel #5
0
int main()
{
	Log log("./data/temp_log.txt", 1024 * 1024);
	rrdWeather *rrdW = new rrdWeather();
	rrdBattery *rrdB = new rrdBattery();
	string port = "/dev/ttyAMA0";
	int iSpeed = 9600;
	char szReadBuffer[128];
	int iBytesRead = 0;
	bool bLoop = true;
	memset(&szReadBuffer[0], 0, 128*sizeof(char));

	LLAP llap;
	Serial serial;
	serial.Configure(port, iSpeed);
	serial.Open();

	int tem = fcntl(0, F_GETFL, 0);
  	fcntl (0, F_SETFL, (tem | O_NDELAY));

	cout << "creating database if not exist" << endl;
	rrdW->create();
	rrdB->create();
	cout << "Listening serial port " << port << endl;

	string tmp;
	int illapRet = 0;

	while(true == bLoop)
	{
		memset(&szReadBuffer[0], 0, 128*sizeof(char));
		iBytesRead = serial.Read(&szReadBuffer[0], 128, 2500, 12);
		if(0 < iBytesRead)
		{
			illapRet = llap.ParseMsg(szReadBuffer, iBytesRead);
			if(MSG_READY == illapRet || MSG_READY_PARTIAL == illapRet)
			{
				LLAP_MSG *pMsg = llap.GetLastMsg();
				string strTime = GetCurrentTime(false);
				//Write to rrd
				if(0 == strcasecmp("TMPA", pMsg->m_szCmd))
				{
					rrdW->update(atof(pMsg->m_szValue));
					//Copy file to /var/www/rpi_web
					CopyFile(rrdW->GetFile(1), "/var/www/rpi_web/01_daily.png");
					CopyFile(rrdW->GetFile(2), "/var/www/rpi_web/02_weekly.png");
					CopyFile(rrdW->GetFile(3), "/var/www/rpi_web/03_monthly.png");
					CopyFile(rrdW->GetFile(4), "/var/www/rpi_web/04_yearly.png");
				}
				if(0 == strcasecmp("BATT", pMsg->m_szCmd))
				{
					rrdB->update(atof(pMsg->m_szValue));
					//Copy file to /var/www/rpi_web
					CopyFile(rrdB->GetFile(1), "/var/www/rpi_web/battery.png");
				}
				if(0 == strcasecmp("BATTLOW", pMsg->m_szCmd))
                                {
                                        rrdB->LowBattWarning();
                                        //Copy file to /var/www/rpi_web
                                        CopyFile(rrdB->GetFile(1), "/var/www/rpi_web/battery.png");
                                }

				//Write to screen and log
				tmp = strTime + "\t" + pMsg->m_szId + " " + pMsg->m_szCmd + " " + pMsg->m_szValue + "\t\t" + pMsg->m_szOriginal;
				cout << tmp << endl;
				log.Write(tmp);
			}
		}
		if('q' == getchar())
		{
			bLoop = false;
		}
	}
	serial.Close();
	cout << "Exiting program" << endl;
	fcntl(0, F_SETFL, tem);

	delete rrdW;
	delete rrdB;
	return 0;
}
Beispiel #6
0
int main(int /*argc*/, char** /*argv*/)
{
	char panval[BUFFERSIZE];
	char tiltval[BUFFERSIZE];
	char distance[BUFFERSIZE];
	char* str = (char*)malloc(sizeof(char)*BUFFERSIZE);
	/*char* panval;
	panval = "ron01\r\n";
	char* tiltval;
	tiltval = "ron01\r\n";
	char* distance;
	distance = "ron01\r\n";*/
	
	char* camwait;
	camwait = "end00\r\n";
	char* startstr;
	startstr = "frame\r\n";

	
	
	cv::Mat mat;
	cv::Mat mat2;

	cv::Point target;

	Serial serial;

	long long start;
	long long elapsed;

	//char* str;
	//str = "ron01\r\n";

	if (!serial.InitSerial("COM3", 115200)) {
		return 0;
	}

	if (!pgrey.InitSingleCamera())
	{
		return 0;
	}
	FlyCapture2::Error error = pgrey.cam.StartCapture(OnImageGrabbed);
	if (error != PGRERROR_OK)  
	{
		cout << "start capture failed" << endl;
		return 0;
	}

	for (;;)
	{
		//start = milliseconds_now();
		if (status == 0) {
			if (serial.Read(&str)) {
				if (*str == *camwait) {
					//status = 1;
				}
				else if (str[0] == 'P') {
					strncpy(panval, str, sizeof(str));
					
				}
				else if (str[0] == 'T') {
					strncpy(tiltval, str, sizeof(str));
				}
				else if (str[0] == 'D') {
					strncpy(distance, str, sizeof(str));
				}
				else if (*str == *startstr) {
					status = 1;
					
				}
			}


			if (status == 1) {
				FlyCapture2::Error error;
				// Retrieve an image
				start = milliseconds_now();

				//pgrey.cam.tim

				error = pgrey.cam.RetrieveBuffer(&pgrey.rawImage);
				FlyCapture2::TimeStamp frametime = pgrey.rawImage.GetTimeStamp();
				cout << frametime.seconds << frametime.microSeconds << endl;
				//FlyCapture2::TimeStamp

				if (error != PGRERROR_OK)
				{
					pgrey.PrintError(error);
					return -1;
				}

				// Create a converted image
				Image convertedImage;

				// Convert the raw image
				error = pgrey.rawImage.Convert(PIXEL_FORMAT_MONO8, &convertedImage);
				if (error != PGRERROR_OK)
				{
					pgrey.PrintError(error);
					return -1;
				}


				unsigned int rowBytes = (double)convertedImage.GetReceivedDataSize() / (double)convertedImage.GetRows();
				mat2 = cv::Mat(convertedImage.GetRows(), convertedImage.GetCols(), CV_8UC1, convertedImage.GetData());

				/*start = milliseconds_now();

				if (HoughFind(mat2, &target))
				{

				}

				elapsed = milliseconds_now() - start;
				cout << "hough time:" << elapsed << endl;*/

				
				cv::imshow("DSfwef", mat2);
				status = 2;

				elapsed = milliseconds_now() - start;
				cout << "hough time:" << elapsed << endl; 


			}

			if (status == 2) {
				//printf("tracking done\n");
				printf("pan: %s", panval);
				printf("tilt: %s", tiltval);
				printf("dist: %s", distance);
				char* msg;
				msg = "s05120512\0";
				serial.WriteToCom(msg);
				status = 0;
			}

			if (cv::waitKey(1) == 27)
			{
				pgrey.CloseSingleCamera();
				return 0;
				//break;
			}


			//elapsed = milliseconds_now() - start;
			//cout << "grab time:" << elapsed << endl;
		}
		
	}

	pgrey.CloseSingleCamera();
	return 0;


}