UART communication between two PSoC

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hi all,

I was trying to for UART communication between two PSoCs.

The first PSoC keeps sending 5bytes of data at every 10ms. The first bytes is constant say 0x08 and rest 4 byte is a float value.

And the second PSoC is recieving.

Both UART were configured at 57600baud rate.

The second PSoC receives everything in correct order, when it is programmed. But after a power cycle, it receives 5 bytes but sometimes not in the correct order.

eg.

if you are sending 200 as float. 200 --> 0x42c80000

first psoc is sending 08 00 00 c8 42.

But SOMETIMES after power cycle, second might receive  c8 42 08 00 00. but sometimes in the rights order.

CY_ISR(uart_rx)

{

    uint8 Index = 0;

    float value;

    if (UART_GetRxBufferSize() == 5)

        {

            for(Index = 0; Index < UART_RX_BUFFER_SIZE; Index++)

                COM_UART_Rx_Buffer[Index]= UART_GetChar();

            if (COM_UART_Rx_Buffer[0] == 0x08 )

                {

                    memcpy(&value,&COM_UART_Rx_Buffer[1],4);  

                }

             UART_ClearRxBuffer(); 

        }      

}

The Rx buffer of second PSoC is 5.

I dont know how can i synchronise the two PSoCs.

Thanks in advance

0 Likes
1 Solution
Anonymous
Not applicable

Hello,

It is possible that your device is booting while in the middle of a packet transmission. So naturally the received bytes are shifted.

I would recommend that you expand your protocol, say have a fixed start byte + 5 bytes of data + fixed end byte. The PSoC receiving this would look for these known start/end bytes and try to synchronize.

You could also make the receiving PSoC send a stop/start command to receive data at startup.

Regards,

View solution in original post

0 Likes
5 Replies
Anonymous
Not applicable

Hello,

It is possible that your device is booting while in the middle of a packet transmission. So naturally the received bytes are shifted.

I would recommend that you expand your protocol, say have a fixed start byte + 5 bytes of data + fixed end byte. The PSoC receiving this would look for these known start/end bytes and try to synchronize.

You could also make the receiving PSoC send a stop/start command to receive data at startup.

Regards,

0 Likes
Anonymous
Not applicable

Thanks for the reply.

I have the start byte 0x08.
But how can I synchronize it with the receiving PSoC.

Since I thought the same, I kept a switch at 1st PSoC, so when I press it the uart trasmission begins from 1st PSoC. This should solve the boot up time.

0 Likes
Anonymous
Not applicable

I see two problems:

1. You are detecting that you are out of sync, by checking for the start byte. But your code waits for the next 5 bytes which will stay out of sync. Possible solution: Read until you find your start byte, then read the following 4 bytes.

2. Only having a start byte is unreliable. You say you have a float number. It very likely that your start byte will also be in the float number. Try at least expanding the start of protocol to 16 bits, reducing considerably the possibility of finding this combination in the float number.

Regards,

This is a matter of protocol. You could resove that problem by

Second PSoC sends a byte

First PSoc answers with a float number (fixed number of bytes)

Bob

I always use a state machine in the receiving devide.

State 0: read byte. If it is the start byte, go to state 1, else stay in state 0.

state 1: receive 4 bytes, then go to state 2.

state 2: receive a checksum (CRC16 recommended). If checksum is correct, evaluate the bytes received in state 1, otherwise ignore them. In any case go back to state 0.

Checking for the start byte in case 0 makes it very likely to be in sync, but it is not guaranteed. After a few cycles the state machine will be in sync.