通訊埠控制LED燈
# PinToo之通訊埠控制LED燈
# 1. 說明
使用PinToo智能開發平臺,通過通訊埠控制連線到Arduino的LED。當從通訊埠輸出高電平時,開啟Arduino開發板上的LED,當通訊埠輸出低位電平時,關閉Arduino開發板上的LED。
LED發光二極管是電子電路中常用的元器件,它便宜且耐用,通常用於訊號指示的電路中。在範例中,我們使用一個LED發光二極管,它的正極(二極管腳較長的一端)與一個電阻相連,LED發光二極管的負極(二極管腳較短的一端)與Arduino開發板的地線(GND)相連。此處使用到一個限流電阻,因為Arduino開發板輸出的電流為5V,超出二極管的工作電壓,接入電阻后可以降低二極管的電壓,避免二極管被擊穿。
此處電阻與Arduino的針腳8相連,因此針腳8為訊號輸出端,我們在程式中也對該針腳進行了定義。當針腳8輸出高位電平時,LED發光二極管點亮;針腳8輸出低位電平時,LED發光二極管熄滅。
通過範例學習,可以掌握TfxUsbSerial元件的基本屬性設備,並結合Arduino開發板進行通訊埠LED的控制功能。
# 2. 零件連線圖
# 3. 使用零件
序號 | 零件名稱 | 數量 |
---|---|---|
1 | Arduino UNO R3 開發板 | 1 |
2 | USB數據線 | 1 |
3 | 麵包板 | 1 |
4 | 杜邦線 | 若干 |
5 | LED發光二極管 | 1 |
6 | 220歐姆電阻 | 1 |
# 4. Arduino流程圖
# 5. Arduino程式
使用Arduino IDE 編譯並上傳以下Arduino程式。
#define LED_PIN 8 // 定義第8針腳輸出LED亮滅的狀態
bool LLedState = false; // 設定初始值
int LVal = 0;
void setup() {
pinMode(LED_PIN, OUTPUT); //設定第8針腳為輸出模式
Serial.begin(9600); //設定通訊位元率為9600
}
void loop() {
if(Serial.available() > 0) { // 檢查是否可以從通訊埠讀取數據
LVal = Serial.read(); // 從通訊埠讀取數據
if(LVal != 0) { // 讀取的數據不能為空
if(LVal == 45) {
Serial.print("READY"); // 傳輸準備完成的字串
} else {
OnSerialCommand(LVal); // 運行LED開關的子程式
}
LVal = 0; // 重新設定初始值
}
}
}
void OnSerialCommand(int cmd) {
if(cmd == 49) { // 如果接收到的字串為'1'
LLedState = true; // 設定LED開關狀態為True
} else if(cmd == 48) { // 收到的字串如果'0'
LLedState = false; // 設定LED開關狀態為False
}
if(LLedState) { // 檢查LED燈開關狀態
digitalWrite(LED_PIN, HIGH); // 第8針腳輸出高位電平,LED燈亮
Serial.print("LED_ON_OK"); // 傳送LED燈亮的資訊
} else {
digitalWrite(LED_PIN, LOW); // 第8針腳輸出低位電平,LED燈滅
Serial.print("LED_OFF_OK"); // 傳送LED燈滅的資訊
}
}
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
31
32
33
34
35
36
37
38
# 6. 設計明細
開啟PinToo設計器,分別加入下插圖之控制元件。或者點選左上角的[打開模板Lib檔案]
選擇模板檔案來打開對應模板。
①:TfxUsbSerial元件,控制元件名稱為fxUsbSerial1
。
②:TfxSuperButton元件,控制元件名稱為fxSuperButtonPaired
。
③:TfxComboBox元件,控制元件名稱為ComboBoxPaired
。
④:TfxSwitchButton元件,控制元件名稱為fxSwitchButton2
。
⑤:TfxSuperButton元件,控制元件名稱為fxSuperButtonServices
。
⑥:TfxSwitchButton元件,控制元件名稱為fxSwitchButton1
。
fxRunFrame屬性設定
Height
:設定頁面高度=800
。Width
:設定頁面寬度=401
。
①fxUsbSerial1屬性設定
UsbSerialType
:設定USB通訊埠的連線型別,設定為RS232
。
②fxSuperButtonPaired屬性設定
Height
:設定控制元件高度=40
。Width
:設定控制元件寬度=40
。ButtonType
:設定按鈕型別,設定為TfxSvgButton
。ButtonType.SvgData
:設定SVG數據,其數據內容如下。
<path d="M35.4421844482422,20.57493019104 L31.6380081176758,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.1317272186279,14.2644805908203 L25.7234687805176,10.8562164306641 L30.3848686218262,6.19481563568115 C31.0734081268311,5.5062780380249 31.0734081268311,4.37707614898682 30.3848686218262,3.68853807449341 C29.6963348388672,3 28.5671272277832,3.00000023841858 27.8785934448242,3.68853807449341 L23.217191696167,8.34993934631348 L19.4130210876465,4.54576778411865 C18.7279262542725,3.86067247390747 17.6090507507324,3.86067247390747 16.9239559173584,4.54576778411865 C16.2388610839844,5.23086261749268 16.2388610839844,6.34973669052124 16.9239559173584,7.03483200073242 L32.9531211853027,23.0639934539795 C33.6382141113281,23.7490882873535 34.7570877075195,23.7490882873535 35.4421768188477,23.0639934539795 C36.1272735595703,22.3788967132568 36.1272735595703,21.2600250244141 35.4421768188477,20.57493019104 Z M31.5278415679932,23.3394088745117 L16.6244430541992,8.43600654602051 C16.5590305328369,8.37059593200684 16.4523086547852,8.37059593200684 16.3868980407715,8.43600654602051 L14.3281688690186,10.4947357177734 L10.4207172393799,14.4021883010864 L10.4138307571411,14.4090738296509 L10.3931741714478,14.4297304153442 C9.0367546081543,17.0633888244629 9.0367546081543,20.1927928924561 8.90593338012695,22.6715278625488 L17.3198661804199,31.0854587554932 C19.7813873291016,30.8995532989502 23.3514575958252,30.9993915557861 25.5306777954102,29.567232131958 L25.558219909668,29.5396938323975 L25.5616626739502,29.5431346893311 L31.5278415679932,23.5769538879395 C31.5932502746582,23.5115432739258 31.5932502746582,23.4048194885254 31.5278415679932,23.3394088745117 Z M15.6363906860352,31.0475902557373 L8.91626167297363,24.3274612426758 C8.44805526733398,23.8592567443848 7.68722057342529,23.8592567443848 7.2190146446228,24.3274612426758 C6.75080871582031,24.7956657409668 6.75080871582031,25.5565013885498 7.2190146446228,26.0247058868408 L13.9425888061523,32.7482757568359 C14.4107933044434,33.2164840698242 15.1716279983521,33.2130432128906 15.6398324966431,32.7482757568359 C16.1045951843262,32.2800712585449 16.1045951843262,31.5157947540283 15.6363897323608,31.04758644104 Z M5.88669443130493,29.0852565765381 C5.25323963165283,29.718713760376 5.25323963165283,30.7584037780762 5.88669443130493,31.3918590545654 L6.41686820983887,31.9220352172852 L3.01204872131348,35.3268547058105 L4.68519592285156,37 L8.09001541137695,33.5951805114746 L8.62018966674805,34.125358581543 C9.25364398956299,34.7588119506836 10.2933368682861,34.7588119506836 10.9267921447754,34.125358581543 L12.2866535186768,32.765495300293 L7.24655628204346,27.7253952026367 L5.88669395446777,29.0852565765381 Z" p-id="1371" fill="#FF1296DB" stroke="Null"></path>
1Name
:設定控制元件名稱為fxSuperButtonPaired
。
③ComboBoxPaired屬性設定
Height
:設定控制元件高度=30
。Width
:設定控制元件寬度=150
。
④fxSwitchButton2屬性設定
Height
:設定控制元件高度=200
。Width
:設定控制元件寬度=200
。HitTest
:是否響應點選操作,設定為False
。SwitchOffBmp
:設定開關狀態為關時顯示的影象,雙擊該屬性或者點選屬性右側的[...]
打開影象編輯器。點選[Load...]
打開資源管理器,選擇所需的圖片後點擊[打開]
,選擇的影象會顯示在預覽框中。可在該頁面下進行裁切或者縮放畫素以使影象與控制元件的寬度高度相適應,修改完成後點選[OK]
按鈕完成影象修改。
SwitchOnBmp
:設定開關狀態為開時顯示的影象,雙擊該屬性或者點選屬性右側的[...]
打開影象編輯器。點選[Load...]
打開資源管理器,選擇所需的圖片後點擊[打開]
,選擇的影象會顯示在預覽框中。可在該頁面下進行裁切或者縮放畫素以使影象與控制元件的寬度高度相適應,修改完成後點選[OK]
按鈕完成影象修改。
⑤fxSuperButtonServices屬性設定
Height
:設定控制元件高度=40
。Width
:設定控制元件寬度=40
。ButtonType
:設定按鈕型別,設定為TfxSvgButton
。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>
1Name
:設定控制元件名稱為fxSuperButtonServices
。
⑥fxSwitchButton1屬性設定
Height
:設定控制元件高度=200
。Width
:設定控制元件寬度=200
。SwitchOffBmp
:設定開關狀態為關時顯示的影象,雙擊該屬性或者點選屬性右側的[...]
打開影象編輯器。點選[Load...]
打開資源管理器,選擇所需的圖片後點擊[打開]
,選擇的影象會顯示在預覽框中。可在該頁面下進行裁切或者縮放畫素以使影象與控制元件的寬度高度相適應,修改完成後點選[OK]
按鈕完成影象修改。
SwitchOnBmp
:設定開關狀態為開時顯示的影象,雙擊該屬性或者點選屬性右側的[...]
打開影象編輯器。點選[Load...]
打開資源管理器,選擇所需的圖片後點擊[打開]
,選擇的影象會顯示在預覽框中。可在該頁面下進行裁切或者縮放畫素以使影象與控制元件的寬度高度相適應,修改完成後點選[OK]
按鈕完成影象修改。
# 7. 程式設計
點選程式設計界面右下角的按鈕,切換至單元選擇界面,勾選需要使用的單元。該程式需要引用uUsbSerial
單元。
# 7.1. 程式初始設定
該程式無初始設定。
# 7.2. 事件設定
- ⑥fxSwitchButton1-OnSwitch事件
點選切換開關狀態以開啟或者關閉LED燈。
Procedure fxSwitchButton1OnSwitch(Sender: TObject);
//開啟、關閉LED
Begin
if fxSwitchButton1.IsChecked Then
//開啟LED燈
begin
fxUsbSerial1.WriteByte(49,0);
fxSwitchButton2.IsChecked := True;
fxRDToastMessage1.ShowToastMessage('LED燈已開啟',2.0,100);
End
else
//關閉LED燈
Begin
fxUsbSerial1.WriteByte(48,0);
fxSwitchButton2.IsChecked := False;
fxRDToastMessage1.ShowToastMessage('LED燈已關閉',2.0,100);
End;
End;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- ②fxSuperButtonPaired-OnClick事件
點選按鈕載入設備列表。
Procedure fxSuperButtonPairedOnClick(Sender: TObject);
//載入USB設備列表
Begin
ComboBoxPaired.Items := fxUsbSerial1.Devices;
ComboBoxPaired.DropDown;
fxSuperButtonPaired.Enabled := False;
End;
2
3
4
5
6
7
- ③ComboBoxPaired-OnChange事件
點選列表,切換使用的設備。
Procedure ComboBoxPairedOnChange(Sender: TObject);
//切換通訊埠設備
Begin
fxUsbSerial1.UsbDevice := ComboBoxPaired.Items[ComboBoxPaired.ItemIndex];
End;
2
3
4
5
- ⑤fxSuperButtonServices-OnClick事件
點選服務按鈕啟動通訊埠。
Procedure fxSuperButtonServicesOnClick(Sender: TObject);
//打開通訊埠
begin
fxUsbSerial1.Open(True);
End;
2
3
4
5
# 8. 運行結果
使用滑鼠在 PinToo 功能表,點選[儲存至資料庫]
按鈕,將其儲存至資料庫,點選[除錯運行]
確認能夠正常打開。
通過同步中心,將程式上傳至手機PinToo運行;同步時,請確保手機已經運行PinToo,並且已經登陸。
將手機與Arduino的USB埠進行連線,點選設備搜尋按鈕,彈出的下拉選單中選擇對應的設備,點選服務按鈕開啟通訊埠連線,點選下方的開關按鈕,Arduino的LED燈亮,狀態顯示區的燈處於亮燈的狀態。再次點選開關按鈕,Arduino的LED燈滅,狀態顯示區的燈處於滅燈的狀態。