Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I try to send 1 character and 8 variables (26 bytes) from arduino as struct over com port, i receive something but my inexperience in C# do note help,

I use this code to send data from arduino to app over com port. the first character sent
Serial.write('/');
is to know when to start reading the struct
C++
Typedef struct {
  char 1start;
  float 2a;
  float 3a;
  float 4a;
  float 5a;
  float 6a;
  float 7a; 
  char 8end;
  } Payload;
  Payload payload; 

void setup() {
  Serial.begin(9600);

  }

void loop() {

  payload.2a=2.22;
  payload.3a=3.33;
  payload.4a=4.44;
  payload.1start='#';
  payload.8end='&';
  payload.5a=5.55;
  payload.6a=6.66;
  payload.7a=7.77;

_writeAnything(0, payload);
}

template <class T> long int _writeAnything(int ee, T& valuebt)
{
  Serial.write('/');
  Serial.write((byte *) &valuebt, sizeof(valuebt));
}


On the c# app side is the problem here i receive something but by far is nothing that i send, i know the first character sent is received
Serial.write('/');
the problem is to put the information back in one structure

C#
namespace ConsoleApp1
{
    class Program
    {
        public struct PayloadSerial
        {
            public char 1start;
            public float 2b;
            public float 3b;
            public float 4b;
            public float 5b;
            public float 6b;
            public float 7b;
            public char 8tend;
        }



        static SerialPort _serialPort;
        static char rc;
 


        public static unsafe void Main()
        {
            _serialPort = new SerialPort();
            _serialPort.PortName = "COM7";//Set your board COM
            _serialPort.BaudRate = 9600;
            _serialPort.DataBits = 8;
            _serialPort.Parity = Parity.None;
            _serialPort.StopBits = StopBits.One;
            _serialPort.Handshake = Handshake.None;
            _serialPort.DtrEnable = true;
            _serialPort.RtsEnable = true;
            _serialPort.Open();

           
            PayloadSerial payloadSerial;
            while (true)
            {
     
                char someText = (char)_serialPort.ReadChar();
                rc = someText;
                if (rc == '/')
                {

                    byte* p = (byte*)&payloadSerial;
                         int i;
                         for (i = 0; i < 26; i++)
                         {
                             *p++ = (byte)_serialPort.ReadByte();
                         }




                    //-------------------
                    //  if (payloadSerial.1start == '#' && payloadSerial.8end == '&') {

                    Console.WriteLine(payloadSerial.2b);
                     Console.WriteLine(payloadSerial.3b);
                     Console.WriteLine(payloadSerial.4b);
                     Console.WriteLine(payloadSerial.1tstart);
                     Console.WriteLine(payloadSerial.8tend);
                     Console.WriteLine(payloadSerial.5b);
                     Console.WriteLine(payloadSerial.6b);
                     Console.WriteLine(payloadSerial.7b);
                         Console.WriteLine(" ");
                         Console.WriteLine(" ");

               
                    Thread.Sleep(50);

                 }
            }

        }

    }
}




what i receive is values not randomly 3.54846E-38 ..... or E-40 but are constant

What I have tried:

Because i do not have experience in C#, i tried all things over google but nothing was useful because all the time i find pieces of code and i do not know what to include or where to put it.
try changing char / byte
*p++ = (byte)_serialPort.ReadByte();

try changing bytes read
for (i = 0; i < 26; i++)
Posted
Updated 1-Dec-19 0:35am
Comments
Richard MacCutchan 1-Dec-19 6:30am    
A char type in C# is 16 bits wide, are you sure that Arduino is the same?

1 solution

The first thing to note is that you can't define those structs in C, C++, or C# - variable names cannot start with a numeric digit in any of those languages.
So quite probably, what you are sending / receiving is nothing like what you expected because that code will not compile, and you are probably running old EXE files.

But ... even if you fix that, when you define a struct like this:
C++
Typedef struct {
  char c1;
  float f;
  char c2;
  } Payload;
You will not get a 6 byte struct - floats have to be aligned to a 4 byte address boundary, so your first char will be "padded" to occupy 4 bytes. Aditionally, the final char will also be padded to a 4 byte boundary, which is why you get 12 bytes shown here:
C++
#include <stdio.h>

typedef struct {
  char c1;
  float f;
  char c2;
  } Payload;
int main()
{
    printf("%lu\n", sizeof(Payload));

    return 0;
}
Exactly the same thing will happen with C#, for the same reasons.

The next thing to consider is "endianness" - in this case you are probably OK, because both the PC and the Arduino are "little endian" - which means that numbers are stored with the least significant byte of the value in the lowest memory byte address. But be aware that that is not the case for all systems!
 
Share this answer
 
Comments
HUNT333R 1-Dec-19 7:58am    
First of all tahnk you verry much for the response, in my code the struct variables are not including numbers I just changed them here for a better understanding.

In this case the define of the variables are not the problem,.
Now the data sent, I think I send 26 bytes of data ... How do I know ? On the arduino side I put a line SerialPrint(sizeof(Payload)) and allways is returning 26
Can you please help me with an example how to read the struct? I find only pices of examples.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900