自由口模式下S7-200PLC与计算机的通信4
程序清单
主程序:
NETWORK 1 | ||
LD | SM0.1 | //第一次扫描调用初始化子程序 |
CALL | initialize | |
NETWORK 2 | ||
LDB= | VB134, VB199 | //指令中的站地址与本机站地址相符 |
AB= | VB102, 5 | //指令类型为读指令 |
A | M0.0 | //BCC码校验正确 |
CALL | Read | //调用读子程序 |
NETWORK 3 | ||
LDB= | VB134, VB199 | //指令中的站地址与本机站地址相符 |
AB= | VB102, 6 | //指令类型为写指令 |
A | M0.0 | //BCC码校验正确 |
CALL | Write | //调用写子程序 |
NETWORK 4 | ||
LD | M0.1 | //指令接收完成后调用BCC码校验子程序 |
CALL | Verify | |
NETWORK 5 | ||
LD | SM4.5 | //当端口空闲时启动RCV |
RCV | VB100, 0 |
Read子程序:
NETWORK 1 | ||
LD | SM0.0 | //停止端口0的接收 |
R | SM87.7, 1 | |
R | M0.0, 1 | |
RCV | VB100, 0 | |
NETWORK 2 | ||
LD | SM0.0 | //将数据写入发送缓冲区 |
MOVB | 103, VB154 | |
MOVB | 1, VB155 | |
HTA | *VD135, VB156, 16 | |
MOVB | 26, VB174 | |
MOVB | 21, VB153 | |
NETWORK 3 | ||
LD | SM0.0 | //计算BCC校验码 |
FOR | VW177, +1, +16 | |
NETWORK 4 | ||
LD | SM0.0 | |
XORB | *VD181, VB180 | |
NETWORK 5 | ||
LD | SM0.0 | |
INCD | VD181 | |
NETWORK 6 | ||
NEXT | ||
NETWORK 7 | ||
LD | SM0.0 | |
HTA | VB180, VB172, 2 | //BCC校验码写入发送缓冲区 |
NETWORK 8 | ||
LD | SM4.5 | //发送反馈信息 |
XMT | VB153, 0 |
Write子程序:
NETWORK 1 | ||
LD | SM0.0 | //停止端口0的接收 |
R | SM87.7, 1 | |
R | M0.0, 1 | |
RCV | VB100, 0 | |
NETWORK 2 | ||
LD | SM0.0 | //装入要写如数据源的地址指针 |
MOVD | &VB115, VD145 | |
NETWORK 3 | ||
LD | SM0.0 | //写入数据 |
ATH | *VD145, *VD135, VB139 | |
NETWORK 4 | ||
LD | SM0.0 | //指令执行的反馈信息写入发送缓冲区 |
MOVB | 21, VB153 | |
MOVB | 103, VB154 | |
MOVB | 2, VB155 | |
MOVB | 26, VB174 | |
NETWORK 5 | ||
LD | SM4.5 | //发送指令执行的反馈信息 |
XMT | VB153, 0 |
Verify子程序:
NETWORK 1 | ||
LD | SM0.0 | |
R | M0.1, 1 | //复位verify子程序的执行条件 |
NETWORK 2 | ||
LD | SM0.0 | //计算BCC码 |
FOR | VW175, +1, +29 | |
NETWORK 3 | ||
LD | SM0.0 | |
XORB | *VD149, VB179 | |
NETWORK 4 | ||
LD | SM0.0 | |
INCD | VD149 | |
NETWORK 5 | ||
NEXT | ||
NETWORK 6 | ||
LDB= | VB179, VB140 | //当BCC码校验正确时,M0.0置1 |
AB= | VB133, 71 | |
S | M0.0, 1 | |
NETWORK 7 | ||
LDB= | VB133, 71 | //BCC码错误时发送反馈信息 |
AB<> | VB179, VB140 | |
MOVB | 21, VB153 | |
MOVB | 103, VB154 | |
MOVB | 3, VB155 | |
MOVB | 26, VB174 | |
R | SM87.7, 1 | |
RCV | VB100, 0 | |
XMT | VB153, 0 | |
NETWORK 8 | ||
LDB<> | VB133, 71 | //指令格式错误或RCV超时时发送反馈信息 |
MOVB | 21, VB153 | |
MOVB | 103, VB154 | |
MOVB | 4, VB155 | |
MOVB | 26, VB174 | |
R | SM87.7, 1 | |
RCV | VB100, 0 | |
XMT | VB153, 0 |
Initialize子程序:
NETWORK 1 | ||
LD | SM0.0 | |
MOVB | 9, SMB30 | //0口"9600,N,8,1" |
NETWORK 2 | ||
LD | SM0.0 | //RCV指令初始化 |
MOVB | 16#EC, SMB87 | |
MOVB | 103, SMB88 | |
MOVB | 71, SMB89 | |
MOVB | +1000, SMW92 | |
MOVB | 35, SMB94 | |
R | SM87.2, 1 | |
NETWORK 3 | ||
LD | SM0.0 | |
ATCH | RCVcomplete, 23 | //连接口0接收完成的中断 |
NETWORK 4 | ||
LD | SM0.0 | |
ATCH | XMTcomplete, 9 | //连接口0发送完成的中断 |
NETWORK 5 | ||
LD | SM0.0 | |
ENI | //中断允许 | |
NETWORK 6 | ||
LD | SM0.0 | |
MOVB | 2, VB199 | //将本机站地址装入寄存器 |
NETWORK 7 | ||
LD | SM0.0 | |
MOVB | &VB102, VD149 | //装入地址指针 |
MOVB | 0, VB179 | //BCC码寄存器清零 |
MOVB | &VB156, VD181 | //装入地址指针 |
MOVB | 0, VB180 | //BCC码寄存器清零 |
RCVcomplete中断程序
NETWORK 1 | ||
LD | SM0.0 | |
ATH | VB103, VB134, 2 | //指令译码(ASCII码到十六进制) |
ATH | VB105, VB135, 8 | |
ATH | VB113, VB139, 2 | |
ATH | VB131, VB140, 2 | |
S | M0.1, 1 | //置位Verify子程序的触发条件 |
MOVB | 0, VB179 | //BCC码寄存器清零 |
MOVD | &VB102, VD149 | //装入地址指针 |
XMTcomplete中断程序
NETWORK 1 | ||
LD | SM0.0 | |
R | M0.0, 1 | //复位BCC校验码正确的标志位 |
S | SM87.7, 1 | //允许口0进行接收 |
MOVB | 0, VB179 | //BCC校验码寄存器清零 |
MOVB | 0, VB180 | //BCC校验码寄存器清零 |
MOVD | &VB102, VD149 | //重新装入地址指针 |
MOVD | &VB156, VD181 | |
MOVB | 0, VB133 | //接收缓冲区中存放指令结束字符的字节清零 |