当前位置:网站首页>[Tera term] black cat takes you to learn TTL script -- serial port automation skill in embedded development
[Tera term] black cat takes you to learn TTL script -- serial port automation skill in embedded development
2022-07-06 06:20:00 【Black cat senior】
1 Preface
1.1 Statement
This article is based on personal learning experience and network data + Written by , Unauthorized reprint .
TTL Script video learning :TTL Script syntax and practice —— Serial port automation skill in embedded development
1.2 Reference link
TTL Simple script tutorial :
https://imlane.zhanglintc.co/post/ttljiao-ben-jian-yi-jiao-cheng
official TTL Command Reference :
https://ttssh2.osdn.jp/manual/4/en/macro/command/index.html
Official Help Center :
https://ttssh2.osdn.jp/manual/4/en/
2 summary
2.1 TTL background
So-called TTL, It's actually Tera Term Language Abbreviation , That is to say Tera Term Special scripting language . that Tera Term(Wiki) What is it ? It's actually a terminal (Terminal), That is to say, with what XSehll, SecureCRT, Putty Things like that have almost the same meaning .
Tera Term It's something made by the Japanese . In fact, it's just this Tera Term For me, it's not easy to use , be a fool to Xshell. But the key is that it has its own scripting language Tera Term Language ah . With the blessing of its own scripting language , This terminal instantly turns into an automatic weapon , Various operations can be designed in advance , The rest is for the terminal to run automatically .
Tera Term I won't introduce this thing more , As long as you get there Tera Term Open Source Project Download and install . After double clicking the written script, the tool will be called automatically . The following content will introduce TTL The grammar and use of .
TTL The script is for tera term This software serves .tera term load TTL After script , The script will run automatically , Then execute the commands in the script .
2.2 Why study TTL Script
For embedded developers , The serial port tool must be familiar to everyone . Usually use serial port tools to issue commands to the development board 、 receive log, Or connect to a remote terminal , And this requires our manual control . Just imagine , You can manually issue a command , Then send it 1000 An order ?
In embedded development , There is such a widely used serial port tool ——Tera Term, Built in TTL Scripting language , The language is rich in grammar but easy to learn . adopt TTL Script , Automatic testing of serial port tools can be realized . Of course , Its function is far more than that , Wherever serial port tools are used , Both can pass TTL Scripts to meet your automation needs . For example, automatic connection to remote terminals 、 Automatically execute remote terminal scripts 、 Analyze the... Displayed in the serial port tool log、 Count whether the test item is successful, and so on .
2 Learn a serial port automation script language when you are young , Half a day to master , Let you complete the embedded project development faster and more efficiently . Master a lifelong skill in the least time , This will make you better than most embedded engineers .
3 data type
TTL Language (TeraTerm Language) There are only four data types in total , Respectively Integer、Character string、Integer Array、String Array.
4 notes
; A semicolon is followed by a comment
; The line starting with a semicolon is the comment line
var = 0 ; Here, starting from the right of the semicolon is also a comment
5 Literal
; Positive and negative numbers are supported , Floating point is not supported
123
-11
; $ The starting number is 16 Base number
$3a
$10F
; character string ’Hello, world’“I can’t do that”“ Chinese characters also support ”
; # The first is ASCII Code value characters , That is to say # Digital search after ASCII The character corresponding to the code table is the character
#65 ; written words “A”
#$41 ; written words “A”
#13 ; CR written words
; Strings can be spliced directly
‘cat readme.txt’#13#10 ; amount to “cat readme.txt\r\n”
‘abc’#$0d#$0a’def’#$0d#$0a’ghi’
6 Variable
Variables include user-defined variables and system variables .
The following will introduce .
6.1 Custom variable
There is no need to specify a type to define a variable , Use it directly Variable name = value The grammar of .
Be careful : Assignment time = Dexter “10” or ’10’ Representation string ,10 It means numbers , for example cycle = “10” and cycle = 10 It's different , One is a string , One is the number .
; Define common variables
_var = ‘valid’ ; The variable name starting with an underscore is legal .
_var = “valid” ; You can also use double quotation marks
var_1 = 1 ; This is a number
var_2 = ‘str’ ; This is a string
; Define arrays and assign values
intdim a 3 ; int Array
a[0] = 1
a[1] = 2
a[2] = 3
strdim s 3 ; str Array
s[0] = ‘1’
s[1] = ‘2’
s[2] = ‘3’
6.2 System variables
TTL Presets use some system variables , When writing scripts, be careful not to take up . Note that these are system variables when reading scripts , Exists as the return value of a system function .
common :
inputstr ; waitln The received value of the function is stored here
matchstr ; strmatch The values of such functions are stored here
param1 ; The first parameter passed in when calling the script
params ; An array of all parameters passed in when calling the script
result ; waitln And so on code Store here
timeout ; Timeout time
…
Please check Tera Term Help file for :
https://ttssh2.osdn.jp/manual/4/en/macro/syntax/variables.html
How to view variable values ?
TTl There seems to be no printf Functions like that , You can directly output the value of a variable . What if you want to see the value of a variable in the process of writing code ?
In fact, we can change our thinking , Write the variables you want to print directly into the file .
7 Process control
TTL It also supports basic process control operations .
7.1 do, loop
; loop 10 Time
i = 10
do while i > 0
i = i - 1
loop
7.2 while, endwhile
; loop 10 Time
i = 10
while i > 0
i = i - 1
endwhile
7.3 until, enduntil
; loop 10 Time
i = 1
until i > 10
i = i + 1
enduntil
7.4 for, next
; loop 10 Time
for i 1 10
sendln 'abc'
next
; loop 5 Time
for i 5 1
sendln 'abc'
next
7.5 if, then, elseif, else, endif
if a = 1 then
b = 1
c = 2
d = 3
endif
if i < 0 then
i = 0
else
i = i + 1
endif
if i = 1 then
c = '1'
elseif i = 2 then
c = '2'
elseif i = 3 then
c = '3'
else
c = '?'
endif
7.6 break, continue
break
while 1
recvln
strcompare inputstr "OK"
if result = 0 then
break
else
sendln "abc..."
endif
endwhile
continue
a = 0
b = 0
while a < 5
a = a + 1
if a > 2 then
continue
endif
b = b + 1
endwhile
sprintf2 var "a=%d b=%d" a b
dispstr var; a=5 b=2
8 Subprocess
TTL Some sub processes can be defined , Easy to repeat . But there is no return value , It cannot be called a function .
To define a subprocess, you must first understand label label
; The colon starts with the label
: I_am_a_label ; This is a label
You can define and call subprocesses like this :
messagebox "I'm in main." "test"
; Jump to ":sub"
call sub
messagebox "Now I'm in main" "test"
end
; The sub process is specifically defined
:sub
messagebox "Now I'm in sub" "test"
return ; Return to principal
1、 The subprocess is represented by return return .
2、 There is no return, Description is not a sub process , Just labels . Labels are also recommended end end .
9 Common use
Here are some TTL Common writing methods in scripts .
9.1 Connect to server
Use connect command , And many other commands can only be used after connecting to the server :
connect ‘hostname:22 /ssh /2 /auth=password /user=username /passwd=password’
9.2 Execute the given command
sendln date
; Send to remote server date
Command and CR, Execute the command
9.3 Wait for specific characters
wait ‘$’ ‘#’ ; Wait for the remote server screen to appear $ perhaps #. If you don't appear, wait . The wait timeout is determined by the system variable timeout control , if 0 Then there is no timeout , Waiting indefinitely
9.4 String concatenation
Use sprintf2 Define variables directly . among %s It's a placeholder , Replaced by the variable following the string .
hostname = ‘your_hostname’
username = ‘your_username’
password = ‘your_password’
sprintf2 server ‘%s:22 /ssh /2 /auth=password /user=%s /passwd=%s’ hostname username password
Declare the variable directly and add the string after it .
hostname = ‘your_hostname’
msg = hostname
strconcat msg ‘:22 /ssh /2 /auth=password /user=’
strconcat msg username
strconcat msg ’ /passwd=’
strconcat msg password
9.5 Save the server operation log
getdir logdir ; Get the path where the current script is located
changedir logdir ; Adjust the log folder path to the current path
logfile = “operation.log” ; Log file name
logopen logfile 0 0 1 ; Open the log file for writing
logwrite “first line in log file”#13#10 ; Write to the log file "first line in log file\r\n", among #13#10 It means CRLF
10 include Include other files
include ‘1.ttl’
the 1.ttl The contents are included in this TTL Script .
11 Common commands
Reference link
tera term Of ttl How to use the script
https://blog.csdn.net/f2157120/article/details/99822653?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_baidulandingword-0&spm=1001.2101.3001.4242
official TTL Command Reference :
https://ttssh2.osdn.jp/manual/4/en/macro/command/index.html
Common orders can be directly introduced by referring to official orders , Very clear , Not listed here . List only some fallible / Important orders :
fileopen
If the file doesn't exist , Then create the file and open .
changedir
Pay attention to and setdir The difference between , Both commands change the path , So what's the difference ?
chagedir What has changed is Tera Term The path of .setdir What has changed is MACRO The path of .
Then there is a problem again , What is? Tera Term The path of , What is? MACRO route ?
File names specified for the communication commands (e.g. kmtsend) are based on the current directory for Tera Term.
File names specified for other commands (e.g. fileopen) are based on the current directory for MACRO.
Maybe it's still not clear to see here Tera Term The path and MACRO The path difference , Don't worry , Another order , When the path is used , Will explain which path , as follows :
filereadln
Format :filereadln
Notice the environment variables result Value :
If you find that the pointer has reached the end of the file when reading a line ,result Set to 1. Instead of reading a line and finding that the pointer reaches the end of the file , Just go back to 1, Bear in mind !!
If the file contains only a new line , that strvar The value of is empty ,result Be set to 0.
Strtrim
Format :strtrim
function : Delete strval At the beginning and end of the string trimchars character string
Be careful :1、 If in strval In the middle of the string trimchars, Will not be removed , It will only remove all that appear in the middle or at the end trimchars character string .
2、 If strval The beginning or end of the string appears trimchars Some characters in the string , It will also be removed .
3、 If strval There are several consecutive occurrences at the beginning or end of the string rimchars character string , Will be removed .
Example :
src = “YEE_ONLY YEE_ONLY verify YEE_ONLY test YEE_ONL YEE_ONLY”
strtrim src 'YEE_ONLY ’
messagebox src ‘result’
The result is :verify YEE_ONLY test
gettime
Example :gettime starttime “%Y%m%d-%H%M%S”
%Y%m%d-%H%M%S What time format is it ?
Refer to the connection to know :https://baike.baidu.com/item/strftime/9569073?fr=aladdin
12 How to run ttl Script
There are two ways to run ttl Script :
ttermpro Software loading TTL Script
adopt Mcro Choose what you need to load ttl The script can be .
TTPMACRO.EXE Software loading ttl Script
In the installation tera term Software time , The installation path is not only ttermpro.exe Software , also ttpmacro.exe Software , open ttpmacro.exe Software loading ttl The script can be .
13 ttermpro How to use the software
13.1 Chinese display garbled
As shown in the figure ,ttermpro Software link virtual machine , There is garbled code in Chinese :
The following settings are enough :
边栏推荐
- [postman] test script writing and assertion details
- 通过修改style设置打印页样式
- Hypothesis testing learning notes
- Testing and debugging of multithreaded applications
- 【Postman】动态变量(也称Mock函数)
- 【Postman】测试(Tests)脚本编写和断言详解
- 黑猫带你学UFS协议第18篇:UFS如何配置逻辑单元(LU Management)
- [postman] collections configuration running process
- Properties file
- 模拟卷Leetcode【普通】1249. 移除无效的括号
猜你喜欢
B - The Suspects
Pat (Grade B) 2022 summer exam
【Postman】测试(Tests)脚本编写和断言详解
Construction and integration of Zipkin and sleuth for call chain monitoring
联合索引的左匹配原则
Data type of MySQL
技术分享 | 常见接口协议解析
F - True Liars (种类并查集+DP)
Nodejs realizes the third-party login of Weibo
Win10 cannot operate (delete, cut) files
随机推荐
黑猫带你学eMMC协议第10篇:eMMC读写操作详解(read & write)
[postman] collections configuration running process
模拟卷Leetcode【普通】1109. 航班预订统计
Caused by:org. gradle. api. internal. plugins . PluginApplicationException: Failed to apply plugin
这些年用Keil遇到的坑
职场进阶指南:大厂人必看书籍推荐
Simulation volume leetcode [general] 1091 The shortest path in binary matrix
黑猫带你学UFS协议第8篇:UFS初始化详解(Boot Operation)
F - true liars (category and search set +dp)
LeetCode 729. 我的日程安排表 I
MFC on the conversion and display of long string unsigned char and CString
模拟卷Leetcode【普通】1143. 最长公共子序列
MFC 动态创建的对话框及改变控件的大小和位置
Isam2 operation process
Manhattan distance and Manhattan rectangle - print back font matrix
[postman] dynamic variable (also known as mock function)
10M25DCF484C8G(FPGA) AMY-6M-0002 BGA GPS模块
模拟卷Leetcode【普通】1218. 最长定差子序列
【eolink】PC客户端安装
Nodejs realizes the third-party login of Weibo