Jump to content

Serial Port /COM Port UDF


martin
 Share

Recommended Posts

  • 1 month later...

Apparent bug in the _CommSwitch function: for a valid channel, the function will always return 1 (and not the channel which is what the docs say).

Fix: the line that reads "Return $vDllAns[0]" should read "Return $vDllAns[1]"

 

(Is there a better place to report this?)

Link to comment
Share on other sites

Wow this UDF may be just what I need!

I have GPS devices that need to be set to a specific Comm Port (25 in this case) and sometimes the devices lose the port number, or if they are external usb devices they get plugged into a different usb port and lost the comm assignment.

I have this so far:

#Include <CommMG.au3>
#Include <Array.au3>

 _CommSetDllPath(@ScriptDir & "\commmg.dll")
 $aListPortNames = _ComGetPortNames(0)
 _ArrayDisplay($aListPortNames)
 For $i = 1 to $aListPortNames[0]
If StringinStr($aListPortNames[0][$i], "Prolific USB-to-Serial Comm Port") Then

EndIf

 Next

But unless I am missing something I do not see a function to change a Comm port assignment?  In the above I would just find the current Comm port and if its not 25 assign it.

So is the next step going to be registry editing (what I was going to do originally) or I see this UDF uses a lot of Object based queries, so maybe I can change it via an Object method? 

Link to comment
Share on other sites

  • 2 months later...
  • 3 weeks later...
  • 2 weeks later...
  • 3 months later...

Hi Martin

I know that you udf/dll has  _CommReadByteArray($pAddr,$iNum,$iWait).

Bytes sent or received can have value = 0 ?

My application is a loop all time checking if something arrived at serial port (it can´t stay waiting, as it must do other things).

When some bytes arrives, it goes getting the bytes and mounting the messages in an array.

At each 5 seconds arrives about 5K bytes, at 19.200 bauds. They arrive in a bunch at the beginning of each 5 seconds period (about 2.5 seconds receiving).

Do you have a very simple example on how to use  _CommReadByteArray and  _CommSendByteArray

Best Regards

Jose

see also: https://www.autoitscript.com/forum/topic/182549-serial-port-reading-bytes-with-value0/

Link to comment
Share on other sites

  • 3 weeks later...

Hi Martin, Hi All,

I am using the Commg.dll since years and it always worked fine. Thank you for the good work, Martin !

Unfortunately on Windows 10 I experience big problems. The Scripts are working but the serial communication seems to be very slow. The problems have been reported in this thread by several people before. Did anybody find a solution for the problem ? 

Not using Windows 10 is not a solution because sooner or later every PC will run Win10 or Higher ... So it is a matter of time until everybody using the Commg.dll will face the same problem.

Best Regards,

Gregor

Link to comment
Share on other sites

  • 2 weeks later...

Hello everyone, I have a question to ask.
I'm working with the serial port and CommMg.au3 and I'm working with satisfaction.
But now I need to not only send but amìnche to receive data from the serial .... and here I do not know how to make sure that the script remains always listening to what comes from the Com port.
Someone would be so kind as to postarmi two lines of an example, then I gave it right for me needs?
For sending the serial data I use the command "_CommSendbyte ()"
Thank you all for any help given.

Alberto

Edited by trescon

Thank You

Alberto

---------------------------------------------------

I am translate with Google.

Link to comment
Share on other sites

  • 5 months later...
On 8/6/2016 at 0:06 PM, Bonec said:

Hi Martin, Hi All,

I am using the Commg.dll since years and it always worked fine. Thank you for the good work, Martin !

Unfortunately on Windows 10 I experience big problems. The Scripts are working but the serial communication seems to be very slow. The problems have been reported in this thread by several people before. Did anybody find a solution for the problem ? 

Not using Windows 10 is not a solution because sooner or later every PC will run Win10 or Higher ... So it is a matter of time until everybody using the Commg.dll will face the same problem.

Best Regards,

Gregor

Completely agree. Are you using a x64 W10 system? I have a x86 W10 updated from Windows 8.1 that works fine and two W10 (x64) that do not with the same script :(

Link to comment
Share on other sites

  • 3 months later...

Hi all

I´m using this UDF/DLL, version available at the first post. My application receives 480 * 8 bytes =~ 4K binary bytes (0-255) in a burst  at 19200 bps at each 5 seconds (2 serial ports)

The 32 bits compiled .exe works with win7 and win 10  with no problem. As these are 480 real time sensors, up to now I didn't have any problem, data lost, speed or performance issues, CPU usage, etc. No noticeable difference between win10 and win7 at task manager. It consumes very little CPU.

On my case I´m just using    _CommSetPort,  CommSwitch,  _CommSendByte,  _CommReadByte,  _CommClearInputBuffer,  _CommClearOutputBuffer

Jose

Edited by joseLB
Link to comment
Share on other sites

  • 4 weeks later...

Hi All,

Martin,

  you did a great job, and thank you for the dll. 

I encountered a problem with _CommListPorts(). I have an IC programmer driven via serial port and if I disconnect the device during programming Operation, from device manager disappear but _CommListPorts() still return it as an available port. Only with a  WIN restart can fix it. I am using WIN7. 

Can you have a fix?

DLL and commg.au3 are the latest

thx

 

Link to comment
Share on other sites

  • 3 weeks later...

I've experienced very rare, intermittent script freezes with this dll (though it has happened at least a dozen times in the past year, on an application running 24x7 on a handful of computers. My application reads data from a TCP connection, sends it out to connected TCP clients, and sends it to the serial port. If I disable serial functionality my script will never freeze. Nothing in my routine will allow a loop that will never exit. I intentionally make sure that I won't try to send any more data than 2048 bytes, and handshaking is turned off. It appears that the output on the serial lines freezes halfway through writing to the com port.

Anyways, as I'm having this stability issue, and others are issuing concerns about compatibility with Windows 10, and there hasn't been much action from Martin lately, I'm going to look at an alternate serial method.

Com0Com is an open source project for virtual com ports. Signed copy (64 bit Windows requires signed drivers) is available here. These virtual ports may not strictly be needed, but the project has tools "hub4com" and "com2tcp". These can link a serial port to a TCP port. I may look at using this, then sending data back to AutoIT via TCP These do not require a driver, and are standalone executable (com2tcp.exe is only 92kB) .

Link to comment
Share on other sites

  • 1 month later...
On 5/12/2007 at 8:04 AM, martin said:

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 is 32 bit so it will not work with a 64 bit app.

 

First, I want to thank you for a very helpful UDF and .dll.  In testing, we have been using your UDF and .dll file in combination with a podium button panel to cycle between Clone and Extended monitor modes in some of our classrooms.  Unfortunately, it looks like Windows 10 1703 (Creators Update) uses a 64-bit version of DisplaySwitch.exe and the only way to call that is to compile AutoIt scripts to 64-bit mode.  But the commg.dll is only 32-bit so we can no longer use your UDF and .dll.

Is there any hope for a 64-bit version of commg.dll?

Link to comment
Share on other sites

  • 2 months later...

Hi,

I've changed @Brian39 code  from  this topic  and change it to send whole string. I've tried to use same script to manage two COM ports with different Baud rate, but without any luck. Can anyone help me and show me what ned to be changed to manage more than one COM port with different Baud Rate using below script - if possible. Otherwise how to achieve this?

Functions SendData2() & SendDataOPTIC() should use different Baud rate but apart of using different COM port it does not want to use different Baud rate, it picking up (whichever is first assigned)

(...)
Global $CMPortScanner = 7              ; Port
Global $CMPortOptic = 10              ; Port
Local $CmBoBaudOptic = 19200        ; Baud
Local $CmBoBaudScanner = 115200        ; Baud
(...)

Please help.

code below:

#include "FilesHandling.au3"
;Include the Serial UDF
#include 'CommMG.au3'

;Internal for the Serial UDF
Global $sportSetError = ''

;COM Vars
;_CommSetPort($iPort, ByRef $sErr, $iBaud = 9600, $iBits = 8, $iPar = 0, $iStop = 1, $iFlow = 0, $RTSMode = 0, $DTRMode = 0)
Global $CMPortScanner = 7              ; Port
Global $CMPortOptic = 10              ; Port
Local $CmBoBaudOptic = 19200        ; Baud
Local $CmBoBaudScanner = 115200        ; Baud
Global $CmboDataBits =  8       ; Data Bits
Global $CmBoParity = "none"     ; Parity
Global $CmBoStop = 1            ; Stop
Global $setflow = 2             ; Flow
Global $RTSMode = 1
Global $DTRMode = 1
Global $iWait = 0


;==========================Funcions=============================

;DailyQC

Func SerialPort_UsingCommMG_DailyQC($FileName1, $LineNumber1)
    SendData2($FileName1, $LineNumber1)
    Sleep(500)
EndFunc


;Single Test

Func SerialPort_UsingCommMG_SingleTest($FileName1, $LineNumber1, $FileName2, $LineNumber2)
    SendData2($FileName1, $LineNumber1)
    Sleep(500)
    SendData2($FileName2, $LineNumber2)
    Sleep(500)
EndFunc

; BatchMode

Func SerialPort_UsingCommMG_BatchMode($NumberOfTests, $FileName1, $FileName2)
    For $i = 1 To $NumberOfTests
        SendData2($FileName1, $i)
        Sleep(500)
        SendData2($FileName2, $i)
        Sleep(500)
    Next

EndFunc

Func SerialPort_UsingCommMG_OpticSend($Event)

    Sleep(500)
    Switch($Event)
        Case 1 ;"AK"
            SendDataOPTIC("OpticsData.txt",1)
        Case 2 ;"SHUT"
            SendDataOPTIC("OpticsData.txt",2)
        Case 3 ;"OPEN"
            SendDataOPTIC("OpticsData.txt",3)
        Case 4 ;"Data"
            SendDataOPTIC("OpticsData.txt",4)
        Case 5 ;"SData"
            SendDataOPTIC("OpticsData.txt",5)
    EndSwitch


EndFunc

;~ ;Func SerialPort_UsingCommMG_
;~ ;for testing only
Func SerialPort_Test_SHUT()
    SendDataOPTIC("OpticData.txt", 1)
    SendDataOPTIC("OpticData.txt", 6)
    Sleep(100)
    SendDataOPTIC("OpticData.txt", 2)
    SendDataOPTIC("OpticData.txt", 6)
    Sleep(100)
    SendDataOPTIC("OpticData.txt", 3)
    SendDataOPTIC("OpticData.txt", 6)
    Sleep(100)
    SendDataOPTIC("OpticData.txt", 4)
    SendDataOPTIC("OpticData.txt", 6)
    Sleep(100)
EndFunc


;==========================EOF Funcions===================================

Func SendData2($FileName, $LineNumber)
    ;Start up communication
    _CommSwitch($CMPortScanner)
    _CommSetPort($CMPortScanner, $sportSetError, $CmBoBaudScanner, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow, $RTSMode, $DTRMode)

    $sStrData  = ReadFile($FileName, $LineNumber)
    $iNumString = StringLen($sStrData)
    $tStrData = DllStructCreate("String["&$iNumString&"]")
    $iRet = _CommSendString($sStrData,1)

    If @error Or $iRet = -1 Then ConsoleWrite("!Error: " &  @error & @CRLF)


    _CommClearOutputBuffer()
    _CommClearInputBuffer()
    _Commcloseport()
EndFunc

Func SendDataOPTIC($FileName, $LineNumber)
    ;Start up communication
    _CommSwitch($CMPortOptic)
    _CommSetPort($CMPortOptic, $sportSetError, $CmBoBaudOptic, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow, $RTSMode, $DTRMode)

    $sStrData  = ReadFile($FileName, $LineNumber)
    $iNumString = StringLen($sStrData)
    $tStrData = DllStructCreate("String["&$iNumString&"]")
    $iRet = _CommSendString($sStrData,1)

    If @error Or $iRet = -1 Then ConsoleWrite("!Error: " &  @error & @CRLF)


    _CommClearOutputBuffer()
    _CommClearInputBuffer()
    _Commcloseport()
EndFunc

 

Link to comment
Share on other sites

On 15/09/2017 at 3:38 PM, cheeroke said:

Hi,

I've changed @Brian39 code  from  this topic  and change it to send whole string. I've tried to use same script to manage two COM ports with different Baud rate, but without any luck. Can anyone help me and show me what ned to be changed to manage more than one COM port with different Baud Rate using below script - if possible. Otherwise how to achieve this?

Functions SendData2() & SendDataOPTIC() should use different Baud rate but apart of using different COM port it does not want to use different Baud rate, it picking up (whichever is first assigned)

(...)
Global $CMPortScanner = 7              ; Port
Global $CMPortOptic = 10              ; Port
Local $CmBoBaudOptic = 19200        ; Baud
Local $CmBoBaudScanner = 115200        ; Baud
(...)

Please help.

code below:

#include "FilesHandling.au3"
;Include the Serial UDF
#include 'CommMG.au3'

;Internal for the Serial UDF
Global $sportSetError = ''

;COM Vars
;_CommSetPort($iPort, ByRef $sErr, $iBaud = 9600, $iBits = 8, $iPar = 0, $iStop = 1, $iFlow = 0, $RTSMode = 0, $DTRMode = 0)
Global $CMPortScanner = 7              ; Port
Global $CMPortOptic = 10              ; Port
Local $CmBoBaudOptic = 19200        ; Baud
Local $CmBoBaudScanner = 115200        ; Baud
Global $CmboDataBits =  8       ; Data Bits
Global $CmBoParity = "none"     ; Parity
Global $CmBoStop = 1            ; Stop
Global $setflow = 2             ; Flow
Global $RTSMode = 1
Global $DTRMode = 1
Global $iWait = 0


;==========================Funcions=============================

;DailyQC

Func SerialPort_UsingCommMG_DailyQC($FileName1, $LineNumber1)
    SendData2($FileName1, $LineNumber1)
    Sleep(500)
EndFunc


;Single Test

Func SerialPort_UsingCommMG_SingleTest($FileName1, $LineNumber1, $FileName2, $LineNumber2)
    SendData2($FileName1, $LineNumber1)
    Sleep(500)
    SendData2($FileName2, $LineNumber2)
    Sleep(500)
EndFunc

; BatchMode

Func SerialPort_UsingCommMG_BatchMode($NumberOfTests, $FileName1, $FileName2)
    For $i = 1 To $NumberOfTests
        SendData2($FileName1, $i)
        Sleep(500)
        SendData2($FileName2, $i)
        Sleep(500)
    Next

EndFunc

Func SerialPort_UsingCommMG_OpticSend($Event)

    Sleep(500)
    Switch($Event)
        Case 1 ;"AK"
            SendDataOPTIC("OpticsData.txt",1)
        Case 2 ;"SHUT"
            SendDataOPTIC("OpticsData.txt",2)
        Case 3 ;"OPEN"
            SendDataOPTIC("OpticsData.txt",3)
        Case 4 ;"Data"
            SendDataOPTIC("OpticsData.txt",4)
        Case 5 ;"SData"
            SendDataOPTIC("OpticsData.txt",5)
    EndSwitch


EndFunc

;~ ;Func SerialPort_UsingCommMG_
;~ ;for testing only
Func SerialPort_Test_SHUT()
    SendDataOPTIC("OpticData.txt", 1)
    SendDataOPTIC("OpticData.txt", 6)
    Sleep(100)
    SendDataOPTIC("OpticData.txt", 2)
    SendDataOPTIC("OpticData.txt", 6)
    Sleep(100)
    SendDataOPTIC("OpticData.txt", 3)
    SendDataOPTIC("OpticData.txt", 6)
    Sleep(100)
    SendDataOPTIC("OpticData.txt", 4)
    SendDataOPTIC("OpticData.txt", 6)
    Sleep(100)
EndFunc


;==========================EOF Funcions===================================

Func SendData2($FileName, $LineNumber)
    ;Start up communication
    _CommSwitch($CMPortScanner)
    _CommSetPort($CMPortScanner, $sportSetError, $CmBoBaudScanner, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow, $RTSMode, $DTRMode)

    $sStrData  = ReadFile($FileName, $LineNumber)
    $iNumString = StringLen($sStrData)
    $tStrData = DllStructCreate("String["&$iNumString&"]")
    $iRet = _CommSendString($sStrData,1)

    If @error Or $iRet = -1 Then ConsoleWrite("!Error: " &  @error & @CRLF)


    _CommClearOutputBuffer()
    _CommClearInputBuffer()
    _Commcloseport()
EndFunc

Func SendDataOPTIC($FileName, $LineNumber)
    ;Start up communication
    _CommSwitch($CMPortOptic)
    _CommSetPort($CMPortOptic, $sportSetError, $CmBoBaudOptic, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow, $RTSMode, $DTRMode)

    $sStrData  = ReadFile($FileName, $LineNumber)
    $iNumString = StringLen($sStrData)
    $tStrData = DllStructCreate("String["&$iNumString&"]")
    $iRet = _CommSendString($sStrData,1)

    If @error Or $iRet = -1 Then ConsoleWrite("!Error: " &  @error & @CRLF)


    _CommClearOutputBuffer()
    _CommClearInputBuffer()
    _Commcloseport()
EndFunc

 

ok, i managed it to work. Removing

Local $CmBoBaudOptic = 19200        ; Baud
Local $CmBoBaudScanner = 115200        ; Baud

from top of the script and placing it within desired functions SendData2() and SendDataOPTICS() like below:

Func SendDataOPTIC($FileName, $LineNumber)
    $CmBoBaudOptic = 19200        ; Baud
    ;Start up communication
    _CommSwitch($CMPortOptic)
(...)

and

Func SendData2($FileName, $LineNumber)
    $CmBoBaudScanner = 115200        ; Baud
    ;Start up communication
    _CommSwitch($CMPortScanner)
(...)

 

Link to comment
Share on other sites

  • 2 months later...

 I  have a device attach to a serial port, my  question is how do i do a  basic check  to know   or to find out,  if  the device is connected  to the serial port. I am not sure if there is a command line available, however I look around and google and I could not find anything. I would appreciate any help.

Thanks ahead

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...