Results 1 to 5 of 5

Thread: Serial Relays Controller Using COMM Module

  1. #1

    Serial Relays Controller Using COMM Module

    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
    tb_ser4rel_gui.jpg

    Photo of SER-4REL Serial Relay Device
    ser4rel_640.jpg
    Last edited by ChandraMDE; 13-05-2013 at 11:40.

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    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
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    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.

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    Hi Chandra,

    I think the Comm_Open not generating Err is possible error, I made a bug report here.
    ThinBASIC Support 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
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  5. #5
    Thanks, Petr.
    To always close back what we have opened is a good programming habit.

Similar Threads

  1. can't opening comm
    By hendryawan in forum COMM (Serial communication)
    Replies: 15
    Last Post: 11-12-2012, 02:38
  2. USB to Serial emulator
    By zak in forum COMM (Serial communication)
    Replies: 1
    Last Post: 01-09-2011, 23:02
  3. Comm Module Comm_Recv
    By GKeely in forum COMM (Serial communication)
    Replies: 13
    Last Post: 13-03-2008, 19:01
  4. COMM Module?
    By Lee Allen in forum COMM (Serial communication)
    Replies: 5
    Last Post: 24-03-2007, 19:21
  5. Activating Problem , cant find serial number
    By Petr Schreiber in forum Installation
    Replies: 4
    Last Post: 17-06-2006, 12:49

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •