PDA

View Full Version : Serial Relays Controller Using COMM Module



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

Petr Schreiber
14-05-2013, 08:42
Hi Chandra,

I don't know about the errors, but I think you should close the port before the application ends. You can do it in the WM_CLOSE handling inside callback, like:


...

Case %WM_CLOSE
COMM_Close(hComm)
...


Optional tip: Multiple used modules
In case your script uses multiple modules, you can do it also this way, with single Uses:


Uses "Console", "COMM"



Petr

ChandraMDE
14-05-2013, 09:19
Thanks, Petr.
The thing is, the COMM_Open() is not generating an error when the serial port is already used by another application.
So, the error trap in WM_INITDIALOG below is never executed, even when it should be.


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

And I have been testing it to open a non-exist serial port number, but still the COMM_Open() is not generating any error.

About the COMM_Close() function, I found no trouble for not closing the serial port in WM_CLOSE. I'm not sure about this, so correct me if I'm wrong. Do you think the garbace collector of ThinBASIC has done it?

Please advice.

Petr Schreiber
14-05-2013, 09:33
Hi Chandra,

I think the Comm_Open not generating Err is possible error, I made a bug report here (http://www.thinbasic.com/community/project.php?issueid=399).
ThinBASIC Support (http://www.thinbasic.com/community/project.php) section is place, where you can report bugs and post ideas for new features - it keeps everything organised, so we don't forget about the issues and users can track if problem is solved.

Regarding Comm_Close - maybe it is not necessary, but I tend to close everything I open :)


Petr

ChandraMDE
14-05-2013, 09:44
Thanks, Petr.
To always close back what we have opened is a good programming habit. :)