Sample Script: Script for Asynchronous communication with remote GSM modem over Internet using virtual Com port.

This script queries data from remote GSM modem by running each minute in the WinTr.(see Control Panel - General Setup - Script Setup)
Script, has asynchronous working structure. In this way WinTr continues working on other tasks while script waits response from GSM Modem.
When tag values fetched from GSM Modem by script, they written to SQL Inp/Out tags in the SQL Server. This tag values can be monitored on WinTr Screen at Real Time.
GSM Modem accessed by virtual Com10 port.
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.
               seri.Encoding = System.Text.Encoding.Default
               seri.BaudRate = 9600
               seri.Parity = IO.Ports.Parity.None
               seri.PortName = "Com10"
               '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)

               'Because of Asynchronous function run once in a minute,
               ' timer object is disposed after first run.

               'Data that read from serial port assigned to str variable
               str = seri.ReadExisting

               'serial port closing
               '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
               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)

               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)
       End If
      Catch ex As Exception
        MessageBox.Show(Ex.ToString, "Her Dakika Script" & Ex.Message)                
      End Try
       End Sub

End Class
End Namespace

                                                                                                                                               GSM Modem ile Asenkron İletişim Scripti

                                                                                                                                                                               English                Turkish
Scada System        Scada Software        Free Scada            What is Scada