Jump to content

Serial Port /COM Port UDF


martin
 Share

Recommended Posts

It looks that it works, but I think it was my error...

The uC answer my request with 2 bytes (something like 0A 89). For these reason I have to use "_CommReadByte" (2 times) instead of "_CommGetline". This way I'm receiving data.

So the communication shouldn't be the matter, even if I changed the dll you've posted.

I've had some trouble to understand what I received with "_CommReadByte". From the explanation in "CommMG.au3" I expected me 2 string like "0A"and "89", but before both strings will be converted to decimal value (so I receive a string "10" and another "137").

I've a rudimental test system, so I've had difficult to understand.

The solution that I've found is:

1) receive byte1

2) convert to hex with function Hex($byte1,2)

3) receive byte2

4) convert to hex with function Hex($byte2,2)

5) concatenate the two hex ($byte_rec = $byte1_hex & $byte2_hex)

6) convert $byte_rec to decimal (Dec($byte_rec)) to obtain the decimal value (the result).

Do you think this conversion/result is reliable?

Thank you very much!

Link to comment
Share on other sites

It looks that it works, but I think it was my error...

The uC answer my request with 2 bytes (something like 0A 89). For these reason I have to use "_CommReadByte" (2 times) instead of "_CommGetline". This way I'm receiving data.

So the communication shouldn't be the matter, even if I changed the dll you've posted.

I've had some trouble to understand what I received with "_CommReadByte". From the explanation in "CommMG.au3" I expected me 2 string like "0A"and "89", but before both strings will be converted to decimal value (so I receive a string "10" and another "137").

I've a rudimental test system, so I've had difficult to understand.

The solution that I've found is:

1) receive byte1

2) convert to hex with function Hex($byte1,2)

3) receive byte2

4) convert to hex with function Hex($byte2,2)

5) concatenate the two hex ($byte_rec = $byte1_hex & $byte2_hex)

6) convert $byte_rec to decimal (Dec($byte_rec)) to obtain the decimal value (the result).

Do you think this conversion/result is reliable?

Thank you very much!

Glad you got it working.

Perhaps I made things a bit awkward by returning a string of the decimal value, but I wanted to avoid the problem of dealing with null characters.

Anyway, your conversion should be OK, but I think the simplest way to deal with calculating the value of the two bytes is

$result = Execute($byte1) * 256 + $byte2

Even though byte2 is a string this will work, and $result will be a number, but if you don't like that you can have

$result = Execute($byte1) * 256 + Execute($byte2)

Edit:spelling

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • 5 weeks later...

I am thinking of making a new version so that multiple COM ports can be used.

The latest addition, not released yet, has been a setting for no flow control, and the ability to have 2 COM ports controlled if required. It may be that no-one needs more than 2 in which case I don't need to add multiple COM ports.

The have been over 200 downloads so there could be dozens of people using it, but I have very little idea of the applications or what people would like for extra features. Maybe some people didn't use it for a reason I don't know about.

To help me make the next version I would be interested in any wish lists, criticisms and problems. It would also be useful to know of any good points so that I don't design them out!

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Added link for V2 in first post.

Added option for flow control = NONE in _CommSetPort.

Added _CommSwitch function to switch between channels, up to 4 COM ports can be opened at once.

Next version will depend on any response to previous post.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • 4 weeks later...

Hi Martin,

I suppose you mean this thread to follow up. I got it working fine now, but go one small question left.

You asked :

NOt sure what you mean.

Do you mean you want to send 'Console Login' to a COM port, or do you mean you want to detect when 'Console Login' is received?

Answer :

I want to detect when 'Console Login:' is received. So I can use _CommSendstring to send the right string.

thx.

The more you learn, the less you know.

Link to comment
Share on other sites

I want to detect when 'Console Login:' is received. So I can use _CommSendstring to send the right string.

thx.

You can do it a number of ways. Here are 2. If these don't suit then you need to give me more information.

Assuming you have the port set up so that you can communicate then to wait for 'Console Login:'

Method 1) This will be ok if you are prepared to have the script wait for the prompt

$LoginPrompt = 'Console Login:'
while 1
  $ComSTring = _CommGetLine(':',0,0);':' = wait for a string ending in ':',0 =  don't care how long the returned string is, 0 = wait forever 
  if stringRight($ComString,StringLen($LoginPrompt)) = $LoginPrompt then exitloop
wend
;do the send login thing

Method 2) This would be better if you need to do other things while waiting for a response

Global $ComChr = '',$ComStr = '',$waitforLogIn = false
$LoginChr1 = StringLeft($LoginPrompt,1)

while 1;this could be your main loop with GuiGetMsg() for example
;some code
;
;
    
    if $WaitforLogin then
        $ComChr = _CommReadChar();get the next char,
        if $CommChr <> '' then
            $ComStr &= $ComChr
            if StringinStr($LoginPrompt,$Comstr) <> 1 then
                If $ComChr = $LoginChr1 Then
                    $ComStr = $LoginChr1
                Else
                    $Comstr = ''
                EndIf
                
            EndIf
            
            if $ComStr = $LoginPrompt then
                $waitforLogin = false
                DoLoginResponeThing()
            endif
        EndIf
    EndIf
    
wend

EDIT:Changed method 2. NB not tested

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Yup that's it,

thx M8!

I've written a very dirty code, but it works so :)

On more thing If I may, besides what I am sending and receceiving I would like to have a seperate dialog where I can see what's happening on the coms port (send and receive).

How should I go about that?

many thanks

The more you learn, the less you know.

Link to comment
Share on other sites

name='gertsolo' date='Sep 25 2007, 08:39 AM' post='408893']

Yup that's it,

thx M8!

NP, glad to help.

On more thing If I may, besides what I am sending and receceiving I would like to have a seperate dialog where I can see what's happening on the coms port (send and receive).

How should I go about that?

If you mean you want to spy on what is happening without interfering with it then you can't do that directly with my UDF.

If you had an extra 2 comports to break the connection then you could write the script to simply pass data between these two extra ports. With USB to COM port adapters that would be quite simple, but it's not so neat as being able to spy on a port. You would also need version 2 of my udf so that you could handle extra ports.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

K,

I'm gonna try and work your example file, where one can enter a string to send an see it passing along with the response, into my thingy.

Still kinda tricky for me being a novice and all, but hey...

anyway, any tip to get me started is always welcome.

Cheers!

The more you learn, the less you know.

Link to comment
Share on other sites

K,

I'm gonna try and work your example file, where one can enter a string to send an see it passing along with the response, into my thingy.

Still kinda tricky for me being a novice and all, but hey...

anyway, any tip to get me started is always welcome.

Cheers!

That's the spirit, you won't be a novice for long if you keep playing with your er, um, thingy.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

The latest addition, not released yet, has been a setting for no flow control, and the ability to have 2 COM ports controlled if required. It may be that no-one needs more than 2 in which case I don't need to add multiple COM ports.

The have been over 200 downloads so there could be dozens of people using it, but I have very little idea of the applications or what people would like for extra features. Maybe some people didn't use it for a reason I don't know about.

To help me make the next version I would be interested in any wish lists, criticisms and problems. It would also be useful to know of any good points so that I don't design them out!

I could definitely use multiple com ports to control multiple loaders (cd duplicators). And I also really appreciate the new flow control setting, very useful for the loaders as they typically only have 2 or 3 wires wired up to the serial port.

I just coded this up today and ran into the flow control problem on one unit. And voila, your v2 update solved it!

C:\SYNC-BGH\development\_AutoIT-LoaderCLI>UniversalLoaderCLI.exe --drive=d --open --command=C:X:60:10:E --comport=COM2
port = 2, baud = 9600, bits = 8, par = 0, stop = 1, flow = 2
CX
C:\SYNC-BGH\development\_AutoIT-LoaderCLI>UniversalLoaderCLI.exe --drive=d --open --command=I:X:60:10:E --comport=COM2
port = 2, baud = 9600, bits = 8, par = 0, stop = 1, flow = 2
IE
C:\SYNC-BGH\development\_AutoIT-LoaderCLI>

Thanks martin!

-brendan

Link to comment
Share on other sites

I could definitely use multiple com ports to control multiple loaders (cd duplicators). And I also really appreciate the new flow control setting, very useful for the loaders as they typically only have 2 or 3 wires wired up to the serial port.

I just coded this up today and ran into the flow control problem on one unit. And voila, your v2 update solved it!

That's the fastest fix I've ever done!

Thanks martin!

-brendan

It's a pleasure.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Hi martin and all,

CommMG is a fantastic tool your work is great, i have been testing it last days for sending and receiving info to my digital satellite receiver and it works fine, but i am sending and receiving binary data. The problem is that sending binary data byte by byte, is slow, could you please allow to send an array of binary data in a function like _CommSendByteArray($byteArray,$numOfBytes,$iWaitComplete=0)? It will be interesting if we could read binaryArrays of data from com port in a function like _CommReadByteArray($byteArray,$numOfBytes,$wait=0) it will be faster for transfering and receiving binary data and fantastic for sending large amounts of binary data.

Thanks again for your work.

Link to comment
Share on other sites

Hi martin and all,

CommMG is a fantastic tool your work is great, i have been testing it last days for sending and receiving info to my digital satellite receiver and it works fine, but i am sending and receiving binary data. The problem is that sending binary data byte by byte, is slow, could you please allow to send an array of binary data in a function like _CommSendByteArray($byteArray,$numOfBytes,$iWaitComplete=0)? It will be interesting if we could read binaryArrays of data from com port in a function like _CommReadByteArray($byteArray,$numOfBytes,$wait=0) it will be faster for transfering and receiving binary data and fantastic for sending large amounts of binary data.

Thanks again for your work.

Thanks for the compliments.

I can see the need for a better way to deal with binary data. To send or receive an array I think it would be best, and easiest for me, if I did something like this-

_CommSendBytes($pSendAddress,$numOfBytes,$Wait = 0)

_CommReadBytes($pRcvAddress,$numOfBytes,$Wait = 0)

So to send the data you would have something like

$Length = 1000;1k
$Bytestruct = DllStructCreate('byte[' & $Length & ']')
$pSendAddress = DllStructGetPtr($ByteStruct)
_CommSendBytes($pSendAddress,$Length)

How does that sound?

When you send/rcv binary data is there any handshaking required? How do you know when the binary data has all been received or is it always a fixed number of bytes?

Unfortunately the pressure of work means I won't be making these new functions quickly, but the ideas can be developed.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Martin, it sounds great! When i send/rcv binary data there is not any handshaking required. If i send the data correctly formatted (with crc32 in data indicated, required by the software in the receiver), the receiver returns data and i read it, i could call _CommGetInputCount() to detect if there is any data pending to read and _CommGetOutputCount() to detect if there is any data pending to send from the port, and wait until it is all ok.

About your question, it is not always a fixed number of bytes, but i could call _CommGetInputCount() to know if there are binary bytes pending to be read.

Thanks in advance martin

Link to comment
Share on other sites

love this UDF, using it currently on a machine to pc communication thanks for sharing. ;)

Glad it's useful smooth, welcome to the forums :) .

(Communicating with machines is my interest too.)

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Hey Martin,

coming back on a previous question of mine, this is what I want to do:

I know the sequence of actions to perform on the port (I know upon receiving data what I need to send)

I have made an input GUI where I can enter all the necessary parameters that I need. After that input I can startup communication and send the right stuff at the right time.

This process, sending and receiving, I would like to see happening (like your example in the $Edit1)

At the moment I have a bunch of if clauses checking what the port is receiving and based on that input I send stuff, something like this:

; do the login

while 1

_CommSendstring(@CR)

Traytip("Status","Waiting for Login prompt",3)

$ComSTring = _CommGetLine(':',0,0);':' = wait for a string ending in ':',0 = don't care how long the returned string is, 0 = wait forever

if stringRight($ComString,StringLen($LoginPrompt)) = $LoginPrompt then exitloop

wend

_CommSendstring($Login & @CR)

; enter password

while 1

Traytip("Status","Waiting for password prompt",1)

$ComSTring = _CommGetLine(':',0,0);':' = wait for a string ending in ':',0 = don't care how long the returned string is, 0 = wait forever

if stringRight($ComString,StringLen($PwPrompt)) = $PwPrompt then exitloop

wend

_CommSendstring($Login & @CR)

; enter IP address

while 1

Traytip("Status","Waiting for IP prompt",1)

$ComSTring = _CommGetLine(':',0,0);':' = wait for a string ending in ':',0 = don't care how long the returned string is, 0 = wait forever

if stringRight($ComString,StringLen($Network)) = $Network then exitloop

wend

_CommSendstring($InNet & @CR)

; enter IP subnet mask

while 1

Traytip("Status","Waiting for subnet prompt",1)

$ComSTring = _CommGetLine(':',0,0);':' = wait for a string ending in ':',0 = don't care how long the returned string is, 0 = wait forever

if stringRight($ComString,StringLen($Subnet)) = $Subnet then exitloop

wend

_CommSendstring($InSub & @CR)

; enter IP Gateway

while 1

Traytip("Status","Waiting for gateway prompt",1)

$ComSTring = _CommGetLine(':',0,0);':' = wait for a string ending in ':',0 = don't care how long the returned string is, 0 = wait forever

if stringRight($ComString,StringLen($Gateway)) = $Gateway then exitloop

wend

_CommSendstring($InGat & @CR)

I would like to alter your example so I can integrate this.

Could you help me out a bit?

Basically, I need to know where to put the if statements and handle them one at a time. If I put them all in the SendEvent Function I will get the response after all the if's are handled. I would like to see it after each if. (if you understand my French)

Edited by gertsolo

The more you learn, the less you know.

Link to comment
Share on other sites

Hey Martin,

coming back on a previous question of mine, this is what I want to do:

........

Not much time to spend on this at the moment, so here's a quick idea but just written with no testing, checking or very much thought.

It assumes you have an edit somewhere to display text from the COM port.

;do the login
Global $Loginwanted = False, $loginPosition
Dim $loginSequence[5] = [$PwPrompt,$Network,$Network, $Subnet, $Gateway]
Dim $loginToolTip[5] = ["Waiting for Login prompt", "Waiting for password prompt", "Waiting for IP prompt", "Waiting for subnet prompt", "Waiting for gateway prompt"]
Dim $LoginRespons[5] = [$Login, $Login, $InNet, $InSub, $InGat]

_CommSendstring(@CR);not sure if you need this

While 1;main while -idle loop
    
    
    If $Loginwanted Then $DoLog($LoginPosition)
    
    
WEnd


Func SendEvent1(); whatever calls this
    If Not $loginwanted Then $loginPosition = 1;start from beginning of login
    $loginwanted = True; we want to log in
    
EndFunc
;

Func DoLOgin(ByRef $ou)
    
    Traytip("Status",$LoginToolTip[$ou],3)
    $ComSTring = _CommGetLine(':',0,0);':' = wait for a string ending in ':',0 = don't care how long the returned string is, 0 = wait forever
    $editText = GUICtrlRead($edit1)

    if stringRight($ComString,StringLen($loginSequence[$ou])) = $loginSequence[$ou] then
        _CommSendstring($LoginRespons[$ou] & @CR)
        guictrlsetdata($edit1,$EditText & $Comstring & $LoginRespons[$ou] & @CR)
        $ou += 1;
    Else
        guictrlsetdata($edit1,$EditText & $Comstring); Might want to add @CR?
    EndIf

    If $ou = 6 Then
        $ou = 0
        $loginwanted = False; we have finished logging in
    endif

EndFunc
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
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...