Jump to content

Serial Port /COM Port UDF


martin
 Share

Recommended Posts

Posted Image

Unfortunately - it still crashed.

Steve

Ok.

I think the dll is correct now, but have a look at the udf. In _commswitch there is a clause at the start which says something like

if $fportopen = false then
 do something
 return somevalue
endif

Remove that if section because it should never have been there. When a port is closed the global variable $fportopen is set to false and with that if bit you can never switch to another port unless you happen to open one first.

If that doesn't fix it then it will be at least a week until I can spend some time on 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

Hi Martin.

Thanks for your time looking at this.

$fPortOpen is just a flag to say that we have a handle to the dll.

In my code, I was calling the dll directly and not using the UDF calls. So my original code was not affected by the UDF change requested.

I was however using your UDF calls in the code snippet to display the bug.

As requested, I have removed the lines of code from the UDF.

The snippet of code displaying the bug still crashes AutoIt.

:graduated:

Steve

Link to comment
Share on other sites

hi,

can someone explain me why i cant work with bluetooth?

im adding the device to COM6

when i try to connect to the COM6 port CommMg says "Port does not exist"

how to fix it?

(im using the example from first post for testing)

regards

EDIT: the normal RS232 works good (COM1), but the added ports from bluetooth are doing errors (not exist...)

Edited by Vision
Link to comment
Share on other sites

Hi Martin - this has been a busy thread - just shows how useful these Serial Port functions are. Thanks for your contribution. :graduated:

You are already looking at the issue where all the com ports are closed when running the _CommClosePort()command. I look forward to that issue being resolved.

But - I have another issue for you to look at.

Background - I have now got 2 scripts communicating with a production tools; both sending me data @30Hz. One is sending me binary data , one is sending me "string" information. I was puzzled as to why on one computer the process was consuming 4% CPU and the other one 1% CPU.

It turns out the DllCall($hDll, 'str', 'GetString') function is very very very slow compared to using GetInputCount and ReadByteArray. In fact it is much quicker to use the Read Byte array function, bring the result into AutoIt the use the BinaryToString function to get the String information. (I estimate to be 3x quicker and literally using 3x less CPU cycles.)

So my request is this.

When you breaking open your code to look at the _CommClosePort issue - can you have a look at the GetString function - and see if there are any optimization you can make (even if all you do is internally call ByteArray function and then convert to String)

Again, thanks a lot for this useful library.

Steve

Link to comment
Share on other sites

..

Background - I have now got 2 scripts communicating with a production tools; both sending me data @30Hz. One is sending me binary data , one is sending me "string" information. I was puzzled as to why on one computer the process was consuming 4% CPU and the other one 1% CPU.

It turns out the DllCall($hDll, 'str', 'GetString') function is very very very slow compared to using GetInputCount and ReadByteArray. In fact it is much quicker to use the Read Byte array function, bring the result into AutoIt the use the BinaryToString function to get the String information. (I estimate to be 3x quicker and literally using 3x less CPU cycles.)

So my request is this.

When you breaking open your code to look at the _CommClosePort issue - can you have a look at the GetString function - and see if there are any optimization you can make (even if all you do is internally call ByteArray function and then convert to String)

Again, thanks a lot for this useful library.

Steve

I'm surprised about that; I'll look into it during the next week.

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

Hello,

Is it possible to get the name of a com port (The one that is shown in the device manager)?

Thanks. :graduated:

The function _CommListPorts gives the names of all the available ports. Is that what you mean?

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

No I mean the actual name of the device. For example if we look at the Device Manager, it will say "USB CDC serial port emulation (COM3)".

That long name I want.

I solved the problem by listening to every COM port for a special character which is not the best way to detect which COM port your device is connected to.

Here is the sample:

$serialList = _CommListPorts(0)
$goodport = 0
For $p = 1 To Ubound($serialList) -1
    $sSerial = StringRight($serialList[$p],1)
;~  ConsoleWrite($sSerial&@LF)
    Local $sportSetError = ''
    _CommSetPort($sSerial, $sportSetError, 9600, 8, 'none', 1, 'NONE')
    if $sportSetError = '' Then
        ConsoleWrite('Connected to: '&$sSerial&@LF)
    Else
        ConsoleWrite('ERROR: '&$sportSetError)
    EndIf
    Local $starttime = _Timer_Init()
    $found_c = 0

    While 1
        ;gets characters received returning when one of these conditions is met:
        ;receive @CR, received 20 characters or 200ms has elapsed
        $instr = _CommGetString()
        Global $sniff = ''
        If $instr <> '' Then ;if we got something
            $instr = StringReplace($instr,@CR,@CRLF)
            if $found_c ==0 And $instr == 'C' Then
                $found_c = 1
                $goodport = $p
                ExitLoop
            EndIf
        EndIf
        If _Timer_Diff($starttime) > 3000 Then
            ExitLoop
        EndIf
    WEnd
    if $goodport <> 0 Then
        ConsoleWrite("Found good one. "&$sSerial)
    EndIf
Next
Link to comment
Share on other sites

No I mean the actual name of the device. For example if we look at the Device Manager, it will say "USB CDC serial port emulation (COM3)".

That long name I want.

Some descriptions are stored in the registry and they might help you. You can see them using the code below. The list is 'live' so if you plug in a USB serial port device it is added to the list and when you unplug the device it is removed from the list.

$inst = 1
While 1
    $key = RegEnumVal("HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM", $inst);
    If @error = -1 Then ExitLoop
    ConsoleWrite($inst & ' ' & $key & "--->" & RegRead("HKLM\HARDWARE\DEVICEMAP\SERIALCOMM",$key ) & @CRLF)
    $inst += 1
WEnd

- but I don't think they correspond to the descriptions given in device Manager and I don't know where those descriptions come from.

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

@leomoon: This will help

#Include <Array.au3>

$aComPort = _GetComPortNames()
_ArrayDisplay($aComPort)

Func _GetComPortNames()
;funkey 2010, Nov 29th
Local $objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%COM%'", "WQL", 48)
Local $sRet = ""
For $objItem In $colItems
  $sRet &=  $objItem.Name & @CR
Next
Return StringSplit(StringTrimRight($sRet, 1), @CR, 2)
EndFunc

@martin: Thanks for your great work with this very usefull udf!!

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

@leomoon: This will help

#Include <Array.au3>

$aComPort = _GetComPortNames()
_ArrayDisplay($aComPort)

Func _GetComPortNames()
;funkey 2010, Nov 29th
Local $objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%COM%'", "WQL", 48)
Local $sRet = ""
For $objItem In $colItems
  $sRet &=  $objItem.Name & @CR
Next
Return StringSplit(StringTrimRight($sRet, 1), @CR, 2)
EndFunc

Thanks for that funkey, that is indeed useful. I'll add a link to your thread for that in the first post of this thread.

@martin: Thanks for your great work with this very usefull udf!!

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

funkey. SO AWESOME. TNX. :graduated:

@leomoon: This will help

#Include <Array.au3>

$aComPort = _GetComPortNames()
_ArrayDisplay($aComPort)

Func _GetComPortNames()
;funkey 2010, Nov 29th
Local $objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%COM%'", "WQL", 48)
Local $sRet = ""
For $objItem In $colItems
  $sRet &=  $objItem.Name & @CR
Next
Return StringSplit(StringTrimRight($sRet, 1), @CR, 2)
EndFunc

@martin: Thanks for your great work with this very usefull udf!!

Link to comment
Share on other sites

Hi Martin,

Can I use DSR as input signal with this UDF? I've seen you've added option to send/receive with RTS/CTS, but there is no option for DTR/DSR. I've constructed infrared receiver following these instructions, and it works fine with girder, but as you can see, the schematic shows that input signal travels through DSR pin. I would like to be able to make my own remote control receiver software (for RS232 port), and when I saw your UDF I thought that it would be quite easy, just need some guidance as I'm still newbie in RS232 porting...

Link to comment
Share on other sites

Hi Martin,

Can I use DSR as input signal with this UDF? I've seen you've added option to send/receive with RTS/CTS, but there is no option for DTR/DSR. I've constructed infrared receiver following these instructions, and it works fine with girder, but as you can see, the schematic shows that input signal travels through DSR pin. I would like to be able to make my own remote control receiver software (for RS232 port), and when I saw your UDF I thought that it would be quite easy, just need some guidance as I'm still newbie in RS232 porting...

I'll look into it. If I don't post anything about it in the next week, when I should be able to find some time for it, send me a pm as a reminder because I get easily sidetracked by things like work and family and stuff.

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

has anybody done rs232 over IP?

I'm trying to send specific hex commands to an IP and specific port. No COM ports used.

It sounds like you need TCPIP not RS232. Look up TCP* in the 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

Hi Martin,

Just wanted to let you know that I have your Serial COMM code working in a couple of instances. One is quite interesting where on one serial interface I am querying for information, doing some work and then passing this information in a different form to requests on a different serial port. Both ports handling requests at @30Hz and all at remarkably low CPU usage (~ 1.4%) - I am very pleased.

The other thing I am pleased about is that the code appears robust - my code here has been working, collecting data from production machinery 24x7 for a couple of weeks now - no issues - great :graduated:

In a previous post I mentioned that collecting data via the "GetString" call was so much slower (or consume 3x CPU cycles) than using "getByteArray" and converting to string. Did you ever get the chance to have a look ?

Another question (not related - and NOT a request - just a question !!)

Is is possible to add functionality to add "event" handling.

Just now, we poll the port to see if any information is there - the Holy Grail here is to be able to use an event to say "hey I'm here with some data - come and get me". I don't really know enough about callbacks and events to know if this could be made to work.

Thanks again.

Steve

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