Just thought I would share a potential solution if anyone has been wanting to be able to send/receive serial data using ahk via the COM port. I created a simple class lib in ahk that makes sending ASCII via COM dead simple. I have an example sketch and ahk script posted on the ahk forums here: Class Serial - AutoHotkey Community
Because ahk's forums can be pretty spotty i'm going to include a copy of the sketch, and the ahk lib and script here. The sketch is a simple example: It will wait for an integer to be sent via the COM port, then will blink the onboard LED n times where n is the number received. It will then create a simple JSON object and serialize some data then send that out via the COM port.
ASCII_JSON_example.ino (1.3 KB)
The ahk script will then receive that data, causing it to pop up a message box with the raw JSON string. It will then use the JXON.ahk lib to deserialize the string into an array, and then pop up a 2nd message box reporting some of that specific data to the user. (note, the ahk script is set to use ctrl + alt + t to cause the number 5 to be sent via the COM port. And binds the ESC key to close out the COM port and exit the script.
Class Serial.ahk.txt (17.5 KB)
JSON_ASCII_Example.ahk.txt (1.0 KB)
Edit: forgot to include the JXON.ahk lib...
Jxon.ahk.txt (6.2 KB)
Hi spollard. Have you seen this old RS232 library where aobrien solved the problem with two digit port numbers?
;###### Extract/Format the RS232 COM Port Number ######
;7/23/08 Thanks krisky68 for finding/solving the bug in which RS232 COM Ports greater than 9 didn't work.
StringSplit, RS232_Temp, RS232_Settings, `:
RS232_Temp1_Len := StrLen(RS232_Temp1) ;For COM Ports > 9 \\.\ needs to prepended to the COM Port name.
If (RS232_Temp1_Len > 4) ;So the valid names are
RS232_COM = \\.\%RS232_Temp1% ; ... COM8 COM9 \\.\COM10 \\.\COM11 \\.\COM12 and so on...
Else ;
RS232_COM = %RS232_Temp1%
;8/10/09 A BIG Thanks to trenton_xavier for figuring out how to make COM Ports greater than 9 work for USB-Serial Dongles.
StringTrimLeft, RS232_Settings, RS232_Settings, RS232_Temp1_Len+1 ;Remove the COM number (+1 for the semicolon) for BuildCommDCB.
;MsgBox, RS232_COM=%RS232_COM% `nRS232_Settings=%RS232_Settings%
It isn't originally a library, I modified back in the 2010s to control a tv and then a few years later I made an script to communicate with Ardurino. That script was buggy and only worked for sending data. Back then work kept me on the road all the time so I never got into Arduino too much.
A couple of months ago I bought a 2x4 HDMI matrix that has RS232 so I dug this library up and made a script to control it and it worked great with my hacked together library from aobrien's post. Then I dug out my old Arduinos and bought some new relays to make an upgraded controller for my video screen... eventually. They both work really well and I can get the return data from both of them, but while I can send full strings to the HDMI matrix I'm only able to send single digits to Arduino.
Until a couple of hours ago I thought this was because of my lack of programing knowledge, but today I learned to make sketches where I can send strings. But I can't get my old library to work with the new Arduino Sketch, so I was looking for alternatives which led me here.
The next hurdle, I have two Arduinos (that I can retrieve serial data from) and they are on ports 17 and 20. I'm not sure if there is a way to clear out com ports that aren't used any or not, but either way, if this info helps solve your problem with double digits then even better.
Your example script didn't specify AHK2 so I assume running it on AHK1 is ok? Anyway, hope this helps.
To answer your first question, yes I had seen the workaround for higher COM numbers and it is included in my ahk script. If you think you are having a problem related to that you can test it by manually changing your COM port. Just go to your Device Manager in windows, find the COM port then go to Properties and click the Port settings tab, and click the advanced button. about half way down the page you will see a drop down that will allow you to select the COM port assigned to this serial port.
Also yes, this lib was written for ahk v1 and I do not currently have any plans to port it to v2. If you have further trouble with it feel free to post here and I will try to help as time allows.
Thanks for the quick replay, and the info on how to change the com port. I'll give it a try shortly.
I did get my old library working with sending strings. Problem was I was ending with a carriage return instead of a line feed. I'm still interested in trying yours and seeing if it works better, and if it will retrieve data returned by a Leonardo. Since it's set up differently than an Uno my old library does not work for retrieving from it. Thanks
ahk is a bit limited with sending stuff on COM in that it terminates all strings with a Null char, though curiously some devices i've used with ahk register the last char as ASCII char number 255 / 0xFF. So depending on how the device parses the data stream it may be problematic. Most devices i've tested seem to be fine, but I have seen edge cases. Personally I just write up a c# app in those cases as it gives full control of the data stream. But I understand not everyone is comfortable/fluent in c#. I'm pretty sure you can do the same in Python or any number of programming languages as well, but again that's dependent on your comfort level.
As far as what I'm fluent in, basic AHKv1 is it. I've never had any training in programing other than the AHK manual and forum.
I'm sure that for this reason, I can't figure out how to use classes. I had one other situation where I needed to use a class, someone had taken a library I had made to put a rotary knob on a gui and turned it into class_Rotary_Knob. The only way I get it that to work was to copy the entire code from the class to my script.
Now, with this Class Serial I've tried including it in my script and copying the entire code to my script. Whatever I do I get an error message, "Send_Message(red255) Call to nonexistant function".
Can you please explain how to use classes in a script?
Edit:
I did finally get the example script to work. I could not use the include code in the example, "#Include ", the script always gave me a Not Found error message. I instead used "#Include Class Serial.ahk" and the script could find it. The LED blinks and the msgbox says 5, so it's a start, now I need to see if I can get it work in a full script.
I'm happy to help out if you run into further road blocks, but perhaps the better place for those questions would be on the ahk forum or you can find me on the ahk discord server. As long as no one objects I can try to help out here as well.
Thanks. Just letting you know that I moved on from this for now but will be getting back to it in a few days. My project at the time was to go through all the Arduino/Pi stuff packed in a spare drawer and learning what the pieces were and how to use them. I have a ton of stuff from stater kits I bought 20ish years ago and only used the LEDs.
While I was experimenting with this I was getting errors on both sides, Arduino anytime I tried to add a command to get it to do something, and AHK anytime I tried to get controls to send information. So maybe use whichever forum for whatever the error happens to be?
Just fyi, the project I'm wanting to get back to is a DMX lighting console customized for theater. It's a project I've been trying to do for at least 20 years but could never get help in the forums. I was using an Enttec Open DMX converter but could never work out the dll calls. I did get an Arduino shield but my converted serial library was too buggy to use. Maybe now, the year I've retired and don't need it anymore, I'll have the time to get it working.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.