Jump to content

Serial Port /COM Port UDF


martin
 Share

Recommended Posts

Hi martin,

if $sNextChar = '' then sleep(20)

Works perfectly... Thank you!

Ok, thanks for testing Adrian.

New versions uploaded.

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,

I have an application needs to have a baud rate of 230400. When I start my application (either under XP or 7, tested also with 2 different PC), the ComMG.dll (Ver 2.77) returns the error "The parameter is incorrect". The odd part is that the hardware support it (tested with using TeraTerm) and it worked no problem a that speed.

Is there any limitation in the DLL that prevent using that baud rate?

Thanks.

Dom

PS: Maybe I am wrong but I think that the CommMG.au3 V 2.88 you uploaded is a test version no the final one :)

Edited by CoolDomi
Link to comment
Share on other sites

Hi Martin,

I have an application needs to have a baud rate of 230400. When I start my application (either under XP or 7, tested also with 2 different PC), the ComMG.dll (Ver 2.77) returns the error "The parameter is incorrect". The odd part is that the hardware support it (tested with using TeraTerm) and it worked no problem a that speed.

Is there any limitation in the DLL that prevent using that baud rate?

Thanks.

Dom

PS: Maybe I am wrong but I think that the CommMG.au3 V 2.88 you uploaded is a test version no the final one :)

The version 2.77 of the dll only allowed up to 200000 Baud. I've increased this to 256000 in version 2.78.

UDF V2.88 was not intended to be a test version but it had a typo which stopped it running. I have corrected that in V2.89.

The downloads have been set for these versions.

EDIT:

Is there some other reason you thought the UDF V2.88 had a problem?

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

The version 2.77 of the dll only allowed up to 200000 Baud. I've increased this to 256000 in version 2.78.

UDF V2.88 was not intended to be a test version but it had a typo which stopped it running. I have corrected that in V2.89.

The downloads have been set for these versions.

EDIT:

Is there some other reason you thought the UDF V2.88 had a problem?

Martin, thanks for this update, but I still get the same error... In fact I tested a little bit further on and I get the error for any speed that is greater than 115200 bauds... any thoughts??

and no, no other reason that 2.88 had problem it was just not compiling.

But now that you ask I do have another low priority request: you still have some "ConsoleWrite" instead of your "mgdebugCW" into the CommMg.au3. I already changed them on the code I am using but if you could on the next version that would be great. Thanks a lot

- Dom

Link to comment
Share on other sites

The extra ConsoleWrites should have been removed. I have been trying to deal with this too quickly recently I think.

I found an error in setting non-standard baud rates but even after correcting that I get an error for anything above about 115200 as you say. I haven't found out why yet.

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

@CoolDomi

On my laptop if I set COM1 above 115200 baud I get an error with my dll.

Using a UBS to Serial port converter I can set 230400, and 256000 but not any higher. So it looks to me like it is not the dll alone which is preventing baud rates above 115200 but it is dependant on the device.

I can't find a version of TeraTerm which goes above 115200 Baud. The latest version I've tried is 3.1.3. Where did you get it?

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

I can't find a version of TeraTerm which goes above 115200 Baud. The latest version I've tried is 3.1.3. Where did you get it? martin

I found a version at this site that has a version 4.72 that handles baud rates up to 921600 according to the serial port set up menus. The actual website for this fork of TeraTerm is a sourceforge.jp site you should be able to download it from either site.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I found a version at this site that has a version 4.72 that handles baud rates up to 921600 according to the serial port set up menus. The actual website for this fork of TeraTerm is a sourceforge.jp site you should be able to download it from either site.

Thanks BrewManNH.

I have tried V4.72 from the link you gave and indeed I can set higher baud rates up to 912600 without a problem so it looks like there must be something wrong with my dll at high baud rates.

At the moment I can't guarantee I can fix it.

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

Sorry Martin,

I was out for the week-end... Thanks for the update, I am using one on these USB-serial converter (and not trying to use a speed higher than 230400).

Let me know if I can help to test.

Dom

PS: THX BrewManNH to step in with the link of TeraTerm

Link to comment
Share on other sites

Hi

I'm struggling a bit with this UDF, I am trying to send a string "/1ZR" to com3, using the code below. I'm getting "Error Variable Used Without being defined" error, any ideas?

Cheers

_CommSetport($iPort=3,ByRef $sErr=0,$iBaud=9600,$iBits=8,$ipar=0,$iStop=1,$iFlow=0,$RTSMode = 0,$DTRMode = 0)
_CommPortConnection()
_CommSendString("/1ZR" & @CR)
Link to comment
Share on other sites

Hi

I'm struggling a bit with this UDF, I am trying to send a string "/1ZR" to com3, using the code below. I'm getting "Error Variable Used Without being defined" error, any ideas?

Cheers

_CommSetport($iPort=3,ByRef $sErr=0,$iBaud=9600,$iBits=8,$ipar=0,$iStop=1,$iFlow=0,$RTSMode = 0,$DTRMode = 0)
_CommPortConnection()
_CommSendString("/1ZR" & @CR)

Try like this ...

Local $sErr
Local $PortName

_CommSetport(3, $sErr, 9600, 8, 0, 1, 0, 0, 0)
$PortName = _CommPortConnection()
_CommSendString("/1ZR" & @CR)

That should solve your error and I am not sure why you use _CommPortConnection() but this one return the name of the COM port it is connected to

Cheers

-Dom.

Link to comment
Share on other sites

  • 1 month later...

Can I use this to force a com port closed that is not being used by my current script? .

No.

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'm having some troubles using the _CommClosePort() function. I'm using v2.89 of the UDF and v2.78 of the dll. Using the free serial port monitor software from HHD, I found that a port in use is not really closed after calling _CommClosePort(). I noticed that the DllCall in the _CommClosePort() function returns 0 and sets the error code to 4. I guess this indicates the problem. Any ideas? I already tried adding _CommSwitch(1) as suggested before in this thread, but that didn't help.

bds

Link to comment
Share on other sites

Can you make the following change to the function _CommClosePort in the Commg.au3 udf.

Replace

DllCall($hDll, 'int', 'ClosePort', 'int');close cuirrent port

with

DllCall($hDll, 'int', 'CloseDown', 'int');close cuirrent port
exit;

then please let me know if this helps.

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

DllCall($hDll, 'int', 'CloseDown') seemed to work.

That's good, I thought it would but you never know. The problem with CloseDown is that it will close all ports rather than just the currently selected one.

I checked the dll with http://www.nirsoft.net/utils/dll_export_viewer.html and noticed that the ClosePort function cannot be called.

True, there is no such function in the dll. I should have mentioin that in my last post becauseI had discovered it after reading your problem. IPresumablyI had intended to rewrite the function but for some reason I obviously only removed the old one. I don't have the spare time I would like for this at the moment so and my best guess is that it will be up to 2 weeks before I give it attention.

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

  • 2 months later...

Martin,

Thank you very much indeed for your serial comms dll and your dedication to maintaining it. I discovered it and AutoIT about a year ago. I've been using it to retrieve data from my Davis weather station, using a pair of XBees for the serial link. I used your example com script to learn AutoIT by commenting each line with info from the Help file. I ended up using the example as the starting point for my script to get & display my weather data.

Recently, I've been using another pair of XBees to get data from a thermocouple-to-mV converter. The XBees have a number of analog & digital inputs & outputs in addition to D-in and D-out. In this mode, all communication through the serial port must be in hex packets or frames, as defined by the ZigBee protocol. The frame delimiter is hex 7E; there are no carriage returns or line feeds, the frame length is variable--about 21 bytes--and the frame does contain nulls. After several false starts, I ended up using _CommReadByte in a While loop looking for the 7E start byte. The next 2 bytes are the frame length, so I used that to create a struct and use _CommReadByteArray to get the rest of the frame. I again used your example script as the underlying structure. Trying to figure out when a function return was a decimal or hex, and if hex, did it have a 0x prefix, was a continuing problem.

By the way, why not just advise your 64-bit readers to just put #AutoIt3Wrapper_UseX64=n as the first line of any script using your dll; took me a while to figure that out, too, 'cause I was running my scripts without compiling them first.

I'm surprised I don't see any mention of XBees in your thread. They're great for cutting the serial cable and going wireless. There are a bunch of adapters for them for USB ports and Arduinos, etc. available through the experimenter web sites. Seems like most of the scripts for them are in Java or Processing. I haven't run across anyone else using AutoIT.

Alan

Link to comment
Share on other sites

  • 2 weeks later...

Hi Martin,

It's really a fantastic job which gave me a lot of help.

I defined two functions for sending (SendLine) and receiving (WaitFor) based on your UDF. And they works fine!

Local $buff, $dbgSwitch = 0

;============================================================================
; Name: SendLine
; Description: Send one command line
; Return value: 0 - Success
;            -1 - Failure
;============================================================================
Func SendLine($string)
     _CommClearInputBuffer()
     _CommClearOutputBuffer()
    
     _CommSendstring($string & @CR, 1) ; Wait till string sent
     If @error <> 0 Then
             Logger("! Error occured while sending string '" & $string & "', ErrCode: " & @error, 2)
             Return -1
     EndIf
     Logger("T -> " & $string, 6)
     Return 0
EndFunc

;============================================================================
; Name: WaitFor
; Description: Wait for desired output. Use it after each SendLine() to make buff always hold the latest content
; Return value: 0 - Success
;            -1 - Failure
;============================================================================
Func WaitFor($string, $timeout=2000)
     $buff = ""  ; Clear buff before each receiving
     If $string <> "" Then
             Local $timer = TimerInit()
             Do
                     $buff = _CommGetString()
                     If @error <> 0 Then
                             Logger("! Error occured while receiving string '" & $string & "', ErrCode: " & @error, 2)
                             Return -1
                     EndIf
                     If $buff = '' Then
                             Sleep(20)
                     Else
                             mgdebugCW($buff & @CRLF)
                     EndIf
                     If TimerDiff($timer) > $timeout Then
                             Logger("! Timeout when waiting for '" & $string & "'", 2)
                             Return -1
                     EndIf
             Until StringInStr($buff, $string) > 0
     EndIf
     Logger("R <- " & $buff & @LF, 6) ; @LF is for Unix->DOS convertion
     Return 0
EndFunc

Func Logger($msg, $logLevel = 8)
     Switch $logLevel
             Case 1
                     $levelInfo = "Level(1,User)"
             Case 2
                     $levelInfo = "Level(2,Warning)"
             Case 4
                     $levelInfo = "Level(4,Error)"
             Case 6
                     If $dbgSwitch <> "1" Then Return    ; Default: No debug info
                     $levelInfo = "Level(6,Debug)"
             Case 8
                     $levelInfo = "Level(8,Information)"
             Case Else
                     $levelInfo = "Level(?,None)"
     EndSwitch
     Local $logfile = $logFolder & "log" & '_' & @YEAR & @MON & @MDAY & '.txt'
     _FileWriteLog($logfile, $levelInfo & ", " & $msg)
EndFunc

WMI is used to find out the right comm port per DeviceID before the two functions are called, and all goes smoothly in the trial run.

I set up the script for our reliability testing, and the test will run automatically for more than two months.

If any UDF related problem, I'll let you know.

Finally, I'd like to give you a big thank you! !

Edited by AUTTRY
Link to comment
Share on other sites

Hi AUTTRY,

I'm glad you found the udf useful.

Your WaitFor function is faulty. If _CommGetString returns a few characters of the string you are waiting for each time it is called then you will never get the full string received to compare to the test string.

Maybe instead of

$buff = _CommGetString()

you could have

$buff &= _CommGetString()

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