How to use RS485

From IGEP - ISEE Wiki

Jump to: navigation, search

Overview

This How-To is meant to be a starting point for people to learn to use RS-485 port on IGEP devices as quickly and easily as possible. We use IGEP YOCTO Firmware and RS485 example which describe how to setup and write data on the RS-485 port.


Requirements

Add RS-485 support

Flash memory

Boot IGEP with a MicroSD card.

mkdir /tmp/temp
mount -t jffs2 /dev/mtdblock1 /tmp/temp 

Now, mtdblock1 partition is mounted at /tmp/temp directory. Modify igep.ini located in: /tmp/temp/igep.ini

  • Add RS-485 support:
board.ei485=yes

Save changes, power down IGEP, unplug MicroSD card and power up your board.

Micro SD

Plug SD card in your computer, boot partition should be appear. Modify igep.ini located in: /media/boot/igep.ini

  • Add RS-485 support:
board.ei485=yes

Save changes, plug SD card in IGEP and power up your board.

IGEPv2

RS-485 port is located at J940 connector, connect two IGEPv2 devices like this:

 J940                         J940
 ---                           ---
| 1 |---------- GND --------------| 1 |
| 2 |-X                      X-| 2 |
| 3 |-X                      X-| 3 |
| 4 |----------  A  --------------| 4 |
| 5 |----------  B  --------------| 5 |
 ---                           ---

Program

Download an Install IGEP SDK if you don't have it.

Create source code text file:

First of all you need to initialize a suitable environment in the bash shell console inside your machine.
You can do this sourcing once the environment-setup script.

jdoe@ubuntu ~ $ source /opt/poky/1.2/environment-setup-armv7a-vfp-neon-poky-linux-gnueabi 

Create a single .c file (485-example.c), using your preferred editor (vi, nano, gedit, ...)
485-example.c:

/* 485-example.c : Basic example of RS485 half duplex transmission */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>

#include <sys/ioctl.h>
#include <asm/ioctls.h>

#include <linux/serial.h>

int main(void)
{
   char dev[] = "/dev/ttyO0";
   char tosend[] = {'A', 'B', 'C'};
   struct serial_rs485 ctrl485;
   int status;
   int fd;

   struct termios ti;
   speed_t speed;

   /* Open the port */
   fd = open(dev, O_RDWR);
   if (fd < 0) {
      printf("%s: Unable to open.\n", dev);
      return -1;
   }

   /* Set the port in 485 mode */
   ctrl485.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND;
   ctrl485.delay_rts_before_send = 0;
   ctrl485.delay_rts_after_send = 0;

   status = ioctl(fd, TIOCSRS485, &ctrl485);
   if (status) {
      printf("%s: Unable to configure port in 485 mode, status (%i)\n", dev, status);
      return -1;
   }

   /* Set the port speed */
   speed = B9600;
   tcgetattr(fd, &ti);
   cfsetospeed(&ti, speed);
   cfsetispeed(&ti, speed);
   tcsetattr(fd, TCSANOW, &ti);

   /* Send content to RS485 */
   if (write(fd, tosend, sizeof(tosend)) != sizeof(tosend)) {
      printf("%s: write() failed\n", dev);
   }

   return 0;
}
  • Build arm binary executable:

Cross toolchain tools are available into the built-in virtual machine Poky SDK. You only need open bash terminal prompt and write commands:

jdoe@ubuntu ~/Desktop $ arm-poky-linux-gnueabi-gcc -o 485-example 485-example.c

Test

IGEP 1

  • Get a remote shell and execute microcom program
microcom -s 9600 -X /dev/ttyO0

IGEP 2

  • Transfer arm executable file from virtual machine to IGEP Board
  • Execute arm executable file inside IGEP Board (remote shell):
root@igep00x0:~# ./485-example

Results

If you follow steps correctly you can see the next result at IGEP1:

root@igep00x0:~# microcom -s 9600 -X /dev/ttyO0
ABC 

IGEP BERLIN

under construction