• 收藏
  • 加入书签
添加成功
收藏成功
分享

基于Visual Basic和RSlinx OPC Server称重数据采集系统

宫飞
  
安家(建筑与工程)
2021年28期
新疆八一钢铁股份有限公司

打开文本图片集

摘要: 介绍利用Visual Basic的内置的MSComm控件通过计算机串口读取称量系统数字接口数据,写入RockWell PLC,满足现场通过简单改造实现数字接口数据与PLC之间的数据交换,达到了对生产工艺数据采集、管理的目的。

关键词:MSComm、RSLinx OPC Server、控件

引言

伴随着工业大数据系统不断应用,作为支撑系统需求的现场基础数据采集和数据准确性极其重要。工业大数据系统不仅需要解决包括设备诊断、质量分析、成本核算、供销一体化分析预测等等,在生产过程中使用这些大数据,就能分析整个生产流程,掌握每个环节实时状态。

一、项目介绍

新疆八一钢铁股份有限公司炼钢厂铸坯称量改造前通过称重仪表进行数据转换通过4-20mA信号写入PLC系统,存在距离限制和信号干扰情况,造成数据偏差。炼钢厂铸坯称量信息作为产销系统炼钢成本,以及与轧钢系统成品结算的关键数据,由于模拟量数据偏差经常性出现人工介入核算修正。因此通过改造,实现计算机系统采集RS-232信号,通过RSLinx OPC Server写入PLC,保证数据准确。

二、开发方案

1、设置程序变量定义、声明和调用。

Option Base 1

Option Explicit

Public m1_flag1 As Integer

Public m1_flag2 As Integer

Public Instring1 As String

Public inputchar

Public mao_z1 As String

Private Declare Function SetWindowPos Lib “user32” (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Const HWND_TOPMOST = -1

Private Const SWP_SHOWWINDOW = &H40

Dim WithEvents OPCMyserver As OPCServer

Dim WithEvents OPCMygroups As OPCGroups

……

Dim OPCMyitem As OPCItem

2、声明、调用OPC函数。

Private Sub connectopc()

On Error GoTo connecterror

Dim ItemServerHandles() As Long

Dim Errors() As Long

Dim ClientHandles(1) As Long

Dim OPCItemIDs(1) As String

Dim i As Integer

……

For i = 1 To cyclecollitemnum

ClientHandles(1) = i

OPCItemIDs(1) = str_field(i)

OPCMyitems.AddItems 1, OPCItemIDs, ClientHandles, ItemServerHandles, Errors

……

Exit Sub

connecterror:

MsgBox “Error Connecting!”

End Sub

3、根据称重仪表设置的波特率、数据位、奇偶校验位等参数,通过MSComm控件进行设置,实现计算机com口数据采集参数设置功能。

Private Sub Form_Load()

MSComm1.CommPort = 1

MSComm1.Settings = “2400,N,8,1”

MSComm1.InputMode = comInputModeText

MSComm1.InputLen = 1

MSComm1.RThreshold = 1

MSComm1.PortOpen = True

4、对数据显示进行处理。

Private Sub MSComm1_OnComm()

Select Case MSComm1.CommEvent

Case comEvReceive

inputchar = MSComm1.Input

‘Label1.Caption = Label7.Caption & inputchar

Debug.Print inputchar

If inputchar = Chr(2) Then

m1_flag1 = 2

m1_flag2 = 2

Else

m1_flag2 = 1

End If

If (m1_flag1 = 2 And m1_flag2 = 1) Then

Instring1 = Instring1 & inputchar

If (Len(Instring1) = 9) Then

mao_z1 = CStr(Val(Right(Instring1, 6)) / 1000)

m1_flag1 = 1

Instring1 = Space(0)

Label1.Caption = mao_z1

End If

End If

End Select

End Sub

5、称量数据通过RSLinx OPC Server实时写入PLC系统。

Public str_field(2) As String

Public constr_field() As String

Public relationstr_field() As String

Public ServerName, nodename As String

Public cyclecollitemnum As Integer

Public conditioncollitemnum As Integer

Public cyclecolltime  As Integer

Public unit As String

Public cyclestarttime, cycleendtime As Date

Public Sub getconfiginfo()

cyclecollitemnum = 1

ServerName = “RSLinx OPC Server”

nodename = “”

cyclecolltime = 100

str_field(1) = “[com3]slabweight1”

End Sub

四、结束语

通过本次改造,炼钢厂铸坯称量数据准确性大幅提高,并且相关经验可以覆盖到其他工位。

在改造过程中也发现了一些不足:

1 A是Visual Basic软件已属于古董软件,人机界面不是很完善,计划在后期通过C#、Python等主流软件进行开发。

2 A该系统未与生产过程管理系统互联,未能将铸坯生产相关数据勾连(例如炉号、铸坯号等),因此也未通过数据库对数据进行存储。

下一步工作通过应用主流开发软件和数据库,与生产过程管理系统互联,进行数据存储,形成一套完备的铸坯称量数据管理系统。

*本文暂不支持打印功能

monitor