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)
Private Function CheckForModem(X As Integer) As Boolean
Dim buffer As String
buffer = ""
CheckForModem = False
Times = 0
Timer1.Enabled = True
Do
DoEvents
buffer = buffer & MSCom.Input
If Len(buffer) <> 0 Then
If InStr(1, buffer, "OK") <> 0 Then
CheckForModem = True
Timer1.Enabled = False
Times = 0
Exit Function
End If
End If
If Times > X Then
Timer1.Enabled = False
Exit Function
End If
Loop
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
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?
'This just fills the listbox with ports that CheckPort finds
'You can make this 1 to 20 or 1 to 30 or whatever range you want to check.
'Some USB adapters install as ports in the 30-39 range
For i = 1 To 16
'If the port exists
If CheckPort(i) Then
'Add it to the list
With cmbCom
.AddItem "COM " & Str(i)
.ListIndex = 0
End With
End If
Next i
'This is a list of baud rates - put in the ones you want to support
With cmbBRate
.AddItem "9600"
.AddItem "19200"
.AddItem "38400"
.AddItem "57600"
.AddItem "115200"
'Set it so that 57600 is the default - you can set it to whichever one you want. 9600 is ListIndex 0
.ListIndex = 3
.Text = "57600"
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
'This checks to see if X is a serial port in this computer
Private Function CheckPort(X As Integer) As Boolean
'See if ComX exists
'To start with, we'll say it's not a port
CheckPort = False
Timer1.Enabled = False
'If the port is already open, close it
If MSCom.PortOpen Then MSCom.PortOpen = False
'Start error handling
On Error GoTo ErrorHandler
'Set the port up and open it
MSCom.CommPort = X
MSCom.Settings = "19200,N,8,1"
MSCom.InputLen = 0
MSCom.PortOpen = True
'On Error GoTo 0
'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
MSCom.Output = "ATI1" & Chr$(13)
'Wait for a modem response for 1 second. If a modem responds then exit
If CheckForModem(1) Then Exit Function
MSCom.Output = "ATI4" & Chr$(13)
'Wait for response for 1 second. If a modem responds then exit
If CheckForModem(1) Then Exit Function
'If we got here the port exists and it's open, so close it
MSCom.PortOpen = False
CheckPort = True
Exit Function
ErrorHandler:
'If there's an error, close the port and leave
If MSCom.PortOpen Then MSCom.PortOpen = False
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
Private Function CheckForModem(X As Integer) As Boolean
Dim buffer As String
'X is the number of times we want to read the input to see if we got anything
buffer = ""
CheckForModem = False
Times = 0
Timer1.Enabled = True
Do
'Let things happen while we're waiting
DoEvents
'Get the input from MSComm
buffer = buffer & MSCom.Input
'Wait until we have something in the buffer
If Len(buffer) <> 0 Then
'If we have 'OK' in the buffer it's most likely from a modem
If InStr(1, buffer, "OK") <> 0 Then
CheckForModem = True
'Turn the timer off
'In the timer event we just make Times = Times + 1
Timer1.Enabled = False
Times = 0
Exit Function
End If
End If
'If Times is greater than what we want, we're done and there's no modem on this port
If Times > X Then
Timer1.Enabled = False
Exit Function
End If
Loop 'Do forever, or until we Exit Function
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
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
Last edited by maxbass; Jun 16th, 2006 at 11:00 AM.
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
"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
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
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.