Command line to open CoolTerm instance connected?

If you have specific questions or problems with any of my Freeware applications, post them here.
jmuhammad
Posts: 21
Joined: Wed Aug 17, 2022 10:21 pm

Command line to open CoolTerm instance connected?

Post by jmuhammad »

Greetings,

Is there a way via command line to open CoolTerm instance connected?
Like "com04.cooltermsettings --connected"

For now I am using a compiled ahk script to activate the desired window, check for word "Disconnected" then send CTRL+K.
User avatar
roger
Site Admin
Posts: 536
Joined: Fri Apr 24, 2009 12:41 am
Contact:

Re: Command line to open CoolTerm instance connected?

Post by roger »

Hi,

CoolTerm is scriptable. Take a look at the built-in help and navigate to "CoolTerm Scripting" for more information. There is a simple TCP/IP based protocol it implements for that, and if you can send TCP packets from your AHK script, then you can get CoolTerm to do pretty much anything. This wrapper should be helpful: https://github.com/jleb/AHKsock

Alternatively, there is a Python module that implements all the scripting commands CoolTerm currently supports. It's included with the CoolTerm download, along with a number of example Python script that show how to control CoolTerm. You can have your AHK execute a Python script to achieve what you need.

The scripting command set includes a command that lets you activate a specific CoolTerm window (SET_FRONTMOSTWINDOW), where you pass the ID of the desired window as argument. And there is another command (GET_WINDOW_ID_FROM_NAME) that lets you look up the ID from the window name. With those two commands, you can specify the name of the desired CoolTerm instance and bring it to the foreground. Then you can use the "CONNECT" command to connect. Note that it's not necessary to bring the instance to the front in order to connect because CONNECT takes the window ID as argument to specify which instance you want to connect. This works even if the instance is not in front.

I hope this helps. Let me know if you have any questions.

Roger
jmuhammad
Posts: 21
Joined: Wed Aug 17, 2022 10:21 pm

Re: Command line to open CoolTerm instance connected?

Post by jmuhammad »

OK, thanks. Ideally I wanted to do everything in Python; I did not want to use AHK...I use it because if used Python to just sent CTRL+K on a discovered window then it could toggle CoolTerm off. So I just use AHK for two things: 1) Check for existence of CoolTerm window with discovered COM# in its title; 2) If the CoolTerm form has "Disconnected" then send CTRL+K.
---
I tried Example2 just to load the settings file. FYI, I have "CoolTerm.py", "ex2_loading_connection_ settings.py", "Settings_COM05.cooltermsettings" in same folder:

Code: Select all

import CoolTerm
import sys
s = CoolTerm.CoolTermSocket()

FilePath = "Settings_COM05.cooltermsettings" # Load connection settings file.
ID = s.LoadSetting(FilePath)
if ID < 0:
    print("The settings could not be loaded")
    sys.exit()
input("The settings have been loaded successfully. Press ENTER to close the window")
s.CloseWindow(ID) # Close the window
s.Close() # Disconnect from CoolTerm
I get this error:

Code: Select all

ERROR: Could not connect to CoolTerm
Traceback (most recent call last):
  File "\my-coolterm\ex2_loading_connection_ settings.py", line 9, in <module>
    ID = s.LoadSetting(FilePath)
  File "\my-coolterm\CoolTerm.py", line 352, in LoadSetting
    Response = self._SendPacket(Packet)
  File "\my-coolterm\CoolTerm.py", line 283, in _SendPacket
    self.skt.sendall(Packet)
OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
User avatar
roger
Site Admin
Posts: 536
Joined: Fri Apr 24, 2009 12:41 am
Contact:

Re: Command line to open CoolTerm instance connected?

Post by roger »

Make sure you enable the remote control socket in the Preferences under "Scripting". The socket is disabled by default.
jmuhammad
Posts: 21
Joined: Wed Aug 17, 2022 10:21 pm

Re: Command line to open CoolTerm instance connected?

Post by jmuhammad »

^ Same error(s) w/ remote control socket in the Preferences under "Scripting" enabled.
User avatar
roger
Site Admin
Posts: 536
Joined: Fri Apr 24, 2009 12:41 am
Contact:

Re: Command line to open CoolTerm instance connected?

Post by roger »

What port number is the remote control socket set to in the Preferences? The default is 51413, and that is what CoolTerm.py uses by default. If it's set to something else, then you need to either set it to 51413 in the CoolTerm Preferences, or adjust your script to use what the setting is in CoolTerm, like this, where YOUR_PORT_SETTING is the port number that's specified for the remote control socket in the CoolTerm Preferences:

Code: Select all

import CoolTerm
s = CoolTerm.CoolTermSocket("127.0.0.1", YOUR_PORT_SETTING)
print(s.CoolTermVersion())
jmuhammad
Posts: 21
Joined: Wed Aug 17, 2022 10:21 pm

Re: Command line to open CoolTerm instance connected?

Post by jmuhammad »

It is set to 51413.
I also tried the 's = CoolTerm.CoolTermSocket("127.0.0.1", YOUR_PORT_SETTING)'; it never gets past this line.
FYI, I have Python 3.10.11 and Python 3.12.6 installed, and CoolTerm Version 2.10 (Build 14251) 64-bit

I can zip the whole folder and send it to you. It has:
H:\mycoolterm\CoolTerm.py
H:\mycoolterm\CoolTerm_Prefs.plist
H:\mycoolterm\ex2_loading_connection_settings.py
H:\mycoolterm\Settings_COM05.cooltermsettings
User avatar
roger
Site Admin
Posts: 536
Joined: Fri Apr 24, 2009 12:41 am
Contact:

Re: Command line to open CoolTerm instance connected?

Post by roger »

Let's check if Python properly connects to the CoolTerm remote control socket:

- open a command line (or window powershell) and navigate to the location where you keep CoolTerm.py
- Start Python by typing: python
- in python, type: import CoolTerm
- then type: s = CoolTerm.CoolTermSocket() this makes a connection to CoolTerm's remote control socket.
- go to CoolTerm and select the "Remote" menu. Under that menu, it should show "Connected" next to a light blue LED icon. If instead it shows "Disconnected" next to a dark blue icon the remote socket is not connected and there is an issue.
- Assuming it connected in the previous set, type the following command: print(s.CoolTermVersion())
- This should show the current CoolTerm version. If it instead throws an error, check the "Remote" menu again to see if CoolTerm is still connected or if it somehow disconnected.

You can send me your files to my e-mail address which you can find in the "About CoolTerm" dialog.
jmuhammad
Posts: 21
Joined: Wed Aug 17, 2022 10:21 pm

Re: Command line to open CoolTerm instance connected?

Post by jmuhammad »

I have to have CoolTerm open first before the 'type: s = CoolTerm.CoolTermSocket()' would connect (i.e. that is what I did after the ERROR message you see below):

Code: Select all

C:\WINDOWS\system32>cd /d H:\Python\_PyProjects\my-coolterm
H:\Python\_PyProjects\my-coolterm>python
Python 3.12.10 (tags/v3.12.10:0cc8128, Apr  8 2025, 12:21:36) [MSC v.1943 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import CoolTerm
>>> s = CoolTerm.CoolTermSocket()
ERROR: Could not connect to CoolTerm
>>> s = CoolTerm.CoolTermSocket()
>>> print(s.CoolTermVersion())
2.4.0.3.0.1425
>>>
jmuhammad
Posts: 21
Joined: Wed Aug 17, 2022 10:21 pm

Re: Command line to open CoolTerm instance connected?

Post by jmuhammad »

I combined Examples 2,3,12 and got it working. The key is (at least for me) CoolTerm has to be running to connect a socket then load the settings file.

I just need to learn how to further control CoolTerm with the socket. But for this project -- despite the need for hard-coded delays because I don't know when it is ready -- this will do. I have an option to do it all in Python rather than ahk+python.

Code: Select all

# Example 2, 12 - Launching CoolTerm from Python.py | Loading CoolTerm connection settings
import subprocess  # Desc:
import sys  #        Desc: system-level functions; provides access to variables used by the interpreter
import time  #       Desc: provides time and date function, including measuring time intervals and implementing delays
import CoolTerm
import platform
import psutil

appName = "CoolTerm.exe"
appPath = r"H:\CoolTerm\CoolTermWin\\"
# Check if CoolTerm is running
isRunning = appName in (i.name() for i in psutil.process_iter())
if not isRunning:
    print("Launching CoolTerm...")
    process = subprocess.Popen(appPath + appName)
    time.sleep(5)
else:
    print("CoolTerm is already running.")

time.sleep(5) # Without some delay so that GUI is fully up the socket will not be connected.
s = CoolTerm.CoolTermSocket()
print(s.CoolTermVersion())

FilePath = r"H:\mycoolterm\Settings_COM05.cooltermsettings" # Load connection settings file.
ID = s.LoadSetting(FilePath)
if ID < 0:
    print("The settings could not be loaded")
    sys.exit()
print("The settings have been loaded successfully.")

# Example 03 - Capture received data to a text file.py
# Open the serial port
if s.Connect(ID):
    pass
else:
    print("Not Connected")

#input("The settings have been loaded successfully. Press ENTER to close the window")
#s.CloseWindow(ID) # Close the window
#s.Close() # Disconnect from CoolTerm

r'''
>>> %Run ex2_loading_connection_settings.py
Launching CoolTerm...
2.4.0.3.0.1425
The settings have been loaded successfully.
>>> 
'''
Post Reply