User Tools

Site Tools


k20:k20.6:k20.6.2:start

20.6.2 Task projects 3 and 4

The realisation of the data transmission is interesting in both projects. Although both projects send the data with the print or error statement, project 4 focuses on the serialisation and de-serialisation functions that enable the transmission of data with different native data types.

20.6.2.1 Project 3

Project 3 is a variant of project 1. Data is continuously sent to the (main) programme from the started task.

The day of the week, the current date and the local time for New York are calculated one after the other with a fixed clock pulse and sent to the (main) programme as a string. The clock time is passed once to the task as an argument.

B1

Figure 20.6.2.1.1: Normal operation with analogue and digital (local) time

Figure 20.6.2.1.2: Main programme with started task

You can read the source code of the (task) class TripleTask.class in full:

  ' Gambas class file
 
  Inherits Task
 
' Start-Argument für die Wartezeit zwischen den drei Anzeigen
  Public iWaitTime As Integer
 
  Public Sub Main()
    Dim aTagesListe As String[]
    Dim dTNY, dDate As Date
 
    aTagesListe = Split("Sonntag,Montag,Dienstag,Mittwoch,Donnerstag,Freitag,Samstag", ",")
 
'-- Continuous but clocked output of data (Task → Programme)
    Do
      dDate = Now() ' Zeitstempel
      Print aTagesListe[WeekDay(dDate)] & "  (Tag " & Str(WeekDay(dDate)) & " der Woche)" '-- DataType = String
      Wait iWaitTime ' Wartezeit
      Print Format$(dDate, "dd. mmmmm yyyy") '-- DataType = String
      Wait iWaitTime
      dTNY = Time(DateAdd(Now, gb.Hour, -6)) ' NewYork-LocalTime
      Print "New York:  " & Format(dTNY, "hh:nn") & " Uhr" '-- DataType = String
      Wait iWaitTime
    Loop
 
  End

The source code for the (main) programme is only given in excerpts:

Public Sub btnTaskStart_Click()
  If $hTask Then $hTask = Null '-- An existing task object is destroyed
  $hTask = New TripleTask As "TripleTask" '-- => Task class name
  $hTask.iWaitTime = 3 '-- Assignment of a global variable in the class TripleTask
...
End
 
Public Sub TripleTask_Read(Data As String)
  lblAdditionalInformations.Text = Replace(Data, gb.NewLine, "")
End
 
Public Sub TripleTask_Error(Data As String)
 lblAdditionalInformations.Text = Replace(Data, gb.NewLine, "")
End
 
Public Sub TripleTask_Kill()
  SetLEDColor("red")
  btnTaskStopp.Enabled = False
  FMain.Text = "TASK"
  lblAdditionalInformations.Text = "Task error!"
End

20.6.2.2 Project 4

At the heart of project 4 is this module:

  ' Gambas module file
 
  Public Function Encode(vValue As Variant) As String
    Dim hStream As Stream
    Dim sBinary As String
 
    hStream = Open String For Write '-- Dynamically growing buffer
    Write #hStream, vValue As Variant
    sBinary = Close #hStream
    Return Base64$(sBinary) '-- Newline-free coding
  End
 
  Public Sub Send(vValue As Variant)
    Print Encode(vValue)
  End
 
  Public Function Decode(sCode As String) As Variant
    Dim sBinary As String
    Dim hStream As Stream
    Dim vValue As Variant
 
    sBinary = UnBase64$(sCode)
    hStream = Open String sBinary For Read
    vValue = Read #hStream As Variant
    Return vValue
 
  End

The module's power will only become apparent to you if you try out Project 4 intensively and send data with different data types - for example, string, boolean, float or collection - to the higher-level process in the task class.

' Gambas class file
 
Inherits Task
 
Public Sub Main()
 
  Do
    TaskIPC.Send(22 / 7)
    TaskIPC.Send(["Now": Now(), "timer": Timer()])
    TaskIPC.Send(["Key": "stringvalue", "log2": Log(2), "YD": DateAdd(Now, gb.Day, -1), "User": User.Name])
    TaskIPC.Send([Pi(3), (1001 / 1000) ^ 1000, 10, (355 / 113) - Pi])
    TaskIPC.Send(["Produkt = ": 6.66 * 34.78])
    TaskIPC.Send(6 < Day(Now()))
    TaskIPC.Send("Task - Background process")
    Wait 2
  Loop
 
End

Figure 20.6.2.2.1: Demonstration programs

Some data changes periodically (time values), while others do not change - like the string 'Task - Background Process'.

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k20/k20.6/k20.6.2/start.txt · Last modified: 22.10.2023 by emma

Page Tools