-
- Tổng tiền thanh toán:

Part 12. Phần mềm SCADA Cimon Ultimate Access - Script
Part 12. Phần mềm SCADA Cimon Ultimate Access - Công thức Script
Ngoài việc sử dụng tính năng Tự động hóa OLE trong CIMON-SCADA để thực thi chương trình, người dùng có thể tự tay viết chương trình do người dùng định nghĩa bằng hàm script.
Ngôn ngữ được sử dụng trong hàm script tương tự như Basic Script. Người dùng cũng có thể sử dụng các hàm nội bộ do CIMON-SCADA cung cấp.
1. Features
- Bằng cách sử dụng ngôn ngữ tương tự như Basic Script, người dùng có thể kiểm soát dữ liệu nội bộ. Ngoài ra, có thể kết nối với các ứng dụng bên ngoài.
- Có nhiều chức năng nội bộ khác nhau.
- Người dùng có thể viết một tập lệnh để thực hiện hành động tự động trong dự án.
- Với các chức năng hỗ trợ SQL, người dùng có thể truy vấn hoặc lấy dữ liệu từ cơ sở dữ liệu chung
2. Compositions of Window
To bring up the Script editor, double-click the [Script] in the project workspace or select
[Tools] – [Script].
2.1 Menu
(1) Add Script
Select [Edit] – [Add Script] or press icon to create a new script. In the script tree area, a new script (E.g. Script1) will be created. On the right side of the script editor, you can write the desired program.
Khi bạn thay đổi tên tập lệnh và biên dịch nó, tập lệnh sẽ được thêm vào dự án nếu không có lỗi. Ví dụ, khi bạn viết lại Sub Script1() thành Sub ShowWin() và biên dịch tập lệnh, tập lệnh sẽ được thêm vào là ‘ShowWin()’ nếu không có lỗi.
(2) Compile Script
Chọn [Edit] – [Compile Script] hoặc nhấn biểu tượng để kiểm tra tập lệnh đã chọn và biên dịch nó. Sau đó, tên tập lệnh và biến toàn cục sẽ được thêm vào cây tập lệnh.
Khi xảy ra lỗi, một thông báo sẽ bật lên như hình dưới đây. Nhấn [OK] để đóng thông báo và sửa lỗi
Nếu có một tập lệnh có biểu tượng màu xám, điều đó có nghĩa là tập lệnh chưa được biên dịch. Tập lệnh chưa được biên dịch sẽ không được thực thi trong CimonX.
(3) Register Script
Chọn [Edit] – [Register Script] hoặc nhấn biểu tượng để đăng ký tập lệnh. Tất cả các tập lệnh sẽ được biên dịch và thêm vào cây tập lệnh. Nếu có lỗi với tập lệnh, tập lệnh tương ứng sẽ được chọn và thông báo sẽ bật lên
(4) Sort
Chọn [Edit] – [Sort] hoặc nhấn nút để sắp xếp các tập lệnh theo thứ tự bảng chữ cái
(5) Search Tag
Select icon to bring up the [Select Tag] window. In the [Select Tag] window, select a tag and press [OK] to add the tag to the script
(6) Search Function
Press icon to bring up the [Search Function] window. Select a desired function and press [OK] to add it to the script. You can search the functions by alphabet or type. In the List field, you may check the parameters to assign.
(7) Search Page
Chọn nút để mở cửa sổ [Trang tìm kiếm]. Chọn trang mong muốn và nhấn [OK] để thêm vào tập lệnh
3 Writing a Script
Trong phần này, bạn có thể tìm thấy phương pháp viết một tập lệnh bằng cách sử dụng các hàm và chương trình con1. Khi bạn viết một tập lệnh, hãy lưu ý những biện pháp phòng ngừa sau
- Khi bạn sử dụng một hàm trong tập lệnh, phải có dấu ngoặc vuông vì nó có giá trị trả về. Dấu ngoặc vuông phải được xóa trong các chương trình con.
- Khi bạn sử dụng các hàm và chương trình con trong biểu thức lệnh, chúng phải được sử dụng với dấu ngoặc vuông.
- Trong tập lệnh, các hàm và chương trình con phải được phân biệt.
- Vui lòng tham khảo phần phụ lục của hướng dẫn 'Tập lệnh' và 'Biểu thức lệnh và thao tác'.
Chương trình con thực hiện một hành động nhất định mà không trả về giá trị nào, không giống như hàm có giá trị trả về.
(1) Writing a Script
a) In the database editor, create an analog tag named ‘VALUE’
b) Select [Tools] – [Script] or icon to bring up the script editor. Press icon to create a new script.
c) Write a script as shown below. This script changes the value of the ‘VALUE’ tag
Mô tả
1. Chỉ định tên tập lệnh là ValueChange.
2. Tăng giá trị của thẻ ‘VALUE’ thêm một, tối đa là 5. Khi giá trị = 5, tập lệnh sẽ thay đổi giá trị của thẻ ‘VALUE’ thành 1.
3. Khoảng thời gian thay đổi giá trị được đặt là 2 giây
d) Khi tập lệnh được gọi, nó thực thi các hàm và tự động kết thúc. Nếu bạn sử dụng câu lệnh ‘While’ như được sử dụng trong c), tập lệnh sẽ lặp vô hạn. Trong trường hợp này, bạn có thể dừng tập lệnh bằng hàm StopScript()
Hàm Sleep phải được bao gồm trong câu lệnh While để tạm dừng
hoạt động ít nhất 100msec. Nếu không, tập lệnh sẽ lặp liên tục, điều này sẽ gây ra độ trễ trong hoạt động của dự án.
e) Press button in the editor to compile the script
f) Select [File] – [Save] or icon to save the script to the project.
g) Select [Draw] – [Tag Value] or icon to add the tag value object to the graphic page, which will display the value of the ‘VALUE’ tag
(2) Script Run/Stop
a) Bạn có thể chạy hoặc dừng tập lệnh bằng các chương trình con sau.
Chương trình con để thực thi tập lệnh
Subroutine | Command | RunScript(“Script Name”) |
Script | RunScript “Script Name” | |
Description | Executes the assigned script. | |
Example | Runs the script named ‘ValueChange’. RunScript(“ValueChange”) |
Subroutine | Command | RunScriptEx(“Script Name”, Option, Parameter) | |
Script | RunScriptEx “Script Name”, Option, Parameter | ||
Description | Executes the assigned script according to the option and parameter. | ||
Option | 0 | No action is taken if the assigned script is currently running. | |
1 | Waits until the script stops and runs the script with Parameter. | ||
Parameter | A parameter conveyed to the script. The script can receive the parameter using GetParameter() function. | ||
Example | Convey the parameter ‘Test’ to the script and executes the script named ‘ValueChange’. RunScriptEx(“ValueChange”, 1, Test) |
Subroutine to stop the script
Subroutine | Command | StopScript(“ValueChange”) |
Script | StopScript “ValueChange | |
Description | Stops the assigned script. | |
Example | Stops the script named ‘ValueChange’. StopScript(“ValueChange”) |
b) Select [Draw] – [Button] or icon to add a button for the script execution
c) Execute the project with CimonX. Press [Run Script] button to check the value change
Exercise 1 : Read/Write the Tag Value
a) Select [Tools] – [Script] or icon and write the following script.
Description | 1. Reads the value of ‘TAG_1’ using GetTagVal() function and save it to the variable ‘Tag1’. 2. Use SetTagVal function to write the value of variable ‘Tag1’ to the tag named ‘TAG_2’. |
Script | Sub ValueInput() Tag1 = GetTagVal("TAG_1") SetTagVal "TAG_2", Tag1 End Sub |
b) Execute the project with CimonX and check the operation
Exercise 2 : Acquiring the Current System Time
a) Select [Tools] – [Script] or icon and write the following script
Using Basic Script Function ‘Now()’
Description | 1. Using Now() function to acquire the current system time and save it to the variable named ‘CurTime#’. 2. Use functions such as Year, Month, Day, Hour, Minute, and Second to acquire the corresponding time value and save it to the variables named ‘YYYY’, ‘MM’, ‘DD’, ‘HH’, ‘Min’, ‘Sec’. 3. The variables will be written to the string tags named ‘YEAR’, ‘MONTH’, ‘DATE’, ‘HOUR’, ‘MINUTE’ and ‘SECOND’ 4. Writes the time value as ‘0000/00/00 00:00:00’ to the string tag named ‘CURTIME’. |
Script | Sub GetCurrTime() 'Get Current System Time CurTime# = Now() YYYY = Year(CurTime#) MM = Month(CurTime#) DD = Day(CurTime#) HH = Hour(CurTime#) Min = Minute(CurTime#) Sec = Second(CurTime#) SetTagVal "YEAR", YYYY SetTagVal "MONTH", MM SetTagVal "DATE", DD SetTagVal "HOUR", HH SetTagVal "MINUTE", Min SetTagVal "SECOND", Sec SetTagVal "CURTIME", YYYY & "/" & MM & "/" & DD & " " & HH & ":" & Min & ":" & Sec End Sub |
Using CIMON-SCADA Function ‘TimeStr()’
Description | Use TimeStr() function provided by CIMON-SCADA to display the current system time as ‘0000/00/00 00:00:00’. |
Script | Sub GetCurrTime() SetTagVal "CURTIME", TimeStr(33) End Sub |
b) Execute the project with CimonX to check the operation
Exercise 3 : Executing the External Application - Internet Explorer
a) Select [Tools] – [Script] or icon and write the following script
Description | 1. Use Basic Script function Shell() to execute the Internet Explorer and assign the website address (http://cimon.com/) as a factor. 2. When the script is executed, the CIMON website will be opened. |
Script | Sub RunExProgram() id = Shell("C:\Program Files\Internet Explorer\iexplore.exe ""http://cimon.com/""",3) End Sub |
b) Execute the project with CimonX to check the operation
Exercise 3: Executing the External Application – An Excel File in [This PC]
a) Select [Tools] – [Script] or icon and write the following script
Description | 1. Use Basic Script function Shell() to execute the Microsoft Excel and assign the filename (C:\Sample.xlsx) as a factor. 2. When the script is executed, the file will be opened. |
Script | Sub OpenExcelFile() id = Shell("C:\Program Files\Microsoft Office\Office15\EXCEL.EXE ""C:\Sample.xlsx""",3) End Sub |
b) Execute the project with CimonX to check the operation
Exercise 5 : Read Data From Excel
a) Select [Tools] – [Script] or icon and write the following script
Description | 1. Add an object to execute the assigned file. (C:\Sample.xlsx) 2. Open the file and read the value from the desired cell, and save to the variable named ‘A1#’. 3. Write the value to ‘ANA1’ tag. |
Script | Sub ReadExcel() Dim ExcelApp As Object Dim DayRpt As Object Dim Sheet1 As Object 'Read File name fName$ = "D:\Sample.xlsx" Set ExcelApp = CreateObject("Excel.Application") Set DayRpt = ExcelApp.Workbooks.Open(fName$) Set Sheet1 = DayRpt.Worksheets(1) 'Read a value from the cell and save to the tag A1# = Sheet1.Range("A1").Value SetTagVal "ANA1", A1# DayRpt.Save DayRpt.Close ExcelApp.Quit Set ExcelApp = Nothing End Sub |
b) Execute the project with CimonX to check the operation
Exercise 6 : Write Value to Excel
a) Select [Tools] – [Script] or icon and write the following script
Description | 1. Create an Excel object and open the corresponding file (C:\Sample.xlsx). 2. Write the value of ‘ANA1’ tag to the desired cell. |
Script | Sub WriteExcel() Dim ExcelApp As Object Dim DayRpt As Object Dim Sheet1 As Object ' Write File Name fName$ = "D:\Sample.xlsx" Set ExcelApp = CreateObject("Excel.Application") Set DayRpt = ExcelApp.Workbooks.Open(fName$) Set Sheet1 = DayRpt.Worksheets(1) 'Write Value to the corresponding cell A1# = GetTagVal("ANA1") Sheet1.Range("A1").Value = A1# DayRpt.Save DayRpt.Close ExcelApp.Quit Set ExcelApp = Nothing End Sub |
b) Execute the project with CimonX to check the operation.