2

I am working on arduino Uno board.I am trying to send request Serially from out Side using docklight.I am sending COmmand is request send every 5S to 10S and get response.When i tested this code with arduino Uno board its working fine. When i tested same code with Customised board whos schematic as below Schemactic. For this i got response like this. I am suspecting it might be issue with crystal. I have replace KDS16 with SCTF16.00crystals . It behave same.Can someone suggest what might be wrong

Respose

enter image description here

My code

#include "glob.h"
const int BUFFER_SZ = 32;
char sendBuffer[200]; 
static int OverRide=0;

void Take_Reading() {

  for (int row = 0; row < 8; row++)
  {
    //   // get rid of me:
    digitalWrite(SO_enable, array[row][0]);
    digitalWrite(S1_enable, array[row][1]);
    digitalWrite(S2_enable, array[row][2]);
    delay(50);
    analog_1_pv[row] = ANALOG_SCALING * analogRead(A0);
    analog_2_pv[row] = ANALOG_SCALING * analogRead(A1);
    analog_3_pv[row] = ANALOG_SCALING * analogRead(A2);
    if ( (analog_1_pv[row] <= MINV_RANGE || (analog_1_pv[row] >= MAXV_RANGE) ))
    {
      Current_Value1[row] =  0.0;
    }
    else
    {
      Current_Value1[row] =  (analog_1_pv[row] * 12.5) - 31.25;

    }

    if (  (analog_2_pv[row] <= MINV_RANGE || (analog_2_pv[row] >= MAXV_RANGE) ))
    {
      Current_Value2[row] =  0.0;
    }
    else
    {
      Current_Value2[row] =  (analog_2_pv[row] * 12.5) - 31.25;

    }

    if (  (analog_3_pv[row] <= MINV_RANGE || (analog_3_pv[row] >= MAXV_RANGE) ))
    {
      Current_Value3[row] =  0.0;
    }
    else
    {
      Current_Value3[row] =  (analog_3_pv[row] * 12.5) - 31.25;

    }


    Mod_current[row] = (uint16_t)(Mul_Factor * Current_Value1[row]);
    Mod_current[row + 8] = (uint16_t)(Mul_Factor * Current_Value2[row]);
    Mod_current[row + 16] = (uint16_t)(Mul_Factor * Current_Value3[row]);


  }

}









int ID_Check() {

  int ID_value;
  for (int row = 0; row < 8; row++)
  {
    digitalWrite(SO_enable, array[row][0]);
    digitalWrite(S1_enable, array[row][1]);
    digitalWrite(S2_enable, array[row][2]);
    Status_Out[row] = digitalRead(Output_Read);

  }

  ID_value = 1 * Status_Out[7] + 2 * Status_Out[6] + 4 * Status_Out[5] + 8 * Status_Out[4] + 16 * Status_Out[3] + 32 * Status_Out[2] + 64 * Status_Out[1] + 128 * Status_Out[0];
  return (ID_value);

}




int  Take_Temp_Reading()
{

  Temp_Total = Temp_Total - Temp_readings[Temp_index];
  Temp_readings[Temp_index] = analogRead(A5);
  Temp_Total = Temp_Total + Temp_readings[Temp_index];
  Temp_index = Temp_index + 1;

  if (Temp_index >= numReadings)
  {
    Temp_index = 0;
    Temp_Average = Temp_Total / numReadings;
  }
  temp = (Temp_Average * 5.0) / 1023.0;


  //  temp = float(analogRead(A5)) * 5.0 / 1024.0;
  temp_int = (int)(temp * 100.0);
  return (temp_int);
  // Serial.print("Temp"); Serial.println(temp);
}

int SPD_Check()
{

  SPD_STATUS = digitalRead(SPD_STATUS_PIN);
  return (SPD_STATUS);
}

int  DC_Status()
{

  DC_STATUS = digitalRead(DC_STATUS_PIN);
  return (DC_STATUS);

}



int   Take_HV_Reading()
{

  //int analog_int= analogRead(A4);

  total = total - readings[index];
  // read from the sensor:
  readings[index] = analogRead(A4);
  // add the reading to the total:
  total = total + readings[index];
  // advance to the next position in the array:
  index = index + 1;

  // if we're at the end of the array...
  if (index >= numReadings1)
  {
    index = 0;
    average = total / numReadings1;

  }
  else
  {

    HV_voltage = (average * 5.0) / 1023.0;
  }
  if (HV_voltage <= 0.25)
  {

    Conv_HV_voltage = 0.0;
  }
  else
  {
    Conv_HV_voltage = 197.837837838 * HV_voltage + 10.8108108108;
  }

  HV_Reading = (uint16_t)(Conv_HV_voltage * 10);

  //  HV_Reading = (uint16_t)(Conv_HV_voltage * 10);

  return(HV_Reading);
}


void  Serial_Command1()

{
  Take_Reading();
  int Temp_read=Take_Temp_Reading();
  int HV_Read=Take_HV_Reading();
  SPD=SPD_Check();
  DISCONNECTOR=DC_Status();
  // Device_ID=ID_Check();
  Device_ID=1;
  Serial.print("SMCB1");
  Serial.print(",");
  Serial.print(Device_ID);
  Serial.print(",");
  for (int i = 0; i < 14; i++) {
    Serial.print(Mod_current[i]);
    Serial.print(",");
  }

  for (int i = 23; i > 13; i--) {
    Serial.print(Mod_current[i]);
    Serial.print(",");
  }

  Serial.print(Temp_read);
  Serial.print(",");
  Serial.print(HV_Read);
  Serial.print(",");
  Serial.print(SPD);
  Serial.print(",");
  Serial.print(DISCONNECTOR);
  Serial.print(",");
  Serial.print(DISCONNECTOR);
  Serial.print(",");
  Serial.println("END");
}

void Serial_Command2(int a)
{
  Take_Reading();
  int Temp_read=Take_Temp_Reading();
  int HV_Read=Take_HV_Reading();
  SPD=SPD_Check();
  DISCONNECTOR=DC_Status();
  Device_ID=1;
  // Serial.print("Command2 executed: ");
  snprintf(sendBuffer, a, "SMCB1,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d ,END",Device_ID,
  Mod_current[0], Mod_current[1], Mod_current[2], Mod_current[3],
  Mod_current[4], Mod_current[5], Mod_current[6], Mod_current[7],
  Mod_current[8], Mod_current[9], Mod_current[10], Mod_current[11],
  Mod_current[12], Mod_current[13], Mod_current[14], Mod_current[23],
  Mod_current[22], Mod_current[21], Mod_current[20], Mod_current[19],
  Mod_current[18], Mod_current[17], Mod_current[16], Mod_current[15],
  Mod_current[14], Temp_read, HV_Read, SPD, DISCONNECTOR);
  Serial.println(sendBuffer);
  //Serial.println(a);
}





void setup()
{
  Serial.begin(57600);
  Serial.println("Format 1: <SMCB1,1>");
  Serial.println("Format 2: <SMCB1,1,Length>");
  Serial.println("Format 2: <SMCB1,1,Length,ON> ON- turn on timer ,off to turn off timer");
}

// Parse the request, WITHOUT the '<' and '>' delimiters.
void parseRequest(char *request)
{
  // Check the request starts with the prefix "SMCB1,1".
  if (strncmp(request, "SMCB1,1", 7) != 0) {
    Serial.println(F("Error: bad request prefix"));
   //  Serial_Command1();
    return;
  }

  // Remove that known prefix.
  // Now we have either "", ",Length" or ",Timer,On_OFF"
  request += 7;

  // Format 1: ""
  if (request[0] == '\0') {
    //Serial.println(F("Received format 1"));
    Serial_Command1();
    return;
  }

  // Remove the leading comma.
  // Now we have either "Length" or "Timer,On_OFF"
  if (request[0] != ',') {
    Serial.println(F("Error: ',' expected"));
    return;
  }
  request++;

  // Format 2: "Length", no comma.
  char * comma = strchr(request, ',');
  if (!comma) {

    int length = atoi(request);
    Serial_Command2(length);
    //Serial.print(F("Received format 2, length = "));
    // Serial.println(length);
    return;
  }

  // format 3
  int timer = atoi(request);
  char * on_off = comma + 1;
  char * Compare="ON";
  bool on = strcmp(on_off, "ON") == 0;
  if (!on && strcmp(on_off, "OFF") != 0) {  // sanity check
    Serial.println(F("Error: expected either ON or OFF"));
    return;
  }
  /*  Serial.print(F("Received format 3, timer = "));
   Serial.print(timer);
   Serial.print(F(", on_off = "));
   Serial.println(on ? "ON" : "OFF");
   Serial.println(on_off);
   Serial.println(Compare);*/
  int v = strcmp(on_off, Compare);
  if(v==0)
  {
    Serial.println("Timer is on");
    OverRide=1;

  }
  else
  {
    Serial.println("Timer is OFF");
    OverRide=0;
  }

}

void loop()
{
  static char buffer[BUFFER_SZ];  // received chars
  static size_t pos;              // current position in buffer
  static bool insideRequest;      // are we between '<' and '>'?

  if (Serial.available()) {
    char c = Serial.read();
    if (insideRequest) {
      if (c == '>') {  // end of request received
        buffer[pos] = '\0';  // terminate the string
        parseRequest(buffer);
        pos = 0;             // get ready for the next request
        insideRequest = false;
      }
      else if (pos < BUFFER_SZ-1) {  // add char to buffer
        buffer[pos++] = c;
      }
    }
    else if (c == '<') {  // start of request
      insideRequest = true;
    }



  }


}
6
  • 1
    Test your custom board with a much simpler sketch, for example one that just gives a simple serial output or maybe counts, and edit exactly what you receive compared to what is expected into your question. It is worrisome that there does not seem to be a ground connection on the serial header on this custom board - you must have a common ground with the other end of the serial link. Jun 7 '16 at 7:13
  • i dont have problem in receving data. I tested already. I am printing Serial statement for Every 1S-10S code seem wok fine.
    – AMPS
    Jun 7 '16 at 7:16
  • I tested code with Simple [print statement . Where i can able to receive data without any garbage value or miss of data.Yes i have connected Common Ground connection.I am worried code working fine over Arduino not over customised board.Whenever change crystal ,For some crystal more response coming normally.i.e out of 10 request i am getting error for 2
    – AMPS
    Jun 7 '16 at 7:21
  • Could you post a photo of the board? The traces to the crystal might be too long e.g.. Could you use a lower speed, than 57600 baud.
    – Gerben
    Jun 7 '16 at 9:36
  • 1
    Could you post a photo of the board?
    – Gerben
    Jun 7 '16 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.