ModbusRTU控制LED燈
# PinToo之ModbusRTU控制LED燈
# 1. 說明
範例採用ModbusRTU通訊協議,控制駁接在Arduino上的LED燈。ModbusRTU傳輸方式為1個主機對應多個從機,此時使用從機的從機號來做識別,如果主機傳輸的從機號對應到該從機的號碼,如果格式正確該從機會作迴應。因此在Modbus傳輸中,每個從機都是被動的,在運行過程中會不斷監聽是否有收到請求。範例中的Arduino ModbusRTU設定為從機,等待主機發出指令進行各種控制活動。每個從機都有固定的保持暫存器的地址,需參照規範來進行存取。範例中也設定從機號與控制參數的地址。
範例中使用TTL轉RS-485模組(簡稱為TTL485),作為數據傳輸轉換的裝置,用於PC設備與Modbus RTU設備間的數據傳輸,單條485匯流排可以支援32個ModbusRTU設備。範例中採用TTL485擴充套件Arduino開發板在ModbusRTU的功能。TTL485與Arduino開發板的連線方式如下:
TTL485針腳 | Arduino針腳 | 功能 |
---|---|---|
RO | RX | 接受資訊 |
RE | D2 | 收發控制 |
DE | D2 | 收發控制 |
DI | TX | 發送資訊 |
VCC | 5V | 供電 |
GND | GND | 接地線 |
通過範例學習,可以掌握TfxUsbSerial元件的基本屬性設備,並結合Arduino開發板進行通訊埠LED燈的控制功能。
# 2. 零件連線圖
# 3. 使用零件
序 | 零件名稱 | 數量 |
---|---|---|
1 | Arduino UNO R3 開發板 | 1 |
2 | USB數據線 | 1 |
3 | TTL轉RS-485模組 | 1 |
4 | USB轉485接頭 | 1 |
5 | 麵包板 | 1 |
6 | 杜邦線 | 若干 |
7 | LED燈 | 1 |
8 | 220歐姆電阻 | 1 |
# 4. Arduino流程圖
# 5. Arduino程式
使用Arduino IDE 編譯並上傳以下Arduino程式。
// 使用鏈接庫 https://drive.google.com/file/d/0Bx5wWRfN_z7oMDlZMDhWbXM2VFU/view
// 使用擴充模組 Arduino Sensor Shield 感測器擴充套件板 V5 XBee RS485
#include <modbus.h>
#include <modbusDevice.h>
#include <modbusRegBank.h>
#include <modbusSlave.h>
#define LED_PIN 8 // 定義LED PIN為8
modbusDevice regBank; // 宣告 Modbus
modbusSlave slave;
#define RS485TxEnablePin 2
#define RS485Baud 9600 //波特率9600
#define RS485Format SERIAL_8N1 //通訊格式 8個數據位 無校驗 一個停止位
void setup() {
pinMode(LED_PIN, OUTPUT); // 設定PIN 8為輸出模式
regBank.setId(3); // 設定 Modus 從機號=3
regBank.add(40001); // 設定會使用到之 Holding Register 之地址
regBank.add(40002);
slave._device = ®Bank; // 啟動 modbus RTU
slave.setBaud(&Serial,RS485Baud,RS485Format,RS485TxEnablePin);
}
void loop() {
// PIN 8輸出為HIGH or 1 ,LED點亮 輸出為LOW or 0,LED熄滅
digitalWrite(LED_PIN, regBank.get(40001));
delay(500); //延時500ms
slave.run();
delay(500);
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 6. 設計明細
開啟PinToo設計器,分別加入下插圖之控制元件。或者點選左上角的[打開模板Lib檔案]
選擇模板檔案來打開對應模板。
①:TfxSuperButton元件,控制元件名稱為fxSuperButtonPaired
。
②:TfxUsbSerial元件,控制元件名稱為fxUsbSerial1
。
③:TfxSwitchButton元件,控制元件名稱為fxSwitchButton2
。
④:TfxSwitchButton元件,控制元件名稱為fxSwitchButton1
。
⑤:TfxComboBox元件,控制元件名稱為fxComboBoxPaired
。
⑥:TfxSuoerButto鈕元件,控制元件名稱為fxSuperButtonServices
。
fxRunFrame屬性設定
Height
:設定頁面高度=800
。Width
:設定頁面寬度=400
。
①fxSuperButtonPaired屬性設定
Height
:設定控制元件高度=40
。Width
:設定控制元件寬度=40
。ButtonType
:設定按鈕型別,設定為TfxSvgButton
。Name
:設定控制元件名稱=fxSuperButtonPaired
。ButtonType.SvgData
:設定SVG數據,其數據內容如下。
<path d="M35.4421844482422,20.57493019104 L31.6380100250244,16.7707595825195 L36.2994155883789,12.109356880188 C36.9879531860352,11.4208192825317 36.9879531860352,10.2916164398193 36.2994155883789,9.60307884216309 C35.6108703613281,8.91454124450684 34.4816741943359,8.91454124450684 33.7931365966797,9.60307884216309 L29.1317291259766,14.2644805908203 L25.7234668731689,10.8562164306641 L30.3848686218262,6.19481563568115 C31.0734062194824,5.5062780380249 31.0734062194824,4.37707614898682 30.3848686218262,3.68853807449341 C29.6963329315186,3 28.5671291351318,3.00000023841858 27.8785915374756,3.68853807449341 L23.2171897888184,8.34993934631348 L19.4130191802979,4.54576778411865 C18.7279243469238,3.86067247390747 17.6090488433838,3.86067247390747 16.9239540100098,4.54576778411865 C16.2388591766357,5.23086261749268 16.2388591766357,6.34973669052124 16.9239540100098,7.03483200073242 L32.9531173706055,23.0639934539795 C33.6382141113281,23.7490882873535 34.7570877075195,23.7490882873535 35.4421768188477,23.0639934539795 C36.1272735595703,22.3788967132568 36.1272735595703,21.2600231170654 35.4421768188477,20.57493019104 Z M31.5278415679932,23.3394088745117 L16.6244411468506,8.43600654602051 C16.5590286254883,8.37059593200684 16.4523067474365,8.37059593200684 16.3868961334229,8.43600654602051 L14.3281669616699,10.4947357177734 L10.4207153320313,14.4021883010864 L10.4138288497925,14.4090738296509 L10.3931722640991,14.4297304153442 C9.03675270080566,17.0633888244629 9.03675270080566,20.1927928924561 8.90593147277832,22.6715278625488 L17.3198642730713,31.0854587554932 C19.7813854217529,30.8995532989502 23.3514556884766,30.9993915557861 25.5306758880615,29.567232131958 L25.5582180023193,29.5396938323975 L25.5616607666016,29.5431346893311 L31.5278396606445,23.5769538879395 C31.5932502746582,23.5115432739258 31.5932502746582,23.4048194885254 31.5278396606445,23.3394088745117 Z M15.6363887786865,31.0475902557373 L8.916259765625,24.3274612426758 C8.44805335998535,23.8592567443848 7.68721866607666,23.8592567443848 7.21901273727417,24.3274612426758 C6.75080680847168,24.7956657409668 6.75080680847168,25.5565013885498 7.21901273727417,26.0247058868408 L13.9425859451294,32.7482757568359 C14.4107913970947,33.2164840698242 15.1716260910034,33.2130432128906 15.6398305892944,32.7482757568359 C16.1045932769775,32.2800712585449 16.1045932769775,31.5157947540283 15.6363878250122,31.04758644104 Z M5.8866925239563,29.0852565765381 C5.2532377243042,29.718713760376 5.2532377243042,30.7584037780762 5.8866925239563,31.3918590545654 L6.41686630249023,31.9220352172852 L3.01204681396484,35.3268547058105 L4.68519401550293,37 L8.09001350402832,33.5951805114746 L8.62018775939941,34.125358581543 C9.25364208221436,34.7588119506836 10.2933349609375,34.7588119506836 10.9267902374268,34.125358581543 L12.2866516113281,32.765495300293 L7.24655437469482,27.7253952026367 L5.88669204711914,29.0852565765381 Z" p-id="1591" fill="#FF1296DB" stroke="Null"></path>
1②fxUsbSerial1屬性設定
UsbSerialType
:通訊埠通訊協議型別,此處設定為RS485
。
③fxSwitchButton2屬性設定
Height
:設定控制元件高度=200
。Width
:設定控制元件寬度=200
。HitTest
:是否響應點選操作,設定為False
。SwitchOffBmp
:設定開關狀態為關時顯示的影象,雙擊該屬性或者點選屬性右側的[...]
打開影象編輯器。點選[Load...]
打開資源管理器,選擇所需的圖片後點擊[打開]
,選擇的影象會顯示在預覽框中。可在該頁面下進行裁切或者縮放畫素以使影象與控制元件的寬度高度相適應,修改完成後點選[OK]
按鈕完成影象修改。
SwitchOnBmp
:設定開關狀態為開時顯示的影象,雙擊該屬性或者點選屬性右側的[...]
打開影象編輯器。點選[Load...]
打開資源管理器,選擇所需的圖片後點擊[打開]
,選擇的影象會顯示在預覽框中。可在該頁面下進行裁切或者縮放畫素以使影象與控制元件的寬度高度相適應,修改完成後點選[OK]
按鈕完成影象修改。
④fxSwitchButton1屬性設定
Height
:設定控制元件高度=200
。Width
:設定控制元件寬度=200
。SwitchOffBmp
:設定開關狀態為關時顯示的影象,雙擊該屬性或者點選屬性右側的[...]
打開影象編輯器。點選[Load...]
打開資源管理器,選擇所需的圖片後點擊[打開]
,選擇的影象會顯示在預覽框中。可在該頁面下進行裁切或者縮放畫素以使影象與控制元件的寬度高度相適應,修改完成後點選[OK]
按鈕完成影象修改。
SwitchOnBmp
:設定開關狀態為開時顯示的影象,雙擊該屬性或者點選屬性右側的[...]
打開影象編輯器。點選[Load...]
打開資源管理器,選擇所需的圖片後點擊[打開]
,選擇的影象會顯示在預覽框中。可在該頁面下進行裁切或者縮放畫素以使影象與控制元件的寬度高度相適應,修改完成後點選[OK]
按鈕完成影象修改。
⑤fxComboBoxPaired屬性設定
Height
:設定控制元件高度=30
。Width
:設定控制元件寬度=150
。
⑥fxSuperButtonServices屬性設定
Height
:設定控制元件高度=40
。Width
:設定控制元件寬度=40
。ButtonType
:設定按鈕型別,設定為TfxSvgButton
。Name
:設定控制元件名稱=fxSuperButtonServices
。ButtonType.SvgData
:設定SVG數據,其數據內容如下。
<path d="M23.2725524902344,3.01327514648438 L16.9796981811523,3.01327514648438 L13.8332691192627,8.47858810424805 L16.9796981811523,13.943902015686 L23.2725524902344,13.943902015686 L26.4189777374268,8.47858810424805 L23.2725524902344,3.01327514648438 Z M22.2060432434082,12.0852537155151 L18.0506324768066,12.0852537155151 L15.9707164764404,8.47858810424805 L18.0506324768066,4.87192440032959 L22.2060432434082,4.87192440032959 L24.2815361022949,8.47858810424805 L22.2060432434082,12.0852537155151 Z M33.7517929077148,20.3562431335449 L27.4545116424561,20.3562431335449 L24.3080863952637,25.8215560913086 L27.4545116424561,31.2868709564209 L33.6721343994141,31.2868709564209 L36.0397033691406,27.3173294067383 L36.9026489257813,25.8215560913086 L33.7517929077148,20.3562431335449 Z M34.4421463012695,26.365873336792 L32.6189002990723,29.4237937927246 L28.525447845459,29.4237937927246 L26.449951171875,25.8171291351318 L28.525447845459,22.2104663848877 L32.6808547973633,22.2104663848877 L34.75634765625,25.8171291351318 L34.4421463012695,26.365873336792 Z M14.7271909713745,27.3129005432129 L15.5901355743408,25.8171291351318 L12.4437074661255,20.3518161773682 L6.14642715454102,20.3518161773682 L3,25.8171291351318 L6.14642715454102,31.2824420928955 L12.3640518188477,31.2824420928955 L14.7271909713745,27.3129005432129 Z M7.21736335754395,29.4237937927246 L5.14187145233154,25.8171291351318 L7.21736240386963,22.2104663848877 L11.3683462142944,22.2104663848877 L13.4438381195068,25.8171291351318 L13.1296377182007,26.365873336792 L11.3063907623291,29.4237937927246 L7.21736240386963,29.4237937927246 Z M23.4274387359619,29.6185111999512 C23.2858276367188,29.3751163482666 22.9981784820557,29.2644863128662 22.7282333374023,29.3485641479492 C21.8343124389648,29.6273612976074 20.8917102813721,29.7689743041992 19.9269847869873,29.7689743041992 C18.9755325317383,29.7689743041992 18.0506324768066,29.6317901611328 17.1699886322021,29.3662662506104 C16.904468536377,29.2866153717041 16.6212463378906,29.3928184509277 16.4796333312988,29.6317901611328 C16.2804908752441,29.9681186676025 16.4442310333252,30.4018001556396 16.8159599304199,30.5168609619141 C17.8160915374756,30.8222141265869 18.8604736328125,30.9771003723145 19.9269847869873,30.9771003723145 C21.0111961364746,30.9771003723145 22.0688571929932,30.8177871704102 23.0866889953613,30.4991588592529 C23.4539928436279,30.3796768188477 23.6221561431885,29.9548454284668 23.4274425506592,29.6185169219971 Z M26.972146987915,34.5837631225586 C26.8039817810059,34.3049621582031 26.4455280303955,34.212028503418 26.1623077392578,34.3624877929688 C24.2284278869629,35.2829666137695 22.1042575836182,35.7786102294922 19.9269828796387,35.7786102294922 C17.732006072998,35.7786102294922 15.5945606231689,35.2785415649414 13.6562557220459,34.3492202758789 C13.3686084747314,34.1722030639648 12.99245262146,34.2651290893555 12.8242893218994,34.5616302490234 L12.811014175415,34.5837631225586 C12.6517009735107,34.8625564575195 12.749059677124,35.212158203125 13.0234308242798,35.3847427368164 L13.0278568267822,35.3891754150391 L13.2402725219727,35.4953842163086 C15.3423166275024,36.4866638183594 17.5903968811035,36.9867248535156 19.9269847869873,36.9867248535156 C22.2237453460693,36.9867248535156 24.4408473968506,36.4999389648438 26.5119113922119,35.5396347045898 C26.5959930419922,35.4998016357422 26.6800746917725,35.4599761962891 26.7685813903809,35.4201507568359 C27.0518035888672,35.2431335449219 27.144739151001,34.8714065551758 26.972146987915,34.5837554931641 Z M26.9411716461182,12.2843942642212 C26.7951374053955,12.5233631134033 26.839391708374,12.8331384658813 27.0429534912109,13.027853012085 C27.7156105041504,13.6783809661865 28.2953281402588,14.435115814209 28.7599906921387,15.2847843170166 C29.2202301025391,16.1167507171631 29.5432796478271,16.9973964691162 29.7335681915283,17.9001693725586 C29.7911014556885,18.1701164245605 30.0212211608887,18.3692569732666 30.3000183105469,18.3781070709229 C30.6894454956055,18.3913822174072 30.9903717041016,18.037353515625 30.9107151031494,17.656774520874 C30.693868637085,16.630090713501 30.3265647888184,15.6388120651245 29.8132247924805,14.7050638198853 C29.291036605835,13.7536125183105 28.6405067443848,12.9039440155029 27.8704967498779,12.1604852676392 C27.5916996002197,11.8949632644653 27.1403121948242,11.9569177627563 26.9411716461182,12.2843942642212 Z M29.5698318481445,6.77925252914429 C29.4105186462402,7.06247520446777 29.4990253448486,7.42092943191528 29.7689723968506,7.59351778030396 C31.5081386566162,8.84589290618896 32.9640808105469,10.4744243621826 34.0128860473633,12.3817529678345 C35.0705528259277,14.3067817687988 35.6635475158691,16.4265270233154 35.7874565124512,18.5772495269775 C35.7697525024414,18.9135761260986 36.0352745056152,19.2012252807617 36.3760261535645,19.2056503295898 L36.4025764465332,19.2056503295898 C36.7256278991699,19.210075378418 36.9778785705566,18.9578304290771 37,18.6347808837891 L37,18.6303539276123 L36.9867286682129,18.3913860321045 C36.8406867980957,16.0680751800537 36.1945877075195,13.8509712219238 35.0661239624023,11.8020315170288 C33.9597854614258,9.78407001495361 32.4640121459961,8.07588291168213 30.6230621337891,6.71729946136475 C30.5478324890137,6.6597695350647 30.472599029541,6.60666561126709 30.3929424285889,6.55356121063232 C30.1097183227539,6.3898229598999 29.7379913330078,6.48718070983887 29.5698261260986,6.77925491333008 Z M9.61590576171875,18.3073024749756 C9.89470291137695,18.2763233184814 10.1115465164185,18.0550556182861 10.1469488143921,17.7718353271484 C10.2531566619873,16.8380832672119 10.4965515136719,15.9176111221313 10.8859834671021,15.0325403213501 C11.2665643692017,14.1607446670532 11.7622032165527,13.3641805648804 12.3596258163452,12.6649751663208 C12.5366401672363,12.4525585174561 12.5543413162231,12.1516332626343 12.3950281143188,11.9259405136108 C12.1693353652954,11.6073141098022 11.7046728134155,11.585186958313 11.4524278640747,11.8816862106323 C10.770923614502,12.6782503128052 10.2133293151855,13.572172164917 9.7840690612793,14.5501747131348 C9.35038471221924,15.545880317688 9.07158660888672,16.581413269043 8.95652675628662,17.643497467041 C8.9078483581543,18.0285053253174 9.23089981079102,18.3515548706055 9.61590385437012,18.307300567627 Z M3.74345946311951,19.5729541778564 C4.06208515167236,19.5286998748779 4.29220294952393,19.2410526275635 4.27007675170898,18.9180011749268 C4.20369625091553,16.7761325836182 4.60197830200195,14.6254081726074 5.47377300262451,12.6251468658447 C6.35441827774048,10.6116104125977 7.6643238067627,8.85031890869141 9.29285430908203,7.44305562973022 C9.57165145874023,7.24834060668945 9.63360786437988,6.86775970458984 9.4344654083252,6.59338808059692 L9.42119026184082,6.57126140594482 C9.23090171813965,6.31016540527344 8.87244701385498,6.26591205596924 8.60692596435547,6.44292593002319 L8.60249900817871,6.44735145568848 C8.5405445098877,6.50045585632324 8.4830150604248,6.54913520812988 8.42105960845947,6.60223913192749 C6.67304420471191,8.1334114074707 5.31446075439453,10.0009117126465 4.37628555297852,12.142783164978 C3.45581150054932,14.2492523193359 3.01327586174011,16.4796314239502 3.06195497512817,18.7675380706787 C3.06195497512817,18.8604717254639 3.06638073921204,18.9578304290771 3.07080554962158,19.0507621765137 C3.10620856285095,19.3826637268066 3.41155791282654,19.6172065734863 3.74345946311951,19.5729522705078 Z" p-id="4161" fill="#FF1296DB" stroke="Null"></path>
1
# 7. 程式設計
點選程式設計界面右下角的按鈕,切換至單元選擇界面,勾選需要使用的單元。該程式需要引用uUsbSerial
單元。
# 7.1. 程式初始設定
程式在關閉視窗前對開通訊埠連線。
Procedure fxRunFrameBeforeClose(Sender: TObject);
//關閉窗體前關閉通訊埠
Begin
fxUsbSerial1.Close;
End;
2
3
4
5
# 7.2. 事件設定
- ①fxSuperButtonPaired-OnClick事件
點選按鈕,載入通訊埠設備列表並顯示。
Procedure fxSuperButtonPairedOnClick(Sender: TObject);
//載入設備列表
Begin
fxUsbSerial1.Close;
ComboBoxPaired.Items := fxUsbSerial1.Devices;
ComboboxPaired.DropDown;
fxSuperButtonPaired.Enabled := False;
End;
2
3
4
5
6
7
8
- ⑤fxComboBoxPaired-OnChange事件
點選按鈕,切換連線的設備。
Procedure ComboBoxPairedOnChange(Sender: TObject);
//切換配對設備
Begin
fxUsbSerial1.Close;
fxUsbSerial1.UsbDevice := ComboBoxPaired.Items[ComboBoxPaired.ItemIndex];
fxRDToastMessage1.ShowToastMessage('通訊埠已關閉,請選擇後重新打開通訊埠',2.0,100);
End;
2
3
4
5
6
7
- ⑥fxSuperButtonServices-OnClick事件
點選以開啟通訊埠通訊。
Procedure fxSuperButtonServicesOnClick(Sender: TObject);
//開啟埠通訊埠
begin
fxUsbSerial1.Open(True);
End;
2
3
4
5
- ④fxSwitchButton1-OnSwitch事件
點選切換開關。
Procedure fxSwitchButton1OnSwitch(Sender: TObject);
//開啟、關閉LED
Begin
if fxSwitchButton1.IsChecked Then
//開啟LED燈
begin
fxUsbSerial1.WriteMultipleRegisters(3,0,1,'1'); //發送 1 字元,打開LED
fxRDToastMessage1.ShowToastMessage('LED燈已開啟',2.0,100);
End
else
Begin
fxUsbSerial1.WriteMultipleRegisters(3,0,1,'0'); //發送 0 字元,關閉LED
fxRDToastMessage1.ShowToastMessage('LED燈已關閉',2.0,100);
End;
fxSwitchButton2.IsChecked := fxSwitchButton1.IsChecked;
End;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 8. 運行結果
使用滑鼠在 PinToo 功能表,點選[除錯運行]
按鈕,測試運行結果。
通過同步中心,將程式上傳至手機PinToo運行;同步時,請確保手機已經運行PinToo,並且已經登陸。
將RS485轉USB的一端與手機的USB相連,點選左上角的配對按鈕,選擇對應的通訊埠,點選右側的按鈕開啟通訊埠連線,點選控制區的開關按鈕,當控制區燈亮時LED燈亮;當控制區燈滅時,LED燈滅。