5 Replies Latest reply on Nov 27, 2014 8:42 AM by DiegoV_Intel

    How to receive data from 0RX?

    freshbirdtww

      Now,i connect my sensor to 0RX of Edison kit for arduino.and i test it success on the arduino1.5.3-intel.and i receive the data from sensor.

      but after,i want to use linux Serial programming to receive the data,and i can't find what's the name of tty* Corresponding to 0RX, is anyone know?

      thx.

        • 1. Re: How to receive data from 0RX?
          freshbirdtww

          En...I find that 0RX is /dev/ttyMFD1(in arduino-1.5.3-Intel) ?  it's right??maybe my serial program wrong?

          • 2. Re: How to receive data from 0RX?
            DiegoV_Intel

            Hi freshbirdtww,

             

            The pins 0 and 1 (RX/TX) are at /dev/ttyMFD1. I'd suggest you to check this following thread, it could be helpful for you: How to enable PIN0/PIN1 for UART usage (TX/RX)

             

            Regards,

            Diego.

            • 3. Re: How to receive data from 0RX?
              freshbirdtww

              i had tried the method mentioned by  How to enable PIN0/PIN1 for UART usage (TX/RX) yesterday.

              unluckly, i can't receive the data eighter.

              and if i use arduino IDE,i can receive the data from Serial1(it is also /dev/ttyMFD1)

              but it can't work well under linux.what's wrong?

              do you have time to check the code?

               

              #include<stdio.h>

              #include<fcntl.h>

              #include<errno.h>

              #include<unistd.h>

              #include<termios.h>

              #include<sys/select.h>

              #define BAUDRATE 115200

              #define DEVICE "/dev/ttyMFD1"

              int serial_read(int fd, char *str, unsigned int len, unsigned int timeout)

              {

                  fd_set rfds;

                  struct timeval tv;

                  int ret;                 //每次读的结果

                  int sret;                //select检测结果

                  int readlen = 0;         //实际读到的字节数

                  char *ptr;

               

                  ptr = str;               //读指针,每次移动,因为实际读出的长度和传入参数可能存在差异

               

                  FD_ZERO(&rfds);          //清除文件描述符集合

                  FD_SET(fd, &rfds);       //将fd加入fds文件描述符,以待下面用select方法监听

               

                  /*传入的timeout是ms级别的单位,这里需要转换为structtimeval结构*/

                  tv.tv_sec = timeout / 1000;

                  tv.tv_usec = (timeout % 1000) * 1000;

               

                  /*开始读*/

                  while(readlen < len)

                  {

                      sret = select(fd + 1, &rfds, NULL, NULL, &tv);

               

                      if(sret == -1)

                      {

                          perror("select:");

                          break;

                      }

                      else if(sret > 0)

                      {

                          ret = read(fd, ptr, 1);

                          printf("sec: %d, usec: %d\n", tv.tv_sec, tv.tv_usec);

                          if(ret <= -1)

                          {

                              perror("read err:");

                              break;

                          }

               

                          else if(ret == 0)

                          {

                              break;

                          }

               

                          readlen += ret;

                          ptr += ret;

                      }

                      else

                      {

                          printf("timeout! \n");

                          break;

                      }

               

                  }

               

                  return readlen;

              }

               

              /* OpenPort:Open Serial port1

              * Return the file descriptor on success or -1 on error

              *

              */

              int OpenPort(void)

              {

                  int fd;

               

                  fd = open(DEVICE, O_RDWR | O_NOCTTY | O_NDELAY);

                  if(fd == -1)

                  {

                      printf("OpenPort:Unable to open /dev/ttyMFD1");

                  }

                  else

                  {

                      fcntl(fd, F_SETFL, 0);

                      return (fd);

                  }

              }

              int main(int argc, char **argv)

              {

                  int fd;

                  struct termios oldtio, newtio;

                  char buf[100] = {};

               

                   fd = OpenPort();

                   tcgetattr(fd, &oldtio);

                   bzero(&newtio, sizeof(newtio));

                 

                   newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;

                   newtio.c_iflag = IGNPAR | ICRNL;

                   newtio.c_oflag = 0;

                   newtio.c_lflag = ICANON;

                   newtio.c_cc[VINTR] = 0; /* Ctrl-c */

                   newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */

                   newtio.c_cc[VERASE] = 0; /* del */

                   newtio.c_cc[VKILL] = 0; /* @ */

                   newtio.c_cc[VEOF] = 4; /* Ctrl-d */

                   newtio.c_cc[VTIME] = 0; /* inter-character timer unused */

                                                  /* 不使用字符间的计时器 */

                   newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */

                                                     /* 阻塞,直到读取到一个字符 */

                   newtio.c_cc[VSWTC] = 0; /* '\0' */

                   newtio.c_cc[VSTART] = 0; /* Ctrl-q */

                   newtio.c_cc[VSTOP] = 0; /* Ctrl-s */

                   newtio.c_cc[VSUSP] = 0; /* Ctrl-z */

                   newtio.c_cc[VEOL] = 0; /* '\0' */

                   newtio.c_cc[VREPRINT] = 0; /* Ctrl-r */

                   newtio.c_cc[VDISCARD] = 0; /* Ctrl-u */

                   newtio.c_cc[VWERASE] = 0; /* Ctrl-w */

                   newtio.c_cc[VLNEXT] = 0; /* Ctrl-v */

                   newtio.c_cc[VEOL2] = 0; /* '\0' */

               

                   tcflush(fd, TCIFLUSH);

                   tcsetattr(fd, TCSANOW, &newtio);

               

                  int n = serial_read(fd, buf, 1, 3000);

               

                  //int n = read(fd, buf, 2);

                  if(n < 0)

                  {

                      fputs("read error", stderr);

                  }

                  printf("%s\n",buf);

              }

              • 4. Re: How to receive data from 0RX?
                DiegoV_Intel

                Hi freshbirdtww,

                 

                Sure, let me test it. When I get some results I will post them here.

                 

                Regards,

                Diego.

                • 5. Re: How to receive data from 0RX?
                  DiegoV_Intel

                  Hi freshbirdtww,

                   

                  I have tested the serial communication using the Edison and an Arduino UNO, and I was able to send and received data. The serial port for the pins 0 and 1 in the Arduno Expansion Board is /dev/ttyMFD1, which is the Serial Port 1 in the Edison. Check this thread for more details about the other Serial Ports of Edison: Using Serialx on Edison.

                   

                  To start transmitting, you have to setup the required gpio pins and the baudrate. For the baudrate, I used 9600. You can check what is the current baudrate of the /dev/ttyMFD1 port with the following command in Linux terminal:

                   

                  stty -F /dev/ttyMFD1 -a
                  

                   

                  And you can modify the baudrate with the following command:

                   

                  stty -F /dev/ttyMFD1 9600  # Replace "9600" with the baudrate you need
                  

                   

                  Then, for setting up the gpio pins required for send and received data with the /dev/ttyMFD1 port, run the following commands in Linux terminal:

                   

                  # TRI_STATE_ALL buffer
                  echo -n "214" > /sys/class/gpio/export
                  # Pin 0 - Rx
                  echo -n "130" > /sys/class/gpio/export # rx (input)
                  echo -n "248" > /sys/class/gpio/export # output enable
                  echo -n "216" > /sys/class/gpio/export # pullup enable
                  #Pin 1 - TX
                  echo -n "131" > /sys/class/gpio/export # tx (output)
                  echo -n "249" > /sys/class/gpio/export # output enable
                  echo -n "217" > /sys/class/gpio/export # pullup enable
                  
                  echo low > /sys/class/gpio/gpio214/direction # Set the TRI_STATE_ALL to low before doing any changes
                  
                  echo low > /sys/class/gpio/gpio248/direction
                  echo in > /sys/class/gpio/gpio216/direction
                  
                  echo mode1 > /sys/kernel/debug/gpio_debug/gpio130/current_pinmux # mode1 is used to set the UART interface in Edison
                  echo in > /sys/class/gpio/gpio130/direction
                  
                  echo high > /sys/class/gpio/gpio249/direction
                  echo in > /sys/class/gpio/gpio217/direction
                  
                  echo mode1 > /sys/kernel/debug/gpio_debug/gpio131/current_pinmux # mode1 is used to set the UART interface in Edison
                  echo out > /sys/class/gpio/gpio131/direction
                  
                  echo high > /sys/class/gpio/gpio214/direction # Set the TRI_STATE_ALL to high after the changes are applied
                  

                   

                  And that's it. Now in order to send data through the port use the following command:

                   

                  echo [data] > /dev/ttyMFD1 # Replace "[data]" with the data you want to send
                  

                   

                  And for display the data received use the following command:

                   

                  cat /dev/ttyMFD1
                  

                   

                  One important thing is that the data sent is in ASCII code, so if you want to send the letter "a" for example, you will receive the number "97 " which correspond to the letter "a" in ASCII code. I hope this be useful for you.

                   

                  Regards,

                  Diego.