CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Data Transfer Between PICs - SOLVED!!!

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Gabriel



Joined: 03 Aug 2009
Posts: 1074
Location: Panama

View user's profile Send private message

Data Transfer Between PICs - SOLVED!!!
PostPosted: Thu Apr 10, 2025 6:56 am     Reply with quote

Hi Guys, ive been fighting with this for a few days now. It feels like looking at Schrödinger's cat.

PREMISE:
Im building an application/board where i have 2 PICs - one handles the machine itself, the other handles the HMI for the operator.

the machine PIC reads sensors and said sensor reading need to be displayed on the HMI screen controled by the second processor.
Data needs to be exchanged between the 2 PICs, and ive provided TX/RX lines between them. Harware UARTS on both Processors.

INTENDED SOLUTION:
I am Packing all my variables into a Struct, and then serializing the struct through the UART. Most of these values are FLOATs, and a few flags.
Im using the flags included in the Struct to control flow.

Processor 2 (HMI) sends a byte to Processor 1 (machine) indicating its ready to receive data, Processor 1 updates its sensor values, and shoots out the struct to Processor 2 where its received and the cycle repeats.
im calling this "interbus" - just for clarity on my code/mind.

PROBLEM !!!!:
On Processor 1 while doing my initial tests, i simply initialized my 2 floats as 12.34 and 56.78, simple right.
if i keep these initial conditions, everything works just fine. i get nice flow control, P2 Requests data, P1 received the request and replys. the world is good.
if i send any other values, like ACTUAL sensor readings the serialized struct is completely messed up. the flow control flags turn into wierd values, the float bytes dont make sense on PROCESSOR 1 (Machine) - this is the wierdest thing ever. you can see from my code im tying to print values before and after etc and the values are fine but the serialized data is garbage BEFORE SENDING IT! the problem seems to be on the SENDING side not even on the receiver.

using actual sensor values:
the code runs, i get to the sending part, PRINT MY STRUCT VALUES - they are PERFECT - the serialize the struct into garbage - send garbage, print the struct again, its perfect - the received obviously fails as it got sent garbage.

using fixed values: all is great.

I am pulling my hair out - i know this solution may have its flaws or tricks - this code does not have any checks yet or a CRC which i will include. this right now is just a small scale test, at which i am failing misserably.

if you have easier methods, im all ears.


TESTS:
Ive coded a small sample program. im using a small struct as follows:
Both processors have the same struct.

Code:
struct P2_Data //DATA FROM P1
{
    float P2_Data_1;
    float P2_Data_2;
   
    int  P2_Data_Ready;
    int P2_Interbus_Token;
};
struct P2_Data P2_Sensors;


defines:
Code:
#define INTERBUS_SIZE   10
#define OLD_DATA        0xFA
#define NEW_DATA        0xAF

#define DATA_READ       0x5F
#define DATA_REQUESTED  0xF5
#define DATA_SENT       0xAA



PROCESSOR 2 - HMI:
Code:

    while(TRUE)
    {
        delay_ms(500);
        if(P1_Sensors.P1_Interbus_Token==DATA_READ) //Previously received data has been read.
        {
           
            fprintf(FTDI,"Formal Request\r\n");
           
            P1_Sensors.P1_Interbus_Token=DATA_REQUESTED; //Give Back Token
            fputc(P1_Sensors.P1_Interbus_Token,INTER);  //Send Token/Request
        }
        else if(P1_Sensors.P1_Data_Ready==NEW_DATA) //ISR Finished receiving data.
        {
            fprintf(FTDI,"NEW DATA!!\r\n"); 

            PTR=&P1_Sensors;                //Convert from Array to Struct
            strcpy(PTR,Serialized);
            CLEAR_INTERBUS_BUFFER();        //Clean for next round
            PRINT_INTERBUS_BUFFER();        //DEBUG
               
            if(P1_Sensors.P1_Interbus_Token==DATA_SENT) //Check Token is mine now
            {
                //fprintf(FTDI,"Correct format\r\n");
               
                fprintf(FTDI,"Data1: %3.2f - Data2: %3.2f\r\n",P1_Sensors.P1_Data_1,P1_Sensors.P1_Data_2); //USE DATA

                P1_Sensors.P1_Interbus_Token=DATA_READ;     //FREE TOKEN
                P1_Sensors.P1_Data_Ready=OLD_DATA;          //Signal data is OLD

                PRINT_INTERBUS_BUFFER(); //DEBUG
            }
            else fprintf(FTDI,"RDY: %X - TOK: %X\r\n",P1_Sensors.P1_Data_Ready,P1_Sensors.P1_Interbus_Token); //Debug
        }
        fprintf(FTDI,"RDY: %X - TOK: %X - Index: %u\r\n",P1_Sensors.P1_Data_Ready,P1_Sensors.P1_Interbus_Token,Inter_Index); //Debug
        delay_ms(500);
    }



PROCESSOR 1 - MACHINE:

Code:

    while(TRUE)
    {
        if(P2_Sensors.P2_Data_Ready==OLD_DATA) //UPDATE SENSORS
        {       
            Get_Pressure(PRESS_3);
            Get_Pressure(PRESS_4);
           
            P2_Sensors.P2_Data_1=(float)PSI[PRESS_3].PSI;
            P2_Sensors.P2_Data_2=(float)PSI[PRESS_4].PSI;
                   
            //P2_Sensors.P2_Data_1=12.34;
            //P2_Sensors.P2_Data_2=56.78;

            P2_Sensors.P2_Data_Ready=NEW_DATA;
            fprintf(FTDI,"DATA1: %3.2f - %3.2f\r\n",P2_Sensors.P2_Data_1,P2_Sensors.P2_Data_2);
            fprintf(FTDI,"RDY1: %X - TOK: %X\r\n",P2_Sensors.P2_Data_Ready,P2_Sensors.P2_Interbus_Token);
        }
       
        if(P2_Sensors.P2_Interbus_Token==DATA_REQUESTED) //Got Request
        {
               
            if(P2_Sensors.P2_Data_Ready==NEW_DATA)  //Data is Fresh
            {
                fprintf(FTDI,"RDY2: %X - TOK: %X\r\n",P2_Sensors.P2_Data_Ready,P2_Sensors.P2_Interbus_Token);
               
                P2_Sensors.P2_Interbus_Token=DATA_SENT; //Give back token
               
                fprintf(FTDI,"DATA1: %3.2f - %3.2f\r\n",P2_Sensors.P2_Data_1,P2_Sensors.P2_Data_2);
                fprintf(FTDI,"RDY3: %X - TOK: %X\r\n",P2_Sensors.P2_Data_Ready,P2_Sensors.P2_Interbus_Token);
               
                fprintf(INTER,&P2_Sensors); //Send Data!
                fprintf(FTDI,"Requested and sent!\r\n");
                PRINT_INTERBUS_BUFFER();
               
                P2_Sensors.P2_Data_Ready=OLD_DATA; //Sent data is old now
                fprintf(FTDI,"RDY4: %X - TOK: %X\r\n",P2_Sensors.P2_Data_Ready,P2_Sensors.P2_Interbus_Token);
            }
            else
            {
                fprintf(FTDI,"RD5: %X - TOK: %X\r\n",P2_Sensors.P2_Data_Ready,P2_Sensors.P2_Interbus_Token);
                P2_Sensors.P2_Data_Ready=OLD_DATA;
            }
        }
        //else
        //{
        //    P2_Sensors.P2_Data_Ready=OLD_DATA;
        //}
        delay_ms(500);
    }     

   
    }


Both PICS are 18F47Q84 - PCH 5.109.
_________________
CCS PCM 5.078 & CCS PCH 5.093


Last edited by Gabriel on Fri Apr 11, 2025 1:30 pm; edited 1 time in total
gaugeguy



Joined: 05 Apr 2011
Posts: 327

View user's profile Send private message

PostPosted: Thu Apr 10, 2025 8:40 am     Reply with quote

Are you sure your sensor data is in the correct float format?
Gabriel



Joined: 03 Aug 2009
Posts: 1074
Location: Panama

View user's profile Send private message

PostPosted: Thu Apr 10, 2025 9:50 am     Reply with quote

its a regular PIC float, which i display as %3.2f as usual.
simply doing

Float#1 = Float#2;

Im not using any IEEE formats or anything, just straight CCS floats.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Gabriel



Joined: 03 Aug 2009
Posts: 1074
Location: Panama

View user's profile Send private message

PostPosted: Thu Apr 10, 2025 12:35 pm     Reply with quote

In the code provided im casting an int32 as a float.
I have tried straight float to float, with similar results.
I tried manually writing values and still fails.
_________________
CCS PCM 5.078 & CCS PCH 5.093
jeremiah



Joined: 20 Jul 2010
Posts: 1383

View user's profile Send private message

PostPosted: Thu Apr 10, 2025 3:15 pm     Reply with quote

The first place I would look is:
Code:

strcpy(PTR,Serialized);


If those aren't actually null terminated strings (or incorrectly terminated), then you can start clobbering other variables and even the stack. It'll copy until it finds a value of NULL (0).
Jerson



Joined: 31 Jul 2009
Posts: 129
Location: Bombay, India

View user's profile Send private message Visit poster's website

PostPosted: Thu Apr 10, 2025 8:49 pm     Reply with quote

Adding to what Jeremiah has stated.

You can use memcpy(&PTR, &Serialized, sizeof(Serialized)) instead of strcpy

Also, rather than have the size of the struct fixed manually, I'd use sizeof(struct P2_data). This removes the chance of human error in calculating the size of the data structure.
Gabriel



Joined: 03 Aug 2009
Posts: 1074
Location: Panama

View user's profile Send private message

PostPosted: Fri Apr 11, 2025 5:50 am     Reply with quote

Thank you Jeremia and Jerson!
I will be trying this ASAP!
_________________
CCS PCM 5.078 & CCS PCH 5.093
Gabriel



Joined: 03 Aug 2009
Posts: 1074
Location: Panama

View user's profile Send private message

PostPosted: Fri Apr 11, 2025 6:22 am     Reply with quote

side note: Jerson i see you are from bombay!
I used to spend all my summers(3 Months) there for 5 years! Ive been to every province in India.
learned to jugle in [spam]. mastered Table Tennis in bombay. used to play TT at the Breach Candy Club, and fly kites on a nearby point by the water (forgot the name). i can stil whoop everyones behind on TT with the skills learned there!

awesome place!
ive seen the pics of the beach clean ups, i would love to see that again more than 25 years later...

EDIT: how is "[spam]", the name of an actual place a bad word?
_________________
CCS PCM 5.078 & CCS PCH 5.093
Gabriel



Joined: 03 Aug 2009
Posts: 1074
Location: Panama

View user's profile Send private message

FROM THE BOTTOM OF MY HEART
PostPosted: Fri Apr 11, 2025 1:30 pm     Reply with quote

I would like to take a moment to thank you both for the help!
I have clean, fast data, switching processors now thanks to your help.

THANK YOUUUUU!!!!!!!
_________________
CCS PCM 5.078 & CCS PCH 5.093
temtronic



Joined: 01 Jul 2010
Posts: 9457
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Apr 11, 2025 1:44 pm     Reply with quote

love to see 'SOLVED' in the title but ...
OK.... what was the 'cure' ?
Gabriel



Joined: 03 Aug 2009
Posts: 1074
Location: Panama

View user's profile Send private message

PostPosted: Fri Apr 11, 2025 3:34 pm     Reply with quote

Hi Temtronic,

sorry about that, the solution was indeed as sugested by Jeremia and Jerson.
I thought that was fairly obvious.

copying the string to and from the Struct was not being done properly.
using memcpy() instead of strcpy() and giving the functions the proper size of the items as the array and the struct are not null terminated.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Jerson



Joined: 31 Jul 2009
Posts: 129
Location: Bombay, India

View user's profile Send private message Visit poster's website

PostPosted: Sat Apr 12, 2025 3:45 am     Reply with quote

Hi Gabriel

Glad to know you still cherish memories of my city. You are perhaps talking about a place called the radio club near marine lines.

You may find it suspicious, but I haven't really been much outside my own city as I am a kind of hermit crab - hehe.

Perhaps you'd like the natural beauty of Goa and Kerala much more.

My city is now more of a concrete jungle with terrible traffic and conjestion - perhaps that is the price of development. I still cherish the good ole days around the 1970s to early 90's

Best wishes


Gabriel wrote:
side note: Jerson i see you are from bombay!
I used to spend all my summers(3 Months) there for 5 years! Ive been to every province in India.
learned to jugle in [spam]. mastered Table Tennis in bombay. used to play TT at the Breach Candy Club, and fly kites on a nearby point by the water (forgot the name). i can stil whoop everyones behind on TT with the skills learned there!

awesome place!
ive seen the pics of the beach clean ups, i would love to see that again more than 25 years later...

EDIT: how is "[spam]", the name of an actual place a bad word?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group