ChandraMDE
13-05-2013, 11:09
Hi, All.
Here is an example code using COMM Module for controlling SER-4REL serial device.
The device is waiting for data:
- "AAA" to activate RELAY-1 and "WWW" to deactivate RELAY-1
- "BBB" to activate RELAY-2 and "XXX" to deactivate RELAY-2
- "CCC" to activate RELAY-3 and "YYY" to deactivate RELAY-3
- "DDD" to activate RELAY-2 and "ZZZ" to deactivate RELAY-4
- "EEE" to activate ALL RELAY and "000" to deactivate ALL RELAY
Btw, the COMM_Open(hcomm, "COM1") is not generating error when opening an already opened serial port.
Please advice.
Thanks & Regards.
The Code:
' TB_SER4REL_GUI
' Start Date 05-13-2013
' Created by Chandra MDE - http://teknikelektrolinks.com
Uses "UI"
Uses "COMM"
Begin ControlID
%btnRelay1On, %btnRelay2On, %btnRelay3On, %btnRelay4On, %btnAllRelayOn
%btnRelay1Off, %btnRelay2Off, %btnRelay3Off, %btnRelay4Off, %btnAllRelayOff
%label1, %label2, %label3, %label4
End ControlID
Begin Const
%MAIN_WIDTH = 505
%MAIN_HEIGHT = 240
%BAUDRATE = 2400
End Const
Dim COMMPort As String = "COM1"
Dim hcomm As Long
Dim RelayOn(5) As String = "AAA", "BBB", "CCC", "DDD", "EEE"
Dim RelayOff(5) As String = "WWW", "XXX", "YYY", "ZZZ", "000"
Function TBMain()
Local hDlg As DWord
Dialog Font "Tahoma", 9
Dialog New Pixels, 0, "SER-4REL Controller Using COMM Module (@COM1)",-1,-1, %MAIN_WIDTH, %MAIN_HEIGHT,_
%WS_POPUP Or %WS_VISIBLE Or %WS_CAPTION Or %WS_SYSMENU Or %WS_MINIMIZEBOX To hDlg
Control Add Button, hDlg, %btnRelay1On, "RELAY-1 ON" , 15, 20, 100, 30, Call DoRelay()
Control Add Button, hDlg, %btnRelay1Off, "RELAY-1 OFF", 15, 55, 100, 30, Call DoRelay()
Control Add Button, hDlg, %btnRelay2On, "RELAY-2 ON" , 140, 20, 100, 30, Call DoRelay()
Control Add Button, hDlg, %btnRelay2Off, "RELAY-2 OFF", 140, 55, 100, 30, Call DoRelay()
Control Add Button, hDlg, %btnRelay3On, "RELAY-3 ON" , 265, 20, 100, 30, Call DoRelay()
Control Add Button, hDlg, %btnRelay3Off, "RELAY-3 OFF", 265, 55, 100, 30, Call DoRelay()
Control Add Button, hDlg, %btnRelay4On, "RELAY-4 ON" , 390, 20, 100, 30, Call DoRelay()
Control Add Button, hDlg, %btnRelay4Off, "RELAY-4 OFF", 390, 55, 100, 30, Call DoRelay()
Control Add Button, hDlg, %btnAllRelayOn, "ALL RELAY ON" , 15, 105, 225, 60, Call DoRelay()
Control Add Button, hDlg, %btnAllRelayOff, "ALL RELAY OFF", 265, 105, 225, 60, Call DoRelay()
Control Add Label, hDlg, %label1, "SER-4REL Controller by Chandra MDE", 20, 190, 300, 15
Control Add Label, hDlg, %label2, "Teknik Elektro Links - http://teknikelektrolinks.com", 20, 206, 300, 15
Control Add Label, hDlg, %label3, "[Created using ThinBASIC]", 340, 190, 300, 15
Control Add Label, hDlg, %label3, "1.9.6.0 and 1.8.9.0", 360, 206, 300, 15
Dialog Show Modal hDlg, Call cbDialog
End Function
CallBack Function cbDialog()
Select Case CBMSG
Case %WM_INITDIALOG
hcomm = COMM_FreeFile
COMM_Open("COM1", hComm)
COMM_Set(hcomm, %COMM_BAUD, %BAUDRATE)
If Err<>0 Then
MsgBox 0, "Error opening COM1"
End
End If
Case %WM_COMMAND
Case %WM_CLOSE
End Select
End Function
CallBack Sub DoRelay()
Local ID As DWord
If Callback_Message=%WM_COMMAND And Callback_Control_Message = %BN_CLICKED Then
ID = Callback_Control - %btnRelay1On + 1
If Inside(ID, 1, 5) Then
COMM_Send(hcomm, RelayOn(ID))
Else
COMM_Send(hcomm, RelayOff(ID-5))
End If
End If
End Sub
Screenshot
8278
Photo of SER-4REL Serial Relay Device
8277
Here is an example code using COMM Module for controlling SER-4REL serial device.
The device is waiting for data:
- "AAA" to activate RELAY-1 and "WWW" to deactivate RELAY-1
- "BBB" to activate RELAY-2 and "XXX" to deactivate RELAY-2
- "CCC" to activate RELAY-3 and "YYY" to deactivate RELAY-3
- "DDD" to activate RELAY-2 and "ZZZ" to deactivate RELAY-4
- "EEE" to activate ALL RELAY and "000" to deactivate ALL RELAY
Btw, the COMM_Open(hcomm, "COM1") is not generating error when opening an already opened serial port.
Please advice.
Thanks & Regards.
The Code:
' TB_SER4REL_GUI
' Start Date 05-13-2013
' Created by Chandra MDE - http://teknikelektrolinks.com
Uses "UI"
Uses "COMM"
Begin ControlID
%btnRelay1On, %btnRelay2On, %btnRelay3On, %btnRelay4On, %btnAllRelayOn
%btnRelay1Off, %btnRelay2Off, %btnRelay3Off, %btnRelay4Off, %btnAllRelayOff
%label1, %label2, %label3, %label4
End ControlID
Begin Const
%MAIN_WIDTH = 505
%MAIN_HEIGHT = 240
%BAUDRATE = 2400
End Const
Dim COMMPort As String = "COM1"
Dim hcomm As Long
Dim RelayOn(5) As String = "AAA", "BBB", "CCC", "DDD", "EEE"
Dim RelayOff(5) As String = "WWW", "XXX", "YYY", "ZZZ", "000"
Function TBMain()
Local hDlg As DWord
Dialog Font "Tahoma", 9
Dialog New Pixels, 0, "SER-4REL Controller Using COMM Module (@COM1)",-1,-1, %MAIN_WIDTH, %MAIN_HEIGHT,_
%WS_POPUP Or %WS_VISIBLE Or %WS_CAPTION Or %WS_SYSMENU Or %WS_MINIMIZEBOX To hDlg
Control Add Button, hDlg, %btnRelay1On, "RELAY-1 ON" , 15, 20, 100, 30, Call DoRelay()
Control Add Button, hDlg, %btnRelay1Off, "RELAY-1 OFF", 15, 55, 100, 30, Call DoRelay()
Control Add Button, hDlg, %btnRelay2On, "RELAY-2 ON" , 140, 20, 100, 30, Call DoRelay()
Control Add Button, hDlg, %btnRelay2Off, "RELAY-2 OFF", 140, 55, 100, 30, Call DoRelay()
Control Add Button, hDlg, %btnRelay3On, "RELAY-3 ON" , 265, 20, 100, 30, Call DoRelay()
Control Add Button, hDlg, %btnRelay3Off, "RELAY-3 OFF", 265, 55, 100, 30, Call DoRelay()
Control Add Button, hDlg, %btnRelay4On, "RELAY-4 ON" , 390, 20, 100, 30, Call DoRelay()
Control Add Button, hDlg, %btnRelay4Off, "RELAY-4 OFF", 390, 55, 100, 30, Call DoRelay()
Control Add Button, hDlg, %btnAllRelayOn, "ALL RELAY ON" , 15, 105, 225, 60, Call DoRelay()
Control Add Button, hDlg, %btnAllRelayOff, "ALL RELAY OFF", 265, 105, 225, 60, Call DoRelay()
Control Add Label, hDlg, %label1, "SER-4REL Controller by Chandra MDE", 20, 190, 300, 15
Control Add Label, hDlg, %label2, "Teknik Elektro Links - http://teknikelektrolinks.com", 20, 206, 300, 15
Control Add Label, hDlg, %label3, "[Created using ThinBASIC]", 340, 190, 300, 15
Control Add Label, hDlg, %label3, "1.9.6.0 and 1.8.9.0", 360, 206, 300, 15
Dialog Show Modal hDlg, Call cbDialog
End Function
CallBack Function cbDialog()
Select Case CBMSG
Case %WM_INITDIALOG
hcomm = COMM_FreeFile
COMM_Open("COM1", hComm)
COMM_Set(hcomm, %COMM_BAUD, %BAUDRATE)
If Err<>0 Then
MsgBox 0, "Error opening COM1"
End
End If
Case %WM_COMMAND
Case %WM_CLOSE
End Select
End Function
CallBack Sub DoRelay()
Local ID As DWord
If Callback_Message=%WM_COMMAND And Callback_Control_Message = %BN_CLICKED Then
ID = Callback_Control - %btnRelay1On + 1
If Inside(ID, 1, 5) Then
COMM_Send(hcomm, RelayOn(ID))
Else
COMM_Send(hcomm, RelayOff(ID-5))
End If
End If
End Sub
Screenshot
8278
Photo of SER-4REL Serial Relay Device
8277