Try missing End Try, but its right there -->

Post

Posted
Rating:
#1 (In Topic #1550)
Trainee
mouse51180 is in the usergroup ‘unknown’
Please be patient with me.  I do not claim to be a programmer and have filled this out the best I can on my own with the little programming skills I do have and have had to use ChatGPT for some of the more difficult parts.

I do not know if I am running into a ChatGPT issue as it keeps sending me in a circle back to the same error or if this might be a bug inside Gambas.

What the program is suppose to do:
This program is intended to be a GUI program with one drop down box populated with names associated to IP addresses and a button.  When the button is pressed it run a shell command of
<COLOR color="#FF0040">usbip list –remote=<IP></COLOR> with the IP address collected from the dropdown box.  

Running this manually outputs a response that looks like:

Code

┌──(kali㉿kali)-[~]
└─$ usbip list -r 192.168.1.30
Exportable USB devices
======================
 - 192.168.1.30
        1-7: Realtek Semiconductor Corp. : Realtek 8812AU/8821AU 802.11ac WLAN Adapter [USB Wireless Dual-Band A
           : USB\VID_0BDA&PID_0811\00E04C000001
           : (Defined at Interface level) (00/00/00)
           :  0 - Vendor Specific Class / Vendor Specific Subclass / Vendor Specific Protocol (ff/ff/ff)

It then collects the busid # (1-7 in the example above) and run a shell command of "<COLOR color="#FF0040">usbip attach –remote=<IP> –busid=<ID#></COLOR>"  

The issue:
When I hit the play button it stops and I get an error claiming "Unexpected end of line (FMain.class:39)"

Code (gambas)

  1.     ' Capture the output from the usbip list command
  2.     Dim outputString As String = Exec$(shellCmd) ' Run the command and capture the output
  3.     output123 = Split(outputString, gb.NewLine) ' Split the output into an array by newlines
  4.   Catch
  5.     Message.Error("Failed to scan usb device list.")
  6.     Return
  7.   End Try

From my research all I keep getting is that my code is missing an "END TRY" or "FINALIZE" to close the section out, but as you can see…its there. ?!?

Thanks for any help or guidance.
P.S. The "output123" and "line123" are named this way because when I tried to just use "output" or "line" as the name it would capitalize the O and the L and ChatGPT said the capitalization could be an issue.  I could not get Gambas to type keep these lowercase so I just added 123 to the end to get past this being a possible issue.

FULL PROGRAM:

Code (gambas)

  1. ' Gambas class file
  2.  
  3. ' Create a name-IP map
  4. Private ipMap As New Collection 'associative array
  5.  
  6. Public Sub Form_Open()
  7.   cbb_NUCs.Clear
  8.  
  9. ' Populate the ComboBox and IP Addresses
  10.   ipMap["NUC 03"] = "192.168.1.3"
  11.   ipMap["NUC 08"] = "192.168.1.8"
  12.   ipMap["NUC 30"] = "192.168.1.30"
  13.  
  14.   For Each name As String In ipMap.Keys
  15.     cbb_NUCs.Add(name)
  16.   Next
  17.  
  18. Public Sub btn_Attach_Click()
  19.  
  20.   Dim SelectedName As String = cbb_NUCs.Text
  21.   Dim ip As String
  22.   Dim cmd As String
  23.   Dim output123 As String[]
  24.   Dim busid As String = ""
  25.   Dim line123 As String
  26.   Dim shellCmd As String
  27.  
  28. ' Get IP from selected name
  29. ip = ipMap[SelectedName]
  30.  
  31. ' Build and run command
  32. cmd = "usbip list --remote=" & ip
  33. shellCmd = "sh -c " & Chr(34) & cmd & Chr(34)
  34.  
  35. Print "Running: "; shellCmd ' <-- Debugging line
  36.  
  37.          
  38.   Try
  39.     ' Capture the output from the usbip list command
  40.     Dim outputString As String = Exec$(shellCmd) ' Run the command and capture the output
  41.     output123 = Split(outputString, gb.NewLine) ' Split the output into an array by newlines
  42.   Catch
  43.     Message.Error("Failed to scan usb device list.")
  44.     Return
  45.   End Try
  46.    
  47.   ' Iterate through the output and look for the Realtek 8812AU adapter
  48.   For Each line123 In output123
  49.     If InStr(line123, "Realtek") > 0 And InStr(line123, "8812AU") > 0 Then  
  50.       busid = Trim(Split(line123, ":")[0]) ' Get the bus ID
  51.       busid = Replace(busid, "- ", "") ' Clean up the bus ID
  52.       Exit ' Exit the loop when the adapter is found
  53.     End If  
  54.   Next  
  55.                              
  56.   If busid = "" Then  
  57.     Message.Error("Realtek 8812AU adapter not found.")  
  58.     Return  
  59.   End If  
  60.                                    
  61.   ' Run the attach command
  62.   cmd = "usbip attach --remote=" & ip & " --busid=" & busid  
  63.   Shell cmd Wait ' <-- Run the attach command and wait for completion
  64.  
  65.   Message.Info("Adapter attached successfully.")
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’
gbWilly leads the usergroup ‘GambOS Contributor’
gbWilly is in the usergroup ‘Blogger’

mouse51180 said

I do not know if I am running into a ChatGPT issue as it keeps sending me in a circle back to the same error or if this might be a bug inside Gambas.
ChatGTP produces non sense code and non sense answers, stay away from it. I didn't even bother looking at further code after seeing the fist part  :shock: .

Have you ever tried asking the Gambas wiki. Adding these 3 words (gambas wiki try) in a search engine gave me: https://gambaswiki.org/wiki/lang/try.

Start there and TRY  :lol:  again, you might run into the risk of figuring out thing yourself, learn some skills and have meaningful questions on the road there  ;) in stead of garbage produced by AI you don't understand yourself. What's the point then?..

gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories


… there is always a Catch if things go wrong!
Online now: No Back to the top

Post

Posted
Rating:
#3
Banned
That code is quite wrong.  bad AI
Read the wiki, AI is rubbish and gives you wrong answers, AI will not really help you, only confuse you.

For example…

"Try" is for a single line statement only.
There is no such syntax as "End Try" in gambas.
/lang/try - Gambas Documentation

And "Catch" can only be used as the final lines of code. If there is any error raised all code after "Catch" will be run.
/lang/catch - Gambas Documentation

If you had read the gambas wiki for "Try" and "Catch" you would have seen how your code does not make sense.

To stop a command raising an error and check the error state yourself you use "Try" , like this…

Code (gambas)

  1.  
  2.     ' Capture the output from the usbip list command
  3.  
  4.    Dim outputString As String
  5.   ' "Try" to run a statement and then check for any error...
  6.    Try Shell shellCmd Wait To outputString ' Run the command and capture the output
  7.     If Error Or If Process.LastValue Then ' Process.LastValue will be non zero if there was an error.
  8.       Message.Error("Failed to scan usb device list.\n" & if(Process.LastValue, Process.LastValue, Error.Text))
  9.       Return
  10.     Endif
  11.  
  12.     output123 = Split(outputString, gb.NewLine) ' Split the output into an array by newlines
  13.  
  14.  
Online now: No Back to the top

Post

Posted
Rating:
#4
Banned
Ps. "Output" and "Line" (Line Input) are both reserved words and cannot be used as variable names.
/lang - Gambas Documentation
/lang/output - Gambas Documentation
/lang/lineinput - Gambas Documentation
Online now: No Back to the top

Post

Posted
Rating:
#5
Trainee
mouse51180 is in the usergroup ‘unknown’
 Thank you for the reply.  I will take another stab at it.
Online now: No Back to the top
1 guest and 0 members have just viewed this.