How Arduino actually gets measurements

I was given a project on a small power system. Basically an Arduino Uno (programmed in C) obtains measurements and outputs them onto a small LCD touchscreen display. The display can also set a couple of parameters (which i am not concerned about).

I am only concerned about the measurements the arduino is obtaining from this FPGA device. I was given this lengthy code, but I need help on understanding how the Arduino actually gets the measurements from the FPGA device. The section which I think is important is the //START Gather data from FPGA & display in Engineering Terms.

I have attached the code, and any help to understand this would be greatly appreciated.

Thanks.

UWA_UI_mega14i.ino (32.7 KB)

It looks like a simple raw data serial protocol over RS485. There should be a specification of the protocol? That would be very bad engineering if not.

Anyway the data is a sequence of 24 integers (16-bit) sent with LSB first. The last integer is a CRC. The format could be written in C++ as:

struct msg_t {
  int16_t header;
  int16_t V[8];
  int16_t I[8];
  int16_t T[4];
  int16_t Vfeedback[2];
  int16_t CRC;
};

There are a lot of obvious errors in the code that can give problems (such as the size of data[19] which should be data[24]).

Cheers!

Thanks for that!

I was wondering if you could summarize the how its actually checking for Serial data, Reading it, then storing it and printing it to the screen, perhaps in a flow diagram of sorts?

    for (k = 0; k < 2; k++)
    {
      if (Serial1.available()) 
      {
        int inByte = Serial1.read();
        Serial.print(inByte, DEC); 
        Serial.print("  ");
      }
    }
    Serial.print("V  ");
    for (k = 0; k < 8; k++)
    {
      for (j = 0; j < 2; j++)
      {
        bufout[j] = Serial1.read();
      }
      data[k] = bufout[0];
      data[k] += (bufout[1] << 8);
      Serial.print(data[k], DEC); 
      Serial.print("\t");   
      //    Serial.print(data[k]/327.68, 2); Serial.print("\t");   
    }
    S

That second set of Serial1.read looks suspect; reading from serial without first checking there’s data there to read - it looks like it’s relying on the delay introduced by the prints to “ensure” it works.

Edit: In fact, printing to a serial port that hasn’t been initialised is a really Bad IdeaTM

Do you want me to move this to “Gigs and collaborations” ?

Sure!

Hi, What is the device that the UNO gets its data from. If it is a commercial device we maybe able to help and make sure the input process is correct.

Thanks... Tom... :)

What is the device that the UNO gets its data from

if (Serial1.available())

Uno?

Hi,

Does the arduino and display work? Its hard to tell your first post. What is the problem?

Tom... :)

Ok, sorry not an Uno, it's actually a Mega.

The code does work, not particularly well. It was given to me.

I guess what i'm looking for is a breakdown of what's going on in the code, and ways I can improve it, so I can apply it to a different interface.

Sorry for not being clear in the original post.

TomGeorge: Hi, What is the device that the UNO gets its data from. If it is a commercial device we maybe able to help and make sure the input process is correct.

It get's its data from an FPGA connected to a small power system

You might want to start clearing up the problems I pointed-out in reply #3.