• Support
  • Articles
  • Resources
  • Products

How to connect to USB Serial COM port?

Moderator: boris

  • 1
  • 2

29 postsPage 1 of 2

JamesL
Newbie

Posts:
16
Joined: Wed Aug 31, 2016 7:20 pm
by JamesL » Wed Aug 31, 2016 7:58 pm
So I wrote this up to connect to the first COM Port that has a valid connection and link. It starts from COM6 all the way to COM20.

If you try do a New Connection (Alt + N), choose Serial, and click on the drop down Port, it lists some options:
[img]
Serial%20COM.png
[/img]

I want it automatically connect to COM port 10 because it is the only USB Serial Port. Is there any good way to do this?

I wrote this macro which connects to the first valid COM port that has a valid connection and link, and it starts from COM6 all the way up to COM20:

Code: Select all

  1. USB_COM=0
  2.  
  3. ; Search for USB COM port; assumes port number will be greater than 5
  4. for i 6 20
  5.     ; Disconnect between Tera Term and host (COM channel)
  6.     disconnect
  7.     ; Wait for Full Disconnection
  8.     do
  9.         mpause 10
  10.         testlink
  11.     loop while result = 2
  12.     sprintf2 port '/C=%d' i
  13.     do
  14.         connect port
  15.         mpause 10
  16.         testlink
  17.     loop while !result
  18.     gethostname hostName
  19.     if result=2 then
  20.         USB_COM=i
  21.         break
  22.     endif    
  23.     ; Uncomment below for debugging
  24.     sprintf2 itr '%s link: %d' hostName result
  25.     messagebox itr 'Count'
  26. next
  27. setbaud 115200
  28. if USB_COM then
  29.     sprintf2 msg 'USB port is %s' hostName
  30.     messagebox msg 'USB port'
  31. else
  32.     messagebox "Couldn't find USB serial COM port!" 'USB port'
  33. endif
  34.  
  35.  

Attachments

Serial COM.png
Serial COM.png (32.63 KiB) Viewed 1485 times
JamesL
Newbie

Posts:
16
Joined: Wed Aug 31, 2016 7:20 pm
by JamesL » Thu Sep 01, 2016 3:31 pm
In this case, the USB serial adapter has COM port 10, but this is not always the case.

My computer has 5 default valid COM channels (COM1- COM5), but I can't assume that will be true for other people who will be running the macro.

I would prefer to have something that goes from COM1 to COM256 (MaxComPort=256 in TERATERM.ini), and selects the first valid COM port that has the USB Serial Port description, as seen in the selection menu.
IshmaelCallMe
Guru

Posts:
672
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Fri Sep 02, 2016 4:44 pm
Probably easiest to try each COM port and then stay with the first one that actually responds with a login prompt (or whatever)
JamesL
Newbie

Posts:
16
Joined: Wed Aug 31, 2016 7:20 pm
by JamesL » Tue Sep 06, 2016 6:34 pm
IshmaelCallMe wrote:Probably easiest to try each COM port and then stay with the first one that actually responds with a login prompt (or whatever)


Unfortunately the computers I'm working with have 5 default COM channels, COM1 through COM5 but this is not always the case. Sometimes you get a COM 6 or even COM 8 to replace one of the usual COM(1-5) channels.

Also, only one of the usual 5 COM ports is connected to a device that has serial output, but somehow they end up with a testlink result of 2. The extra USB serial COM port does not initially have any serial device attached to it (when the script runs).
IshmaelCallMe
Guru

Posts:
672
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Mon Sep 12, 2016 2:12 pm
I am sure you could do it with an AutoIt script, but I don't think a pure TeraTERM script would be able to do what you describe then.
JamesL
Newbie

Posts:
16
Joined: Wed Aug 31, 2016 7:20 pm
by JamesL » Mon Sep 12, 2016 6:49 pm
Not sure AutoIt would work, unless it could somehow read the "USB Serial Port" from the New Connection menu. Also recently found out that:

Code: Select all

  1. callmenu 50110
does the same thing as File > New Connection (Alt + N)

Code: Select all

  1. callmenu 50350
does the same thing as Setup > Serial Port
IshmaelCallMe
Guru

Posts:
672
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Mon Sep 12, 2016 7:29 pm
Yep, just verified that Autoit can indeed read the "USB Serial Port" from the New Connection menu. It can only read the currently selected one, so it would have to scroll thru them all, but a script could do that faster than a user can see it happen.
JamesL
Newbie

Posts:
16
Joined: Wed Aug 31, 2016 7:20 pm
by JamesL » Mon Sep 12, 2016 7:32 pm
It might be helpful if I could look at the source code for the Tera Term Macros.
JamesL
Newbie

Posts:
16
Joined: Wed Aug 31, 2016 7:20 pm
by JamesL » Tue Sep 13, 2016 2:10 pm
Thanks for the info. Now I just gotta figure out how to use AutoIt to detect the USB Serial Port text as well as realize that (in this case) the COM port number we want is 10, which should just be some simple text parsing.
IshmaelCallMe
Guru

Posts:
672
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Tue Sep 13, 2016 3:17 pm
Probably easiest is to use the callmenu to bring up the new connection dialog box. Then, this seems to work for me for the autoit script:

Code: Select all

  1.  
  2. $hwin = WinWait('Tera Term: New connection')
  3. ControlSend($hwin, '', 'Button10', '!e!r')
  4. While Not StringRegExp(ControlGetText($hwin, '', ''), 'USB Serial Port')
  5.     ControlSend($hwin, '', '', '{DOWN}')
  6. ControlClick($hwin, '', 'Button5')
  7.  


You can use this to call the AutoIt script from the TeraTERM script:

Code: Select all

  1.      exec '"C:\Program Files (x86)\AutoIt3\AutoIt3.exe" "C:\Program Files\TT_Scripts\select_serial.au3"'


Or, probably simpler to just use the AutoIT script to start TeraTERM and also bring up the new connection window. Then just have a shortcut on the desktop for your users to start the AutoIT script.
JamesL
Newbie

Posts:
16
Joined: Wed Aug 31, 2016 7:20 pm
by JamesL » Tue Sep 13, 2016 3:48 pm
Thanks!

Does that select the USB Serial Port COM channel from the New Connection window?

I would prefer using callmenu to open up the New Connection window only to identify the COM that corresponds to USB Serial Port.

Then I would use callmenu again to open up the Serial port setup window and actually switch to the correct USB COM. That way Tera Term macro logging still works.
JamesL
Newbie

Posts:
16
Joined: Wed Aug 31, 2016 7:20 pm
by JamesL » Tue Sep 13, 2016 4:17 pm
Also looks like AutoIt can read from the window. Is there some way to extract the COM port number associated with USB Serial Port (in this case 10)?
JamesL
Newbie

Posts:
16
Joined: Wed Aug 31, 2016 7:20 pm
by JamesL » Tue Sep 13, 2016 5:29 pm
The AutoIt script would have to somehow transfer the information about which COM port number corresponds to USB Serial Port back to the macro that called it. So in this case AutoIt would have to communicate "10" to the macro that calls it. Is there a way to do this?
JamesL
Newbie

Posts:
16
Joined: Wed Aug 31, 2016 7:20 pm
by JamesL » Tue Sep 13, 2016 5:39 pm
Actually if AutoIt could select the right COM port from the Serial port setup window, that would be fine too
IshmaelCallMe
Guru

Posts:
672
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Tue Sep 13, 2016 10:32 pm
The script as given should auto select the first port with "USB Serial Port" in it. You can always start the log with the "logopen" TeraTERM script command, or set auto logging in the ini file. You will have to have AutoIT installed on each PC, or compile the AutoIT script into a exe and distribute (and call) the exe.
IshmaelCallMe
Guru

Posts:
672
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Tue Sep 13, 2016 10:38 pm
You might want to add some error checking such that if it doesn't find a appropriate port it will error out. Right now it will just loop forever. I will leave that as an exercise .... :smile:

Hint: See if it gets the exact same text twice in a row.
JamesL
Newbie

Posts:
16
Joined: Wed Aug 31, 2016 7:20 pm
by JamesL » Wed Sep 14, 2016 2:18 pm
It would be much much better and simpler if we could just make AutoIt select the right COM port from the Serial port setup window:

Code: Select all

  1. callmenu 50110
File > New Connection (Alt + N)
Also selecting a COM from the New Connection opens up another Tera Term window, which is not at all desirable for me. I'd much prefer to have one Tera Term window open for each run of the test. Yes, there's probably a way to have AutoIt launch another macro for the new [USB Serial Port's] Tera Term COM window to log appropriately, but that includes making another macro just for scripting. This would add unnecessary complexity.

Code: Select all

  1. callmenu 50350
Setup > Serial Port
This doesn't open up any new Tera Term window, it just switches the COM in your currently open Tera Term window.
IshmaelCallMe
Guru

Posts:
672
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Wed Sep 14, 2016 5:08 pm
Even simpler, dump http://www.naughter.com/enumser.html into a file and parse it, then open the correct COM with the 'connect' macro command.
JamesL
Newbie

Posts:
16
Joined: Wed Aug 31, 2016 7:20 pm
by JamesL » Wed Sep 14, 2016 6:25 pm
IshmaelCallMe wrote:Even simpler, dump http://www.naughter.com/enumser.html into a file and parse it, then open the correct COM with the 'connect' macro command.


So that would allow me to switch COM ports in the same Tera Term window, just like what the Serial port setup window does?

You said something about using the 'connect' macro command? How would I communicate to the macro which COM port number corresponds to the USB Serial Port?
IshmaelCallMe
Guru

Posts:
672
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Wed Sep 14, 2016 7:48 pm
Yes, it will allow connection in the current TeraTERM window

Dump output of above tool into a file.
Use macro to read and parse file.
Macro opens correct COM with connect.
JamesL
Newbie

Posts:
16
Joined: Wed Aug 31, 2016 7:20 pm
by JamesL » Fri Sep 16, 2016 3:25 pm
Is there a way to do it using only AutoIt and the Tera Term macro? I'd prefer not to use enumser
boris
Moderator, LogMeTT and TTLEditor developer

Posts:
1596
Joined: Sat Jan 08, 2005 2:52 pm
by boris » Fri Sep 16, 2016 4:43 pm
Check is this is something that can help http://logmett.com/support-forums/3/nee ... HELL#p8935
Thanks.
Best regards,
Boris
JamesL
Newbie

Posts:
16
Joined: Wed Aug 31, 2016 7:20 pm
by JamesL » Fri Sep 16, 2016 9:35 pm
boris wrote:Check is this is something that can help http://logmett.com/support-forums/3/nee ... HELL#p8935


That only gives me a list of "COMx", where x is the COM numbers that show up in Setup > Serial port
IshmaelCallMe
Guru

Posts:
672
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Fri Sep 16, 2016 11:45 pm
JamesL wrote:Is there a way to do it using only AutoIt and the Tera Term macro? I'd prefer not to use enumser


Well, sure. Modify the AutoIt script above to close the window instead of hit "OK" at the end. Then have it wait for the serial port setup dialog box and select the appropriate COM port that it parsed out of what it got from controlgettext.

The TeraTerm script will first start the AutoIt script, open the New Connection, then the serial port setup dialogs. It will depend on the AutoIt script to close each of them.
Display posts from previous:
Sort by:
  • 1
  • 2

29 postsPage 1 of 2

Users browsing this forum: No registered users
cron