Results 1 to 12 of 12

Thread: Port choosing by user

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    20

    Arrow Port choosing by user

    hello, i've written a program that sends commands to the computer according to the data that it recieving from the com. port. i wrote the program for port 1, the problem is, what if the user is using that port for something else? can someone give me an example to a setup code that will allow user to choose the port from wich he wants the data to be recieved from for the application, and according to the user choise, the program code will change the com. port number in the code?
    (less complicated solution for the problem are welcome too)

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Port choosing by user

    VB Code:
    1. 'Put 2 listboxes on your form, cmbCom (for the port numbers) and cmbRate (for the speed)
    2.  
    3. 'In your formload or formactivate
    4. For i = 1 To 16
    5.   If CheckPort(i) Then
    6.     With cmbCom
    7.       .AddItem "COM " & Str(i)
    8.       .ListIndex = 0
    9.     End With
    10.   End If
    11. Next i
    12.  
    13. With cmbBRate
    14.   .AddItem "9600"
    15.   .AddItem "19200"
    16.   .AddItem "38400"
    17.   .AddItem "57600"
    18.   .AddItem "115200"
    19.   .ListIndex = 3
    20.   .Text = "57600"
    21. End With
    22. 'end of formload or formactivate
    23.  
    24. Private Function CheckPort(X As Integer) As Boolean
    25. 'See if ComX exists
    26. CheckPort = False
    27. Timer1.Enabled = False
    28.  
    29. If MSCom.PortOpen Then MSCom.PortOpen = False
    30.  
    31. 'Start error handling
    32. On Error GoTo ErrorHandler
    33. MSCom.CommPort = X
    34. MSCom.Settings = "19200,N,8,1"
    35. MSCom.InputLen = 0
    36. MSCom.PortOpen = True
    37. 'On Error GoTo 0
    38.  
    39. 'These tests look for a Com port that DOESN'T have a modem on them - change the sense (if Not CheckForModem(1)) if you want only ports with modems
    40. MSCom.Output = "ATI1" & Chr$(13)
    41. 'Wait for a modem response for 1 second. If a modem responds then exit
    42. If CheckForModem(1) Then Exit Function
    43. MSCom.Output = "ATI4" & Chr$(13)
    44. 'Wait for response for 1 second. If a modem responds then exit
    45. If CheckForModem(1) Then Exit Function
    46.  
    47. MSCom.PortOpen = False
    48. CheckPort = True
    49. Exit Function
    50.  
    51. ErrorHandler:
    52.   'lblError.Caption = "Error " & Err.Description & " checking Com" & Str(X)
    53.   If MSCom.PortOpen Then MSCom.PortOpen = False
    54.  
    55. End Function
    56.  
    57. Private Function CheckForModem(X As Integer) As Boolean
    58. Dim buffer As String
    59.  
    60. buffer = ""
    61. CheckForModem = False
    62. Times = 0
    63. Timer1.Enabled = True
    64.  
    65. Do
    66.   DoEvents
    67.   buffer = buffer & MSCom.Input
    68.     If Len(buffer) <> 0 Then
    69.       If InStr(1, buffer, "OK") <> 0 Then
    70.         CheckForModem = True
    71.         Timer1.Enabled = False
    72.         Times = 0
    73.         Exit Function
    74.       End If
    75.     End If
    76.     If Times > X Then
    77.         Timer1.Enabled = False
    78.         Exit Function
    79.     End If
    80. Loop
    81.  
    82. End Function
    The combo boxes will be filled with existing ports and speeds of your choice. Then just use whatever port is showing in cmbCom.Text as the port you use.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    20

    Re: Port choosing by user

    hey, thanks for the code.
    the problem is that i don't understand it so much, thats why i couldn't manage it to run.
    can you explain the code in more detail, or in a pseudo code form?

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Port choosing by user

    VB Code:
    1. 'This just fills the listbox with ports that CheckPort finds
    2. 'You can make this 1 to 20 or 1 to 30 or whatever range you want to check.
    3. 'Some USB adapters install as ports in the 30-39 range
    4. For i = 1 To 16
    5.   'If the port exists
    6.   If CheckPort(i) Then
    7.     'Add it to the list
    8.     With cmbCom
    9.       .AddItem "COM " & Str(i)
    10.       .ListIndex = 0
    11.     End With
    12.   End If
    13. Next i
    14.  
    15. 'This is a list of baud rates - put in the ones you want to support
    16. With cmbBRate
    17.   .AddItem "9600"
    18.   .AddItem "19200"
    19.   .AddItem "38400"
    20.   .AddItem "57600"
    21.   .AddItem "115200"
    22.   'Set it so that 57600 is the default - you can set it to whichever one you want.  9600 is ListIndex 0
    23.   .ListIndex = 3
    24.   .Text = "57600"
    25. End With
    Last edited by Al42; Jun 16th, 2006 at 09:24 AM.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Port choosing by user

    VB Code:
    1. 'This checks to see if X is a serial port in this computer
    2. Private Function CheckPort(X As Integer) As Boolean
    3. 'See if ComX exists
    4. 'To start with, we'll say it's not a port
    5. CheckPort = False
    6. Timer1.Enabled = False
    7.  
    8. 'If the port is already open, close it
    9. If MSCom.PortOpen Then MSCom.PortOpen = False
    10.  
    11. 'Start error handling
    12. On Error GoTo ErrorHandler
    13.  
    14. 'Set the port up and open it
    15. MSCom.CommPort = X
    16. MSCom.Settings = "19200,N,8,1"
    17. MSCom.InputLen = 0
    18. MSCom.PortOpen = True
    19. 'On Error GoTo 0
    20.  
    21. 'These tests look for a Com port that DOESN'T have a modem on them - change the sense (if Not CheckForModem(1)) if you want only ports with modems
    22. MSCom.Output = "ATI1" & Chr$(13)
    23. 'Wait for a modem response for 1 second. If a modem responds then exit
    24. If CheckForModem(1) Then Exit Function
    25. MSCom.Output = "ATI4" & Chr$(13)
    26. 'Wait for response for 1 second. If a modem responds then exit
    27. If CheckForModem(1) Then Exit Function
    28.  
    29. 'If we got here the port exists and it's open, so close it
    30. MSCom.PortOpen = False
    31. CheckPort = True
    32. Exit Function
    33.  
    34. ErrorHandler:
    35.   'If there's an error, close the port and leave
    36.   If MSCom.PortOpen Then MSCom.PortOpen = False
    37.  
    38. End Function
    Last edited by Al42; Jun 16th, 2006 at 09:30 AM.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Port choosing by user

    VB Code:
    1. 'Check to see if a modem is on the current port
    2. Private Function CheckForModem(X As Integer) As Boolean
    3. Dim buffer As String
    4. 'X is the number of times we want to read the input to see if we got anything
    5.  
    6. buffer = ""
    7. CheckForModem = False
    8. Times = 0
    9. Timer1.Enabled = True
    10.  
    11. Do
    12.   'Let things happen while we're waiting
    13.   DoEvents
    14.   'Get the input from MSComm
    15.   buffer = buffer & MSCom.Input
    16.     'Wait until we have something in the buffer
    17.     If Len(buffer) <> 0 Then
    18.       'If we have 'OK' in the buffer it's most likely from a modem
    19.       If InStr(1, buffer, "OK") <> 0 Then
    20.         CheckForModem = True
    21.         'Turn the timer off
    22.         'In the timer event we just make Times = Times + 1
    23.         Timer1.Enabled = False
    24.         Times = 0
    25.         Exit Function
    26.       End If
    27.     End If
    28.     'If Times is greater than what we want, we're done and there's no modem on this port
    29.     If Times > X Then
    30.         Timer1.Enabled = False
    31.         Exit Function
    32.     End If
    33. Loop 'Do forever, or until we Exit Function
    34.  
    35. End Function
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    20

    Re: Port choosing by user

    well, the code is much clearer now, thanks to your comments, although i still can't manage it to run (in the simulation i dont get any form window, although it set to visible).
    *i've added two lists named them cmbCom and cmbBrate, added a clock, added MSCom, puted the loop in the form load, the rest of the code outside the loop, defined i as : dim i as integer.*

    and some other thing, the function of the code is not exactly what i've been looking for (nevertheless it got some great insights on the issue of serial ports in VB). what i was looking for is to let the user (assuming he knows what is connected to his ports) choose (by clicking) the port he want's the aplication to use.
    as i said before, i've got the aplication thats reading data from a com port and sending commands to the computer. the problem with the aplication is that i want to let the user decide from wich port he wants to read with the aplication. meanwhile the aplication is written for port 1, but on other computer it may be another port, so instead of writing many versions for the program:
    i wanted to build some mini setup program that has a list of ports, say com1 to com6. then the user will choose the port he likes, by clicking on the name, and that action will change the aplication code, in the line of:
    MSComm1.CommPort = 4
    (suppose the user chose COM4)
    so now the aplication (thats reading data from the port) will be working on port 4 everytime the aplication will be loaded.
    other solution is to set the program to read the port setup
    (MSComm1.CommPort = 4
    MSComm1.Settings = "19200,N,8,1"
    MSComm1.PortOpen = True
    MSComm1.RThreshold = 1
    MSComm1.InputLen = 1)
    from a file, and the configuration program (that one that i'm looking for) will change the port number in the port configuration file. any idea on how to get the program to read another file for the port setup (instead of setting up the port in the form load) and how to write to that file from the configuration program?

    visual examle attached
    Attached Images Attached Images  
    Last edited by maxbass; Jun 16th, 2006 at 11:00 AM.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    20

    Re: Port choosing by user

    anyone?

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Port choosing by user

    Use my code to find out which ports exist in the computer. Make the Option buttons for the ports that don't exist invisible. Use the port for the button the user chooses.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    20

    Re: Port choosing by user

    "Use the port for the button the user chooses."
    that is the exact thing that i'm looking to do, and i dont know how to make a program that will setup one time the other program, and the other program will work on that port everytime it will be loaded

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Port choosing by user

    You want someone using computer A to set the port that computer B will use?

    Then you have to establish communications between the computers that doesn't involve serial ports. You can't send the port number to computer B until after computer B knows which port to use.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jun 2006
    Posts
    20

    Re: Port choosing by user

    i have program A that will be installed on a computer, and i need program B that will setup program A (on the same computer) by telling program A wich port to use from now on (it will tell program A wich port to use by letting the user on the same computer to choose the port himself in program B interface). program B will have the interface similar to the image example above.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width