Jump to content

Serial Port /COM Port UDF


martin
 Share

Recommended Posts

Thanks Martin... In some days I'll try it and we came back to talk... ^_^

The site that I downloaded the file is a server that you did with autoit? Martin's Autoit Downloader.

Regards,

Brunomusw

No, it's written in PHP.

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

I wrote a version which gave greater control over the handshaking lines which might do what you want. It also has some other additions. You would need to set the port twice with different options to toggle the RTS line on and off. The explanation of the settings is given in the comments before the _CommSetport function. I don't remember why this hasn't been included in the current version, maybe there's a problem and you can tell me

You can get it from here. Because it was experimental I called the dll commgaa.dll and the udf is Commg2_5.au3 instead of commg.au3.

Martin, I tested your new functions and I couldn´t control the RTS and DTR pins.

When flowcontrol = 0 (flowcontrol on) it doesn´t matter if RTS and DTR are 0 or 1 or 2 or 3, both changed to 1 to 0 logics and stayed on 0, but when I turn the flowcontrol = 2 (flowcontrol off) both goes to 1. Soo both are changing the logic levels on the same time and It couldn´t stay on diferents logics levels.

One thing, on your CommMG2_5.au3 on the _CommSetPort you include the Commg.dll and not the new one. I did It but still have the same problems.

Could I separete the _CommSetPort function just to control the RTS and DTR pins? Change the dll??

regards,

Brunomusw

Link to comment
Share on other sites

Martin, I tested your new functions and I couldn´t control the RTS and DTR pins.

When flowcontrol = 0 (flowcontrol on) it doesn´t matter if RTS and DTR are 0 or 1 or 2 or 3, both changed to 1 to 0 logics and stayed on 0, but when I turn the flowcontrol = 2 (flowcontrol off) both goes to 1. Soo both are changing the logic levels on the same time and It couldn´t stay on diferents logics levels.

One thing, on your CommMG2_5.au3 on the _CommSetPort you include the Commg.dll and not the new one. I did It but still have the same problems.

Could I separete the _CommSetPort function just to control the RTS and DTR pins? Change the dll??

regards,

Brunomusw

Sounds like I made a mistake with the dll name in the UDF so apologies for that. When I tested it I probably kept the name as the standard name.

I'll look at it again this weekend; I'm sure I can set the RTS line on or off so maybe I'll add a new function.

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

Sounds like I made a mistake with the dll name in the UDF so apologies for that. When I tested it I probably kept the name as the standard name.

I'll look at it again this weekend; I'm sure I can set the RTS line on or off so maybe I'll add a new function.

Just a function for set RTS and DTS let it better... Talking about it, is it hard to read the DCD, DST and CTS? Is it possible with Tx and Rx?

Thanks Martin... I'll way for your answer... ^_^

Brunomusw

Link to comment
Share on other sites

Martin, any news about the library???

best regards,

brunomusw

Forgot all about it brunomusw! I remembered I had a new function to make for my print udf but it seems there is only room in my brain for one job. I'll have a look tonight.

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

  • 2 weeks later...

Hello.

I need to make a simple serial port relay like this:

http://www.windmeadow.com/node/4 (there is a C code)

and this:

http://stats.the-abbeys.com/blog/index.php...ry060116-095620 (there is a C code too)

As far I know I can control voltage (not precisely, on off control is fine for me) of pins 4-5 (DTR and Gnd) and other one couple (not so sure witch). Can I do this with this UDF and if YES - how, and if NOT - please let some real programmer do a DDL with UDF or just simple command line tool to do this like "port.exe COM5 DTRGND OFF" or someting like that.

Very thank you in advance.

BR

Yavor Indzhev

Link to comment
Share on other sites

  • 2 weeks later...

I must be missing something obvious.

I'm startting with just a snippet of code, to test communication with a Mitutoyo drop gage on COM1:

#include <CommMg.au3>
While 1
$instring = _CommGetString()
If $instring <> "" Then
MsgBox(4096, "data received", $instring)
Exit
EndIf
Sleep(500)
WEnd

When I run this, I get "error in _commgetstring".

If I replace _CommGetString() with $instring = _CommGetLine(@CR,0,5000), I simply get nothing.

CommGetVersion returns the appropriate dll or au3 version (2.1 in both cases).

ComListPorts returns COM1 as expected (the only port on the machine).

CommSetPort(1, $sErr, 9600, 8, 0, 1, 2) returns 1 (no error)

CommPortConnection returns COM1

If any of the above four lines are above the CommGetString line, I don't get the "error in _commgetstring" error, but I don't get anything else, either.

With Hyperterminal, I do get data:

P02,M2102,2.4,U0D0,G#099(051),f=1-2+e-,o=P+U+N+H+ (at initialization) or:

2, .90415, ,02(051),f=1-2+e-,o=P+U+N+H+ (when data is sent)

The gage interface has an LED that indicates that data is ready. Hyperterminal clears this LED when the string is received, then it flashes when a new string is sent. The AutoIt script never clears the LED.

Even "copy com1: con" at a command prompt echos the received data and lets the LED's go out.

What am I doing wrong?

Edited by Dana
Link to comment
Share on other sites

What am I doing wrong?

Quite a lot I think :)

I suggest you have a look at my example first in the first post or at least read through the comments in the UDF. For example, you cannot use COM1 or any other port with my UDF untill you have set the features of the port first using _CommSetPort. Even Hyperterminal needs to be told how to set the port , so if you don't know the baud rate, parity, stop bits etc then maybe you can get the values used in Hyperterminal.

The minimum you need is

_CommSetPort at th estart

then use

_CommSendString

_CommGetString

EDIT: I diodn't noptice, welcome to the AutoIt forums.

I will admit that my example is not as simple as it could be so ask again if you are stuck.

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

Martin, thanks for the reply (and the welcome... actually I've been using AutoIt for some years now for various things, some pretty elaborate, but never tried talking to a com port before).

Right, I did try using _CommSetPort before _CommGetString as I said, but although I don't then get the error, I don't get any data, either. The port parameters I gave above are the same ones that Hyperterminal successfully uses.

Ah... got it. :) I changed:

_CommSetPort(1, $sErr, 9600, 8, 0, 1, 2)

to

_CommSetPort(1, $sErr, 9600, 8, 0, 1, 0)

and now I'm receiving data. This seems odd because I rechecked the hyperterminal settings and it's using "none" for flow control... but according to the comments in commmg.au3, the last "0" means hardware flow control, "2" for none... is this correct?

Could it be that the device is sending a hardware handshake and hyperterminal (or the command prompt "copy com1:") is seeing that and changing it on the fly? It's an old device and I don't have any documentation for it.

At any rate, now that I'm receiving data, I can proceed with the actual programming.

Link to comment
Share on other sites

Martin, thanks for the reply (and the welcome... actually I've been using AutoIt for some years now for various things, some pretty elaborate, but never tried talking to a com port before).

Right, I did try using _CommSetPort before _CommGetString as I said, but although I don't then get the error, I don't get any data, either. The port parameters I gave above are the same ones that Hyperterminal successfully uses.

Ah... got it. :) I changed:

_CommSetPort(1, $sErr, 9600, 8, 0, 1, 2)

to

_CommSetPort(1, $sErr, 9600, 8, 0, 1, 0)

and now I'm receiving data. This seems odd because I rechecked the hyperterminal settings and it's using "none" for flow control... but according to the comments in commmg.au3, the last "0" means hardware flow control, "2" for none... is this correct?

Could it be that the device is sending a hardware handshake and hyperterminal (or the command prompt "copy com1:") is seeing that and changing it on the fly? It's an old device and I don't have any documentation for it.

At any rate, now that I'm receiving data, I can proceed with the actual programming.

I thought that the code you posted was what you were using to communicate, it was obvious to me that it wasn't. Gladd you fixed it without my help anyway.

Yes, the flow parameter should be 0 for hardware handshaking, but it isn't the last parameter in the latest versions, though the extra parameters aren't usually needed and they have default values.

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

  • 1 month later...

Hi martin, I really want to be able to use this UDF but when i try to read from my port i get the error that says that the "port does not exist" (using the example)

The port I am using is an emulated port from a Bluetooth connection with a GPS-unit, i do get data when using HyperTerminal.

Is there known issues with emulated ports?

[font="Impact"]Use the helpfile, It´s one of the best exlusive features of Autoit.[/font]http://support.microsoft.com/kb/q555375ALIBI Run - a replacement for the windows run promptPC Controller - an application for controlling other PCs[size="1"]Science flies us to the moon. Religion flies us into buildings.[/size][size="1"]http://bit.ly/cAMPZV[/size]
Link to comment
Share on other sites

Hi martin, I really want to be able to use this UDF but when i try to read from my port i get the error that says that the "port does not exist" (using the example)

The port I am using is an emulated port from a Bluetooth connection with a GPS-unit, i do get data when using HyperTerminal.

Is there known issues with emulated ports?

No issues that I can think of with emulated ports.

The example gives a list of the available ports, so do you mean you select the port and then later my program says that the port doesn't exist?

If the port appears in device manager under ports you should be able to use it with my udf.

If Hyperterminal can use it then so can my udf.

Can you post some code that is failing? What OS are you using? (I don't know of anyone using this udf in Windows 7)

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

Well, you do already got the code as it is your example, and yes, I do get the port enumerated in the list, which is kinda wierd :)

Might it be that HyperTerminal uses some other API for accessing the ports?

I am on a WinXP SP3 machine.

[font="Impact"]Use the helpfile, It´s one of the best exlusive features of Autoit.[/font]http://support.microsoft.com/kb/q555375ALIBI Run - a replacement for the windows run promptPC Controller - an application for controlling other PCs[size="1"]Science flies us to the moon. Religion flies us into buildings.[/size][size="1"]http://bit.ly/cAMPZV[/size]
Link to comment
Share on other sites

Well, you do already got the code as it is your example, and yes, I do get the port enumerated in the list, which is kinda wierd :)

And the port number?

Might it be that HyperTerminal uses some other API for accessing the ports?

I don't know but it's more likely that there is some error in my code.

I am on a WinXP SP3 machine.

So am I.
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

It's emulated to port COM14, I might able to change that though, I'll try that.

Edit: i'll see if it can be changed. . .

Edited by colafrysen
[font="Impact"]Use the helpfile, It´s one of the best exlusive features of Autoit.[/font]http://support.microsoft.com/kb/q555375ALIBI Run - a replacement for the windows run promptPC Controller - an application for controlling other PCs[size="1"]Science flies us to the moon. Religion flies us into buildings.[/size][size="1"]http://bit.ly/cAMPZV[/size]
Link to comment
Share on other sites

I changed the port to COM1 and it now receives data :)

Well, hope i'll get this working. and thanks for the UDF :)

[font="Impact"]Use the helpfile, It´s one of the best exlusive features of Autoit.[/font]http://support.microsoft.com/kb/q555375ALIBI Run - a replacement for the windows run promptPC Controller - an application for controlling other PCs[size="1"]Science flies us to the moon. Religion flies us into buildings.[/size][size="1"]http://bit.ly/cAMPZV[/size]
Link to comment
Share on other sites

I changed the port to COM1 and it now receives data :)

Well, hope i'll get this working. and thanks for the UDF :)

I'm glad you found a fix, but I'll investigate the problem with COM14.

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

  • 2 weeks later...

Hello

Thanks for the UDF! It's very nice to be able to talk Autoit with my Arduino. I'm having one small problem perhaps someone could help with. My Arduino program listens for a command over the serial port (numbers 0-9), switches the corresponding relay on my circuit board, sends a confirmation (1 hi), then switches it off when it recieves a different command (letters A-H) and confirms (1 lo). Everything was working great when I was using Autoit to initiate the sequence (using a button). I've since added a button on my circuit board which sends the command "go" to Autoit over the serial bus. Once I've added the bit of code to listen for serial command, none of my buttons in the GUI work anymore.

If (_CommSetPort (5, $setport_error, 9600, 8, 0, 1, 0)) <> 1 Then
    MsgBox (0, $t, "SetPort error: " & $setport_error)
    Fin ()
EndIf
Sleep (2000)

_Main ()

Func _Main ()

    ;gui
    GUICreate ($t, 800, 600)    ;main window
    
    For $e = 0 To 9     ;creates a matrix of labels and buttons
        If $e = 0 Then $x = 25
        If $e = 5 Then $x = 645
        If $e < 5 Then $y = 250
        if $e >= 5 Then $y = 25
        $lbl[$e] = GUICtrlCreateLabel ("", $x, $y, 130, 200, $WS_BORDER)
        $btn[$e] = GUICtrlCreateButton ("", $x, $y, 130, 200, $WS_BORDER)
        GUICtrlSetState ($lbl[$e], $GUI_HIDE)
        IF $e < 5 Then $x = $x + 155
        If $e >= 5 Then $x = $x - 155
    Next
    
    $btn_program = GUICtrlCreateButton ("Program All", 25, 466.725, 750, 100)
    GUICtrlSetFont (-1, 32)
    GUISetState()
    While 1
        $msg = GUIGetMsg()
        
        
        Switch $msg
            Case $GUI_EVENT_CLOSE
                Fin ()

            Case $m_exit
                Fin ()
                
            Case $btn_program
                _Start ()

            Case $btn[0]
                _Program (0)
            Case $btn[1]
                _Program (1)
                EndSwitch

        $inn = _CommGetLine (@CR, 0, 0)
        If StringInStr ($inn, "go") <> 0 Then
            _Start ()
        EndIf
    WEnd
    
EndFunc

I've also tried putting the _CommGetLine into the Case Else of my GUI loop but it doesn't work either. With the code, my hardware button works to initiate _Start (), but not my GUI buttons. Any ideas?

Sorry if this is off topic, I do not mean to jack your thread and I'd be happy to start a new thread if you'd like. Thanks!

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...