3 Replies Latest reply on Sep 14, 2015 6:51 PM by CMata_Intel

    mraa: initiate mraa_uart_context var and use it with diffrent files\ communicating Edison by uart using two services.

    barlesh

      Hallo friends

      (a bit background, if you want you can skip to the question bellow (:   it's basically  pure C programming question, but because the use of mraa i post it here. and because I like you guys better then stackOverflow (: (: (:   )

       

      I try to create a connection between my erlang app, running on my Edison, to another modem over UART.

      The idea was to communicate between the app and a C program, which will use mraa lib to send data over UART pins 0,1.

       

      I didnt want to write a complicated c program that will need  to deal with communication of both incoming & outcoming data from both erlang and uart, so I separate it to two "lines" of flow:

      main.erl -----(creates)----->transmitter.erl  -----(initiate and send data to)------->  trans_port.c ---------(send using mraa lib)------> UART Tx

      main.erl-----(creates)------>receiver.erl  -----(initiate and wait for data from)------->  rec_port.c ---------(listening using mraa lib to)------> UART Rx

      (potentially) ::: main.erl------->port.c -----> configure the uart

       

      I'm currently still trying to understand what is the format of incoming data from the modem, so for now I'm only dealing the transmitting flow.

       

      So, my problem is:

      When I declare and define a new  mraa_uart_context var at the trans_port.c, (not at port.c)  and use it to write data to uart ( write(uart, buf, len) ) it succeed.

      I'm sure that creating another var at rec_port.c isn't smart and\or not a good programming, so the solution is to create a single globaly used mraa_uart_context var.

      But when I try to create it at port.c, and to make it usable at trans_port.c I fail using write(uart,buf,len).


      My  questions:

      1) Does 2 services is  the right way (or legit) to deal with uart communication?

      2) Can a mraa_uart_context var be used with different files and work properly (pretty sure it's not a problem, just making sure)?

      3) if the answer to 1,2 is yes, then what is the best way to share it between files?

      All the methods I used here are taken from answer all over the web.

       


      My first try was (using extern):

      port.h

      #include "mraa.h"

      extern mraa_uart_context uart;

      port.c

      #include "port.h"

      mraa_uart_context uart

       

      main(){

      uart = mraa_uart_init(0);

      }

      trans_port.c

      #include "port.h"

      .....

      mraa_uart_context uart;

      .....

      int main(){

      ...

      mraa_uart_write(uart, buf, len);

      }

       

       

       

       

       

       

      My seconed try was (other extern method):

      port.h

      #include "mraa.h"

      mraa_uart_context uart;

      port.c

      include "port.h"

      extern mraa_uart_context uart;

       

      main(){

      ....

      uart = mraa_uart_init(0);

      }

      trans_port.c

      #include "port.h"

      .....

      extern mraa_uart_context uart;

      .....

      int main(){

      ...

      mraa_uart_write(uart, buf, len);

      }

       

       

      My third try was (static deceleration with getter & setter):

      port.h

      static mraa_uart_context uart;

       

       

      mraa_uart_context get_uart();

      void set_uart(mraa_uart_context new_uart);

       

       

      mraa_uart_context get_uart(){

          return uart;

      }

       

      void set_uart(mraa_uart_context new_uart){

          memcpy(&uart, &new_uart, sizeof(uart));

      }

       

      port.c

      #include port.h

      main(){

      ....

      set_uart(mraa_uart_init(0));

      }

      trans_port.c

      #include "port.h"

      .....

      sendToUART(buf, len){

       

      mraa_uart_context uart_copy = get_uart();

          if((ans = mraa_uart_write(uart_copy, buf, len))!=len) {

      .....

       

      }

       

      int main(){

      ...

      SendToUART(bef, len);

      .......

      }

       

      Each one of them passed compiling but neither one worked.

      Can you please help me choose the right method and the correct use of it?

       

      Thank you very much

      Bar

        • 1. Re: mraa: initiate mraa_uart_context var and use it with diffrent files\ communicating Edison by uart using two services.
          CMata_Intel

          Hi Bar,

           

          Does the other device use Flow Control?

          Have you tried with this basic example? mraa/uart.c at master · intel-iot-devkit/mraa · GitHub

          Are you using the latest image and MRAA library version?

          Does the device use a specific communication protocol?

          Are you testing the signals in Rx and Tx to check you are writing what you want?

          Which expansion board are you using?

           

          Regarding your questions:

          1. I would use 1 process/code where you can read and write.

          2. You can use mraa_uart_context in different codes but you will be calling the same UART, you could have some timing issues and start losing some data.

           

          Regards,

          Charlie

          • 2. Re: mraa: initiate mraa_uart_context var and use it with diffrent files\ communicating Edison by uart using two services.
            barlesh

            Hi Charlie

            I really missed some basic information about my system.

            I use Edison on arduino expansion board, with the latest image(downloaded 5 days ago, 159 I think, when typing configure_edison --version.

            I've already had a connection by UART, with the basic UART example , so I know everything wired and working (didn't try to get incoming data, though).

            I think that flow control is off.

             

            1. Will mutexes work? (still has to solve the shared variables)

            2. doesn't UART full duplex? why should I loose data?

             

             

            Thank you

            Bar

            • 3. Re: mraa: initiate mraa_uart_context var and use it with diffrent files\ communicating Edison by uart using two services.
              CMata_Intel

              Hi bar,

               

              The problem you are having could be related with the address of the device or the parameters that it expects like Data Bits, Stop Bits, Flow Control and others. I suggest you to check the parameters that your device uses and set them in the Edison to configure the UART. Also, check the communication protocol that it needs.

              As you know you can do this in different ways. Mutexes, it will depend on how you control the process, how you set the conditions and how you will determine all the routines. If you use two processes you will have to create a "global variable" (I haven't seen a variable working between two different codes, one alternative could be: write into a file and then read the file from the other process but this will make the code slower) that will call the same resources so you will have to be careful while synchronizing the communication in order to not be asking information that is not available yet in the buffer because the other process may be still running and asking information from the same buffer.

              In my case, I suggest you to use one process because the Serial Communication is slow and you have to consider that you will have running two codes working together and accessing a file at the same time.

              Also, try again with the example and start to write and receive data I think this will help you to debug the code and to choose the way to develop your project.

               

              Kind regards,

              Charlie