14 Replies Latest reply on Aug 11, 2015 2:15 PM by DiegoV_Intel

    tx/ rx pin not working.

    sidd1231

      Hi,

      I am using the arduino development board for edison. I am trying to get the tx and rx pins work (pins 0 and 1).

      here's the code im using

       

       

      // code begins

       

      void setup()

      { Serial.begin(9600)

      }

      void loop(){

      Serial.write("hii");

      }

       

      // code ends

       

      ideally this code is supposed to show me some activity on tx pin (pin 1). I see no activity on that pin (cant detect any signal on an ossciloscope too) but i can see data being printed on serial monitor on the arduino IDE.

      could you guys please help me or point me to resources where i could find out, how to get my tx/rx pins to work.

        • 1. Re: tx/ rx pin not working.
          DiegoV_Intel

          Hi sidd1231,

           

          In order to send and receive data from pins 0 and 1, you should use the Serial1 object in your sketch. The object Serial is used for the Serial Monitor only. You should add the following lines in your sketch:

           

          // code begins
          
          void setup()
          {
          Serial.begin(9600);     // "Serial" is for the Serial Monitor
          Serial1.begin(9600);    // "Serial1" is for the pins 0 and 1 (Add this line)
          }
          void loop(){
          Serial.write("hii");    // Print in the Serial Monitor
          Serial1.write("hii");   // Send data to Tx (pin 1) (Add this line)
          }
          
          // code ends
          

           

          Regards,

          Diego.

          • 2. Re: tx/ rx pin not working.
            sidd1231

            Hi,

            Thank you for your reply. It worked.i have another problem right now. Description below.

            things being used:

            ->edison with arduino expansion board.

            ->USB to UART RS232

            ->Putty

             

            So i am first connecting to edison through serial (using putty), logging in and issuing this comand: (stty -F /dev/ttyMFD1 9600) and then issuing this comand : (echo "hello edison" >> dev/ttyMFD1)

             

            now through one more putty terminal i am connected to the USB to UART converter at baud rate 9600 (with 8,N,1(same for both))

            instead of receiving "hello edison" im getting some junk like "▒▒▒R▒▒▒j▒Z". Please help me out.

            • 3. Re: tx/ rx pin not working.
              sidd1231

              never mind. My usb to uart was faulty.

              Thankk you

              • 4. Re: tx/ rx pin not working.
                peter__wu

                Hi Diego,

                Noted Serial for Serial monitor and Serial1 for pin 0 and pin 1 uart under Arduino environment. However, in intel-iot-devkit/mraa sample, shows below javascript code.

                ****************************************************

                var m = require('mraa'); //require mraa

                u = new m.Uart(0)

                console.log(u.getDevicePath())

                ****************************************************

                This code will use pin 0 and pin 1 as uart port. How is this defined? If I want to send data to serial monitor,usb-serial port,how to setup it?

                 

                Thank you!

                Peter

                • 5. Re: tx/ rx pin not working.
                  DiegoV_Intel

                  Hello peter__wu,

                   

                  The Serial objects such as "Serial" and "Serial1" are defined only under the Arduino environment. Check this post for additional details: Using Serialx on Edison. You can use MRAA in Python or JavaScript to use the serial port as well, but the Serial objects won't be defined in those environments. You can check more details about the MRAA library here: mraa/README.md at master · intel-iot-devkit/mraa · GitHub.

                   

                  As you said, MRAA will set the pins 0 and 1 for UART usage. If you want to send data through the USB port you can do it by a system call. In JavaScript you can do a system call as shown below:

                   

                  var exec = require('child_process').exec;
                  var cmd = 'echo "test_message" > /dev/ttyGS0';
                  
                  exec(cmd, function(error, stdout, stderr) {
                    // command output is in stdout
                  });
                  

                   

                  The /dev/ttyGS0 device corresponds to the USB port used by the Serial Monitor. If you write a script with the lines above and run it using Node.js, you will see the message “test_message” in the Arduino Serial Monitor.

                   

                  Regards,

                  Diego.

                  • 6. Re: tx/ rx pin not working.
                    peter__wu

                    Hi Diego,

                    Thanks for the answer.In addition to that, I created below code to communicate with a serial device. After serial data is sent,I can detect the response. However, below code could not get response data at all. Could you check and advise?

                     

                    #include <stdio.h>

                    #include <stdlib.h>

                    #include <unistd.h>     // UNIX standard function definitions

                    #include "mraa.h"

                     

                     

                    int main() {

                          mraa_uart_context uart;

                        uart = mraa_uart_init(0);

                        mraa_uart_set_baudrate(uart,9600);

                        mraa_uart_set_mode(uart,8,MRAA_UART_PARITY_NONE , 1);

                       // mraa_uart_set_flowcontrol(uart,0,0);

                        //mraa_uart_set_timeout(uart,0,0,0);

                     

                        if (uart == NULL) {

                            fprintf(stderr, "UART failed to setup\n");

                            return EXIT_FAILURE;

                        }

                     

                        unsigned char buffer[] = {0x11,0x01,0x01,0xED};

                        unsigned char revBuf[50];

                        unsigned int revBytes;

                        unsigned int i;

                     

                        for (i=0;i<1;i++){

                        printf("serial sent %d\n",i);

                        mraa_uart_write(uart, buffer, sizeof(buffer));

                     

                        usleep(1000000);

                        printf("try to read response...\n");

                     

                        if (mraa_uart_data_available(uart,0)){

                        printf("dataReceived\n");

                        revBytes=mraa_uart_read(uart,revBuf,sizeof(revBuf));

                        printf("data receive:%x %x %x\n",revBuf[0],revBuf[1],revBuf[2]);

                        }

                        usleep(300000);

                        }//end for

                     

                        mraa_uart_stop(uart);

                        mraa_deinit();

                        return EXIT_SUCCESS;

                    }

                    • 7. Re: tx/ rx pin not working.
                      DiegoV_Intel

                      Hello peter__wu,

                       

                      I've tested your code and it works fine. Check the screenshot below:

                      Serial_output.JPG

                       

                      As you can see, I was able to receive data. I tested it sending data from an Arduino UNO and the Edison received the data successfully. I did a little change in your code, however. In the for loop, I changed the condition by this one: for (i=0;i<50;i++). This way the for loop will be executed 50 times instead of only 1.

                       

                      Since the code is fine, I suggest you to make sure if the serial device is actually sending data. The issue might be related to the serial device attached to the Edison board.

                       

                      Regards,

                      Diego.

                      • 8. Re: tx/ rx pin not working.
                        peter__wu

                        Hi Diego,

                        Followed yours, I also created a Arduino test code and had below results. (Arduino UNO send uart data and Edison try to catch the data)

                        1. If UNO send serial data with TEXT format(by Serial.print), Edison is able to receive uart data.

                         

                        2. If UNO send serial data with binary format(by Serial.write), Edison is not able to receive data.

                         

                        Thank you!

                        Peter

                         

                         

                        Below is the code from Arduino that send data with binary format and Edison is not able to receive.

                        ********************************************************************************************************************

                        #include <SoftwareSerial.h>

                        int revCt=0;

                        SoftwareSerial Softserial(11, 12);//rx,tx

                        void setup() {

                          // put your setup code here, to run once:

                          Serial.begin(9600);

                          Softserial.begin(9600);

                          }

                         

                        void loop() {

                        int i=0;

                        char Buf[4]={'a','b','c','d'};

                        Softserial.write(Buf,4); // not receiving data at edison side

                        //    Softserial.println(Buf); //works fine at edison side

                        Serial.println("Sent");

                        Serial.println(revCt++);

                        delay(800);

                        }

                        • 9. Re: tx/ rx pin not working.
                          peter__wu

                          Hi Diego,

                          Re-send.

                          Is there any update on below?

                           

                          Peter

                           

                          Hi Diego,

                          Followed yours, I also created a Arduino test code and had below results. (Arduino UNO send uart data and Edison try to catch the data)

                          1. If UNO send serial data with TEXT format(by Serial.print), Edison is able to receive uart data.

                           

                          2. If UNO send serial data with binary format(by Serial.write), Edison is not able to receive data.

                           

                          Thank you!

                          Peter

                           

                           

                          Below is the code from Arduino that send data with binary format and Edison is not able to receive.

                          ********************************************************************************************************************

                          #include <SoftwareSerial.h>

                          int revCt=0;

                          SoftwareSerial Softserial(11, 12);//rx,tx

                          void setup() {

                            // put your setup code here, to run once:

                            Serial.begin(9600);

                            Softserial.begin(9600);

                            }

                           

                          void loop() {

                          int i=0;

                          char Buf[4]={'a','b','c','d'};

                          Softserial.write(Buf,4); // not receiving data at edison side

                          //    Softserial.println(Buf); //works fine at edison side

                          Serial.println("Sent");

                          Serial.println(revCt++);

                          delay(800);

                          }

                          • 10. Re: tx/ rx pin not working.
                            DiegoV_Intel

                            Hi peter__wu,

                             

                            I tested the Serial.write() method and as you said, the Edison didn't receive the data. Actually, if you try using Serial.print() the Edison won't receive data either. The detail here is that you have to send the ASCII values for the carriage return and the newline. That is the reason why the Edison only receive data when the method use to send it is Serial.println(). This method sends the carriage return and the newline values automatically.

                             

                            In order to send data using Serial.write() you should use the following lines:

                            Serial.write("data");
                            Serial.write(0x0D); // This is the Hex value for the carriage return
                            Serial.write(0x0A); // This is the Hex value for the newline
                            

                             

                            Regards,

                            Diego.

                            • 11. Re: tx/ rx pin not working.
                              peter__wu

                              Hi Diego,

                              Yes,I got it.Thank you!

                              In that cast, uart part of MRAA could not receive binary data from third party device since it would not end with 0x0a...

                              Do we have to wait a new MRAA version?

                               

                              Best regards,

                              Peter

                              • 12. Re: tx/ rx pin not working.
                                DiegoV_Intel

                                Hello peter__wu,

                                 

                                I did the same test but using an Arduino sketch instead of the C code to receive the data from the Arduino UNO. This time the Edison was able to receive the data whether or not I used Serial.println(), Serial.print() or Serial.write() in the Arduino UNO sketch to send the data. You could wait for a new MRAA version to check if the UART feature is improved or you can use an Arduino sketch - like I did - as an alternative.

                                 

                                Regards,

                                Diego.

                                • 13. Re: tx/ rx pin not working.
                                  peter__wu

                                  Hi Diego,

                                  After I added below code to setup tty, I can get data without "newline". Therefore, under javascript(node.js), I have to do extra setup outside of MRAA.

                                  I got two questions:

                                  (1)I am not sure if I could do similar system setup in javascript or I have to create a c lib for it?

                                  (2)If I create my own c lib, how can I call it from javascript?

                                   

                                  Thanks in advance!

                                  Peter

                                   

                                  int setupTTY(int fd, speed_t baud)

                                  {

                                    if (fd < 0)

                                    return 0;

                                    struct termios termio;

                                    /* get current modes */

                                    tcgetattr(fd, &termio);

                                  /* setup for a 'raw' mode.  8bit, 1 stop bit, no parity, */

                                  /* no echo or special character handling, */

                                  /* no flow control or line editing semantics. */

                                    cfmakeraw(&termio);

                                    // set our baud rates

                                    cfsetispeed(&termio, baud);

                                    cfsetospeed(&termio, baud);

                                    // make it so

                                    if (tcsetattr(fd, TCSAFLUSH, &termio) < 0)

                                    {

                                     printf("tcsetattr failed\n");

                                     return 0;

                                    }

                                    return 1;

                                  }

                                  • 14. Re: tx/ rx pin not working.
                                    DiegoV_Intel

                                    Hello peter__wu,

                                     

                                    Please check my suggestions below. Consider that I haven’t tested them and you probably will need to do some extra research for them.

                                     

                                    The first suggestion would be to write the function directly in JavaScript so you would not need to call any C library or similar. However, you should know what exactly your C code does in order to re-write it using JavaScript.

                                     

                                    Another option would be, as you pointed, calling the C library from JavaScript; however I haven't done it before so you probably have to spend some time investigating about it. I have found two sites that might help you as a starting point:

                                     

                                    I hope I have answered your questions. Another option would be using C for your application instead of JavaScript. If you already have a C code that works you could continue the project in C.

                                     

                                    Regards,

                                    Diego.