串口电子秤
# FastWeb 串口电子秤
# 1. 说明
本范例采用串口通讯协议,控制驳接在设备上的电子秤。PC端发送读取指令,电子秤返回称重重量的信息;PC端发送去皮与置零的指令,电子秤端执行相应的操作。电子秤是工业生产过程中经常使用到的设备,现如今大部分近期生产的工业用电子秤都已经预留了串口通讯的接口。在本范例中使用的电子秤也具有该功能。
本范例使用到的电子秤为耀华XK3190-A12+E,其秤头的外观如下图所示。


地磅与秤头按照上图所示的内容进行连接,电子秤的外接串口端与USB串口转接线的串口端相连,另外一端与PC的USB端口连接,打开电子秤的开关,待连接确认后,打开Windows中的设备管理器,查看该串口端口号与串口设备名称,在本范例中会使用到该端口号。
通过本范例学习,可以掌握串口的基本通讯原理,并结合电子秤进行数据读取的功能。
# 2. 设计明细
开启FastWeb设计器,分别加入下插图之控件。或者点击左上角的[导入]
选择模板文件来打开对应模板。

1:TUgLabel组件,控件名称为UgLabel01
。
2:TUgImage组件,控件名称为 UgImage01
。
3:TUgPanel组件,控件名称为 UgPanel01
。
4:TUgWebSerial组件,控件名称为 UgWebSerial01
。
5:TUgFSButton组件,控件名称为 UgFSButton01
。
6:TUgFSButton组件,控件名称为 UgFSButton04
。
7:TUgFSButton组件,控件名称为 UgFSButton02
。
8:TUgFSButton组件,控件名称为 UgFSButton03
。
UgWebRunFrame属性设置
Height
:设置页面高度=640
。Width
:设置页面宽度=480
。
1:UgLabel01属性设置
Alignment
:设置对齐方式,设置为taCenter
。AutoSize
:设置控件尺寸是否跟随文本自动变化,设置为False
。Font
:设置字体,点击右侧的[√]
打开字体编辑窗口,按照下图的样式进行设置。
Text
:设置标签显示的文本=串口电子秤
。
2:UgImage01属性设置
Picture
:设置要显示的图片,点击右侧的[√]
按钮,打开图像编辑器,选择编辑的图像。Stretch
:设置图像是否跟随空间进行自适应拉伸,设置为True
。
3:UgPanel01属性设置
Alignment
:设置对齐的方式,设置为taRightJustify
。Caption
:设置显示的字幕=0.00
。Color
:设置控件的背景颜色,设置为clBlack
。Font
:设置字体,点击右侧的[√]
打开字体设置对话框,设置字体,示例如下。
5:UgFSButton01属性设置
Caption
:设置按钮上显示的文字,设置为打开串口
。StyleButton
:设置按钮显示的样式,设置为GoogleGreenRound
。
6:UgFSButton04属性设置
Caption
:设置按钮上显示的文字,设置为读数
。StyleButton
:设置按钮显示的样式,设置为GoogleBlueRound
。
7:UgFSButton02属性设置
Caption
:设置按钮上显示的文字,设置为去皮
。StyleButton
:设置按钮显示的样式,设置为GoogleBlueRound
。
8:UgFSButton03属性设置
Caption
:设置按钮上显示的文字,设置为清零
。StyleButton
:设置按钮显示的样式,设置为GoogleBlue3Round
。
# 3. 程序设计
点击程序设计界面右下角的按钮,切换至单元选择界面,勾选需要使用的单元,该程式的程序不需要引用单元。
# 3.1. 程序初始设置
该程式程序初始设置。
//JScript
{
ugWebSerial01.Visible = False;
}
2
3
4
//PasScript
Begin
ugWebSerial01.Visible := False;
End.
2
3
4
// Make sure to add code blocks to your code group
# 3.2. 事件设置
- 5:UgFSButton01-OnClick事件
点击以启用串口选择界面。
//JScript
function UgFSButton01OnClick(sender)
{
ugWebSerial01.Request();
}
2
3
4
5
//PasScript
procedure UgFSButton01OnClick(sender: tobject);
begin
ugWebSerial01.Request;
end;
2
3
4
5
// Make sure to add code blocks to your code group
- 6:UgFSButton04-OnClick事件
点击以开启串口读数。
//JScript
function UgFSButton04OnClick(sender)
{
ugWebSerial01.Open(0);
}
2
3
4
5
//PasScript
procedure UgFSButton04OnClick(sender: tobject);
begin
ugWebSerial01.Open(0);
end;
2
3
4
5
// Make sure to add code blocks to your code group
- 4:UgWebSerial01-OnReceived事件
当串口接收到数据时,获取数据并显示。
//JScript
function ugWebSerial01OnReceived(adata)
//连续模式下的读数获取方式
{
var text,v1,v2,d1;
if (copy(adata, 1, 2) == "wn")
{
//wn000.530kg
Text = adata;
//UgEdit01.Text =adata;
if (Text == "")
{
UgPanel01.Caption = "0.00";
exit;
}
v1 = copy(Text, 3, 1);
if (v1 == "0")
{
v2 = copy(Text, 3, 7);
d1 = StrToFloat(v2);
}
else if(v1 == "-"){
v2 = copy(Text, 4, 7);
d1 = StrToFloat(v2) * (-1);
}
UgPanel01.Caption = floattostr(d1);
if (Text=="wn000.000kg"){
d1 = 0;
UgPanel01.Caption = "0.00";
}
}
}
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
//PasScript
procedure ugWebSerial01OnReceived(const adata: string);
//连续模式下的读数获取方式
var
Text: String;
v1, v2: string;
d1: Double;
begin
if copy(adata, 1, 2) = 'wn' Then
Begin
//wn000.530kg
Text := adata;
//UgEdit01.Text :=adata;
if (Text = '') then
begin
UgPanel01.Caption := '0.00';
exit;
end;
v1 := copy(Text, 3, 1);
if v1 = '0' then
begin
v2 := copy(Text, 3, 7);
d1 := StrToFloat(v2);
end
else if v1 = '-' then
begin
v2 := copy(Text, 4, 7);
d1 := StrToFloat(v2) * (-1);
end;
UgPanel01.Caption := floattostr(d1);
if Text='wn000.000kg' then
begin
d1 := 0;
UgPanel01.Caption := '0.00';
end;
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
// Make sure to add code blocks to your code group
7:UgFSButton02属性设置
此项仅在电子秤处于连续读数模式下有效,用于向电子秤发送去皮的指令。
//JScript
function UgFSButton02OnClick(sender)
//去皮(连续模式下此项无效)
{
ugWebSerial01.WriteLines("T");
}
2
3
4
5
6
//PasScript
procedure UgFSButton02OnClick(sender: tobject);
//去皮(连续模式下此项无效)
begin
ugWebSerial01.WriteLines('T');
end;
2
3
4
5
6
// Make sure to add code blocks to your code group
8:UgFSButton03属性设置
此项仅在电子秤处于连续读数模式下有效,用于向电子秤发送置零的指令。
//JScript
function UgFSButton03OnClick(sender)
//置零(连续模式下此项无效)
{
ugWebSerial01.WriteLines("Z");
}
2
3
4
5
6
//PasScript
procedure UgFSButton03OnClick(sender: tobject);
//置零(连续模式下此项无效)
begin
ugWebSerial01.WriteLines('Z');
end;
2
3
4
5
6
// Make sure to add code blocks to your code group
# 4. 运行结果
使用鼠标在FastWeb菜单,点击[保存至数据库]
按钮,将其保存至数据库,点击[调试运行]
确认能够正常打开。

如果是首次使用,点击 [打开串口]
,选择要打开的串口后,显示当前的读数信息。在首次配对后,运行时可点击 [读数]
按钮,可自动获取读数信息。