void loop()
{
    sendMsg = "";
    recvMsg = "";
    while (Serial.available()) {
        sendMsg += (char)Serial.read();
        delay(2);
    }

    if(sendMsg.length() > 0)
    {
        mySerial1.println(sendMsg);
        Serial.print("I send: ");
        Serial.println(sendMsg);
    }
    while (mySerial2.available()) {
        recvMsg += (char)mySerial2.read();
        delay(2);
    }
    if(recvMsg.length() > 0)
    {
        Serial.print("I recv: ");
        Serial.println(recvMsg);
    }
    //delay(20);
}
template<class T> int readObject(HardwareSerial ser, T& value, char name) {
	unsigned char* p = (unsigned char*) (void*) &value;
	unsigned int i;
	char checksum = '0';
	for (i = 0; i < sizeof(value); i++) {
		*p++ = ser.read();
		checksum = checksum ^ *p;
	}
	//TODO: make sure this actually works
	if (ser.read() == checksum) {
		Serial.print("Checksum valid");
	} else {
		Serial.print("Checksum invalid");
	}

	return i;
}
示例#3
0
int
main(int argc,char **argv) {

	pinMode(led_pin, OUTPUT);
	digitalWrite(led_pin, led_off);
	digitalWrite(reset_pin, HIGH);
	pinMode(reset_pin, OUTPUT);
	Serial.begin(baud);	// USB, communication to PC or Mac
	Uart.begin(baud);	// UART, communication to Dorkboard

	for (;;) {
		unsigned char c, dtr;
		static unsigned char prev_dtr = 0;

		if (Serial.available()) {
			c = Serial.read();
			Uart.write(c);
			digitalWrite(led_pin, led_on);
			led_on_time = millis();
			continue;
		}
		if (Uart.available()) {
			c = Uart.read();
			Serial.write(c);
			digitalWrite(led_pin, led_on);
			led_on_time = millis();
			continue;
		}
		dtr = Serial.dtr();
		if (dtr && !prev_dtr) {
			digitalWrite(reset_pin, LOW);
			delayMicroseconds(250);
			digitalWrite(reset_pin, HIGH);
		}
		prev_dtr = dtr;
		if (millis() - led_on_time > 3) {
			digitalWrite(led_pin, led_off);
		}
		if (Serial.baud() != baud) {
			baud = Serial.baud();
			if (baud == 57600) {
				// This ugly hack is necessary for talking
				// to the arduino bootloader, which actually
				// communicates at 58824 baud (+2.1% error).
				// Teensyduino will configure the UART for
				// the closest baud rate, which is 57143
				// baud (-0.8% error).  Serial communication
				// can tolerate about 2.5% error, so the
				// combined error is too large.  Simply
				// setting the baud rate to the same as
				// arduino's actual baud rate works.
				Uart.begin(58824);
			} else {
				Uart.begin(baud);
			}
		}
	}
}
示例#4
0
void Utils::readUntil (HardwareSerial& stream, char terminator, char* buf, int bufLen) {
	int idx = 0;
	while (idx < bufLen - 1) {
		int c = stream.read ();
		if (c == terminator) {break;}
		if (c < 0) {continue;}
		buf[idx++] = c;
	}
	buf[idx++] = 0;
}
示例#5
0
/***
 * SerSend [hard | soft] [port]
 *
 * After this command, each character sent is mirrored to the chosen serial
 * port until the NULL character (0x00) is sent (also mirrored)
 */
void cmdSerSend(char **argV) {
	boolean isSoftSerial = (strcasecmp(argV[1], "soft") == 0);
	int currPort = strtol(argV[2], NULL, 10);
	Serial.println(doneString);

	if (currPort < 1 || currPort > ((isSoftSerial)? SOFT_SER_MAX_PORTS : HARD_SER_MAX_PORTS)) {
		return;
	}
	if (isSoftSerial) {
#ifdef USE_SOFTWARE_SERIAL
		softSerDescs[currPort-1].txMsgLen = 0;
#endif
	}

	// mirror the hardware serial and the software serial
	while (true) {
		if (Serial.available()) {
			char c = Serial.read();
			if (isSoftSerial) {
#ifdef USE_SOFTWARE_SERIAL
				softSerDescs[currPort-1].txMsg[softSerDescs[currPort-1].txMsgLen++] = c;
#endif
			} else {
#if HARD_SER_MAX_PORTS > 0
				hardSerHandler[currPort-1]->write(c);
#else
				return;
#endif
			}

			if (c == '\0') {
				// acknowledge
				Serial.println(doneString);
				delay(10);
#ifdef USE_SOFTWARE_SERIAL
				if (isSoftSerial) {
					// send the message, and remember the answer
					for (int i = 0; i < softSerDescs[currPort-1].txMsgLen; i++) {
						softSerDescs[currPort-1].handler->write(softSerDescs[currPort-1].txMsg[i]);
					}
					softSerDescs[currPort-1].rxMsgLen = 0;
					while (!Serial.available()) {
						if (softSerDescs[currPort-1].handler->available() && softSerDescs[currPort-1].rxMsgLen < SOFT_SER_MSG_SIZE) {
							softSerDescs[currPort-1].rxMsg[softSerDescs[currPort-1].rxMsgLen++] = softSerDescs[currPort-1].handler->read();
						}
					}
				}
#endif
				return;
			}
		}
	}
}
示例#6
0
// get next byte from serial stream
byte Comm::serial_get(bool block)
{
    if (block) {
        while (true) {
            if (Serial.available() > 0) {
                break;
            }
        }
    }

    return Serial.read();
}
示例#7
0
//call like : serialRead (Serial1, buffer, 12, 5)
uint8_t serialRead (HardwareSerial theSerial, 
		uint8_t *buf, uint8_t leng, uint8_t timeout) {
	int sub;
	if (theSerial.available ()) {
		for (sub=0; sub<leng; sub++) {
			uint32_t start_time = millis ();
			while (!theSerial.available ()) {
				if (millis () - start_time > timeout)
					return sub;
			}
			buf[sub] = theSerial.read ();
		}
		return sub;
	}
	return 0;
}
示例#8
0
void loop()
{
    if (Dennao.available() > 0) {
        read_buf_len = Dennao.recv(read_buf, DENNAO_RX_SIZE, 0);
        for(int i=0;i<read_buf_len;i++){
            Uart.print((char)read_buf[i]);
        }
    }
    if (Uart.available() > 0) {
        uint8_t incomingByte = Uart.read();
        if(incomingByte=='\r'){
            Dennao.send(out_buf, out_buf_len, 0);
        }else{
            out_buf[out_buf_len++] = incomingByte;
        }
    }
}
示例#9
0
void updateSerialReader()
{
    while(Serial.available() > 0 && parserReadyDelegate())
    {
        (*lineBuffer) = Serial.read();

        if(*lineBuffer == '\n')
        {
            lineBuffer = &buffer[0];
        	if(readingRequest)
        		parseRequest(lineBuffer);
        	else
        		parseGCodeDelegate(lineBuffer);
        }
        else if(*lineBuffer == '@')
        	readingRequest = true;
        else if(*lineBuffer != 32)
        	lineBuffer++;
    }
}
示例#10
0
int main()
{

    int incomingByte = 0;   // for incoming serial data

    setup();

    // register Ctrl-C signal
    signal(SIGINT, &trap);

    while(execute)
    {

        //numc = Serial.available();
        //            bytedata = Serial.read();
        //printf("0x%d\n", data);


        // send data only when you receive data:
        if (Serial.available() > 0)
        {

            for(int ii=0; ii<Serial.available(); ii++){
                //cout << "Serial.available() returned: " << Serial.available() << endl;
                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                printf("%c", incomingByte);
            }
        }

        boost::this_thread::sleep(boost::posix_time::milliseconds(10));
    }

    Serial.end();
    return(0);
}
示例#11
0
void loop()                     // run over and over again
	{
	Modes();
	char c;
	if (Uart.available())
		{
		c = Uart.read();
		if(c == '<') // Start of packet
			{
			started = 1;
			ended = 0;
			}
		else if(c == '>') // End of packet
			{
			ended = 1;
			}
		else
			{
			buffer[serialIn] = c;
			serialIn++;
			buffer[serialIn] = '\0';
			}
		}
	if(started && ended) // complete packet
		{
		if(buffer[0] == 'H' && buffer[1] == 'M' && buffer[2] == 'G') //header
			{
			Serial.println("RECEIVED HEADER");
			parseSerialData(); // sorts data based on channel
			}
		serialIn = 0;
		buffer[serialIn] = '\0';
		started = 0;
		ended = 0;
		}
	}
示例#12
0
void loop() {
   // Get serial input from Processing
   if (Serial.available()) {

     char ch = Serial.read();
     switch(ch){

      case '0'...'9':
        rawSerial = rawSerial * 10 + ch - '0';
        Serial.println(rawSerial);
      break;

      case 'x':
       //pulsewidth = map(rawSerial, 0,100, 0,180);
       servos[3].write(rawSerial);
       rawSerial = 0;
      break;
      case 'y':
       //pulsewidth = map(rawSerial, 0,100, 0,180);
       servos[3].write(rawSerial);
       rawSerial = 0;
      break;
     }
     }
示例#13
0
/***
 * The loop function is called in an endless loop
 */
void loop() {
	char c, argC;
	char *argV[ARGV_MAX];
	int i, pin;
	unsigned long curMs;

	// Take care of blinking LED
	if (startBlinking == true) {
		curMs = millis();
		if (curMs > blinkLastChangeMs + blinkingDelayMs) {
			blinkLastChangeMs = curMs;
			if (digitalRead(blinkingPin) == HIGH) {
				digitalWrite(blinkingPin, LOW);
			} else {
				digitalWrite(blinkingPin, HIGH);
			}
		}
	}

#ifdef USE_PID
	// Take care PID-relay variables
	for (i = 0; i < PID_RELAY_MAX_VARS; i++) {
		if (pidRelayDescs[i].isOn) {
			pidRelayDescs[i].inputVar = analogRead(pidRelayDescs[i].pinAnalIn);
			pidRelayDescs[i].handler->Compute();

			// turn relay on/off according to the PID output
			curMs = millis();
			if (curMs - pidRelayDescs[i].windowStartTime > pidRelayDescs[i].windowSize) {
				//time to shift the Relay Window
				pidRelayDescs[i].windowStartTime += pidRelayDescs[i].windowSize;
			}
			if (pidRelayDescs[i].outputVar > curMs - pidRelayDescs[i].windowStartTime) {
				digitalWrite(pidRelayDescs[i].pinDigiOut, HIGH);
			}
			else {
				digitalWrite(pidRelayDescs[i].pinDigiOut, LOW);
			}
		}
	}
#endif

	// Read characters from the control serial port and act upon them
	if (Serial.available()) {
		c = Serial.read();
		switch (c) {
		case '\n':
			break;
		case '\r':
			// end the string and init pMsg
			Serial.println("");
			*(pMsg++) = NULL;
			pMsg = msg;
			// parse the command line statement and break it up into space-delimited
			// strings. the array of strings will be saved in the argV array.
			i = 0;
			argV[i] = strtok(msg, " ");

			do {
				argV[++i] = strtok(NULL, " ");
			} while ((i < ARGV_MAX) && (argV[i] != NULL));

			// save off the number of arguments
			argC = i;
			pin = strtol(argV[1], NULL, 10);

			if (strcasecmp(argV[0], "Set") == 0) {
				cmdSet(argV);
			} else if (strcasecmp(argV[0], "Reset") == 0) {
				cmdReset();
			} else if (strcasecmp(argV[0], "BlinkPin") == 0) {
				cmdBlinkPin(argV);
			} else if (strcasecmp(argV[0], "Read") == 0) {
				cmdRead(argC, argV);
			} else if (strcasecmp(argV[0], "Write") == 0) {
				cmdWrite(argV);
			} else if (strcasecmp(argV[0], "SetPwmFreq") == 0) {
				cmdSetPwmFreq(argV);
#ifdef USE_PID
			} else if (strcasecmp(argV[0], "PidRelayCreate") == 0) {
				cmdPidRelayCreate(argV);
			} else if (strcasecmp(argV[0], "PidRelaySet") == 0) {
				cmdPidRelaySet(argV);
			} else if (strcasecmp(argV[0], "PidRelayTune") == 0) {
				cmdPidRelayTune(argV);
			} else if (strcasecmp(argV[0], "PidRelayEnable") == 0) {
				cmdPidRelayEnable(argV);
#endif
			} else if (strcasecmp(argV[0], "HardSerConnect") == 0) {
				cmdHardSerConnect(argV);
			} else if (strcasecmp(argV[0], "SoftSerConnect") == 0) {
				cmdSoftSerConnect(argV);
			} else if (strcasecmp(argV[0], "SerSend") == 0) {
				cmdSerSend(argV);
			} else if (strcasecmp(argV[0], "SerReceive") == 0) {
				cmdSerReceive(argV);
#ifdef USE_WIRE
			} else if (strcasecmp(argV[0], "I2cWrite") == 0) {
				cmdI2cWrite(argC, argV);
#endif
			} else {
				// Wrong command
				return;
			}

			// Acknowledge the command
			Serial.println(doneString);
			break;
		default:
			// Record the received character
			if (isprint(c) && pMsg < msg + sizeof(msg)) {
				*(pMsg++) = c;
			}
			break;
		}
	}
}