5 Replies Latest reply on Mar 6, 2018 8:10 AM by user_1269049

    UART communication between two PSoC

    spgermany7_1996721

      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

        • 1. Re: UART communication between two PSoC
          cfmm

          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,

          • 2. Re: UART communication between two PSoC
            spgermany7_1996721

            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.

            • 3. Re: UART communication between two PSoC
              cfmm

              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,

              1 of 1 people found this helpful
              • 4. Re: UART communication between two PSoC
                bob.marlowe

                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

                1 of 1 people found this helpful
                • 5. Re: UART communication between two PSoC
                  user_1269049

                  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.

                  1 of 1 people found this helpful