Shop Learn P1 Docs P2 Docs
Facing issue with UART communication — Parallax Forums

Facing issue with UART communication

Hello,

I'm testing UART communication for P2 and Raspberry Pi. Started with sending single char from R-pi. But, often I'm getting garbage at P2. Also even when nothing is sent from R-pi, there is some garbage receiving at P2.

I used Flexprop C for P2, and jm_serial.spin2

#include "stdio.h"
#include "Simpletools.h"

struct __using("jm_serial.spin2") jm;

int main(){  
        jm.startx(14,15,921600); 
        while (1){
            int temp_data = jm.rxcheck(); 
            if(temp_data != -1 && temp_data !=255)
            {
                    jm.tx(temp_data);
                printf("%X\n",temp_data);
                 }
                        _waitus(1);
      }
}

In R-pi :

import serial
from time import sleep

ser = serial.Serial ("/dev/ttyS0", 921600)
print("start...")

data = 'A'
temp_data = data.encode()
ser.write(temp_data)
print("sent:",temp_data.decode())
sleep(0.03)
data2 = ser.read()
print("Received:",data2)
sleep(2)

Any suggestion for proper UART communication to get data without any loss and no garbage?

Comments

  • JonnyMacJonnyMac Posts: 8,311
    edited 2022-11-03 13:59

    Please discard jm_serial.spin2 and use jm_fullduplexserial.spin2 instead -- the former is not buffered on the RX side; the latter is.

  • Hi @JonnyMac
    I used jm_fullduplexserial.spin2. Also used level shifter between P2 and R-pi, it worked for me. Thanks :smile:

  • JonnyMacJonnyMac Posts: 8,311

    Isn't the RPi 3.3v? There should be no need for a level-shifter. That said, I'm glad it's working.

  • @JonnyMac said:
    Isn't the RPi 3.3v? There should be no need for a level-shifter. That said, I'm glad it's working.

    Yes, it's 3.3v. By using level shifter I am able to reduce noise voltage.

  • Hello,

    I'm sending 1 MB data from R-pi to P2 using UART. Data is divided into chunks and one chunk(of 32768 Bytes) at a time sent from R-pi. In P2, there is a buffer (arr[32768]) which store this data. Next step is to store this buffer in SD card. After stored buffer in SD card, buffer is to be reset and again it receives another chunk of data from R-pi. This process repeats till whole data(here 1 MB) received.

    Firstly, I've tried without cogs. I am able to receive proper data and store it in SD card. Below is my code (code_1.c) and screenshot of terminal(Screenshot_1).

    Now, I've started working with multi-cogs (UART_multicog.c). First cog to receive data and other to store data in SD card.

    In first cog, data is received and stored in a buffer. After receiving one chunk, cog-1 sends attenuation to cog-2. It will start storing this data in a file in SD card. Meanwhile cog-1 waits till attenuation from cog-2. Once a buffer is written in SD card, cog-2 will send attenuation to cog-1. Now, cog-1 got attenuation, and will receive another chunk from R-pi.

    When started to test this code with 1 MB (1048576 bytes) data, up to 720896 bytes(22 buffers of 32768 bytes) could be stored in SD card(Screenshot_2). After that buffer can not be written in SD card.

    int data_cnt = fwrite(arr, sizeof(char), sizeof(arr), fp);

    fwrite returns the total count of data written. So, here for buffer (arr[]) it should be 32768. But after writing 22 buffers, fwrite returns 0 or -1.

    It seems that fwrite is failed. I tried to close file and reopen it and append buffer, but then it was failed to open file.

    However for 720896 bytes it works properly. Sent only 720896 bytes from R-pi. (Screenshot_3).

    I am not getting where I'm doing wrong. Any help can be very useful for me. :)

    c
    c
    1920 x 1040 - 47K
    1920 x 1040 - 143K
    1920 x 1040 - 153K
  • pik33pik33 Posts: 1,813

    Maybe default heap size is too short. I had this problem in my FlexBasic program, increasing the heap helped there, however I don't know how to declare the heap size in FlexC.

    Then what is FILE *fh for? You open fp, write first to the opened fp, then it seems your loop writes to fh, which is declared but not initialized.

  • evanhevanh Posts: 13,849

    Add it in the list of enums of top level source file. eg:

    enum {
        _xtlfreq = 20_000_000,
        _clkfreq = 350_000_000,
    
        HEAPSIZE = 8000,
    
    };
    
  • @pik33 said:
    Maybe default heap size is too short. I had this problem in my FlexBasic program, increasing the heap helped there, however I don't know how to declare the heap size in FlexC.

    Then what is FILE *fh for? You open fp, write first to the opened fp, then it seems your loop writes to fh, which is declared but not initialized.

    I've mistyped fp.
    I was also getting some error related to heap corruption before I modified my code to this level. Increasing heap sorted out my issue.

  • @pik33 & @evanh,
    Thank you very much. My issue is solved. :)

  • Glad you solved your problem, but I was going to suggest that the problem might be on the RasPi side. I've observed Python corrupting the serial data stream with junk during initialization that takes a few seconds to propagate.

  • MicksterMickster Posts: 2,304

    A shout out to @JonnyMac, also, for the awesome jm_fullduplexserial :+1:

    Many thanks Jon :smile:

    Craig

  • Stephen MoracoStephen Moraco Posts: 234
    edited 2022-11-29 07:20

    @disha_sudra I know I'm late to the game here but have you made sure the console over serial port feature has been disabled?

    This is a common source of garbage over the serial lines when talking with an RPi.

    See my RPi configure services you need page. This is from my P2 RPi ioT Gateway where I walk through configuring your RPi to avoid these kinds of problems.

    -Stephen

Sign In or Register to comment.