Jump to content

Serial Port /COM Port UDF


martin
 Share

Recommended Posts

I seem to be having trouble with the '_CommSendByteArray' function. I have tried a few examples from this post, and all appear to fail (-1). '_CommSendByte' works fine.

Here is one of the examples I have tried:

#include <CommMg.au3>
$Numbytes1 = 16;say we have 16 bytes

$Struct1 = DllStructCreate("byte[" & $Numbytes1 & "]")

For $n = 1 To 16;put some random numbers in the struct
 DllStructSetData($Struct1, 1, Random(0, 255, 1), $n)
Next

;send the bytes
_CommSendByteArray(DllStructGetPtr($Struct1), $Numbytes1, 1)
if @error then MsgBox(0, 'ERROR', '_CommSendByteArrray failed')

I am using AutoIt v3.3.4.0 on WinXP SP3.

Any thoughts?

Thanks for the great stuff!

First thing is that sendbytearray will only work when a port has been opened, so use _CommSetPort(1,$sErr) first or some other function which opens the dll.

Second but more important, because of some cock up, the functions SendByteArray and ReadByteArray have been wiped from the dll.Posted Image

I have now updated the dll and the download should now get you a version which works.

Apologies for the error.

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

Are these 5 other ports on something like a PCI card or usb? Do you see all 6 ports in device manager?

I add print screen in previous post.

I ran your script and pasted all the ports on my device manager.

EDIT: I know close to nothing about W7, but could you try running the program in compatibility mode for XP SP3?

Autoit3_x64 compiled script. 64-bit versions of windows is the only Vista, Server 2008 and W7.

If you compile in 32bit everything works fine.

Edited by kmeleon

Feed Polish children: pajacyk - just go to this page and close it. Thanks. World website: free rice.

Link to comment
Share on other sites

I add print screen in previous post.

I ran your script and pasted all the ports on my device manager.

Autoit3_x64 compiled script. 64-bit versions of windows is the only Vista, Server 2008 and W7.

If you compile in 32bit everything works fine.

Ah. The dll is 32 bit and I don't think you can use a 32 bit dll from a 64 bit app. I'll add a note in the first 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

Man, i cant send Ctrl-z

I tested _CommSendByte(26,0) and _CommSendString(Chr(26))

I need to send Ctrl-z to the modem to send a SMS

Thanks

$AT_send = 'AT+CMGS="'
$number = "+123456789"
$time = 10
$text = "Send SMS"
$data1 = (""&$AT_send &$number&'"'&@CR)
$data2 = (""&$text &Chr(26) &@CR)
sleep(10)
_CommSendString($data1,$time)
sleep(400)
_CommSendString($data2,$time)

Feed Polish children: pajacyk - just go to this page and close it. Thanks. World website: free rice.

Link to comment
Share on other sites

Man, i cant send Ctrl-z

I tested _CommSendByte(26,0) and _CommSendString(Chr(26))

I need to send Ctrl-z to the modem to send a SMS

Thanks

Both of those should work. Maybe, as kmeleon showed, you need to end the string with @CR.

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

MAny thanks for your answers.

It works!

but i must had to change:

$AT_send = 'AT+CMGW='  
$number = '"+34666555444"'
$time = 10

And there is no need to send @CR after sending Ctrl-Z,

The question is: now it works, but just because i added $time ?

in my tests i didnt use $time option. Maybe it was the problem... :(

Link to comment
Share on other sites

  • 2 weeks later...

Hello Martin,

i don't know if you remember me, but we corresponded about your UDFs a few years ago. Since then i have used them reliably.

One question though. Is there a way to test if a serial cable has been removed after a successful connection has been made? The _CommPortConnection() function does not test for this type of (dis)connection.

For USB to serial cables, I've used the error codes returned by _CommSetPort() function to detect disconnection, but this does not work with the standard (on board) UART since a cable removal does not result in the COM port being disabled like with the USB to serial cables. Sending a string with a UART type does not result in an error that I can use for detection.

EDIT: Removing a USB to Serial cable at the DEVICE end, does not result in the COM port disappearing from the computer's hardware configuration (in this way it mimic's the standard UART). Disconnecting it from the COMPUTER end WILL result in removal of the COM port from the computer's configuration.

By the way I am still using version 2.1 :(

Thanks in advance.

Regards,

Keymaker

Edited by Keymaker
Link to comment
Share on other sites

Hello Martin,

i don't know if you remember me, but we corresponded about your UDFs a few years ago. Since then i have used them reliably.

One question though. Is there a way to test if a serial cable has been removed after a successful connection has been made? The _CommPortConnection() function does not test for this type of (dis)connection.

For USB to serial cables, I've used the error codes returned by _CommSetPort() function to detect disconnection, but this does not work with the standard (on board) UART since a cable removal does not result in the COM port being disabled like with the USB to serial cables. Sending a string with a UART type does not result in an error that I can use for detection.

EDIT: Removing a USB to Serial cable at the DEVICE end, does not result in the COM port disappearing from the computer's hardware configuration (in this way it mimic's the standard UART). Disconnecting it from the COMPUTER end WILL result in removal of the COM port from the computer's configuration.

By the way I am still using version 2.1 :(

Thanks in advance.

Regards,

Keymaker

Hi Keymaker,

I don't have much time to deal with this at the moment. If the device you connect to can set a line high, like ring indicator o data terminal ready, then I could add something to check it. Then if the connector is removed, or the equipment is turned off the th esignal will go.

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

Hi everybody,

I've got only a little question. I hope it's O.K. to write directly into this topic!

Is it possible to read the CTS status?

I found that it's possible to set the RTS status, but how to read at the receiver-end the CTS? (I use a crosslink cable)

I'm very sorry for my bad english!

Thank you for your reply!

Kind Regards

Veronesi

Link to comment
Share on other sites

Hi everybody,

I've got only a little question. I hope it's O.K. to write directly into this topic!

Is it possible to read the CTS status?

I found that it's possible to set the RTS status, but how to read at the receiver-end the CTS? (I use a crosslink cable)

I'm very sorry for my bad english!

Thank you for your reply!

Kind Regards

Veronesi

I didn't notice your post here at first Veronesi, but I replied to your post in general help.
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 didn't notice your post here at first Veronesi, but I replied to your post in general help.

Here is a new version of the udf and dll to test. If it is OK then I will remove it from here and update the first post.

Read the udf to see the new function GetLineStates.

It would be better to have a custom defined function to be called when a line state changed but that will have to wait till I have more time.

The new function returns an array of 4 elements giving the ON state (True or False) for the lines CTS, DSR, Ring Indicator and DCD.

(I also corrected the returns from the functions SetDTR and SetRTS when there is an error. Note that SetRTS and SetDTR only work if flow control is set to none or xon/xoff. They do not work if hardware handshaking is set because then the lines are controlled by the data flow only.)

CommMG.zip

Any results, good or bad, I would like to know about please.

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

Here is a new version of the udf and dll to test. If it is OK then I will remove it from here and update the first post.

Read the udf to see the new function GetLineStates.

It would be better to have a custom defined function to be called when a line state changed but that will have to wait till I have more time.

The new function returns an array of 4 elements giving the ON state (True or False) for the lines CTS, DSR, Ring Indicator and DCD.

(I also corrected the returns from the functions SetDTR and SetRTS when there is an error. Note that SetRTS and SetDTR only work if flow control is set to none or xon/xoff. They do not work if hardware handshaking is set because then the lines are controlled by the data flow only.)

CommMG.zip

Any results, good or bad, I would like to know about please.

Hi Martin,

thank you very much.

But my AutoIt crash with this command!

My code:

Local $COM1, $COM2, $sErr = "", $aStatus[4]
    GUICtrlSetBkColor($CheckBoxNr,$LightGray)
    IESetText($Text, $LightGreen, $Size)
    _CommSetDllPath(@TempDir & "\commg.dll")
    
    If Not FileExists(@ScriptDir & "\Settings.ini") Then ;No File for COM-Port settings found. Create default File!
        IniWrite(@ScriptDir & "\Settings.ini","COM-Ports","COM1","1")
        IniWrite(@ScriptDir & "\Settings.ini","COM-Ports","COM2","4")
    EndIf
    
    $COM1 = IniRead(@ScriptDir & "\Settings.ini","COM-Ports","COM1","1") ;Read COM-Port settings from File
    $COM2 = IniRead(@ScriptDir & "\Settings.ini","COM-Ports","COM2","4")
        
    _CommSwitch(1)
    _CommSetPort($COM1,$sErr,9600,8,0,1,2,0,0) ;Define Com-Port (first usable Com-Port!)
    MsgBox(0,"","Before SetRTS")
    _CommSetRTS(1)
    MsgBox(0,"","After SetRTS")
    _CommSwitch(2)
    _CommSetPort($COM2,$sErr,9600,8,0,1,2,0,0) ;Define Com-Port (first usable Com-Port!)
    $aStatus = _CommGetLineStates()
    
    MsgBox(0,"",$aStatus[3])
        
    _CommSwitch(1)
    _CommSetRTS(0)
    _CommClosePort()

I can see the first MsgBox (Before SetRTS)!

But before the second MsgBox will appear, AutoIt crashes! (Exit code: 250477278)

But I can see the RTS (DCD) was set correctly! (I've a seperate Terminal viewer)

Why this crash?

Kind Regards

Veronesi

Link to comment
Share on other sites

So, I changed my script a little bit and added a Sleep(100) after the _CommSwitch(1) command.

Local $COM1, $COM2, $sErr = "", $aStatus[4]
    GUICtrlSetBkColor($CheckBoxNr,$LightGray)
    IESetText($Text, $LightGreen, $Size)
    FileInstall("..\Functions\commg.dll",@TempDir & "\commg.dll",1)
    _CommSetDllPath(@TempDir & "\commg.dll")
    
    If Not FileExists(@ScriptDir & "\Settings.ini") Then ;No File for COM-Port settings found. Create default File!
        IniWrite(@ScriptDir & "\Settings.ini","COM-Ports","COM1","1")
        IniWrite(@ScriptDir & "\Settings.ini","COM-Ports","COM2","4")
    EndIf
    
    $COM1 = IniRead(@ScriptDir & "\Settings.ini","COM-Ports","COM1","1") ;Read COM-Port settings from File
    $COM2 = IniRead(@ScriptDir & "\Settings.ini","COM-Ports","COM2","4")
        
    _CommSwitch(1)
    Sleep(100)
    _CommSetPort($COM1,$sErr,9600,8,0,1,2,0,0) ;Define Com-Port (first usable Com-Port!)
    _CommSetRTS(1)
    _CommSwitch(2)
    _CommSetPort($COM2,$sErr,9600,8,0,1,2,0,0) ;Define Com-Port (first usable Com-Port!)
    $aStatus = _CommGetLineStates()
    
    _CommSwitch(1)
    _CommSetRTS(0)
    _CommClosePort()
    
    If $aStatus[3] Then ;Check if RTS (DCD) was set!
        GUICtrlSetBkColor($CheckBoxNr,$GUIBackgroundColor) ;Set color for Checkbox Background
        GUICtrlSetColor($CheckBoxNr,$LightGreen) ;Set color for Checkbox Text
        GUICtrlSetState($CheckBoxNr,1) ;Set Checkbox checked
        Return 1
    Else
        GUICtrlSetBkColor($CheckBoxNr,$LightRed) ;Set color for Checkbox Background
        GUICtrlSetState($CheckBoxNr,1) ;Set Checkbox checked
        Return 0
    EndIf

Now it's working (for the moment; WinXP 32bit)

I hope AutoIt will not crash again in the future!

But well done! Thank you very much!

Best Regards

Veronesi

Edited by veronesi
Link to comment
Share on other sites

Hmm, on my Windows 7 (64bit) AutoIt always crash when I try to set the RTS Line (_CommSetRTS(1))

Any idea?

I don't know. I have tested on XP PRO and W7 PRO, both 32 bit, and I didn't get any problems. I don't have a 64 bit version of W7 to test untill the end of the month. I assume you were running the script in 32 bit mode since the dll will not run in 64 bit.?

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 don't know. I have tested on XP PRO and W7 PRO, both 32 bit, and I didn't get any problems. I don't have a 64 bit version of W7 to test untill the end of the month. I assume you were running the script in 32 bit mode since the dll will not run in 64 bit.?

Yes, I had to run the script in 32bit mode.

The problem was an other: My serial com-port was blocked in any way. (USB-to-Serial).

Restart, unplug serial cable, replug and try again => Now it's working!

Thanks again for your great work!

Kind Regards

Veronesi

Link to comment
Share on other sites

Yes, I had to run the script in 32bit mode.

The problem was an other: My serial com-port was blocked in any way. (USB-to-Serial).

Restart, unplug serial cable, replug and try again => Now it's working!

Thanks again for your great work!

Kind Regards

Veronesi

Ah, good news :idea:

I'll update the first post with the latest version later today.

Thanks Veronesi.

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

Ah, good news :idea:

I'll update the first post with the latest version later today.

Thanks Veronesi.

Only a small question:

I have to test some products with two serial ports.

In testmode, all Bytes which I send to the first port of this product are directly redirected to the second port. (Without any measurable delay)

At the moment I use two AutoIt programms: One for sending, and one for receiving. Because I want only one EXE I installed the "receive.exe" into the "send.exe" (FileInstall).

(I have to use EXE because this program should work on different computers)

Is there a better way?

Is it possible to send and receive in the same program?

Kind Regards

Veronesi

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