Calling ByRef in Task's
Posted: 22 September 2009, 15:11 PM
Hi!
Trying to control relay's with Netmedia LCD+. ZBasic doc says it's not recommended to pass value to task's ByRef. Code below works if I pass a value ByREf in an ordinary Sub but not in a task. Tried to preserve the RelayStatus value in a tmpRelayStatus variable but still not working. Could anyone give me a hint how to get Byref to work in a task. Tried to solve the problem by reading the doc's/forum but I'm stucked. Any help would be appreciated
Regards Hucke
[/code]
test.bas
'***************************''''
'
'
'
'
'
Sub Main
RelaysALL_OFF' set relay's off and initialize 'tmpRelayStatus' to &H00
Do
'#### DON't WORK
CallTask SwitchRelay(tmpRelayStatus,3,1),RelayStack
CallTask SwitchRelay(tmpRelayStatus,4,1),RelayStack
'#### WORKING
call SwitchRelay(tmpRelayStatus,2,1)
call SwitchRelay(tmpRelayStatus,1,1)
Loop
End Sub
' Relay_1.Bas
'
' Public Sub Relays_OFF()- turn OFF ALL relays
' Sub SwitchRelay(ByRef RelayStatus as Byte, ByVal RelayNumber as Byte)
'
'Call RelaysALL_OFF() in Sub Main to set all relay's off as default and to set 'tmpRelayStatus'
Public RelayMsg(0 to 7) As String
Public RelayStack(1 To 73) As Byte
Public tmpRelayStatus As Byte 'to hold value of RelayStatus
'********************************************************************************************
Sub SwitchRelay(Byref RelayStat as Byte,ByVal RelayNumber as Byte,ByVal tmpStatus As Byte)
' Turns ON/OFF specified relay 1-8
If RelayNumber > 8 Then
Exit Sub
End If
Select Case tmpStatus
Case 1'On
Call PutBit(RelayStat, RelayNumber-1, 1)
Case 0'Off
Call PutBit(RelayStat, RelayNumber-1, 0)
End Select
Call PutByte_3(&H12)
Call PutByte_3(RelayStat XOR &Hff)
Relay_Msg(RelayStat)
Call Delay(0.0)
End Sub
Public Sub RelaysALL_OFF()
Dim RelayStatus As Byte
RelayStatus = &H00
Call PutByte_3(&H12) ' relay command
Call PutB_3(RelayStatus XOR &Hff) ' turn OFF all relay's
Call Relay_Msg(RelayStatus)
tmpRelayStatus = RelayStatus
End Sub
Public Sub Relay_Msg(ByRef RelayStatus As Byte)
Dim X As Integer
Dim Y As Integer
Dim TmpBitStatus As Byte
Dim tmpMsg As String
Dim tmpPos As Byte
RelayMsg(0) = "Pump = "
RelayMsg(1) = "Chlor = "
RelayMsg(2) = "VP = "
RelayMsg(3) = "Sun = "
RelayMsg(4) = "Light1 ="
RelayMsg(5) = "Light2 ="
RelayMsg(6) = "Light3 ="
RelayMsg(7) = "Light4 ="
tmpPos = 0
For X = 0 To 7
TmpBitStatus = GetBit(RelayStatus, X)
tmpMsg = RelayMsg(X) & Cstr(TmpBitStatus)
If tmpPos > 79 Then
tmpPos = 0
End If
LCDSetCursorPosition(tmpPos)
PutStr_3 tmpMsg
tmpPos = tmpPos + 10
Next
End Sub
Trying to control relay's with Netmedia LCD+. ZBasic doc says it's not recommended to pass value to task's ByRef. Code below works if I pass a value ByREf in an ordinary Sub but not in a task. Tried to preserve the RelayStatus value in a tmpRelayStatus variable but still not working. Could anyone give me a hint how to get Byref to work in a task. Tried to solve the problem by reading the doc's/forum but I'm stucked. Any help would be appreciated
Regards Hucke
[/code]
test.bas
'***************************''''
'
'
'
'
'
Sub Main
RelaysALL_OFF' set relay's off and initialize 'tmpRelayStatus' to &H00
Do
'#### DON't WORK
CallTask SwitchRelay(tmpRelayStatus,3,1),RelayStack
CallTask SwitchRelay(tmpRelayStatus,4,1),RelayStack
'#### WORKING
call SwitchRelay(tmpRelayStatus,2,1)
call SwitchRelay(tmpRelayStatus,1,1)
Loop
End Sub
' Relay_1.Bas
'
' Public Sub Relays_OFF()- turn OFF ALL relays
' Sub SwitchRelay(ByRef RelayStatus as Byte, ByVal RelayNumber as Byte)
'
'Call RelaysALL_OFF() in Sub Main to set all relay's off as default and to set 'tmpRelayStatus'
Public RelayMsg(0 to 7) As String
Public RelayStack(1 To 73) As Byte
Public tmpRelayStatus As Byte 'to hold value of RelayStatus
'********************************************************************************************
Sub SwitchRelay(Byref RelayStat as Byte,ByVal RelayNumber as Byte,ByVal tmpStatus As Byte)
' Turns ON/OFF specified relay 1-8
If RelayNumber > 8 Then
Exit Sub
End If
Select Case tmpStatus
Case 1'On
Call PutBit(RelayStat, RelayNumber-1, 1)
Case 0'Off
Call PutBit(RelayStat, RelayNumber-1, 0)
End Select
Call PutByte_3(&H12)
Call PutByte_3(RelayStat XOR &Hff)
Relay_Msg(RelayStat)
Call Delay(0.0)
End Sub
Public Sub RelaysALL_OFF()
Dim RelayStatus As Byte
RelayStatus = &H00
Call PutByte_3(&H12) ' relay command
Call PutB_3(RelayStatus XOR &Hff) ' turn OFF all relay's
Call Relay_Msg(RelayStatus)
tmpRelayStatus = RelayStatus
End Sub
Public Sub Relay_Msg(ByRef RelayStatus As Byte)
Dim X As Integer
Dim Y As Integer
Dim TmpBitStatus As Byte
Dim tmpMsg As String
Dim tmpPos As Byte
RelayMsg(0) = "Pump = "
RelayMsg(1) = "Chlor = "
RelayMsg(2) = "VP = "
RelayMsg(3) = "Sun = "
RelayMsg(4) = "Light1 ="
RelayMsg(5) = "Light2 ="
RelayMsg(6) = "Light3 ="
RelayMsg(7) = "Light4 ="
tmpPos = 0
For X = 0 To 7
TmpBitStatus = GetBit(RelayStatus, X)
tmpMsg = RelayMsg(X) & Cstr(TmpBitStatus)
If tmpPos > 79 Then
tmpPos = 0
End If
LCDSetCursorPosition(tmpPos)
PutStr_3 tmpMsg
tmpPos = tmpPos + 10
Next
End Sub