示例#1
0
/*
* Serial_read_string
*
* Reads a string form a specified serial port.
*
* @param int port - the port ID
* @param char * data - the array to be read into
* @param int data_length - the length of the char array
* @return int - 1 if sucessful, 0 if not
*/
int Serial_read_string(int port, char * data, int data_length) {
	char latest;
	int i = 0;

	//loop until end of data
	while (i < data_length) {
		//get latest character
		latest = Serial_read(port);
		if (latest != 0xFF) {
			if (latest == 0x0D) {
				//the input has terminated
				data[i] = 0x00;//null terminate string
				return 1;
			}
			//write the next character into the buffer
			data[i++]=latest;
		}
		//if we got back a -1 from Serial_read, just loop again
		x_yield();
	}
	//we've used more than the whole array, error
	return 0;
}
示例#2
0
void CommandProcess()
{
	// Read incoming control messages
	if (Serial_available() >= 2)
	{
		char start=Serial_read();
		if (start == '@') {// Start of new control message
			int command = Serial_read(); // Commands
			if (command == 'h') {//Hook AHRS Stack Device
				// Read ID
				char id[2];
				id[0] = GetChar();
				id[1] = GetChar();
				// Reply with synch message
				printf("@HOOK");
				Serial_write(id, 2);
			}
			else if (command == 'v') {//Check Version
				CheckVersion();
			}
			else if (command == 'c') {// A 'c'calibration command
				SensorCalibration();
			}
			else if (command == 'b') {// 'b'lock erase flash
				FlashControl();
			}
			else if (command == 'p') {// Set 'p'id command
				SetPID();
			}
			else if (command == 'm') {// Set report 'm'ode
				char mode = GetChar();
				if (mode == 'e') {// Report AHRS by 'e'uler angle
					report_mode = REPORT_AHRS_EULER;
				}
				else if (mode == 'q') {// Report // Report AHRS by 'q'quaternion
					report_mode = REPORT_AHRS_QUATERNION;
				}
				else if (mode == 'r') {// Report sensor 'r'aw data
					report_mode = REPORT_SENSORS_RAW;
				}
				else if (mode == 'c') {// Report sensor 'c'alibrated data
					report_mode = REPORT_SENSORS_CALIBRATED;
				}
				else if (mode == 'm') {// Report 'm'otor power distribution
					report_mode = REPORT_MOTOR_POWER;
				}
				else if (mode == 'v') {// Report 'v'elocity
					report_mode = REPORT_VELOCITY;
				}
				else if (mode == 's') {// Report 's'tatus
					report_status();
				}
				else if (mode == 'p') {// Report controller 'p'id
					char type = GetChar();
					if (type == 'p') // 'p'id
						report_mode = REPORT_PID;
					else if (type == 'r') //'r'ate pid
						report_mode = REPORT_RATE_PID;
					else if (type == 'a') //'a'ltitude hold pid
						report_mode = REPORT_ALTHOLD_PID;
				}
			}
			else if (command == 'f') {// Set report 'f'ormat
				char format = GetChar();
				if (format == 'b') {// Report 'b'inary format
					report_format = REPORT_FORMAT_BINARY;
				}
				else if (format == 't') {// Report 't'ext format
					report_format = REPORT_FORMAT_TEXT;
				}
			}
			else if (command == 's') {// 's'tream output control
				char mode = GetChar();
				if (mode == 's') {// 's'tart stream
					stream_mode = STREAM_START;
				}
				else if (mode == 'p') {// 'p'ause stream
					stream_mode = STREAM_PAUSE;
				}
				else if (mode == 't') {// 't'oggle stream
					if(stream_mode==STREAM_START)
						stream_mode = STREAM_PAUSE;
					else
						stream_mode = STREAM_START;
				}
			}
		}
		else { 
			if (report_format == REPORT_FORMAT_TEXT) {
			printf("Unknown command.\n");
			}
		} // Skip character
	}
}
示例#3
0
void Plugin_011_FLC(void)                                                       // wordt vanuit de hoofdloop van de Nodo-core zeer frequent aangeroepen.
  {  
  static byte count=0;
  char received;

  if(PLUGIN_011_LedTimer>millis())
    digitalWrite(PIN_WIRED_OUT_2, HIGH);
  else
    digitalWrite(PIN_WIRED_OUT_2, LOW);

  while(Serial_available())
    {
    received=Serial_read();

    if(count<PLUGIN_011_BUFFERSIZE && isprint(received) && (isalpha(received) || isxdigit(received)))
      PLUGIN_011_buffer[count++]=received;

    if(received==0x0A)
      {
      PLUGIN_011_buffer[count]=0;

      PLUGIN_011_WatchDogTimer=millis() + PLUGIN_011_WATCHDOG_TIMEOUT;

      #if HARDWARE_SERIAL_1
      Serial.print(PLUGIN_011_buffer);
      #endif // HARDWARE_SERIAL_1
              
      byte source = PLUGIN_011_buffer[0];
      byte b1     = PLUGIN_011_hextobyte(PLUGIN_011_buffer,1);
      byte b2     = PLUGIN_011_hextobyte(PLUGIN_011_buffer,3);
      byte b3     = PLUGIN_011_hextobyte(PLUGIN_011_buffer,5);
      byte b4     = PLUGIN_011_hextobyte(PLUGIN_011_buffer,7);
      
      OTGW_Data=(((unsigned long)b1)<<24) + (((unsigned long)b2)<<16) + (((unsigned long)b3)<<8) + ((unsigned long)b4);
      
      if(source==0x54)                                                          // Source = Thermostat
        {
        if (b2 == 0x18)					                                                // +1 Room Temperature
          {
          TempFloat=b3+b4*((float)1/256);
          PLUGIN_011_VarChange(PLUGIN_011_CORE + 1, TempFloat, OTGW_Data);
          }

        if (b2 == 0x10)					                                                // +5 Thermostat Set Point
          {
          TempFloat=b3+b4*((float)1/256);
          PLUGIN_011_VarChange(PLUGIN_011_CORE + 5, TempFloat, OTGW_Data);
          }
        } // if(source==0x54)


      if(source==0x42)                                                          // Source = Boiler
        {
      	if (b2 == 0x19)					                                                // +2 Boiler Water Temperature
          {
          TempFloat=b3+b4*((float)1/256);
          PLUGIN_011_VarChange(PLUGIN_011_CORE + 2, TempFloat, OTGW_Data);
          }

        if (b2 == 0x11)					                                                // +3 Relative Modulation
          {
          TempFloat=b3+b4*((float)1/256);
          PLUGIN_011_VarChange(PLUGIN_011_CORE + 3, TempFloat, OTGW_Data);
          }

        if (b2 == 0x12)					                                                // +4 Boiler Water Pressure
          {
          TempFloat=b3+b4*((float)1/256);
          PLUGIN_011_VarChange(PLUGIN_011_CORE + 4, TempFloat, OTGW_Data);
          }

        if (b2 == 0x00)					                                                // Boiler Status
          {
          TempFloat=(float)((b4 & 0x8) >> 3);                                   // +6 Flame Status in bit 3
          PLUGIN_011_VarChange(PLUGIN_011_CORE + 6, TempFloat, OTGW_Data);

          TempFloat=(float)((b4 & 0x4) >> 2);	  		                            // +8 DHW Mode in bit 2
          PLUGIN_011_VarChange(PLUGIN_011_CORE + 8, TempFloat, OTGW_Data);
          }

        if (b2 == 0x1C)					                                                // +7 Boiler Water Return Temperature
          {
          TempFloat=b3+b4*((float)1/256);
          PLUGIN_011_VarChange(PLUGIN_011_CORE + 7, TempFloat, OTGW_Data);
          }

        }// if (source==0x42)
        
      #if HARDWARE_SERIAL_1
      Serial.println();
      #endif

      PLUGIN_011_buffer[0]=0;
      count=0;      
      } // if(received[x]==..)
/* Model output function */
void XbeeSerialRead_output(void)
{
  uint8_T rtb_DataTypeConversion_j;
  int16_T rtb_SerialReceive_o2;
  uint8_T tmp[11];

  /* S-Function (arduinoserialread_sfcn): '<Root>/Serial Receive' */
  Serial_read(XbeeSerialRead_P.SerialReceive_p1,
              XbeeSerialRead_P.SerialReceive_p2, &rtb_DataTypeConversion_j,
              &rtb_SerialReceive_o2);

  /* Switch: '<Root>/Switch' incorporates:
   *  UnitDelay: '<Root>/Unit Delay'
   */
  if (rtb_SerialReceive_o2 > XbeeSerialRead_P.Switch_Threshold) {
    XbeeSerialRead_B.Switch = rtb_DataTypeConversion_j;
  } else {
    XbeeSerialRead_B.Switch = XbeeSerialRead_DW.UnitDelay_DSTATE;
  }

  /* End of Switch: '<Root>/Switch' */

  /* DataTypeConversion: '<S1>/Data Type Conversion' */
  if (XbeeSerialRead_B.Switch < 256.0) {
    if (XbeeSerialRead_B.Switch >= 0.0) {
      rtb_DataTypeConversion_j = (uint8_T)XbeeSerialRead_B.Switch;
    } else {
      rtb_DataTypeConversion_j = 0U;
    }
  } else {
    rtb_DataTypeConversion_j = MAX_uint8_T;
  }

  /* End of DataTypeConversion: '<S1>/Data Type Conversion' */

  /* S-Function (arduinoanalogoutput_sfcn): '<S1>/PWM' */
  MW_analogWrite(XbeeSerialRead_P.PWM_pinNumber, rtb_DataTypeConversion_j);

  /* DataTypeConversion: '<S2>/Data Type Conversion' incorporates:
   *  Constant: '<S2>/Footer 1'
   *  Constant: '<S2>/Footer 2'
   *  Constant: '<S2>/Header'
   */
  if (XbeeSerialRead_P.Header_Value < 256.0) {
    if (XbeeSerialRead_P.Header_Value >= 0.0) {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[0] = (uint8_T)XbeeSerialRead_P.Header_Value;
    } else {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[0] = 0U;
    }
  } else {
    /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
    tmp[0] = MAX_uint8_T;
  }

  if (XbeeSerialRead_B.Switch < 256.0) {
    if (XbeeSerialRead_B.Switch >= 0.0) {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[1] = (uint8_T)XbeeSerialRead_B.Switch;
    } else {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[1] = 0U;
    }
  } else {
    /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
    tmp[1] = MAX_uint8_T;
  }

  if (XbeeSerialRead_B.Switch < 256.0) {
    if (XbeeSerialRead_B.Switch >= 0.0) {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[2] = (uint8_T)XbeeSerialRead_B.Switch;
    } else {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[2] = 0U;
    }
  } else {
    /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
    tmp[2] = MAX_uint8_T;
  }

  if (XbeeSerialRead_B.Switch < 256.0) {
    if (XbeeSerialRead_B.Switch >= 0.0) {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[3] = (uint8_T)XbeeSerialRead_B.Switch;
    } else {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[3] = 0U;
    }
  } else {
    /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
    tmp[3] = MAX_uint8_T;
  }

  if (XbeeSerialRead_B.Switch < 256.0) {
    if (XbeeSerialRead_B.Switch >= 0.0) {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[4] = (uint8_T)XbeeSerialRead_B.Switch;
    } else {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[4] = 0U;
    }
  } else {
    /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
    tmp[4] = MAX_uint8_T;
  }

  if (XbeeSerialRead_B.Switch < 256.0) {
    if (XbeeSerialRead_B.Switch >= 0.0) {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[5] = (uint8_T)XbeeSerialRead_B.Switch;
    } else {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[5] = 0U;
    }
  } else {
    /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
    tmp[5] = MAX_uint8_T;
  }

  if (XbeeSerialRead_B.Switch < 256.0) {
    if (XbeeSerialRead_B.Switch >= 0.0) {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[6] = (uint8_T)XbeeSerialRead_B.Switch;
    } else {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[6] = 0U;
    }
  } else {
    /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
    tmp[6] = MAX_uint8_T;
  }

  if (XbeeSerialRead_B.Switch < 256.0) {
    if (XbeeSerialRead_B.Switch >= 0.0) {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[7] = (uint8_T)XbeeSerialRead_B.Switch;
    } else {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[7] = 0U;
    }
  } else {
    /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
    tmp[7] = MAX_uint8_T;
  }

  if (XbeeSerialRead_B.Switch < 256.0) {
    if (XbeeSerialRead_B.Switch >= 0.0) {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[8] = (uint8_T)XbeeSerialRead_B.Switch;
    } else {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[8] = 0U;
    }
  } else {
    /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
    tmp[8] = MAX_uint8_T;
  }

  if (XbeeSerialRead_P.Footer1_Value < 256.0) {
    if (XbeeSerialRead_P.Footer1_Value >= 0.0) {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[9] = (uint8_T)XbeeSerialRead_P.Footer1_Value;
    } else {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[9] = 0U;
    }
  } else {
    /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
    tmp[9] = MAX_uint8_T;
  }

  if (XbeeSerialRead_P.Footer2_Value < 256.0) {
    if (XbeeSerialRead_P.Footer2_Value >= 0.0) {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[10] = (uint8_T)XbeeSerialRead_P.Footer2_Value;
    } else {
      /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
      tmp[10] = 0U;
    }
  } else {
    /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
    tmp[10] = MAX_uint8_T;
  }

  /* End of DataTypeConversion: '<S2>/Data Type Conversion' */

  /* S-Function (arduinoserialwrite_sfcn): '<S2>/Serial Transmit' */
  Serial_write(XbeeSerialRead_P.SerialTransmit_portNumber, tmp, 11UL);
}