Imports System
Imports System.IO
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports System.Threading
Imports System.Data.SqlClient
Imports System.Globalization
Namespace WinTr
Public Class MainClass
'Timer object created.
Private oTimer As System.Threading.Timer
Dim bayt(3) As Byte
Dim seri As New System.IO.Ports.SerialPort
Dim str As String
Public Tank_Level As Single
Public Sub Load
'Serial port connection is opened and Query is sent when script runs.
Try
seri.Encoding = System.Text.Encoding.Default
seri.BaudRate = 9600
seri.Parity = IO.Ports.Parity.None
seri.PortName = "Com10"
seri.Open()
seri.Write("3")
'The asynchronous function runs by using timer. This reads response
'after 5000 milisecond from serial port.
'Then writes tag values to SQL Inp/Out tags on the SQL Server.
Dim oCallback As New TimerCallback(AddressOf OnTimedEvent)
oTimer = New System.Threading.Timer(oCallback, Nothing, 5000, 100)
Catch ex As Exception
MessageBox.Show(Ex.ToString, "Her Dakika Script" & Ex.Message)
End Try
End Sub
'Asynchronous function is defining
Private Sub OnTimedEvent(ByVal state As Object)
Try
'Because of Asynchronous function run once in a minute,
' timer object is disposed after first run.
oTimer.Dispose()
'Data that read from serial port assigned to str variable
str = seri.ReadExisting
'serial port closing
seri.Close()
'SQL Server connection object is creating.
Dim SQL_cn As New SqlConnection()
SQL_cn.ConnectionString = "Server=(local)\WinTr;uid=sa;pwd=12341234;database=Hamidiye_Scada"
'Connecting to SQL Server
SQL_cn.Open()
Dim SQL_val As String
Dim SQL_val_2 As String
Dim SQL_Ins As String
Dim Tank_Level_ As String
Dim Tank_Urt_Syc As String
Dim Tank_CnFault As Boolean
'Data divided into parts and parts assigned to related variables.
If str.Length = 8 Then 'if string length is 8 then continuing.
'Level Data
bayt(3) = Asc(Mid(str, 1, 1))
bayt(2) = Asc(Mid(str, 2, 1))
bayt(1) = Asc(Mid(str, 3, 1))
bayt(0) = Asc(Mid(str, 4, 1))
Tank_Level_ = BitConverter.ToSingle(bayt, 0)
'other data
bayt(3) = Asc(Mid(str, 6, 1))
bayt(2) = Asc(Mid(str, 7, 1))
bayt(1) = Asc(Mid(str, 8, 1))
bayt(0) = Asc(Mid(str, 9, 1))
Tank_Urt_Syc = BitConverter.ToSingle(bayt, 0)
Tank_CnFault = False
SQL_val = Tank_Level_.ToString("G", CultureInfo.CreateSpecificCulture("en-US"))
SQL_val_2 = Tank_Urt_Syc.ToString("G", CultureInfo.CreateSpecificCulture("en-US"))
'New values writing to first row of SQL server.
SQL_Ins = "UPDATE Table1 SET [Tank_Level]='" & SQL_val & "',[Tank_Urt_Syc]='" & SQL_val_2 & "',[Tank_CnFault]='" & Tank_CnFault & "' WHERE [LastValue] = 1"
Dim SQL_cmdnon As SqlCommand = New SqlCommand(SQL_Ins, SQL_cn)
SQL_cmdnon.ExecuteNonQuery()
Else 'if string length is not 8 Tank_CnFault tag made true.
Tank_CnFault = True
SQL_Ins = "UPDATE Table1 SET [Tank_CnFault]='" & Tank_CnFault & "' WHERE [LastValue] = 1"
Dim SQL_cmdnon As SqlCommand = New SqlCommand(SQL_Ins, SQL_cn)
SQL_cmdnon.ExecuteNonQuery()
End If
SQL_cn.Close()
Catch ex As Exception
MessageBox.Show(Ex.ToString, "Her Dakika Script" & Ex.Message)
End Try
End Sub
End Class
End Namespace