Easyarm i.mx28 Learning notes-controlling gpio via Modbus TCP

Source: Internet
Author: User

0 PrefaceThis paper uses the Freemodbus protocol stack to implement the Modbus TCP slave on the Easyarm i.mx287. A coil register is defined in the slave, where the 4-bit with the lower coil register address and the Easyarm P2.4 to P2.5 are associated with the output of the Gpio via the modbus instruction. This article modifies the self-freemodbus sample linuxtcp and is also available for other Linux development boards with simple modifications.
"Related blog posts""Easyarm i.mx28 Study notes--File IO mode operation Gpio" "Easyarm i.mx28 Learning notes-install and use tftp" "Raspberry Pi Study notes-realization Modbus RTU slave" "Modbus protocol Finishing-summary" "Freemodbus Modbus TCP Learning Note" "Modbus learning note--modbus TK modbus TCP host Implementation" "Linux learning Notes--Example Makefile Index blog post"
"Code Warehouse"The code warehouse is located in Bitbucket--easyarm-modbus-tcp, please use HG clone or download Zip package directly. Please do not use any version of IE browser to access the link unless you already know that the IE browser used complies with the HTML5 standard. It is recommended to use Google or Firefox browser, if using a domestic dual-core browser, please switch to fast mode.
1 part Code"Gpio-sysfs"Add enable Gpio, prohibit Gpio and gpio periodic processing function in GPIO-SYSFS. wherein the gpio_poll passed in the parameter is Modbus coil register
int gpio_enable (void) {    gpio_export (P24); gpio_direction (p24,out); Gpio_write (p24,0)    ; Gpio_export (P25); gpio_direction (p25,out); Gpio_write (p25,0)    ; Gpio_export (P26); gpio_direction (p26,out); Gpio_write (p26,0)    ; Gpio_export (P27); gpio_direction (p27,out); Gpio_write (p27,0)    ; return 0;} int gpio_disable (void) {    gpio_write (p24,0); Gpio_unexport (P24);    Gpio_write (p25,0); Gpio_unexport (P25);    Gpio_write (p26,0); Gpio_unexport (P26);    Gpio_write (p27,0); Gpio_unexport (P27);    return 0;} int Gpio_poll (unsigned char status) {    status & 0x01 Gpio_write (p24,1): Gpio_write (p24,0);    Status & 0x02? Gpio_write (p25,1): Gpio_write (p25,0);    Status & 0x04? Gpio_write (p26,1): Gpio_write (p26,0);    Status & 0x08? Gpio_write (p27,1): Gpio_write (p27,0);    return 0;}
"Modbus poll"The Pvpollingthread thread obtains the coil register result--ucregcoilsbuf[0] and passes it to the Gpio_poll function.
void* pvpollingthread (void *pvparameter) {    esetpollingthreadstate (RUNNING);    if (embenable () = = Mb_enoerr)    {do        {            gpio_poll (ucregcoilsbuf[0]);//change IO Port status            if (Embpoll ()! = Mb_e NOERR) break                ;        }        while (Egetpollingthreadstate ()! = SHUTDOWN);    }    (void) embdisable ();    Esetpollingthreadstate (STOPPED);    return 0;}
"Coil register Read and write function"
EMBERRORCODEEMBREGCOILSCB (UCHAR * pucregbuffer, USHORT usaddress, USHORT usncoils, Embregistermode emode) {Emberrorco    De estatus = Mb_enoerr;    int incoils = (int) usncoils;       int usbitoffset;     if ((usaddress >= reg_coils_start) && (usaddress + usncoils <= Reg_coils_start + reg_coils_size))        {usbitoffset = (int) (Usaddress-reg_coils_start); Switch (emode) {case Mb_reg_read:while (Incoils > 0) {*PUCREGB uffer++ = Xmbutilgetbits (Ucregcoilsbuf, Usbitoffset, (UCHAR) (Incoils & Gt 8?                8:incoils));                Incoils-= 8;            Usbitoffset + = 8;                   } break; Case Mb_reg_write:while (Incoils > 0) {xmbutilsetbits (Ucregcoilsbuf, usbitoffs                    ET, (UCHAR) (Incoils > 8 8:incoils),           *pucregbuffer++);                Incoils-= 8;            Usbitoffset + = 8;        } break;    }} else {estatus = Mb_enoreg; } return estatus;}
    " Makefile "
# Specify Compiler cross = ARM-FSL-LINUX-GNUEABI-CC = $ (cross) Gccstrip = $ (cross) strip# cflag include header file directory Cflags =-g-wall# header File Lookup Path inc = I . -iport-i. /.. /modbus/rtu-i. /.. /modbus/ascii-i. /.. /modbus/include-i. /.. /modbus/tcp#libs =-lpthread# Destination target = modbustcp# source file src = demo.c gpio-sysfs.c port/portother.c port/portevent.c por T/porttcp.c. /.. /MODBUS/MB.C. /.. /modbus/tcp/mbtcp.c. /.. /MODBUS/FUNCTIONS/MBFUNCCOILS.C. /.. /modbus/functions/mbfuncdiag.c. /.. /modbus/functions/mbfuncholding.c. /.. /modbus/functions/mbfuncinput.c. /.. /modbus/functions/mbfuncother.c. /.. /MODBUS/FUNCTIONS/MBFUNCDISC.C. /.. The/modbus/functions/mbutils.c# source file is compiled to the destination file Objs = $ (SRC:.C=.O).  phony:clean# link to executable file $ (TARGET): $ (OBJS) $ (CC) $^-O [email protected] $ (LIBS) $ (STRIP) [email protected]# Executable and destination files Clean:rm-f $ (OBJS) Rm-f $ (target) # Continuous action, first clear and then compile the link, copy to Tftpboot Install:clean $ (target) @echo copy to TFTPB OOT directory CP $ (TARGET) ~/tftpboot @echo Copy End # compilation rule Add header file [email protected] on behalf of the target file $< represents the first dependent file%.o:%.c $ (CC) $ (CFLAGS) $ (INC)-O [email protected]-C $< 

2 Experiments"1" in virtual machine make get executable file--modbustcp "2" Configuration Easyarm IP address, for example Easyarm IP address is 192.168.1.211 ifconfig eht0 192.168.1.211 "2" via TF TP transmits the executable file to the Development Board (assuming that the IP address of the virtual machine is 192.168.1.106) tftp-g-R modbustcp 192.168.1.106 "3" To modify the executable permissions. and run chmod a+x modbustcp./modbustcp "4" Modbus TCP has a simple console, input h can get instruction help.    where e is the Enable protocol stack, q is the exit program. Enter e to enable Modbus slave protocol stack.
Figure 1 Running Modbus TCP
"5" opens the Modbus debugging software in Windows, the connection Easyarm,ip address is 192.168.1.211, the port number is the default port number 205.
Figure 2 Controlling Gpio via Modbus TCP
Figure 3 Experimental results
4 Summary"1" Operation steps more, the realization of Modbus TCP requires a lot of basic knowledge.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.