www.homeseer.com    
 

Go Back   HomeSeer Message Board > Developer Support > Scripts & Plug-Ins:Development and Libraries > HS3 Scripting

HS3 Scripting Discussions related to the scripting under HS3.

Reply
 
Thread Tools Display Modes
  #1  
Old June 14th, 2018, 10:50 AM
huntley1 huntley1 is offline
Seer Deluxe
 
Join Date: Jul 2016
Location: Canada
Posts: 164
Is it wise to leave COM ports open in scripts

Hi,

I have a lighting system that a customer wants to integrate with Homeseer. There is no plug-in for the lighting system. It's called Wattstopper by Legrand. It does have a serial module and I've got a list of the commands for. Its' using ASCII. My plan is to use scripts to send and receive data to the Wattstopper serial module. My question is is it wise to leave the COM port open at all times when not sending a command or receiving a response. Also, there is a command for requesting the status of a giving light. To get a "real-time" like feedback it is wise to keep sending the command to request light status every second or two? By doing this will I crash HS3?

Thanks
Reply With Quote
  #2  
Old June 14th, 2018, 11:07 AM
freeon freeon is offline
Seer Master
 
Join Date: Aug 2012
Location: Canada
Posts: 702
I have 3 devices connected via serial and I modified HS startup script to open all 3 ports on startup and they remain open all the time. Been like this for several years with no issue. Before this I had issues sometimes that I would get error saying port is already in use by another device. This has solved it. I don't see any downside to doing this, someone else might.
Reply With Quote
  #3  
Old June 14th, 2018, 12:37 PM
Uncle Michael's Avatar
Uncle Michael Uncle Michael is offline
OverSeer
 
Join Date: Feb 2006
Location: Rochester, NY
Posts: 5,937
Quote:
Originally Posted by freeon View Post
I have 3 devices connected via serial and I modified HS startup script to open all 3 ports on startup and they remain open all the time. Been like this for several years with no issue. . . . I don't see any downside to doing this. . .
I do the same. No issues at all. I would definitely recommend you open the port with your Startup script and leave it open as long is HS is running.
__________________
Mike____________________________________________________________ __________________
HS3 Pro Edition 3.0.0.435

HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF, Rain8Net+ | RFXCOM | QSE100D | Vantage Pro | Green-Eye | X10:
Compose, XTB-232, -IIR | Edgeport/8 | Way2Call | Ecobee3

Reply With Quote
  #4  
Old June 14th, 2018, 02:25 PM
huntley1 huntley1 is offline
Seer Deluxe
 
Join Date: Jul 2016
Location: Canada
Posts: 164
Thanks you guys!

I will do the same
Reply With Quote
  #5  
Old June 15th, 2018, 01:19 AM
huntley1 huntley1 is offline
Seer Deluxe
 
Join Date: Jul 2016
Location: Canada
Posts: 164
How do I define which port to open in the startup script?

Is it COMPort.PortName = "COM2"? I running HS3 on a Windows PC. If that matters.

Also, in the event that two commands gets sent through the serial port at the same time from two different scripts what would happen?

Is it wise to call for the com port to open if it's "suppose to open" already. How do I know if the port is open or not?
Reply With Quote
  #6  
Old June 15th, 2018, 10:19 AM
AshaiRey AshaiRey is offline
Seer Deluxe
 
Join Date: May 2009
Location: Netherlands
Posts: 436
Here some info how to do it in a VB script
Code:
   Dim e As String = ""
    'the third hs.OpenComPortTerm parameter you specify either a 1 or a 0 to 
    'return data either in raw or string mode

    '0 = raw mode
    'In this mode, each character that is received on the COM port causes 
    'the specified script and function to be called.  It is up to the called 
    'function to callGetComPortData to actually get the characters.

    '1 = strings mode
    'This mode buffers up characters until a terminator is received.  
    'At this point the specified script and function are called with the data.  
    'This mode makes it easy to deal with devices that send text data terminated 
    'with known characters.  To specify the terminator characters, 
    'see the term parameter description below.  
    'If you do not specify a terminator, the default terminator of 
    'carriage return and line-feed (CrLf) are used.

    e = hs.OpenComPortTerm(SERIALPORTNUMBER, CONNECTIONSTRING, 1, strActionScriptName, "Handle_Event", VbCrLf)
__________________
- Bram

Send from my Commodore VIC-20

Ashai_Rey____________________________________________________________ ________________
HSPro 3.5.0
PIugins: ZMC audio | ZMC VR | ZMC IR | ZMC NDS | RFXcom | AZ scripts | Jon00 Scripts | RFXCOM | BLRadar | BLBackup
Reply With Quote
  #7  
Old June 15th, 2018, 11:15 AM
Uncle Michael's Avatar
Uncle Michael Uncle Michael is offline
OverSeer
 
Join Date: Feb 2006
Location: Rochester, NY
Posts: 5,937
Quote:
Originally Posted by huntley1 View Post
How do I define which port to open in the startup script?
In the HS Help file, under Scripting, Computer, Serial Port Communication, see instructions for "OpenComPort". The port number is the first term in the command.
Quote:
Also, in the event that two commands gets sent through the serial port at the same time from two different scripts what would happen?
I'm not sure what you mean. Can you describe how you envision this happening? What is the structure of the data stream you want to receive?
Quote:
Is it wise to call for the com port to open if it's "suppose to open" already. How do I know if the port is open or not?
The port has to be explicitly 'opened' by a process. Do you expect some other process to open the port besides HS? Under normal circumstances only one process can have access to the the port at one time. Attempting to open a port that is already open in another process will result in an error, and there will be a message in the log.
Reply With Quote
  #8  
Old June 16th, 2018, 07:22 AM
huntley1 huntley1 is offline
Seer Deluxe
 
Join Date: Jul 2016
Location: Canada
Posts: 164
Quote:
I'm not sure what you mean. Can you describe how you envision this happening? What is the structure of the data stream you want to receive?
My thoughts is to have two scripts. One sending commands. For example, setting light 1 to 50%. The other is to send a command to receive the status of each light and wait for the responses. My plan is to have the second script execute or loop every second to achieve a "real-time" like feedback status of the lights. In the event a command is sent to set the light and recieve the status of the lights at the same, what would happen? Would I have you disable or stop the loop of the second script when trying to set/control a light? Am I over thinking this?

Quote:
The port has to be explicitly 'opened' by a process. Do you expect some other process to open the port besides HS? Under normal circumstances only one process can have access to the the port at one time. Attempting to open a port that is already open in another process will result in an error, and there will be a message in the log.
No, the port will not be used my another device or program. I was planning to open the port in the startup script, but also open it in the the lighting scripts. Just to make sure that it is opened. Or is the a check that I can do in my lighting script to make sure the port is open before trying to send commands?
Reply With Quote
  #9  
Old June 16th, 2018, 12:20 PM
Uncle Michael's Avatar
Uncle Michael Uncle Michael is offline
OverSeer
 
Join Date: Feb 2006
Location: Rochester, NY
Posts: 5,937
Quote:
Originally Posted by huntley1 View Post
My thoughts is to have two scripts. One sending commands. For example, setting light 1 to 50%. The other is to send a command to receive the status of each light and wait for the responses. My plan is to have the second script execute or loop every second
It might be prudent to pause your recurring query when executing a command. One way to do that would be to turn on a virtual device before sending a command, then turn if off after the command is sent, and use the state of the device as a condition on the recurring event to query status. (Getting status every second may be overkill. It will also depend on the response time of your external device to your query.)
Quote:
I was planning to open the port in the startup script, but also open it in the the lighting scripts. Just to make sure that it is opened.
In my experience, once HS opens the port it remains open reliably.
Reply With Quote
  #10  
Old Yesterday, 12:14 AM
huntley1 huntley1 is offline
Seer Deluxe
 
Join Date: Jul 2016
Location: Canada
Posts: 164
Thanks for all your help!

So basically I opened the COM port in the startup script as suggest. I have a script that listens for any response from the device. If the response is what I'm looking for I parse the string to pickout the data I need.

Instead of polling for a response every second, my script only executes when the device sends a command. It works pretty slick
Reply With Quote
  #11  
Old Yesterday, 10:16 AM
Uncle Michael's Avatar
Uncle Michael Uncle Michael is offline
OverSeer
 
Join Date: Feb 2006
Location: Rochester, NY
Posts: 5,937
Quote:
Originally Posted by huntley1 View Post
Instead of polling for a response every second, my script only executes when the device sends a command. It works pretty slick
That's great. From your earlier posts I assumed you had to initiate the data send from your device with a query from HS. Since you don't, waiting for the data to arrive and then parsing it is definitely the way to go. That's how I handle my serial link as well.

Does your device send a termination string? If it does, using 'hs.OpenComPortTerm' and 'String mode' (1) makes the process even easier.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Open Ports mikaluch HomeSeer General Discussion Area 6 June 1st, 2016 05:13 PM
What ports to open? zimmer62 Bluetooth Connector 5 January 9th, 2007 04:24 AM
Lets make HS wise as to what resources are available Gogs Script & Plug-In Development 9 August 7th, 2006 10:49 AM
Is there a tool to find out what ports are open at work? Rupp Web Server and Interface 5 July 6th, 2002 01:10 PM
Is there a tool to find out what ports are open at work? grhughes Web Server and Interface 0 January 1st, 1970 12:00 AM


All times are GMT -4. The time now is 04:41 PM.


Copyright HomeSeer Technologies, LLC