Jump to content
Architerion

Why does _Comm hang in loop

Recommended Posts

Architerion

Howdy

I am writing a multichannel batching sequence for a usb relay controller using the FTD chip (denkovi 16 channel board). I couldnt actually get a working example using the FTD udf...port opens etc but nothing happening. I would be happy to pay someone for a working example though. However I ended up doing a quick example just writing to the serial port using the _Comm udf and it works fine eg the following

 

#include <CommMG.au3>

Global $Port=12, $Baud=9600, $Databits=8, $Parity="none", $Stopbits=1, $Flow=0, $RTS=0, $DTR=0, $Received
$Result = _CommSetport($Port, $Received, $Baud, $Databits, $Parity, $Stopbits, $Flow, $RTS, $DTR)
If $Result <> 1 Then    MsgBox($MB_SYSTEMMODAL,"CRITICAL WARNING!", "Could not set USB port connection...is unit plugged in?")
$Result = _CommSendString("06+//",0)
Sleep(1000)
$Result = _CommSendString("06-//",0)

 

When I use it in a loop; it doesnt return though?

 

Local $hDLL = DllOpen("user32.dll")

Global $Port=12, $Baud=9600, $Databits=8, $Parity="none", $Stopbits=1, $Flow=0, $RTS=0, $DTR=0, $Received
$Result = _CommSetport($Port, $Received, $Baud, $Databits, $Parity, $Stopbits, $Flow, $RTS, $DTR)

While 1
   Select
      Case _IsPressed("7B", $hDLL)
         While _IsPressed("7B", $hDLL)
            Sleep(50)
         WEnd
         ; Wait until key is released.
         ConsoleWrite("Pressed")
         TestFunc()
   EndSelect
   Sleep(50)
WEnd

 

Func TestFunc()

    $Result = _CommSendString("06+//",0)
    Sleep(500)
    $Result = _CommSendString("06-//",0)

Return

Am I missing something (Obviously hehe)?

Help!

 

 

Share this post


Link to post
Share on other sites
SadBunny

What do you mean by "it doesn't return"? I assume you mean the TestFunc() doesn't finish. Does it print the "Pressed"? Have you tried ConsoleWriting those $Result vars in the TestFunc(), to see where it hangs? And ConsoleWriting "Done with function" under the call to TestFunc()?

Assuming you included Misc.au3 for the _IsPressed, and ended that function with endfunc, that bit of code there should work.


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites
Architerion

What do you mean by "it doesn't return"? I assume you mean the TestFunc() doesn't finish. Does it print the "Pressed"? Have you tried ConsoleWriting those $Result vars in the TestFunc(), to see where it hangs? And ConsoleWriting "Done with function" under the call to TestFunc()?

Assuming you included Misc.au3 for the _IsPressed, and ended that function with endfunc, that bit of code there should work.

​Thanks for the reply; I have the includes...'pressed' is written. The relays turn on and off but the program itself no longer responds to key presses and I have to hard break the execution. It is as if _Comm runs off with the thread and never comes back. 

Cheers

M

Edited by Architerion
additional info

Share this post


Link to post
Share on other sites
SadBunny

All fine, but we still don't know where exactly it hangs. It enters the function and executes the two commands, apparently, but does it return from that second command? If it does, the problem is somewhere in the code you didn't post. If it doesn't, the problem is somewhere in the udf. Kind of hard to troubleshoot with non-functional code with too little logging...

Try main loop and func like this:

While 1
    Select
        Case _IsPressed("7B", $hDLL)
            While _IsPressed("7B", $hDLL)
                Sleep(50)
            WEnd
            ; Wait until key is released.
            ConsoleWrite("Pressed")
            TestFunc()
            ConsoleWrite("Returned from TestFunc()" & @CRLF)
    EndSelect
    Sleep(50)
WEnd

Func TestFunc()

    $Result = _CommSendString("06+//", 0)
    ConsoleWrite("Result of first call: " & $Result & @CRLF)
    Sleep(500)
    $Result = _CommSendString("06-//", 0)
    ConsoleWrite("Result of second call: " & $Result & @CRLF)
    Return
EndFunc   ;==>TestFunc

It should write "Pressed", then the two results (for this purpose we don't care what those results actually are as long as they get printed), and then "Returned". But it will not, because it gets stuck somewhere. This will tell you where.

Note that you could also use HotKeySet() instead of the _IsPressed thing.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites
Architerion

Cheers. Will give that a go in the morning...just about bedtime here in ol Sydney town.

Im sorry for the incomplete coding...was tired, actually burnt out when I posted. Ill just get this simple loop going first and come back with the rest of the code as its resolved. It actually uses a 3 deep array to run some parametric timers...

Thanks so much for your time/help

M

 

Share this post


Link to post
Share on other sites
SadBunny

No problem worries (right? Am I an Ozzie yet? :)) Dutch boy living in Sydney here. You sure have an early bedtime :)

By the way, try writing a short reproducer that a) actually works and b) actually reproduces the problem. It often helps laying the problem bare.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites
Architerion

Hehe..Kein Problem...yep you are officially an Ozzie...I stand corrected ;-)

Im a German background boy that has to get up real early...and after wind down tasks...its not so early a bedtime.

Advice well taken...will do and see how it goes. 

bis dann!

 

Share this post


Link to post
Share on other sites
Architerion

Ok...here is the actual example (debug) code. Runs fine with debugs printed correctly until I uncomment the commport open stuff. then it only gets to Stage: PreLoop

 

#include <Misc.au3>
#include <MsgBoxConstants.au3>
#include <Date.au3>
#include <GUIConstantsEx.au3>
#include <GuiButton.au3>
#include <WindowsConstants.au3>
#include <Timers.au3>
#include <FTD2XX_UDF.au3>
#include "OnAutoItErrorRegister.au3"
#include <Sound.au3>
#include <CommMG.au3>


Local $hDLL = DllOpen("user32.dll")
Global $Port=13, $Baud=9600, $Databits=8, $Parity="none", $Stopbits=1, $Flow=0, $RTS=0, $DTR=0
Global $Received, $Result, $PortList[100]

;~ $Result = _CommSetport($Port, $Received, $Baud, $Databits, $Parity, $Stopbits, $Flow, $RTS, $DTR)
;~ ConsoleWrite($Result)

;~ $Result = _CommSendString("00", 0)

ConsoleWrite(@CR & "Stage: Pre Loop")

;~ HotKeySet("{F12}", "TestFunc")

While 1
    Select
    Case _IsPressed("7B", $hDLL)
       ConsoleWrite(@CR & "Stage: While Loop")
            While _IsPressed("7B", $hDLL)
                Sleep(50)
            WEnd
            ; Wait until key is released.
            ConsoleWrite(@CR & "F12 Pressed")
            TestFunc()
            ConsoleWrite(@CR & "Returned from TestFunc()" & @CRLF)
    EndSelect
    Sleep(50)
WEnd

Func TestFunc()
    $Result = _CommSendString("09", 0)
    ConsoleWrite(@CR & "Result of first call: " & $Result & @CRLF)
    Sleep(500)
    $Result = _CommSendString("00", 0)
    ConsoleWrite(@CR & "Result of second call: " & $Result & @CRLF)
    Return
EndFunc   ;==>TestFunc

Share this post


Link to post
Share on other sites
SadBunny

So it says "Pre Loop" but it doesn't say "While Loop" when you press the key? Or are you using the hotkeyset instead? If so, disable the Case _IsPressed("7B") and add a line to the beginning of the TestFunc to see if it even gets there.

If it's starting to look like something goes wrong in the first _CommSendString(), you'll have to go into the UDF with the same kind of troubleshooting. Just keep tracing your call down the functions and see where it fails.


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites
mLipok

@Architerion

Please use cod tags when you post your code.
Read more here.

 

mLipok


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2018-03-17

Share this post


Link to post
Share on other sites
Architerion

@Architerion

Please use cod tags when you post your code.
Read more here.

 

mLipok

​Thanks mLipok. Got it.

Share this post


Link to post
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

×