Command line to open CoolTerm instance connected?

If you have specific questions or problems with any of my Freeware applications, post them here.
User avatar
roger
Site Admin
Posts: 542
Joined: Fri Apr 24, 2009 12:41 am
Contact:

Re: Command line to open CoolTerm instance connected?

Post by roger »

jmuhammad wrote: Sat Nov 08, 2025 1:53 pm 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):
Ah, yes, CoolTerm needs to be running before it can accept socket connections.
User avatar
roger
Site Admin
Posts: 542
Joined: Fri Apr 24, 2009 12:41 am
Contact:

Re: Command line to open CoolTerm instance connected?

Post by roger »

jmuhammad wrote: Sat Nov 08, 2025 2:51 pm 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.
Great! I'm glad you got it working!

The learning curve for this shouldn't be too bad. You'll find that you can do almost anything with scripting that you can do via the CoolTerm GUI. Please post back to the forum if you have any further questions.

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

Re: Command line to open CoolTerm instance connected?

Post by jmuhammad »

FYI, in CoolTerm.py:
SerialPortName(SerialPortIndex as integer) as string: Returns the name of the serial port with the specified index.
should be:
GetSerialPortName(SerialPortIndex as integer) as string: Returns the name of the serial port with the specified index.
User avatar
roger
Site Admin
Posts: 542
Joined: Fri Apr 24, 2009 12:41 am
Contact:

Re: Command line to open CoolTerm instance connected?

Post by roger »

Thanks for the heads up.

SerialPortName() is actually the name used internally and with AppleScript. The documentation is correct. I will add the SerialPortName() call to CoolTerm.py in addition to GetSerialPortName() in order to not break any scripts anybody has written. Both will work and do the same.
The same applies to SerialPortCount() and GetSerialPortCount(). The next CoolTerm release will include the updated CoolTerm.py module.

Meanwhile, I have attached the updated CoolTerm.py (remove the .txt extension):
CoolTerm.py.txt
You do not have the required permissions to view the files attached to this post.
jmuhammad
Posts: 26
Joined: Wed Aug 17, 2022 10:21 pm

Re: Command line to open CoolTerm instance connected?

Post by jmuhammad »

I wrote a python script that determines what port a usb-to-serial device is on.
The port number and name of a CoolTerm settings file is passed to function 'open_coolterm(settings_file, port_number)'.
The program works fine using this settings file.

I am trying to launch a second instance of CoolTerm using the same settings file but for another usb-to-serial device on a different port number.

The only way I have gotten this to work is to have a different settings file for each device.
I basically made a copy of the 'template' settings file and added a suffix e.g. the device serial number.

It was my goal for this group of usb-to-serial devices (they are same model just different serial numbers) to use one settings file and open new CT instances with different ports. Again, the settings file does not have the port number -- I use 's.SetCurrentSerialPort()' to set the port.

1) Can you provide understandings of why the same settings file can not be reused?
2) Any ideas of how to make this work?
---
Edit 1:
One challenge is that the CoolTerm titlebar title e.g. 'myapp.cooltermsettings' is the same whether the port is connected or disconnnected. If there was a discerning marker for the instance that is not already connected to a port then I could use this to identify the instance to connect. Example 'myapp.cooltermsettings::[Y]' | 'myapp.cooltermsettings::[N]' or even 'myapp.cooltermsettings*' | 'myapp.cooltermsettings'. Unfortunately I cannot get the HWND early enough so best bet for Application(backend="win32").connect() would be the title since PID, EXE, CLASS_NAME are fixed and always the same even for multiple CoolTerm instances.
---
Edit 2:
I implemented generating a copy of the settings file with device serial number as suffix e.g. 'myapp_AB034CC9.cooltermsettings' (if it does not already exist). It works very well and side effect is I can actually tell what CoolTerm instance is monitoring my device just by the title bar. OTOH I have multiple settings files (which for this project is just two). So this is not a problem to solve, just maybe something to consider in the future.
User avatar
roger
Site Admin
Posts: 542
Joined: Fri Apr 24, 2009 12:41 am
Contact:

Re: Command line to open CoolTerm instance connected?

Post by roger »

Hi,

the reason why it's not possible to open the same settings file twice is the same reason why you can't open the same MS Word document twice. Otherwise both CoolTerm instances would potentially be writing changes to the same document, which can lead to unexpected behavior.

I'm glad you figured out a way that works for you.

Also, you can always check if an instance is connected by using the 'IsConnected' command.

Lastly, there is a relatively simple way to create additional instances with the same settings via scripting, without creating separate settings files. The trick is to create a new instance in the script and then clone the settings from the first instance:

- Open CoolTerm and load your settings
- Get a copy of the current settings with the GetAllParameters command. This gives you a string with all parameters
- Replace the name of the serial port in these settings with the name of the other port you want to use with the new settings
- Create a new CoolTerm window
- Apply the modified settings to the new window

Here is an example of how this would look like:

Code: Select all

# CHECK IF COOLTERM IS RUNNING AND LAUNCH IT IF NOT

# Set app and path names
import os
import platform
match platform.system(): # requires Python 3.10 or newer
    case "Darwin": # macOS
        appName = "CoolTerm"
        appPath = "/Applications/"
        # https://stackoverflow.com/questions/28845242/how-to-open-an-application-in-mac-os-using-python
    case "Windows":
        appName = "CoolTerm.exe"
        appPath = "C:\\Program Files\\CoolTermWin\\"
    case "Linux":
        appName = "CoolTerm"
        appPath = "/home/apps/CoolTermLinux/"

# Check if CoolTerm is running
import psutil
isRunning = appName in (i.name() for i in psutil.process_iter())

# Launch CoolTerm
import subprocess
import time
if not isRunning:
    print("Launching CoolTerm...")
    if platform.system() == "Darwin":
        os.system("open " + appPath + appName + ".app")
    else:
        subprocess.Popen(appPath + appName)
    time.sleep(5)
else:
    print("CoolTerm is already running.")
    
# ------------------------------------------------------

# Connect to CoolTerm
import CoolTerm

s = CoolTerm.CoolTermSocket()

# ------------------------------------------------------

# Load settings
ID1 = s.LoadSetting("PATH_TO_YOUR_SETTINGS_FILE")

# Retrieve the current settings
settings = s.GetAllParameters(ID1)

# Replace the current port name with a new port name
port1 = s.GetParameter(ID1, "Port")
print(port1)
port2 = "NAME_OF_YOUR_OTHER_PORT"
print(port2)
settings = settings.replace(port1, port2)

# Create new CoolTerm instance
ID2 = s.NewWindow()

# Apply settings to new window
# At this point, this needs to be done line by line, but I will add a "ApplyParameters"
# command in the future that takes a string with all parameters and applies all of them at once.
parameters = settings.splitlines()
for parameter in parameters:
	print(parameter)
	key, value = parameter.split(" = ", maxsplit=1)
	s.SetParameter(ID2, key, value)

# ------------------------------------------------------

# Disconnect from CoolTerm
s.Close()
I hope this helps.

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

Re: Command line to open CoolTerm instance connected?

Post by jmuhammad »

Greetings Roger,
Thanks!
I ran the script and a couple of things:
1) It is REALLY slow due to the 'one line at a time' application of settings (but you did kinda warn me);
2) It is ignoring/cannot see the key 'WindowPosition'. So I added this line:

Code: Select all

s.SetParameter(ID2, WindowPosition, "850,120,740,212")
but I get an error message:

Code: Select all

s.SetParameter(ID2, WindowPosition, "850,120,740,212")
NameError: name 'WindowPosition' is not defined
The code does educate me more about CT scripting although I do not think **I** can get around the 'one line at a time' application of settings.
But I'd like to see this approach work as well as it can prior to possible future update that applies all settings at once. Will be pretty 'Cool'. <-- See what I did there? :P
User avatar
roger
Site Admin
Posts: 542
Joined: Fri Apr 24, 2009 12:41 am
Contact:

Re: Command line to open CoolTerm instance connected?

Post by roger »

Thanks for your feedback. It seems you found a bug in the GetAllParameters function. It should include the window position as well. I'll make sure to fix that in the next release.

Be sure to enclose the parameter name in quotation marks when calling SetParameter, since it's passed as a string:

Code: Select all

s.SetParameter(ID2, "WindowPosition", "850,120,740,212")
jmuhammad
Posts: 26
Joined: Wed Aug 17, 2022 10:21 pm

Re: Command line to open CoolTerm instance connected?

Post by jmuhammad »

OK, thanks. I was able to get it working (the overall script as well as manually setting "WindowPosition".
But settings = s.GetAllParameters(ID1) does not get "WindowPosition".
FYI, it takes 12.8 seconds to start 2nd instance and load parameters (11.7 without print statements).
User avatar
roger
Site Admin
Posts: 542
Joined: Fri Apr 24, 2009 12:41 am
Contact:

Re: Command line to open CoolTerm instance connected?

Post by roger »

Great!

Yes, I have confirmed that GetAllParameters not returning the window position as a bug. I will fix that in the next release.
And, a new command to apply all parameters contained in a string at once should reduce the 12 or so seconds to less than a second. I will include that in the next release as well.
Post Reply