It_is_me_Me Posted November 13, 2021 Share Posted November 13, 2021 (edited) I am writing scripts to test four communications... TCP, Serial, USB, and Optical. Let's say that I have 5 test cases: TC#1 is communicating TCP TC#2 is do A TC#3 is do B TC#4 is do C TC#5 is do D Next test case 1 is communicating via Serial 485 now since TCP just finished testing. Then TC#2 to #5 will be used again but not in TCP but in serial 485 now which is identical test. Same goes to USB and Optical ports.. So basically, the test case #1 is the only different to the steps while test #2 to 5 will be reused to all comms (tcp, serial, usb, optical) What would be the best approach to code this so the line of code will be minimized? I want to reduce the redundant use of test cases 2 to 5 since those test are the same to all comms. Any help and critics are welcome. Here are my sample scripts: expandcollapse popup#include <GUIConstantsEx.au3> #include <AutoItConstants.au3> Global $comms $commsTCP = "TCP" $comms485 = "485" $commsUSB = "USB" $commsOptics = "Optical" Example() Func Example() ; Create a GUI with various controls. Local $hGUI = GUICreate("Example", 300, 200) ; Create a button control. Local $idButton_runtest = GUICtrlCreateButton("Run Test", 120, 170, 85, 25) Local $idButton_Close = GUICtrlCreateButton("Close", 210, 170, 85, 25) ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) Local $iPID = 0 ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $idButton_Close ExitLoop Case $idButton_runtest ;TCP test1($commsTCP) test2() test3() test4() test5() ;Serial 485 test1($comms485) test2() test3() test4() test5() ;USB test1($commsUSB) test2() test3() test4() test5() ;Optical test1($commsOptics) test2() test3() test4() test5() EndSwitch WEnd ; Delete the previous GUI and all controls. GUIDelete($hGUI) ; Close the Notepad process using the PID returned by Run. If $iPID Then ProcessClose($iPID) EndFunc ;==>Example Func test1($comms) SplashTextOn("", "Communicating via " & $comms, 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14) Sleep(1000) SplashOff() EndFunc Func test2() SplashTextOn("", "Test Case 2", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14) Sleep(1000) SplashOff() EndFunc Func test3() SplashTextOn("", "Test Case 3", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14) Sleep(1000) SplashOff() EndFunc Func test4() SplashTextOn("", "Test Case 4", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14) Sleep(1000) SplashOff() EndFunc Func test5() SplashTextOn("", "Test Case 5", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14) Sleep(1000) SplashOff() EndFunc I'm somewhat new to programming and just barely manage to write something good (so far). Edited November 13, 2021 by Melba23 Added code tags Link to comment Share on other sites More sharing options...
It_is_me_Me Posted November 13, 2021 Author Share Posted November 13, 2021 (edited) I did this to manage to minimize the line of codes.. but I am somewhat not satisfied since what I made is kinda silly. expandcollapse popup#include <GUIConstantsEx.au3> #include <AutoItConstants.au3> Global $comms $commsTCP = "TCP" $comms485 = "485" $commsUSB = "USB" $commsOptics = "Optical" Example() Func Example() ; Create a GUI with various controls. Local $hGUI = GUICreate("Example", 300, 200) ; Create a button control. Local $idButton_runtest = GUICtrlCreateButton("Run Test", 120, 170, 85, 25) Local $idButton_Close = GUICtrlCreateButton("Close", 210, 170, 85, 25) ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) Local $iPID = 0 ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $idButton_Close ExitLoop Case $idButton_runtest ;TCP test1($commsTCP) all() ;Serial 485 test1($comms485) all() ;USB test1($commsUSB) all() ;Optical test1($commsOptics) all() EndSwitch WEnd ; Delete the previous GUI and all controls. GUIDelete($hGUI) ; Close the Notepad process using the PID returned by Run. If $iPID Then ProcessClose($iPID) EndFunc ;==>Example Func all() test2() test3() test4() test5() EndFunc Func test1($comms) SplashTextOn("", "Communicating via " & $comms, 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14) Sleep(1000) SplashOff() EndFunc Func test2() SplashTextOn("", "Test Case 2", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14) Sleep(1000) SplashOff() EndFunc Func test3() SplashTextOn("", "Test Case 3", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14) Sleep(1000) SplashOff() EndFunc Func test4() SplashTextOn("", "Test Case 4", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14) Sleep(1000) SplashOff() EndFunc Func test5() SplashTextOn("", "Test Case 5", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14) Sleep(1000) SplashOff() EndFunc Edited November 13, 2021 by Melba23 Added code tags Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 13, 2021 Moderators Share Posted November 13, 2021 It_is_me_Me, Welcome to the AutoIt forums. When you post code in future please use Code tags - see here how to do it. Then you get a scrolling box and syntax colouring as you can see above now I have added the tags. Thanks in advance for your cooperation. M23 It_is_me_Me 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Solution Zedna Posted November 13, 2021 Solution Share Posted November 13, 2021 (edited) expandcollapse popup#include <GUIConstantsEx.au3> #include <AutoItConstants.au3> Global $comms $commsTCP = "TCP" $comms485 = "485" $commsUSB = "USB" $commsOptics = "Optical" Example() Func Example() ; Create a GUI with various controls. Local $hGUI = GUICreate("Example", 300, 200) ; Create a button control. Local $idButton_runtest = GUICtrlCreateButton("Run Test", 120, 170, 85, 25) Local $idButton_Close = GUICtrlCreateButton("Close", 210, 170, 85, 25) ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) Local $iPID = 0 ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $idButton_Close ExitLoop Case $idButton_runtest test1($commsTCP) ;TCP test1($comms485) ;Serial 485 test1($commsUSB) ;USB test1($commsOptics) ;Optical EndSwitch WEnd ; Delete the previous GUI and all controls. GUIDelete($hGUI) ; Close the Notepad process using the PID returned by Run. If $iPID Then ProcessClose($iPID) EndFunc ;==>Example Func test_2to5() test2() test3() test4() test5() EndFunc Func test1($comms) SplashTextOn("", "Communicating via " & $comms, 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14) Sleep(1000) test_2to5() SplashOff() EndFunc Func test2() SplashTextOn("", "Test Case 2", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14) Sleep(1000) EndFunc Func test3() SplashTextOn("", "Test Case 3", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14) Sleep(1000) EndFunc Func test4() SplashTextOn("", "Test Case 4", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14) Sleep(1000) EndFunc Func test5() SplashTextOn("", "Test Case 5", 280, 60, -1, -1, $DLG_CENTERONTOP , "", 14) Sleep(1000) EndFunc Edited November 13, 2021 by Zedna It_is_me_Me 1 Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
Nine Posted November 13, 2021 Share Posted November 13, 2021 Another option is to create like an UDF file where you would put the bulk of the code. Then you could include the UDF file into 5 test case scripts with their own specifics... It_is_me_Me 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
It_is_me_Me Posted November 13, 2021 Author Share Posted November 13, 2021 Thanks for all the info. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now