1
Open Discussion / error modbus rtu and tcp
« on: May 03, 2018, 11:20:22 AM »
error modbus rtu and tcp
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Private Sub CreateGroupedReadList()
SyncLock (SubscriptionLock)
GroupedSubscriptionReads.Clear()
SubscriptionListChanged = False
Dim index, ItemCountToGroup, HighestElement, ElementSpan As Integer
While index < SubscriptionList.Count
Try
'* optimize in as few reads as possible - try group reading
'* and perform in Async Mode
ItemCountToGroup = 0
ElementSpan = HighestElement - SubscriptionList(index).Address.Element
While (index + ItemCountToGroup + 1) < SubscriptionList.Count AndAlso
SubscriptionList(index + ItemCountToGroup).Address.ReadFunctionCode = SubscriptionList(index + ItemCountToGroup + 1).Address.ReadFunctionCode AndAlso
SubscriptionList(index + ItemCountToGroup).Address.BitsPerElement = SubscriptionList(index + ItemCountToGroup + 1).Address.BitsPerElement AndAlso
((SubscriptionList(index + ItemCountToGroup + 1).Address.Element + SubscriptionList(index + ItemCountToGroup + 1).Address.NumberOfElements) - SubscriptionList(index).Address.Element) < m_MaxReadGroupSize AndAlso
SubscriptionList(index + ItemCountToGroup).Address.Address.ToUpper = SubscriptionList(index + ItemCountToGroup + 1).Address.Address.ToUpper
ItemCountToGroup += 1
End While
Catch ex As Exception
Throw
End Try
ElementSpan = (SubscriptionList(index + ItemCountToGroup).Address.Element - SubscriptionList(index).Address.Element)
'* Correct is the last element is a 32 bit read
If SubscriptionList(index + ItemCountToGroup).Address.BitsPerElement > 16 Then
ElementSpan += CInt(SubscriptionList(index + ItemCountToGroup).Address.BitsPerElement / 8) - 1
End If
Dim sr As New SubscriptionRead
sr.Address = SubscriptionList(index).Address.Address
'* Are we using a single bit from an integer?
If SubscriptionList(index).Address.BitNumber >= 0 Then
'* If so, then only subscribe to the word
If SubscriptionList(index).Address.Address.LastIndexOf(".") > 0 Then
sr.Address = SubscriptionList(index).Address.Address.Substring(0, SubscriptionList(index).Address.Address.LastIndexOf("."))
End If
End If
sr.NumberToRead = (ElementSpan + 1)
GroupedSubscriptionReads.Add(sr)
index += (1 + ItemCountToGroup)
End While
End SyncLock
End Sub
Public Class ReportingD
Inherits DataSubscriber2
Private sw As System.IO.StreamWriter
Private Sev As Boolean
Private intCunt As Integer
Private FullPaket As New List(Of String)
#Region "Properties"
Private m_FileFolder As String = "C:"
<BrowsableAttribute(True), EditorAttribute(GetType(FileFolderEditor), GetType(System.Drawing.Design.UITypeEditor))> _
Public Property FileFolder As String
Get
Return m_FileFolder
End Get
Set(ByVal value As String)
If value.Length > 0 Then
'* Remove the last back slash if it is there
If value.Substring(value.Length - 1, 1) = "\" Then value = value.Substring(0, value.Length - 1)
m_FileFolder = value
End If
End Set
End Property
Private m_FileName As String = "PLCDataLog.log"
Public Property FileName As String
Get
Return m_FileName
End Get
Set(ByVal value As String)
If m_FileName <> value Then
m_FileName = value
If sw IsNot Nothing Then
sw.Dispose()
sw = New System.IO.StreamWriter(m_FileFolder & "\" & m_FileName, True)
End If
End If
End Set
End Property
Public Enum TriggerType
DataChange
End Enum
Private m_LogTriggerType As TriggerType
Public Property LogTriggerType As TriggerType
Get
Return m_LogTriggerType
End Get
Set(ByVal value As TriggerType)
m_LogTriggerType = value
End Set
End Property
Private m_MaximumPoints As Integer
Public Property MaximumPoints As Integer
Get
Return m_MaximumPoints
End Get
Set(ByVal value As Integer)
m_MaximumPoints = value
End Set
End Property
#End Region
#Region "Constructor/Destructor"
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub
#End Region
#Region "Events"
Private PointCount As Integer
Protected Overrides Sub onDataChanged(ByVal e As MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs)
MyBase.OnDataChanged(e)
If e.PlcAddress = "00001" Then
Sev = CBool(e.Values(0))
End If
If m_LogTriggerType = TriggerType.DataChange Then
If m_MaximumPoints = 0 OrElse PointCount < m_MaximumPoints And Sev = True Then
StoreValue()
PointCount += 1
End If
End If
End Sub
Public Sub insert_DateBase()
Dim x As Integer
Dim dt As DataTable = db.GetRecors("select * from BatchFinal")
x = dt.Rows.Count + 1
db.InsertBatchFinal(x, CStr(FullPaket(0).ToString), CDbl(FullPaket(1).ToString), CDbl(FullPaket(2).ToString), CDbl(FullPaket(3).ToString), CDbl(FullPaket(4).ToString), CDbl(FullPaket(5).ToString), CDbl(FullPaket(6).ToString), CDbl(FullPaket(7).ToString), CDbl(FullPaket(8).ToString), CStr(1), StringToWrite2)
End Sub
Dim StringToWrite2 As String
Private Sub StoreValue()
Try
StringToWrite2 = Date.Now
FullPaket.Clear()
For Each item In PLCAddressValueItems
If item.ScaleFactor = 1 Then
FullPaket.Add(item.LastValue)
Else
Try
Catch ex As Exception
End Try
End If
Next
If Sev = True Then
intCunt = 1
If intCunt = 1 Then
insert_DateBase()
FullPaket.Clear()
End If
Sev = False
intCunt = 0
Else
Sev = False
intCunt = 0
End If
Catch
End Try
End Sub
#End Region
Public Sub InsertBatchFinal(ByVal BatchID As Integer, ByVal BatchName As String, ByVal Tank1 As Double, ByVal Tank2 As Double, ByVal Tank3 As Double, ByVal Tank4 As Double, ByVal Tank5 As Double, ByVal Tank6 As Double, ByVal Tank7 As Double, ByVal Tank8 As Double, ByVal Works As String, ByVal Dates As String)
Dim sqlstr As String = "INSERT INTO BatchFinal (BatchID, BatchName, Tank1, Tank2, Tank3, Tank4, Tank5, Tank6, Tank7, Tank8, [Work], [Date])VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?)"
Dim cmd As New OleDbCommand(sqlstr, conn)
cmd.Parameters.AddWithValue("@BatchID", OleDbType.VarChar).Value = BatchID
cmd.Parameters.AddWithValue("@BatchName", OleDbType.VarChar).Value = BatchName
cmd.Parameters.AddWithValue("@Tank1", OleDbType.Double).Value = Tank1
cmd.Parameters.AddWithValue("@Tank2", OleDbType.Double).Value = Tank2
cmd.Parameters.AddWithValue("@Tank3", OleDbType.Double).Value = Tank3
cmd.Parameters.AddWithValue("@Tank4", OleDbType.Double).Value = Tank4
cmd.Parameters.AddWithValue("@Tank5", OleDbType.Double).Value = Tank5
cmd.Parameters.AddWithValue("@Tank6", OleDbType.Double).Value = Tank6
cmd.Parameters.AddWithValue("@Tank7", OleDbType.Double).Value = Tank7
cmd.Parameters.AddWithValue("@Tank8", OleDbType.Double).Value = Tank8
cmd.Parameters.AddWithValue("@[Work]", OleDbType.VarChar).Value = Works
cmd.Parameters.AddWithValue("@[Date]", OleDbType.VarChar).Value = Dates
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Sub
End Class