2021-09-14 08:11:40 索煒達電子 1214
項目編號:E1187
文件大?。?M
源碼說明:帶中文注釋
開發(fā)環(huán)境:C編譯器
簡要概述:
Modbus概述
-Modbus是一種串行通信工業(yè)協(xié)議,于1979年開發(fā),旨在實現(xiàn)自動化設(shè)備之間的通信;
-Modbus協(xié)議可用于串口、以太網(wǎng)等通信鏈路;
-Modbus協(xié)議有ModbusRTU(二進制表示數(shù)據(jù)方式)和ModbusASCII(文本可讀)兩種通信格式,常用RTU格式;
-Modbus協(xié)議分主從,總線網(wǎng)絡(luò)中只能有一個主,支持大約240從;
-在數(shù)據(jù)采集與監(jiān)控系統(tǒng)中,Modbus常用來鏈接監(jiān)控計算機和遠程終端控制系統(tǒng)(RTU).
Modbus應(yīng)用舉例
1.電子秤
2.多功能數(shù)據(jù)采集儀
3.點陣顯示系統(tǒng)
4.略
摘要
作為工業(yè)用協(xié)議,STM32的普及使得Modbus在各大工控產(chǎn)品上得到了最大限度的應(yīng)用, 而在日常的教學(xué)中,并沒有相關(guān)的案例(Demo)讓初學(xué)者很好的去接觸Modbus協(xié)議,網(wǎng)絡(luò)上普遍是枯燥的Modbus原理講解,相關(guān)書籍也是寥寥,目前市面上針對Modbus協(xié)議開發(fā)的書籍只有《Modbus軟件開發(fā)實戰(zhàn)》一書, 幾乎是純軟件的Modbus編程和Modbus相關(guān)原理介紹,并沒有涉及Modbus設(shè)備方面的開發(fā)介紹。
為了消除困擾,樓主將自己的STM32與LabVIEW上位機Modbus通信項目(Demo)開源,方便廣大STM32愛好者更好地學(xué)習(xí)和開發(fā)Modbus相關(guān)產(chǎn)品。
項目簡介
項目基于正點原子STM32F4探索者開發(fā)板和LabVIEW開發(fā)的上位機軟件進行Modbus協(xié)議通信,項目采用TCP/IP通信方式,移植freeModbus協(xié)議到STM32,利用LabVIEW Modbus庫,編寫配套上位機讀取開發(fā)板按鍵、AD值,并可通過上位機設(shè)置開發(fā)板LED、DAC狀態(tài)。
功能簡介
-線 圈 :led控制
-離散輸入 :按鍵讀取
-保持寄存器:ADC讀取
-輸入寄存器:DAC設(shè)置
注意事項
1.開發(fā)板與運行上位機的PC需要連入同一局域網(wǎng);
2.編譯代碼前需要修改靜態(tài)IP參數(shù),需與您的局域網(wǎng)網(wǎng)段一致;
3.開發(fā)板需要插入LCD顯示屏;
4.PC需要安裝高于LabVIEW2013的開發(fā)環(huán)境,并將NI Modbus庫解壓到對應(yīng)文件夾;
5.運行LabVIEW上位機前,填入開發(fā)板的靜態(tài)IP;
6.不熟悉LabVIEW的同學(xué)可以使用ModbusPoll調(diào)試工具進行學(xué)習(xí)。
上位機效果圖
目錄│文件列表:
└ stm32f4_modbus_tcp
├ LabVIEW-TCP-Modbus-Demo
│ │ ModbusTCP-Demo.vi
│ └ SubVI
│ │ DiscreteInputsReg.vi
│ │ InputRegisters.vi
│ │ WriteCoils.vi
│ └ WriteMultipleReg.vi
├ STM32F4-FreeModBus-Lwip-TCP-uCOSII-Demo
│ │ keilkilll.bat
│ │ readme.txt
│ ├ CORE
│ │ │ core_cm4.h
│ │ │ core_cm4_simd.h
│ │ │ core_cmFunc.h
│ │ │ core_cmInstr.h
│ │ └ startup_stm32f40_41xxx.s
│ ├ FreeModbusTCP
│ │ ├ app
│ │ │ │ user_mb_app.c
│ │ │ └ user_mb_app.h
│ │ ├ modbus
│ │ │ │ mb.c
│ │ │ ├ ascii
│ │ │ │ │ mbascii.c
│ │ │ │ └ mbascii.h
│ │ │ ├ functions
│ │ │ │ │ mbfunccoils.c
│ │ │ │ │ mbfuncdiag.c
│ │ │ │ │ mbfuncdisc.c
│ │ │ │ │ mbfuncholding.c
│ │ │ │ │ mbfuncinput.c
│ │ │ │ │ mbfuncother.c
│ │ │ │ └ mbutils.c
│ │ │ ├ include
│ │ │ │ │ mb.h
│ │ │ │ │ mbconfig.h
│ │ │ │ │ mbframe.h
│ │ │ │ │ mbfunc.h
│ │ │ │ │ mbport.h
│ │ │ │ │ mbproto.h
│ │ │ │ └ mbutils.h
│ │ │ ├ rtu
│ │ │ │ │ mbcrc.c
│ │ │ │ │ mbcrc.h
│ │ │ │ │ mbrtu.c
│ │ │ │ └ mbrtu.h
│ │ │ └ tcp
│ │ │ │ mbtcp.c
│ │ │ └ mbtcp.h
│ │ └ port
│ │ │ port.h
│ │ │ portevent.c
│ │ │ portother.c
│ │ └ porttcp.c
│ ├ FWLIB
│ │ ├ STM32F4x7_ETH_Driver
│ │ │ ├ inc
│ │ │ │ │ stm32f4x7_eth.h
│ │ │ │ └ stm32f4x7_eth_conf.h
│ │ │ └ src
│ │ │ └ stm32f4x7_eth.c
│ │ └ STM32F4xx_StdPeriph_Driver
│ │ ├ inc
│ │ │ │ misc.h
│ │ │ │ stm32f4xx_adc.h
│ │ │ │ stm32f4xx_can.h
│ │ │ │ stm32f4xx_crc.h
│ │ │ │ stm32f4xx_cryp.h
│ │ │ │ stm32f4xx_dac.h
│ │ │ │ stm32f4xx_dbgmcu.h
│ │ │ │ stm32f4xx_dcmi.h
│ │ │ │ stm32f4xx_dma.h
│ │ │ │ stm32f4xx_dma2d.h
│ │ │ │ stm32f4xx_exti.h
│ │ │ │ stm32f4xx_flash.h
│ │ │ │ stm32f4xx_flash_ramfunc.h
│ │ │ │ stm32f4xx_fmc.h
│ │ │ │ stm32f4xx_fsmc.h
│ │ │ │ stm32f4xx_gpio.h
│ │ │ │ stm32f4xx_hash.h
│ │ │ │ stm32f4xx_i2c.h
│ │ │ │ stm32f4xx_iwdg.h
│ │ │ │ stm32f4xx_ltdc.h
│ │ │ │ stm32f4xx_pwr.h
│ │ │ │ stm32f4xx_rcc.h
│ │ │ │ stm32f4xx_rng.h
│ │ │ │ stm32f4xx_rtc.h
│ │ │ │ stm32f4xx_sai.h
│ │ │ │ stm32f4xx_sdio.h
│ │ │ │ stm32f4xx_spi.h
│ │ │ │ stm32f4xx_syscfg.h
│ │ │ │ stm32f4xx_tim.h
│ │ │ │ stm32f4xx_usart.h
│ │ │ └ stm32f4xx_wwdg.h
│ │ └ src
│ │ │ misc.c
│ │ │ stm32f4xx_adc.c
│ │ │ stm32f4xx_can.c
│ │ │ stm32f4xx_crc.c
│ │ │ stm32f4xx_cryp.c
│ │ │ stm32f4xx_cryp_aes.c
│ │ │ stm32f4xx_cryp_des.c
│ │ │ stm32f4xx_cryp_tdes.c
│ │ │ stm32f4xx_dac.c
│ │ │ stm32f4xx_dbgmcu.c
│ │ │ stm32f4xx_dcmi.c
│ │ │ stm32f4xx_dma.c
│ │ │ stm32f4xx_dma2d.c
│ │ │ stm32f4xx_exti.c
│ │ │ stm32f4xx_flash.c
│ │ │ stm32f4xx_flash_ramfunc.c
│ │ │ stm32f4xx_fmc.c
│ │ │ stm32f4xx_fsmc.c
│ │ │ stm32f4xx_gpio.c
│ │ │ stm32f4xx_hash.c
│ │ │ stm32f4xx_hash_md5.c
│ │ │ stm32f4xx_hash_sha1.c
│ │ │ stm32f4xx_i2c.c
│ │ │ stm32f4xx_iwdg.c
│ │ │ stm32f4xx_ltdc.c
│ │ │ stm32f4xx_pwr.c
│ │ │ stm32f4xx_rcc.c
│ │ │ stm32f4xx_rng.c
│ │ │ stm32f4xx_rtc.c
│ │ │ stm32f4xx_sai.c
│ │ │ stm32f4xx_sdio.c
│ │ │ stm32f4xx_spi.c
│ │ │ stm32f4xx_syscfg.c
│ │ │ stm32f4xx_tim.c
│ │ │ stm32f4xx_usart.c
│ │ └ stm32f4xx_wwdg.c
│ ├ HARDWARE
│ │ ├ ADC
│ │ │ │ adc.c
│ │ │ └ adc.h
│ │ ├ ADC3
│ │ │ │ adc3.c
│ │ │ └ adc3.h
│ │ ├ BEEP
│ │ │ │ beep.c
│ │ │ └ beep.h
│ │ ├ DAC
│ │ │ │ dac.c
│ │ │ └ dac.h
│ │ ├ ETHERNET
│ │ │ │ lan8720.c
│ │ │ └ lan8720.h
│ │ ├ KEY
│ │ │ │ key.c
│ │ │ └ key.h
│ │ ├ LCD
│ │ │ │ FONT.H
│ │ │ │ lcd.c
│ │ │ └ lcd.h
│ │ ├ LED
│ │ │ │ led.c
│ │ │ └ led.h
│ │ ├ LSENS
│ │ │ │ lsens.c
│ │ │ └ lsens.h
│ │ ├ MALLOC
│ │ │ │ malloc.c
│ │ │ └ malloc.h
│ │ ├ PWM_DAC
│ │ │ │ pwmdac.c
│ │ │ └ pwmdac.h
│ │ ├ SRAM
│ │ │ │ sram.c
│ │ │ └ sram.h
│ │ └ TIMER
│ │ │ timer.c
│ │ └ timer.h
│ ├ LWIP
│ │ ├ arch
│ │ │ │ cc.h
│ │ │ │ cpu.h
│ │ │ │ perf.h
│ │ │ │ sys_arch.c
│ │ │ └ sys_arch.h
│ │ ├ lwip-1.4.1
│ │ │ │ CHANGELOG
│ │ │ │ COPYING
│ │ │ │ FILES
│ │ │ │ README
│ │ │ │ UPGRADING
│ │ │ ├ doc
│ │ │ │ │ contrib.txt
│ │ │ │ │ FILES
│ │ │ │ │ rawapi.txt
│ │ │ │ │ savannah.txt
│ │ │ │ │ snmp_agent.txt
│ │ │ │ └ sys_arch.txt
│ │ │ ├ src
│ │ │ │ │ FILES
│ │ │ │ ├ api
│ │ │ │ │ │ api_lib.c
│ │ │ │ │ │ api_msg.c
│ │ │ │ │ │ err.c
│ │ │ │ │ │ netbuf.c
│ │ │ │ │ │ netdb.c
│ │ │ │ │ │ netifapi.c
│ │ │ │ │ │ sockets.c
│ │ │ │ │ └ tcpip.c
│ │ │ │ ├ core
│ │ │ │ │ │ def.c
│ │ │ │ │ │ dhcp.c
│ │ │ │ │ │ dns.c
│ │ │ │ │ │ init.c
│ │ │ │ │ │ lwip_sys.c
│ │ │ │ │ │ mem.c
│ │ │ │ │ │ memp.c
│ │ │ │ │ │ netif.c
│ │ │ │ │ │ pbuf.c
│ │ │ │ │ │ raw.c
│ │ │ │ │ │ stats.c
│ │ │ │ │ │ tcp.c
│ │ │ │ │ │ tcp_in.c
│ │ │ │ │ │ tcp_out.c
│ │ │ │ │ │ timers.c
│ │ │ │ │ │ udp.c
│ │ │ │ │ ├ ipv4
│ │ │ │ │ │ │ autoip.c
│ │ │ │ │ │ │ icmp.c
│ │ │ │ │ │ │ igmp.c
│ │ │ │ │ │ │ inet.c
│ │ │ │ │ │ │ inet_chksum.c
│ │ │ │ │ │ │ ip.c
│ │ │ │ │ │ │ ip_addr.c
│ │ │ │ │ │ └ ip_frag.c
│ │ │ │ │ ├ ipv6
│ │ │ │ │ │ │ icmp6.c
│ │ │ │ │ │ │ inet6.c
│ │ │ │ │ │ │ ip6.c
│ │ │ │ │ │ │ ip6_addr.c
│ │ │ │ │ │ └ README
│ │ │ │ │ └ snmp
│ │ │ │ │ │ asn1_dec.c
│ │ │ │ │ │ asn1_enc.c
│ │ │ │ │ │ mib2.c
│ │ │ │ │ │ mib_structs.c
│ │ │ │ │ │ msg_in.c
│ │ │ │ │ └ msg_out.c
│ │ │ │ ├ include
│ │ │ │ │ ├ ipv4
│ │ │ │ │ │ └ lwip
│ │ │ │ │ │ │ autoip.h
│ │ │ │ │ │ │ icmp.h
│ │ │ │ │ │ │ igmp.h
│ │ │ │ │ │ │ inet.h
│ │ │ │ │ │ │ inet_chksum.h
│ │ │ │ │ │ │ ip.h
│ │ │ │ │ │ │ ip_addr.h
│ │ │ │ │ │ └ ip_frag.h
│ │ │ │ │ ├ ipv6
│ │ │ │ │ │ └ lwip
│ │ │ │ │ │ │ icmp.h
│ │ │ │ │ │ │ inet.h
│ │ │ │ │ │ │ ip.h
│ │ │ │ │ │ └ ip_addr.h
│ │ │ │ │ ├ lwip
│ │ │ │ │ │ │ api.h
│ │ │ │ │ │ │ api_msg.h
│ │ │ │ │ │ │ arch.h
│ │ │ │ │ │ │ debug.h
│ │ │ │ │ │ │ def.h
│ │ │ │ │ │ │ dhcp.h
│ │ │ │ │ │ │ dns.h
│ │ │ │ │ │ │ err.h
│ │ │ │ │ │ │ init.h
│ │ │ │ │ │ │ lwip_sys.h
│ │ │ │ │ │ │ mem.h
│ │ │ │ │ │ │ memp.h
│ │ │ │ │ │ │ memp_std.h
│ │ │ │ │ │ │ netbuf.h
│ │ │ │ │ │ │ netdb.h
│ │ │ │ │ │ │ netif.h
│ │ │ │ │ │ │ netifapi.h
│ │ │ │ │ │ │ opt.h
│ │ │ │ │ │ │ pbuf.h
│ │ │ │ │ │ │ raw.h
│ │ │ │ │ │ │ sio.h
│ │ │ │ │ │ │ snmp.h
│ │ │ │ │ │ │ snmp_asn1.h
│ │ │ │ │ │ │ snmp_msg.h
│ │ │ │ │ │ │ snmp_structs.h
│ │ │ │ │ │ │ sockets.h
│ │ │ │ │ │ │ stats.h
│ │ │ │ │ │ │ tcp.h
│ │ │ │ │ │ │ tcpip.h
│ │ │ │ │ │ │ tcp_impl.h
│ │ │ │ │ │ │ timers.h
│ │ │ │ │ │ └ udp.h
│ │ │ │ │ ├ netif
│ │ │ │ │ │ │ etharp.h
│ │ │ │ │ │ │ ethernetif.h