Main
Included page "clone:weintek-macros" does not exist (create it now)
To make macro editing and sharing easier, I built this site for our users to discover the possible solutions we provide.
Table of Contents
Registers
- LB
- LW
How to run a macro
- Use [PLC Control] object to execute macro by a bit register. You could use LB-9000 to run a macro when HMI starts.
- Use [Set Bit] object to run once/periodically
- Use [Toggle Switch] object to execute once
- Use [Function Key] object to execute once
Free Protocol
readline()
// variables below are outputs from readline()
char line[128]
short line_size
// readline():
// Read 1024 characters into rlb[] readline buffer and
// return anything before CHR_CR. Because string operations cannot be used
// on global variable, we have to do it manually.
// Note: Max. 128 characters (128 bytes) per line.
char rlb[512] // readline buffer
short rlb_i
short rlb_size
sub bool readline(char terminator)
bool end_condition = false
FILL(line[0], 0, 128)
line_size = 0
while end_condition == false
short rlb_size_local // becuase INPORT cannot access global variable directly
// use rlb_sizep as a temporary var
if rlb_i >= rlb_size or rlb_size == 0 then
char rlb_local[512]
short rlb_i_local
INPORT2(rlb_local[0], "Free Protocol", rlb_size_local, 20)
for rlb_i_local = 0 to rlb_size_local
rlb[rlb_i_local] = rlb_local[rlb_i_local]
next rlb_i_local
rlb_i = 0
rlb_size = rlb_size_local
continue
end if
if rlb[rlb_i] == terminator then
end_condition = true
else
line[line_size] = rlb[rlb_i]
line_size = line_size + 1
if line_size == 128 then
end_condition = true
end if
end if
rlb_i = rlb_i + 1
wend
return true
end sub
readline_c(chr_start, chr_terminator)
// variables below are outputs from readline()
char line[128]
short line_size
// readline():
// Read 1024 characters into rlb[] readline buffer and
// return anything before CHR_CR. Because string operations cannot be used
// on global variable, we have to do it manually.
// Note: Max. 128 characters (128 bytes) per line.
char rlb[512] // readline buffer
short rlb_i
short rlb_size
// readline conditional
sub bool readline_c(char chr_start, char chr_trm)
bool end_condition = false
FILL(line[0], 0, 128)
line_size = 0
while end_condition == false
short rlb_size_local // becuase INPORT cannot access global variable directly
// use rlb_sizep as a temporary var
if rlb_i >= rlb_size or rlb_size == 0 then
char rlb_local[512]
short rlb_i_local
INPORT2(rlb_local[0], "Free Protocol", rlb_size_local, 20)
for rlb_i_local = 0 to rlb_size_local
rlb[rlb_i_local] = rlb_local[rlb_i_local]
next rlb_i_local
rlb_i = 0
rlb_size = rlb_size_local
continue
end if
if rlb[rlb_i] == chr_trm then
line[line_size] = rlb[rlb_i] // includes terminator character
line_size = line_size + 1
end_condition = true
else if rlb[rlb_i] == chr_start or line_size > 0 then
line[line_size] = rlb[rlb_i]
line_size = line_size + 1
if line_size == 128 then
end_condition = true
end if
end if
rlb_i = rlb_i + 1
wend
return true
end sub
Send a simple command with CR (0x0D)
macro_command main()
char request[80] = "IS_A_COMMAND"
short strlength
strlength = StringLength(request[0]) // find Command Length
request[strlength] = 0x0D // Combine CHR_CR to request[]
OUTPORT(request[0], "Free Protocol", strlength + 1) // Send Command to Port
end macro_command
Debug tips
Too slow? Use system time (0.1s) to identify the bottleneck
short systime
float system_in_second
// Do something
GetData(systime, "Local HMI", LW, 9030, 1)
systime_in_second = systime / 10.0
TRACE("(systime:%.1fs) return_value is %d", systime_in_second, return_value)
// Do something
GetData(systime, "Local HMI", LW, 9030, 1)
systime_in_second = systime / 10.0
TRACE("(systime:%.1fs) return_value is %d", systime_in_second, return_value)
Recipe Database related
(EasyBuilder Pro only)
Cyclically update the recipe selection
macro_command main()
int delay_time = 500 // unit: ms
short max_data_index = 4
short i
bool run
GetData(run, "Local HMI", LB, 100, 1)
GetData(i, "Local HMI", RECIPE, "RecipeDatabase_1.Selection")
while run == true
// cyclically increace i
// 0->up->0->up
if i >= 0 and i < max_data_index then
i = i + 1
else
i = 0
end if
// update the recipe selection
SetData(i, "Local HMI", RECIPE, "RecipeDatabase_1.Selection")
// DELAY for a while
DELAY(delay_time)
// retrieve the updated running condition LB-100
GetData(run, "Local HMI", LB, 100, 1)
wend
end macro_command
SQL: Search by Date and Select
Insert a string variable in a string by %():
result = RecipeQuery("SELECT * FROM Employee WHERE On-Board>=%(str_YMD);", n_records)
The code has 3 parts: init, search_by_date_first, search_by_date_next
// macro: init
macro_command main()
bool bEnableNext = false
char name[9]
StringCopy("Standley", name[0])
name[8] = 0
SetData(name[0], "Local HMI", "name", 9)
SetData(bEnableNext, "Local HMI", "enable_next", 1)
end macro_command
// macro: search_by_date_first
macro_command main()
short year=0, month=0, day=0
char str_year[4]
char str_month[2]
char str_day[2]
char str_slash[1]="/"
char str_YMD[10]
GetData(year, "Local HMI", LW, 120, 1)
GetData(month, "Local HMI", LW, 121, 1)
GetData(day, "Local HMI", LW, 122, 1)
DEC2ASCII(year, str_year[0], 4)
DEC2ASCII(month, str_month[0], 2)
DEC2ASCII(day, str_day[0], 2)
FILL(str_YMD[0], 0, 10)
StringCat(str_year[0], str_YMD[0])
StringCat(str_slash[0], str_YMD[0])
StringCat(str_month[0], str_YMD[0])
StringCat(str_slash[0], str_YMD[0])
StringCat(str_day[0], str_YMD[0])
int n_records = 0
bool result = false
result = RecipeQuery("SELECT * FROM Employee WHERE On-Board>=%(str_YMD);", n_records)
bool bEnableNext=false
int current=0
unsigned short n = 0
if result then
if (n_records > 0) then
RecipeQueryGetRecordID(n, 0)
SetData(n, "Local HMI", RECIPE, "Employee.Selection")
SetData(current, "Local HMI", "current_record", 1)
end if
if (n_records > 1) then
bEnableNext = true
end if
end if
SetData(bEnableNext, "Local HMI", "enable_next", 1)
end macro_command
// macro: search_by_date_next
macro_command main()
short year=0, month=0, day=0
char str_year[4]
char str_month[2]
char str_day[2]
char str_slash[1]="/"
char str_YMD[10]
GetData(year, "Local HMI", LW, 120, 1)
GetData(month, "Local HMI", LW, 121, 1)
GetData(day, "Local HMI", LW, 122, 1)
DEC2ASCII(year, str_year[0], 4)
DEC2ASCII(month, str_month[0], 2)
DEC2ASCII(day, str_day[0], 2)
FILL(str_YMD[0], 0, 10)
StringCat(str_year[0], str_YMD[0])
StringCat(str_slash[0], str_YMD[0])
StringCat(str_month[0], str_YMD[0])
StringCat(str_slash[0], str_YMD[0])
StringCat(str_day[0], str_YMD[0])
int n_records = 0
bool result = false
result = RecipeQuery("SELECT * FROM Employee WHERE On-Board>=%(str_YMD);", n_records)
bool bEnableNext=true
int current=0
int n=0
if (n_records > 0) then
GetData(current, "Local HMI", "current_record", 1)
if (current < n_records-1) then
current = current+1
RecipeQueryGetRecordID(n, current)
SetData(n, "Local HMI", RECIPE, "Employee.Selection")
SetData(current, "Local HMI", "current_record", 1)
else
bEnableNext = false
end if
end if
SetData(bEnableNext, "Local HMI", "enable_next", 1)
end macro_command