三色聲光報警器運用
# Smart三色聲光報警器運用
# 1. 說明
Smart 通過 RestAPI 讀取 TARS 物聯網模組中連線的設備的資訊並顯示在頁面中,使用 RestAPI 實現對三色聲光報警器的控制等相關功能。
聲光報警器,是一種用在危險場所,通過聲音和各種光來向人們發出警示訊號的一種報警裝置,可用於鋼鐵冶金、電信鐵塔、起重機械、工程機械、港口碼頭、交通運輸、風力發電、遠洋船舶等行業; 是工業報警系統中的一個配件產品。聲光報警器根據可發光的顏色型別,可分為單色聲光報警器與多色聲光報警器。而多色聲光報警器中以三色聲光報警器居多。三色聲光報警器可發出紅、綠、黃三種顏色,可用於機臺設備狀態的指示。同時也可發出閃光和聲音,可用作警報提示。

在工廠生產過程中,通常會關注設備的大致運行情況,比如設備是否處於運行狀態,設備是否出現故障,設備是否在空轉。使用三色聲光報警器可解決上述需求,通過LED燈指示與聲音報警,現場工作人員可以及時瞭解設備所處的運行狀態,對設備故障及時處理。
目前範例選用帶有數據通信功能的三色聲光報警器作為被採集的對象。三色聲光報警器使用 RS485 通訊埠通訊,使用 ModbusRTU 通訊協議實現狀態讀取與控制。三色聲光報警器採用直流供電的方式,在 12V-36V 之間均可以進行工作。
示例中的聲光報警器 Modbus 暫存器採用保持暫存器,通過讀取寫入特定地址實現控制功能。
| 功能 | 地址位 | 取值說明 |
|---|---|---|
| 設備是否線上 | 63(0x3F) | 低位的取值為 08 時表示設備線上,即取得的數字進行取餘數操作 x mod 256 = 8 表達式成立時設備線上 |
| 播放狀態 | 66(0x42) | 1025 正在播放、1024 停止播放 1026 暫停播放 |
| 音量 | 67(0x43) | 低位地址的數值表示音量等級 01-1E 即 0-30 |
| 音訊檔案數量 | 73(0x49) | 返回的數據表示存放的檔案數量 |
| 目前播放曲目 | 77(0x4D) | 曲目的序號 |
| 聲光狀態 | 112(0x70) | 高位 - 目前播放的曲目 低位 - 聲光狀態 |
數值與聲光狀態對照表
| 數值 | 對應燈狀態 | 數值 | 對應燈狀態 |
|---|---|---|---|
| 17(0x11) | 紅燈常亮 | 35(0x23) | 綠燈慢閃 |
| 18(0x12) | 黃燈常亮 | 49(0x31) | 紅燈爆閃 |
| 19(0x13) | 綠燈常亮 | 50(0x32) | 黃燈爆閃 |
| 33(0x21) | 紅燈慢閃 | 51(0x33) | 綠燈爆閃 |
| 34(0x22) | 黃燈慢閃 | 96(0x10/0x20/0x30/0x60/0x06…)等 | 燈熄滅 |
在上述採集到的數據中,聲光狀態是重要數據,通過對聲光狀態的數據解析可以瞭解聲光報警器的亮燈狀態。
方案採用 TARS 進行數據採集,在物聯網界面中設定採集的設備的通訊埠連線資訊、讀取的位置等資訊。將讀取到的數據通過解析後轉存至指定的資料庫中,等待數據分析。在這個過程中,TARS會將採集到的數據進行解析,獲取聲光狀態,並進行轉存操作。
儲存數據的表結構如下,請在 demo 資料庫中建立執行:
---設備資訊表,用於更新設備目前的狀態
CREATE TABLE [dbo].[oee_device](
[id] [varchar](36) NOT NULL,
[moduleno] [varchar](50) NULL,
[modulename] [varchar](50) NULL,
[moduledesc] [varchar](50) NULL,
[sid] [int] NULL,
[commid] [varchar](36) NULL,
[enabled] [bit] NULL,
[online] [bit] NULL,
[light] [varchar](10) NULL,
[flash] [varchar](10) NULL,
[sound] [int] NULL,
[soundstate] [varchar](10) NULL,
[volume] [int] NULL,
[updatetime] [datetime] NULL,
CONSTRAINT [PK_oee_device] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
---設備數據採集表,用於更新設備資訊
CREATE TABLE [dbo].[oee_devicedata](
[id] [varchar](36) NOT NULL,
[moduleid] [varchar](36) NULL,
[moduleno] [varchar](50) NULL,
[modulename] [varchar](50) NULL,
[site] [varchar](100) NULL,
[time] [datetime] NULL,
[value] [varchar](200) NULL,
[online] [bit] NULL,
[light] [varchar](10) NULL,
[flash] [varchar](10) NULL,
[sound] [int] NULL,
[soundstate] [varchar](10) NULL,
[volume] [int] NULL,
[lastvalue] [varchar](200) NULL,
[lastlight] [varchar](10) NULL,
[lastflash] [varchar](10) NULL,
[lastsound] [int] NULL,
[lastsoundstate] [varchar](10) NULL,
[lastvolume] [int] NULL,
CONSTRAINT [PK_oee_devicedata] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
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
39
40
41
42
43
44
45
46
47
48
在 TARS 物聯網設定項中,在 設備監控設定 專案中建立兩個 Modbus 設備監控項。可參考 TARS-物聯網 進行操作。
| 設備編號 | 設備名稱 |
|---|---|
| 015 | light001 |
| 016 | light002 |
每個設備分別設定如下站點:
| 站點名稱 | 數據型別 | 功能程式碼 | 數據地址 | 數據長度 | 監控週期 | 儲存頻率 | 實時傳值 | 站點型別 |
|---|---|---|---|---|---|---|---|---|
| rsoundstate | Integer | 03ReadHoldingRegister(4x) | 66 | 1 | 30 | 30 | true | read |
| rvolume | Integer | 03ReadHoldingRegister(4x) | 67 | 1 | 30 | 30 | true | read |
| rdevicestate | Integer | 03ReadHoldingRegister(4x) | 63 | 1 | 30 | 30 | true | read |
| rtotalfiles | Integer | 03ReadHoldingRegister(4x) | 73 | 1 | 30 | 30 | true | read |
| rsoundnum | Integer | 03ReadHoldingRegister(4x) | 77 | 1 | 30 | 30 | true | read |
| rledstate | Integer | 03ReadHoldingRegister(4x) | 112 | 1 | 30 | 30 | true | read |
| wsound | Integer | 06PresetSingleRegister(4x) | 15 | 1 | 5 | 30 | true | write |
| wnextplay | Integer | 06PresetSingleRegister(4x) | 1 | 1 | 5 | 30 | true | write |
| wledstate | Integer | 06PresetSingleRegister(4x) | 194 | 1 | 5 | 30 | true | write |
| wpreviousplay | Integer | 06PresetSingleRegister(4x) | 2 | 1 | 5 | 30 | true | write |
| wplayindex | Integer | 06PresetSingleRegister(4x) | 3 | 1 | 5 | 30 | true | write |
| wvolumeplus | Integer | 06PresetSingleRegister(4x) | 4 | 1 | 5 | 30 | true | write |
| wvolumeminus | Integer | 06PresetSingleRegister(4x) | 5 | 1 | 5 | 30 | true | write |
| wvolume | Integer | 06PresetSingleRegister(4x) | 6 | 1 | 5 | 30 | true | write |
| wplayrepeat | Integer | 06PresetSingleRegister(4x) | 8 | 1 | 5 | 30 | true | write |
| wplaydevice | Integer | 06PresetSingleRegister(4x) | 9 | 1 | 5 | 30 | true | write |
| wbaudrate | Integer | 06PresetSingleRegister(4x) | 11 | 1 | 5 | 30 | true | write |
| wchipreset | Integer | 06PresetSingleRegister(4x) | 12 | 1 | 5 | 30 | true | write |
| wplay | Integer | 06PresetSingleRegister(4x) | 13 | 1 | 5 | 30 | true | write |
| wpause | Integer | 06PresetSingleRegister(4x) | 14 | 1 | 5 | 30 | true | write |
| wfolderfileplayrepeat | Integer | 06PresetSingleRegister(4x) | 16 | 1 | 5 | 30 | true | write |
| wplayrepeatall | Integer | 06PresetSingleRegister(4x) | 17 | 1 | 5 | 30 | true | write |
| wstopplay | Integer | 06PresetSingleRegister(4x) | 22 | 1 | 5 | 30 | true | write |
| wfolderplayrepeat | Integer | 06PresetSingleRegister(4x) | 23 | 1 | 5 | 30 | true | write |
| wplayrandom | Integer | 06PresetSingleRegister(4x) | 24 | 1 | 5 | 30 | true | write |
| wnowplayrepeat | Integer | 06PresetSingleRegister(4x) | 25 | 1 | 5 | 30 | true | write |
| wsid | Integer | 06PresetSingleRegister(4x) | 192 | 1 | 5 | 30 | true | write |
其中 rvolume 設定的數據指令碼如下:
begin
This.DataValue := This.DataValue mod 256;
end.
2
3
rvolume 設定的數據轉存指令碼如下:
declare @volume int,
@lastvolume int
SELECT @volume = :DataValue,@lastvolume = :LastValue
INSERT INTO oee_devicedata
(id, moduleid, moduleno, modulename, site,
time, value, online, light, flash,
sound, volume, lastvalue, lastlight,
lastflash, lastsound, lastvolume)
VALUES (NEWID(), :ModuleID, :ModuleNo, :ModuleName, :Site,
GETDATE(), :DataValue, NULL, NULL, NULL,
NULL, @volume, :LastValue, NULL, NULL,
NULL, @lastvolume)
IF exists(SELECT 1 FROM oee_device WHERE ModuleNo = :ModuleNo)
UPDATE oee_device set volume = @volume, updatetime = GETDATE() WHERE ModuleNo = :ModuleNo
ELSE
INSERT INTO oee_device(id,moduleno,modulename,volume,updatetime)
VALUES(NEWID(),:ModuleNo,:ModuleName,@volume,GETDATE())
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
rdevicestate 設定的數據指令碼如下:
begin
This.DataValue := This.DataValue mod 256;
end.
2
3
rdevicestate 設定的數據指令碼如下:
declare @online bit
SELECT @online = CASE :DataValue WHEN 8 THEN 1 ELSE 0 END
INSERT INTO oee_devicedata
(id, moduleid, moduleno, modulename, site,
time, value, online, light, flash,
sound, volume, lastvalue, lastlight,
lastflash, lastsound, lastvolume)
VALUES (NEWID(), :ModuleID, :ModuleNo, :ModuleName, :Site,
GETDATE(), :DataValue, @online, NULL, NULL,
NULL, NULL, :LastValue, NULL, NULL,
NULL, NULL)
IF exists(SELECT 1 FROM oee_device WHERE ModuleNo = :ModuleNo)
UPDATE oee_device set [online] = @online, updatetime = GETDATE() WHERE ModuleNo = :ModuleNo
ELSE
INSERT INTO oee_device(id,moduleno,modulename,[online],updatetime)
VALUES(NEWID(),:ModuleNo,:ModuleName,@online,GETDATE())
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
rsoundstate 設定的數據指令碼如下:
declare @soundstate varchar(10),
@lastsoundstate varchar(10)
SELECT @soundstate = CASE :DataValue WHEN 1025 THEN 'play'
WHEN 1024 THEN 'stop' WHEN 1026 THEN 'pause' END,
@lastsoundstate = CASE :DataValue WHEN 1025 THEN 'play'
WHEN 1024 THEN 'stop' WHEN 1026 THEN 'pause' END
INSERT INTO oee_devicedata
(id, moduleid, moduleno, modulename, site,
time, value, online, light, flash,
sound, soundstate, volume, lastvalue, lastlight,
lastflash, lastsound, lastsoundstate, lastvolume)
VALUES (NEWID(), :ModuleID, :ModuleNo, :ModuleName, :Site,
GETDATE(), :DataValue, NULL, NULL, NULL,
NULL, @soundstate, NULL, :LastValue, NULL, NULL,
NULL, @lastsoundstate, NULL)
IF exists(SELECT 1 FROM oee_device WHERE ModuleNo = :ModuleNo)
UPDATE oee_device set soundstate = @soundstate, updatetime = GETDATE() WHERE ModuleNo = :ModuleNo
ELSE
INSERT INTO oee_device(id,moduleno,modulename,soundstate,updatetime)
VALUES(NEWID(),:ModuleNo,:ModuleName,@soundstate,GETDATE())
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
rledstate 設定的數據指令碼如下:
declare @light varchar(10),
@flash varchar(10),
@sound int,
@lastlight varchar(10),
@lastflash varchar(10),
@lastsound int
SELECT @light = CASE (CAST(:DataValue AS INT) % 16) WHEN 0 THEN 'off'
WHEN 1 THEN 'red' WHEN 2 THEN 'yellow' WHEN 3 THEN 'green' END,
@flash = CASE (CAST(:DataValue/16 AS INT) % 16) WHEN 6 THEN 'off' WHEN 0 THEN 'off'
WHEN 1 THEN 'no' WHEN 2 THEN 'slow' WHEN 3 THEN 'fast' END,
@sound = CAST(:DataValue/256 AS INT)
SELECT @lastlight = CASE (CAST(:LastValue AS INT) % 16) WHEN 0 THEN 'off'
WHEN 1 THEN 'red' WHEN 2 THEN 'yellow' WHEN 3 THEN 'green' END,
@lastflash = CASE (CAST(:LastValue/16 AS INT) % 16) WHEN 6 THEN 'off' WHEN 0 THEN 'off'
WHEN 1 THEN 'no' WHEN 2 THEN 'slow' WHEN 3 THEN 'fast' END,
@lastsound = CAST(:LastValue/256 AS INT)
INSERT INTO oee_devicedata
(id, moduleid, moduleno, modulename, site,
time, value, online, light, flash,
sound, soundstate, volume, lastvalue, lastlight,
lastflash, lastsound, lastsoundstate, lastvolume)
VALUES (NEWID(), :ModuleID, :ModuleNo, :ModuleName, :Site,
GETDATE(), :DataValue, NULL, @light, @flash,
@sound, NULL, NULL, :LastValue, @lastlight, @lastflash,
@lastsound, NULL, NULL)
IF exists(SELECT 1 FROM oee_device WHERE ModuleNo = :ModuleNo)
UPDATE oee_device set light = @light, flash = @flash, sound = @sound, updatetime = GETDATE() WHERE ModuleNo = :ModuleNo
ELSE
INSERT INTO oee_device(id,moduleno,modulename,light,flash,sound,updatetime)
VALUES(NEWID(),:ModuleNo,:ModuleName,@light,@flash,@sound,GETDATE())
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
設定設備的通訊埠通訊連線資訊,打開 TARS 監控,可查詢到輸出的數據。
# 2. 設計明細
開啟Smart智慧控制平臺,分別加入下插圖之控制元件。或者通過點選功能表欄 [檔案] - [打開專案] 選擇專案打開該範例。

第一屏的功能控制元件說明如下:
- TscPageControl 元件,控制元件名稱為
scPageControl1。其中建立了3個標籤。 - TImage 元件,控制元件名稱為
Image1。 - TAbClock 元件,控制元件名稱為
AbClock1。 - TAbLED 元件,控制元件名稱為
LEDRed1。 - TAbLED 元件,控制元件名稱為
LEDYellow1。 - TAbLED 元件,控制元件名稱為
LEDGreen1。 - TiPanel 元件,控制元件名稱為
iPanel1。 - TscGPButton 元件,控制元件名稱為
scGPButton117。 - TscGPButton 元件,控制元件名稱為
scGPButton118。 - TscGPButton 元件,控制元件名稱為
scGPButton119。 - TscGPButton 元件,控制元件名稱為
scGPButton133。 - TscGPButton 元件,控制元件名稱為
scGPButton134。 - TscGPButton 元件,控制元件名稱為
scGPButton135。 - TscGPButton 元件,控制元件名稱為
scGPButton149。 - TscGPButton 元件,控制元件名稱為
scGPButton150。 - TscGPButton 元件,控制元件名稱為
scGPButton151。 - TscGPButton 元件,控制元件名稱為
scGPButton1Start。 - TscGPButton 元件,控制元件名稱為
scGPButton196。 - TscGPButton 元件,控制元件名稱為
scGPButton1Stop。 - TiPanel 元件,控制元件名稱為
iPanel2。 - TscGPButton 元件,控制元件名稱為
scGPButton217。 - TscGPButton 元件,控制元件名稱為
scGPButton218。 - TscGPButton 元件,控制元件名稱為
scGPButton219。 - TscGPButton 元件,控制元件名稱為
scGPButton233。 - TscGPButton 元件,控制元件名稱為
scGPButton234。 - TscGPButton 元件,控制元件名稱為
scGPButton235。 - TscGPButton 元件,控制元件名稱為
scGPButton249。 - TscGPButton 元件,控制元件名稱為
scGPButton250。 - TscGPButton 元件,控制元件名稱為
scGPButton251。 - TscGPButton 元件,控制元件名稱為
scGPButton2Start。 - TscGPButton 元件,控制元件名稱為
scGPButton296。 - TscGPButton 元件,控制元件名稱為
scGPButton2Stop。 - TAbLED 元件,控制元件名稱為
LEDRed2。 - TAbLED 元件,控制元件名稱為
LEDYellow2。 - TAbLED 元件,控制元件名稱為
LEDGreen2。
1:scPageControl1 屬性設定
Align:設定對齊方式,設定為alClient。- 頁面中需建立3個標籤頁,分別為
scTabSheet1、scTabSheet2、scTabSheet3。
3:AbClock1 屬性設定
Font:設定顯示的字型,設定字型大小Font.Size為 20。
4:LEDRed1 屬性設定
Caption:設定顯示的字幕,設定為空。LED:設定LED燈的相關屬性。其中ColorOff設定為$009FA2E8,ColorOn設定為clRed,Height設定為12,Shape設定為sRectangle,Width設定為19。
5:LEDYellow1 屬性設定
Caption:設定顯示的字幕,設定為空。LED:設定LED燈的相關屬性。其中ColorOff設定為clInfoBk,ColorOn設定為clYellow,Height設定為12,Shape設定為sRectangle,Width設定為19。
6:LEDGreen1 屬性設定
Caption:設定顯示的字幕,設定為空。LED:設定LED燈的相關屬性。其中ColorOff設定為clMoneyGreen,ColorOn設定為clLime,Height設定為12,Shape設定為sRectangle,Width設定為19。
7:iPanel1 屬性設定
TitleText:設定標籤的標題,設定為頭端訊號指示。
8:scGPButton117 屬性設定
Caption:設定按鈕顯示的文字,設定為紅燈常亮。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpSegmentedLeftRounded。
9:scGPButton118 屬性設定
Caption:設定按鈕顯示的文字,設定為黃燈常亮。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpRect。
10:scGPButton119 屬性設定
Caption:設定按鈕顯示的文字,設定為綠燈常亮。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpSegmentedRightRounded。
11:scGPButton133 屬性設定
Caption:設定按鈕顯示的文字,設定為紅燈慢閃。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpSegmentedLeftRounded。
12:scGPButton134 屬性設定
Caption:設定按鈕顯示的文字,設定為黃燈慢閃。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpRect。
13:scGPButton135 屬性設定
Caption:設定按鈕顯示的文字,設定為綠燈慢閃。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpSegmentedRightRounded。
14:scGPButton149 屬性設定
Caption:設定按鈕顯示的文字,設定為紅燈快閃。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpSegmentedLeftRounded。
15:scGPButton150 屬性設定
Caption:設定按鈕顯示的文字,設定為黃燈快閃。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpRect。
16:scGPButton151 屬性設定
Caption:設定按鈕顯示的文字,設定為綠燈快閃。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpSegmentedRightRounded。
17:scGPButton1Start 屬性設定
Caption:設定按鈕顯示的文字,設定為播放聲音。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpSegmentedLeftRounded。
18:scGPButton196 屬性設定
Caption:設定按鈕顯示的文字,設定為關閉。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpRect。
19:scGPButton1Stop 屬性設定
Caption:設定按鈕顯示的文字,設定為停止播放。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpSegmentedRightRounded。
20:iPanel2 屬性設定
TitleText:設定標籤的標題,設定為尾端訊號指示。
21:scGPButton217 屬性設定
Caption:設定按鈕顯示的文字,設定為紅燈常亮。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpSegmentedLeftRounded。
22:scGPButton218 屬性設定
Caption:設定按鈕顯示的文字,設定為黃燈常亮。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpRect。
23:scGPButton219 屬性設定
Caption:設定按鈕顯示的文字,設定為綠燈常亮。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpSegmentedRightRounded。
24:scGPButton233 屬性設定
Caption:設定按鈕顯示的文字,設定為紅燈慢閃。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpSegmentedLeftRounded。
25:scGPButton234 屬性設定
Caption:設定按鈕顯示的文字,設定為黃燈慢閃。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpRect。
26:scGPButton235 屬性設定
Caption:設定按鈕顯示的文字,設定為綠燈慢閃。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpSegmentedRightRounded。
27:scGPButton249 屬性設定
Caption:設定按鈕顯示的文字,設定為紅燈快閃。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpSegmentedLeftRounded。
28:scGPButton250 屬性設定
Caption:設定按鈕顯示的文字,設定為黃燈快閃。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpRect。
29:scGPButton251 屬性設定
Caption:設定按鈕顯示的文字,設定為綠燈快閃。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpSegmentedRightRounded。
30:scGPButton2Start 屬性設定
Caption:設定按鈕顯示的文字,設定為播放聲音。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpSegmentedLeftRounded。
31:scGPButton296 屬性設定
Caption:設定按鈕顯示的文字,設定為關閉。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpRect。
32:scGPButton2Stop 屬性設定
Caption:設定按鈕顯示的文字,設定為停止播放。Options:設定按鈕的屬性,其中設定ShapeStyle為scgpSegmentedRightRounded。
33:LEDRed3 屬性設定
Caption:設定顯示的字幕,設定為空。LED:設定LED燈的相關屬性。其中ColorOff設定為$009FA2E8,ColorOn設定為clRed,Height設定為12,Shape設定為sRectangle,Width設定為19。
34:LEDYellow2 屬性設定
Caption:設定顯示的字幕,設定為空。LED:設定LED燈的相關屬性。其中ColorOff設定為clInfoBk,ColorOn設定為clYellow,Height設定為12,Shape設定為sRectangle,Width設定為19。
35:LEDGreen2 屬性設定
Caption:設定顯示的字幕,設定為空。LED:設定LED燈的相關屬性。其中ColorOff設定為clMoneyGreen,ColorOn設定為clLime,Height設定為12,Shape設定為sRectangle,Width設定為19。

第二屏的功能說明如下:
- TRFDataSet 元件,控制元件名稱為
RFDataSet1。 - TDataSource 元件,控制元件名稱為
DataSource1。 - TscDateEdit 元件,控制元件名稱為
scDateEdit1。 - TscTimeEdit 元件,控制元件名稱為
scTimeEdit1。 - TscDateEdit 元件,控制元件名稱為
scDateEdit1。 - TscTimeEdit 元件,控制元件名稱為
scTimeEdit1。 - TscEdit 元件,控制元件名稱為
scEdit1。 - TscGPButton 元件,控制元件名稱為
btnQuery。 - TDBGrid 元件,控制元件名稱為
DBGrid1。
2:DataSource1 屬性設定
DataSet:設定繫結的數據集,設定為RFDataSet1。
3:scDateEdit1 屬性設定
BorderKind:設定控制元件的邊界型別,設定為scebBottomLine。
4:scTimeEdit1 屬性設定
BorderKind:設定控制元件的邊界型別,設定為scebBottomLine。
5:scDateEdit2 屬性設定
BorderKind:設定控制元件的邊界型別,設定為scebBottomLine。
6:scTimeEdit2 屬性設定
BorderKind:設定控制元件的邊界型別,設定為scebBottomLine。
7:scEdit1 屬性設定
BorderKind:設定控制元件的邊界型別,設定為scebBottomLine。
8:btnQuery 屬性設定
Caption:設定按鈕顯示的字幕,設定為查詢。Options:設定按鈕的選項,其中設定ShapeStyle為scgpRoundedRect。
9:DBGrid1 屬性設定
DataSource:設定繫結的數據源,設定為DataSource1。Columns:設定表中顯示的列。設定項如下:
| 欄位名稱 | 顯示名稱 | 寬度 |
|---|---|---|
| moduleno | 設備編號 | 70 |
| modulename | 設備名稱 | 70 |
| site | 站點 | 90 |
| time | 採集時間 | 160 |
| value | 取值 | 70 |
| online | 線上 | 64 |
| light | 燈光型別 | 70 |
| flash | 閃光型別 | 70 |
| sound | 聲音序號 | 70 |
| soundstate | 聲音狀態 | 70 |
| volume | 音量 | 50 |
| lastvalue | 上次取值 | 70 |
| lastlight | 上次燈光型別 | 64 |
| lastflash | 上次閃光型別 | 64 |
| lastsound | 上次聲音序號 | 64 |
| lastsoundstate | 上次聲音狀態 | 64 |
| lastvolume | 上次音量 | 70 |

第三屏的功能說明如下:
- TRFDataSet 元件,控制元件名稱為
RFDataSet2。 - TDataSource 元件,控制元件名稱為
DataSource2。 - TDBGrid 元件,控制元件名稱為
DBGrid2。
2:DataSource2 屬性設定
DataSet:設定繫結的數據集,設定為RFDataSet2。
9:DBGrid2 屬性設定
DataSource:設定繫結的數據源,設定為DataSource2。Columns:設定表中顯示的列。設定項如下:
| 欄位名稱 | 顯示名稱 | 寬度 |
|---|---|---|
| moduleno | 設備編號 | 70 |
| modulename | 設備名稱 | 70 |
| online | 線上 | 64 |
| light | 燈光型別 | 70 |
| flash | 閃光型別 | 70 |
| sound | 聲音序號 | 70 |
| soundstate | 聲音狀態 | 70 |
| volume | 音量 | 50 |
| updatetime | 更新時間 | 200 |
# 3. 程式設計
# 3.1. 程式初始設定
建立自定程式。用於快捷設定燈狀態。
procedure TMyHandler.SetLight(ANum: String; ALED:TAbLED);
begin
ALED.Visible := True;
if StrToInt(ANum) div 16 = 1 then
begin
ALED.Checked := True;
ALED.Flashing := False;
ALED.Frequency := ff1Hz;
end;
if StrToInt(ANum) div 16 = 2 then
begin
ALED.Checked := True;
ALED.Flashing := True;
ALED.Frequency := ff1Hz;
end;
if StrToInt(ANum) div 16 = 3 then
begin
ALED.Checked := True;
ALED.Flashing := True;
ALED.Frequency := ff8Hz;
end;
if (StrToInt(ANum) div 16 = 6) or (StrToInt(ANum) div 16 = 0) then
begin
ALED.Checked := False;
ALED.Flashing := False;
ALED.Frequency := ff1Hz;
end;
end;
procedure TMyHandler.Process1(s: String);
begin
if StrToInt(s) mod 16 = 1 then
begin
SetLight(s,FThis.LEDRed1);
FThis.LEDYellow1.Visible := False;
FThis.LEDYellow1.Checked := False;
FThis.LEDGreen1.Visible := False;
FThis.LEDGreen1.Checked := False;
end
else if StrToInt(s) mod 16 = 2 then
begin
FThis.LEDRed1.Visible := False;
FThis.LEDRed1.Checked := False;
SetLight(s,FThis.LEDYellow1);
FThis.LEDGreen1.Visible := False;
FThis.LEDGreen1.Checked := False;
end
else if StrToInt(s) mod 16 = 3 then
begin
FThis.LEDRed1.Visible := False;
FThis.LEDRed1.Checked := False;
FThis.LEDYellow1.Checked := False;
FThis.LEDYellow1.Checked := False;
SetLight(s,FThis.LEDGreen1);
end
else
begin
FThis.LEDRed1.Visible := False;
FThis.LEDRed1.Checked := False;
FThis.LEDYellow1.Checked := False;
FThis.LEDYellow1.Checked := False;
FThis.LEDGreen1.Visible := False;
FThis.LEDGreen1.Checked := False;
end;
end;
procedure TMyHandler.Process2(s: String);
begin
if StrToInt(s) mod 16 = 1 then
begin
SetLight(s,FThis.LEDRed2);
FThis.LEDYellow2.Visible := False;
FThis.LEDYellow2.Checked := False;
FThis.LEDGreen2.Visible := False;
FThis.LEDGreen2.Checked := False;
end
else if StrToInt(s) mod 16 = 2 then
begin
FThis.LEDRed2.Visible := False;
FThis.LEDRed2.Checked := False;
SetLight(s,FThis.LEDYellow2);
FThis.LEDGreen2.Visible := False;
FThis.LEDGreen2.Checked := False;
end
else if StrToInt(s) mod 16 = 3 then
begin
FThis.LEDRed2.Visible := False;
FThis.LEDRed2.Checked := False;
FThis.LEDYellow2.Checked := False;
FThis.LEDYellow2.Checked := False;
SetLight(s,FThis.LEDGreen2);
end
else
begin
FThis.LEDRed2.Visible := False;
FThis.LEDRed2.Checked := False;
FThis.LEDYellow2.Checked := False;
FThis.LEDYellow2.Checked := False;
FThis.LEDGreen2.Visible := False;
FThis.LEDGreen2.Checked := False;
end;
end;
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 3.2. 事件設定
- scGPButton OnClick事件
設定按鈕的點選事件。適用於燈狀態變化時的事件。呼叫 TARS 控制燈狀態變化,同時控制界面中顯示的值狀態變化。
procedure TMyHandler.scGPButtonClick;
var
id,id1,id2: String;
begin
if FThis.scMode.State = scswOn then exit;
id := RightStr(TscGPButton(Sender).Name,3);
id1 := LeftStr(id,2);
id2 := RightStr(id,2);
if id1 = '1' then
begin
paxfunc.NetHttpGet('http://127.0.0.1:8809/rest/iot/setmodbusvalue?moduleno=015-wledstate&value=' + id2,'');
Process1(id2);
end;
if id1 = '2' then
begin
paxfunc.NetHttpGet('http://127.0.0.1:8809/rest/iot/setmodbusvalue?moduleno=016-wledstate&value=' + id2,'');
Process2(id2);
end;
end;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- SimpleThreadTimer1-OnTimer 事件
設定要重複使用的事件。
procedure TMyHandler.SimpleThreadTimer1Timer;
var
s: String;
vJSON: TJSONValue;
begin
if FThis.scMode.State = scswOn then exit;
btnQueryClick(Sender);
FThis.RFDataSet2.Close;
FThis.RFDataSet2.Open;
s := paxfunc.NetHttpGet('http://127.0.0.1:8809/rest/iot/getiotvalue?moduleno=015-rledstate','');
vJSON := paxfunc.ParseJSONValue(s);
s := paxfunc.GetJSONString(vJSON,'message');
Process1(s);
s := paxfunc.NetHttpGet('http://127.0.0.1:8809/rest/iot/getiotvalue?moduleno=016-rledstate','');
vJSON := paxfunc.ParseJSONValue(s);
s := paxfunc.GetJSONString(vJSON,'message');
Process2(s);
end;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- scGPButton1Start - OnClick事件 scGPButton2Start - OnClick事件
點選播放聲音按鈕,開始播放聲音。
//播放聲音
procedure TMyHandler.scGPButton1StartClick;
begin
if FThis.scMode.State = scswOn then exit;
paxfunc.NetHttpGet('http://127.0.0.1:8809/rest/iot/setmodbusvalue?moduleno=015-wplayrepeat&value=1','');
end;
procedure TMyHandler.scGPButton2StartClick;
begin
if FThis.scMode.State = scswOn then exit;
paxfunc.NetHttpGet('http://127.0.0.1:8809/rest/iot/setmodbusvalue?moduleno=016-wplayrepeat&value=1','');
end;
2
3
4
5
6
7
8
9
10
11
12
- scGPButton1Stop - OnClick事件 scGPButton2Stop - OnClick事件
點選停止播放按鈕,停止播放聲音。
//停止播放
procedure TMyHandler.scGPButton1StopClick;
begin
if FThis.scMode.State = scswOn then exit;
paxfunc.NetHttpGet('http://127.0.0.1:8809/rest/iot/setmodbusvalue?moduleno=015-wstopplay&value=1','');
end;
procedure TMyHandler.scGPButton2StopClick;
begin
if FThis.scMode.State = scswOn then exit;
paxfunc.NetHttpGet('http://127.0.0.1:8809/rest/iot/setmodbusvalue?moduleno=016-wstopplay&value=1','');
end;
2
3
4
5
6
7
8
9
10
11
12
# 4. 運行結果
使用滑鼠點選工具欄運行(Run),測試運行結果。
通過工具欄儲存,將程式儲存為 sdb 專案檔案。
運行后,啟用 TARS 的物聯網監控功能,在 Smart 控制界面可檢視目前設備燈的運行情況,下方可使用按鈕對聲光報警器進行控制。
