Although serial ports are disappearing, they can still be useful.
Here is a COMMs UDF. It provides an easy way to use serial ports without the restrictions and problems some methods have. USB to serial is ok, binary data is ok.
This UDF requires my comMG.dll which can be in either the script folder or the Windows folder by default, or in the path specified using the function _CommSetDllPath.
Note the following shortcomings:
the dll link below is 32 bit so it will not work with a 64 bit apps, but there is a 64 bit version in my post around 25th March 2018 for people to try.
The strings and character functions are all AnsiChar.
Functions in the UDF are
_CommSendBreak; not tested!!!!!!!!!!
_CommSetRTS (NB these will not work if Hardware handshaking is selected because
_CommSetDTR then these lines are controlled by the data being sent.)
Go to Download Page For Commgv2
Download includes the dll and udf.
Most recent changes
28th March 2014 - dll V2.83
Correct error setting 6 data bits as 7.
11th March 2014 dll V2.82
Allow data bits of 4 to 8 instead of only 7 and 8.
19th August 2013
dll v2.81 removes some unwanted eroor message popups. Might not remove popups for some Windows versions.
27th September 2012
Correct error closing port. New version of UDF if V2.90, new dll is commg.dll V2.79. Thanks to tfabris.
18th January 2012
Corrected typo in UDF V 2.87, and uploaded as V2.88
Increased max baud allowed by the dll from 200000 to 256000. New version now V2.78
17th January 2012
Modified thesleep addition to _CommGetLine so that reading data is not slowed down.
14th January 2012
Corrected _CommReadByte in UDF.
Added sleep(20) to while loop in _CommGetLine to reduce CPU usage
20th December 2011
UDF version 2.86. - Changed function GetByte so it returned the error string given by the dll.
Dll version 2.77 - removed an unwanted erro message dialogue from GetByte function. (Thanks funkey)
4th December 2011
New dll and example versions. Dll function SetPort corrected because it was not using the parameters passed for DTR and RTS.
The example was setting flow control incorrectly: the settings for hardware handshaking and XON./XOFF were reversed.
25th August 2011 corrected function _CommClosePort.
Example corrected for setting parity and flow 22nd December 2013 (thanks to MichaelXMike)
How can I load actual connect Com Ports to combobox?
for example this 3....(6,7,8)
I got this code and would like to be able to change Baud Rate and instead of sending character by character i would like to be able (if possible) to send whole string. But i don't know how to change it.
I am taking input from file and processing whole line (this is done in FilesHandling.au3).
To execute this i am just calling SendData("FileName", int) in "main" script.
Any help very appreciated.
#include <WinAPI.au3> #include <Array.au3> #include "FilesHandling.au3" ;init DLL function, we need handle to call the function $h = DllCall("Kernel32.dll", "hwnd", "CreateFile", "str", "\\.\COM19", "int", BitOR($GENERIC_READ,$GENERIC_WRITE), "int", 0, "ptr", 0, "int", $OPEN_EXISTING, "int", $FILE_ATTRIBUTE_NORMAL, "int", 0) $handle=$h Func SendData($FileName, $LineNumber) ;string to be send $c = readFile($FileName, $LineNumber) $cLenght = StringLen($c) $aArray = StringSplit($c, "") ;_ArrayDisplay($aArray, "", Default, 64) For $i = 1 To $cLenght writeChar($handle, $aArray[$i], $cLenght) Next ;move to next line writeChar($handle, @CR,1) EndFunc ;write a single char func writeChar($handle,$c,) $stString = DLLStructCreate("char str") $lpNumberOfBytesWritten = 0 DllStructSetData($stString, 1, $c) $res = _WinAPI_WriteFile($handle, DllStructGetPtr($stString, "str"), 1,$lpNumberOfBytesWritten) if ($res<>true) then ConsoleWrite ( _WinAPI_GetLastErrorMessage() & @LF) EndIf EndFunc