Exemple #1
0
int SendData()
{
	int i = 0;
	int count = 0;
	char ioControl[3];
	int bytesRecieved = 0;

	while (true)
	{
		bytesRecieved = SerialPort.ReadData(ioControl, 1);

		printf("%c %d\t", ioControl[0], bytesRecieved);
		if (ioControl[0] == 'r')
		{
			for (i = 0; i <= NUMBER_OF_BUTTONS; i++)
				SerialPort.SendData(Controller[i].passedValue, sizeof(Controller[i].passedValue));

			if (leapConnected == true)
			{
				for (i = 0; i < NUMBER_OF_LEAP_INPUTS; i++)
					SerialPort.SendData(Leapvalues[i].passedValue, sizeof(Leapvalues[i].passedValue));
			}

			else
			{
				for (i = 0; i < NUMBER_OF_LEAP_INPUTS; i++)
					SerialPort.SendData("0", 1);
			}

			//Sleep(59);
			count++;

			if (count > 5)
			{
				SerialPort.Close();
				Sleep(1000);
				SerialPort.Open(PORT_NUM, BAUD);
				return 0;
			}
		}

		else if (ioControl[0] == 'q')
		{
			return 1;
		}

		else
		{
			/*printf("Error: Unknown codeword passed from arduino [%c], aborting send\n", ioControl[0]);
			Sleep(60);*/
		}
	}
}
void serial_sent_double(double input){
    cout << "                                                     " << "\r";
    //Find the Amount of Integer
    int int_digits = numDigits((int)input); //Cast to int then find
    //convert to CONST CHAR * with fixing 6 precision of decimal
    char tempChar[50];
    int digits = int_digits + 1 + 6;
    snprintf(tempChar,50,"%f",input);
    //Transfer via Serial
        cout << "[Serial] Sending (double ) : " << tempChar << "\r";
        serial.SendData(tempChar,digits);
    //Ending Seperator
        serial.SendData(",",1);
        Sleep(40);
}
Exemple #3
0
int main(int argc, char **argv)
{ 
	const char serialBuffer[6] = "hello";
		int  serialOpen, nBytesRead;


	serialOpen=serial.Open(6,9600);
	cout<<serialOpen;


	if(serialOpen){

		nBytesRead=serial.SendData(serialBuffer,strlen(serialBuffer));
		//ASSERT(nBytesRead==strlen(serialBuffer));
		//delete [] serialBuffer;
	
	}

	serial.Close();

  cout << "\n";
  cout << "=========================================================\n";
  cout << "getTristarLog aborted. \n\n";

  return 0;
  
}
void SendQuadcopterData() {
#ifdef ENABLE_SERIAL
	packet[0] = 'g';
	packet[1] = lift;
	packet[2] = pitch;
	packet[3] = yaw;
	packet[4] = roll;
	packet[5] = gyroDiv;
	if (resetGyro)
		packet[6] = 'r';
	else
		packet[6] = 's';
	packet[7] = '\0';
	resetGyro = false;

	//printf("%d %d %d %d\n", lift, yaw, pitch, roll);

	if (serial.IsOpened()) {
		serial.SendData((const char *)packet, length);
	}
	else {
		printf("Serial not opened!!!\n");
	}
#endif
}
void serial_sent_int(int input){
    cout << "                                                     " << "\r";
    //Find The Amount Of Digits
        int digits = numDigits(input);
        if(input == 0) digits = 1;
    //Convert to CONST CHAR * For transfer
        stringstream temp_str;
        temp_str << (input);
        string str = temp_str.str();
        const char * tempChar = str.c_str();
    //Transfer via Serial
        cout << "[Serial] Sending (integer) : " << tempChar << "\r";
        serial.SendData(tempChar,digits);
    //Ending Seperator
        serial.SendData(",",1);
        Sleep(40);
}
void Programming::sendResetCmd(CSerial &serial) {
    if(!serial.IsOpened()) {
        std::cout << "Serial is not connected" << std::endl;
        return;
    }
    char resetChar = '#';
    serial.SendData(&resetChar, 1);
}
Exemple #7
0
int SendData()
{
	int i = 0;
	int count = 0;
	char ioControlBuffer[3];
	char ioControl[3];
	int bytesRecieved = 0;
	SerialPort.SendData("hgl", 3);

	while (true)
	{
		bytesRecieved = SerialPort.ReadData(ioControlBuffer, 1);

		ioControl[0] = ioControlBuffer[0];
		printf("%c\t%d\t", ioControlBuffer[0], bytesRecieved);
		


	   if (ioControl[0] == 'r')
		{
			for (i = 0; i <= NUMBER_OF_BUTTONS; i++)
				SerialPort.SendData(Controller[i].passedValue, sizeof(Controller[i].passedValue));
			
			count++;
			if (count > 5)
			{
				Sleep(750);
				return 0;
			}
		}

		else if (ioControl[0] == 'q')
		{
			return 1;
		}

		else
		{
			printf("Error: Unknown codeword passed from arduino [%c], aborting send\n", ioControl[0]);
		}
		
	}
}
Exemple #8
0
int main(int argc , char* argv[]){

    for (int portNo = 0; portNo<5 ; ++portNo){
        if(serial.findAvailablePorts(portNo))
            cout<<"COM"<<portNo<<" available!"<<endl;
    }


    if(serial.Open(3,115200)){
        cout<<"Connection initialized!"<<endl;
    }
      char szMessage[3] = {0x75, 0x92 , 0xE7};

      serial.SendData(szMessage,3);
      char* lpBuffer = new char[100];
      char setOn[6] = {0x52, 0x86 , 0xD4 , 0x5A , 0x80 , 0xDA};
      Sleep(300);
      serial.ReadData(lpBuffer , 30);
      if(controlComms.checkValidity(lpBuffer,20)!=0){
          cout<<"Device is ready!"<<endl;
      } else{
          cout<<"Device is not ready!"<<endl;

      }
      Sleep(1000);



      //Sleep(1000);
      serial.SendData(setOn,sizeof(setOn));
      Sleep(300);



      thread t1(readData,"");

    t1.join();
    serial.Close();
    printf("\n");
    return 0;
}
Exemple #9
0
bool sendCommand(string command)
{
	cout << "Sending: " << command << endl;
	command.append("\r");
	if(serialPort.SendData(command.c_str(), command.size()) == command.size())
	{
		return true;
	}
	else
	{
		return false;
	}
}
Exemple #10
0
bool send(int ctl,int x, int y)
{
	char data[20]="";
	data[0]= '#';
	data[1]= ctl;
	data[2]= (x / 256)+1;
	data[3]= (x % 256)+1;
	data[4]= (y / 256)+1;
	data[5]= (y % 256)+1;
	data[6]= '*';
	data[7] = '*';
	return serial.SendData(data, 8);
	
}
Exemple #11
0
int main(int argc, char* argv[]) {
	
	//Initialize OpenCV
	CvSize size640x480 = cvSize(640, 480);
	CvCapture* p_capWebcam;	
	IplImage* p_imgOriginal;
	IplImage* hsv;		
	
	//Initialize globals used throughout the program
	ofstream dataFile;
	int netTime = 0;
	int intervalTime = 1000;
	int hourCount = 1;

	int avgCounter = 1; float avgH =0, avgS =0, avgV =0, avgH2 =0, avgS2 =0, avgV2 =0; 

	//More misc initialization for OpenCV and the program
	CvMemStorage* p_strStorage;	
	char charCheckForEscKey;
	p_capWebcam = cvCaptureFromCAM(0);
	
	//Safety check to make sure the camera is there
	if(p_capWebcam == NULL) {
		printf("error: capture is NULL \n");
		getchar();
		return(-1);	
	}

	//Declare video feed window
	cvNamedWindow("Original", CV_WINDOW_AUTOSIZE);

	//Create new image that will be the converted hsv values
	hsv = cvCreateImage(size640x480, IPL_DEPTH_8U, 3);
	
	//Infinite loop (until esc sequence)
	while(1) {
		//Get the current frame
		p_imgOriginal = cvQueryFrame(p_capWebcam);
		
		//Convert RGB to HSV in a separate image
		cvCvtColor( p_imgOriginal, hsv, CV_BGR2HSV );
		
		//Safety check to make sure camera actually captured stuff
		if(p_imgOriginal == NULL) {
			printf("error: frame is NULL \n");
			getchar();
			break;
		}
		
		//Initilization of vars for the average algorithm
		float H=0, S=0, V=0, R=0, G=0, B=0, pixCount =1;
		float H2=0, S2=0, V2=0, R2=0, G2=0, B2=0, pixCount2 = 1;
		CvScalar curValHSV;
		CvScalar curValRGB;

		//Average algorithm that takes the average for 2 reactors and 2 sets of values (RGB and HSV)
		for(int row=190; row<290; row++){
			
			for(int col=150; col<250; col++){
				curValHSV = cvGet2D(hsv,row,col); 
				curValRGB = cvGet2D(p_imgOriginal,row,col);

				H=(H*(pixCount-1)+curValHSV.val[0])/pixCount;
				S=(S*(pixCount-1)+curValHSV.val[1])/pixCount;
				V=(V*(pixCount-1)+curValHSV.val[2])/pixCount;

				B=(B*(pixCount-1)+curValRGB.val[0])/pixCount;
				G=(G*(pixCount-1)+curValRGB.val[1])/pixCount;
				R=(R*(pixCount-1)+curValRGB.val[2])/pixCount;

				pixCount++;
			}

			for(int col=390; col<490; col++){
				curValHSV = cvGet2D(hsv,row,col); 
				curValRGB = cvGet2D(p_imgOriginal,row,col);

				H2=(H2*(pixCount-1)+curValHSV.val[0])/pixCount;
				S2=(S2*(pixCount-1)+curValHSV.val[1])/pixCount;
				V2=(V2*(pixCount-1)+curValHSV.val[2])/pixCount;

				B2=(B2*(pixCount-1)+curValRGB.val[0])/pixCount;
				G2=(G2*(pixCount-1)+curValRGB.val[1])/pixCount;
				R2=(R2*(pixCount-1)+curValRGB.val[2])/pixCount;

				pixCount2++;
			}

		}

		printf("H1=%f S1=%f V1=%f\n", H, S, V);
		printf("H2=%f S2=%f V2=%f\n", H2, S2, V2);

		//Draw circles of the average colour of each reactor
		cvCircle(p_imgOriginal,	cvPoint(213,500), 50, CV_RGB(R, G, B), CV_FILLED);
		cvCircle(p_imgOriginal, cvPoint(426,500), 50, CV_RGB(R2, G2, B2), CV_FILLED);

		//Draw helper "cross hair" rectangles
		cvRectangle(p_imgOriginal, cvPoint(150,190), cvPoint(250,290), CV_RGB(255,0,0), 3);	
		cvRectangle(p_imgOriginal, cvPoint(390,190), cvPoint(490,290), CV_RGB(255,0,0), 3);
		
		//Allocate necessary memory
		p_strStorage = cvCreateMemStorage(0);

		//Display video feed
		cvShowImage("Original", p_imgOriginal);
			
		//Start serial communication
		CSerial serial;

		//If opening the serial port is succesful
		if (serial.Open(3, 9600))
		{
			//If the algae are green and are unsaturated/dying
			if( (H>28 && H<44) && S<230){
				//Send the microcontroller the message to turn the motor
				static char szMessage = '1';
				serial.SendData(&szMessage, 1);
				serial.Close(); 	
			
				char * outputc = new char[6];
			 
				outputc[6]=0;
				
				//Wait for servo success code and temperature
				for(int i=0; i<6; i++){
						outputc[i]=readCharLoop();
				}
			
				printf("Temperature: %s degrees C \n\n", outputc);
				int tens = outputc[2]-48;
				int ones = outputc[3]-48;
				int dec = outputc[5]-48;
				float num = tens*10+ones+0.1*dec;
				 
			}
			else{
				static char szMessage = '0';
				serial.SendData(&szMessage, 1);
				serial.Close(); 

				char * outputc = new char[5];
			
				outputc[5]=0;
			
				//Wait for temperature to come in
				for(int i=0; i<5; i++){
						 outputc[i]=readCharLoop();
				}
			
				printf("Temperature: %s degrees C \n\n", outputc);
				int tens = outputc[1]-48;
				int ones = outputc[2]-48;
				int dec = outputc[4]-48;
				float num = tens*10+ones+0.1*dec;
				
			}
		}
		else
			printf("Failed to open port!\n\n\n");


		//Release memory
		cvReleaseMemStorage(&p_strStorage);	
		
		//If esc key pressed, then break loop
		charCheckForEscKey = cvWaitKey(10);
		if(charCheckForEscKey == 27) break;		

		//Placeholder time interval component (replace with time.h functions)
		Sleep(intervalTime); 
		netTime += intervalTime;
		
		//Save data locally to a .txt file
		if(netTime/1000/60/60 >= hourCount)
		{
			avgH=(avgH*(avgCounter-1)+H)/avgCounter;
			avgS=(avgS*(avgCounter-1)+S)/avgCounter;
			avgV=(avgV*(avgCounter-1)+V)/avgCounter;

			avgH2=(avgH2*(avgCounter-1)+H2)/avgCounter;
			avgS2=(avgS2*(avgCounter-1)+S2)/avgCounter;
			avgV2=(avgV2*(avgCounter-1)+V2)/avgCounter;
			
			if (avgCounter >=5){

			dataFile.open("data.txt", std::ios_base::app);
			dataFile << "Time since beginning: "<< hourCount << ":00 hours \n" ;
			dataFile << "HSV (SD): " << avgH << ", " << avgS  << ", "<< avgV << "\n";
			dataFile << "HSV (C): " << avgH2 << ", " << avgS2  << ", "<< avgV2 << "\n\n";
			dataFile.close();

			hourCount +=1;
			avgCounter=0;
			avgH =0; avgS =0; avgV =0; avgH2 =0; avgS2 =0; avgV2 =0;
			}

			avgCounter+=1;
		}

	} // end while

	//Release more memory and destroy the window when done
	cvReleaseCapture(&p_capWebcam);
	cvDestroyWindow("Original");

	return(0);

}
Exemple #12
0
bool transmit::changelamp(bool flag, int id, bool status, bool oldstatus){
    char* buffer = new char[4];
    bool tmp;

    if(flag){
        buffer[0] = '?';
    }
    else{
        buffer[0] = '!';
    }

    if(true){
        switch(id+1){
        case 1:
            buffer[1] = '1';
                    break;
        case 2:
            buffer[1] = '2';
                    break;
        case 3:
            buffer[1] = '3';
                    break;
        case 4:
            buffer[1] = '4';
                    break;
        case 5:
            buffer[1] = '5';
                    break;
        case 6:
            buffer[1] = '6';
                    break;
        case 7:
            buffer[1] = '7';
                    break;
        case 8:
            buffer[1] = '8';
                    break;
        case 9:
            buffer[1] = '9';
                    break;
        case 10:
            buffer[1] = 'a';
                    break;
        case 11:
            buffer[1] = 'b';
                    break;
        case 12:
            buffer[1] = 'c';
                    break;
        case 13:
            buffer[1] = 'd';
                    break;
        case 14:
            buffer[1] = 'e';
                    break;
        case 15:
            buffer[1] = 'f';
                    break;
        default:
            break;
        }
    }
    if(status == oldstatus){
        tmp = false;
        return tmp;

    }
    if(status && status != oldstatus){
        buffer[2] = 'f';
        tmp = true;
    }
    if(!status && status != oldstatus){
        buffer[2] = '0';
        tmp = true;
    }
    buffer[3] = char(13);

    if(true){
        CSerial* serial = new CSerial();

        if(!serial->Open(2, 9600))
        {
            qDebug() << "Could not open COM";
        }
        else{
            serial->SendData(buffer, 4);

            serial->Close();
        }
        delete serial;
    }

    qDebug() << buffer[0];
    qDebug() << buffer[1];
    qDebug() << buffer[2];
    qDebug() << buffer[3];
    return tmp;
}