13 Replies - 1777 Views - Last Post: 08 August 2009 - 09:43 AM Rate Topic: -----

#1 oe8pck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-June 09

Changing a Com port No.

Posted 08 July 2009 - 07:49 AM

Hi, I am trying something new for me. Up to now I have only used one Form and do not know how to use two. I do have a book but?????????????????
What I want to do is make the Comport No. in the code below variable so that when the Program starts I am asked which Port No. I rquire ans this is written to a variable which i can call in the main program. I have the feeling i can only do this with an additional form and a textbox and a text question, or with push buttons and predetermined port numbers. I would like to use any port number upto say 20. Can someone please answer the following questions.
Do I have to use an extra form?
If not how do I incorperate the port number entry into the existing program?
If I do need a form how do I go about this.

I do realiSubze that I need to show my existing work, and it is there but I have no idea how to start.
I have listed the code I have below.

The first code list only shows the relevent part i.e. the Commport = Comport



'Option Explicit




Dim Newdata As String   'each imcoming packet is assembled here



Private Sub Form_Load()
		 Form1.Caption = "OE8PCK Digital Wattmeter"
		 With MSComm1
		 
			.CommPort = Comport
			.Handshaking = 2 - comRTS
			.RThreshold = 1
			.RTSEnable = True
			.Settings = "9600,n,8,1"
			.SThreshold = 1
			.PortOpen = True
			' Leave all other settings as default values.
		 End With
		 
		 OutputDisplay.Text = "Infobox" + vbCr + vbLf
		 InformationDisplay.Text = "Databox" + vbCr + vbLf
		 
		 Newdata = ""   'initialize to empty
		
	  End 



Option Explicit

'form2 = 1
Private Sub Form_Load()
Form2.Caption = "OE8PCK Digital Wattmeter"

Dim Comport As Integer
End Sub

Private Sub Option1_Click()
Comport = 1
End Sub

Private Sub Option2_Click()
Comport = 2
End Sub

Private Sub Option3_Click()
Comport = 3
End Sub

Private Sub Option4_Click()
Comport = 4
End Sub

Private Sub Option5_Click()
Comport = 5
End Sub

Public Sub CloseWindow()
Unload Form2
Load Form1
Form1.Show

End Sub


This post has been edited by oe8pck: 08 July 2009 - 07:50 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Changing a Com port No.

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Changing a Com port No.

Posted 08 July 2009 - 08:23 AM

now i dont understand what you want you want to use a commports up to 20 and user can change what port to use ? dont rly know why you want the second form you can put a combobox with numbers up to 20 then user choise one and you use it for your program. you can use a picturebox as well as second form you just have to make it visible at start and invisible when button ok is hit. still i dont understand what rly you want :)

example


private sub form_load()

picturbox1.visible=true
label1.caption="Choise comport" 'a label in picturebox
button1.caption="OK"				'a button in picturebox


end sub

Private Sub button1_Click()

Picture1.Visible = False

mettersomething Combo1.List(Combo1.ListIndex)

End Sub

Private Sub mettersomething(ByVal portnum As Integer)
		 Form1.Caption = "OE8PCK Digital Wattmeter"
		 With MSComm1
		 
			.CommPort = portnum
			.Handshaking = 2 - comRTS
			.RThreshold = 1
			.RTSEnable = True
			.Settings = "9600,n,8,1"
			.SThreshold = 1
			.PortOpen = True
			' Leave all other settings as default values.
		 End With
		 
		 OutputDisplay.Text = "Infobox" + vbCr + vbLf
		 InformationDisplay.Text = "Databox" + vbCr + vbLf
		 
		 Newdata = ""   'initialize to empty
		

End Sub





if you hide a picturebox the button, label and combobox will be hiden too. then the form is there with the textbox and other stuff you have

if that is what you want else with second form you must do a global variable to know what port you use

good luck

This post has been edited by NoBrain: 08 July 2009 - 08:43 AM

Was This Post Helpful? 0
  • +
  • -

#3 oe8pck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-June 09

Re: Changing a Com port No.

Posted 09 July 2009 - 04:53 AM

To answer your question as to what I want. I wish to be able to select the Commport number before the program starts.

I have now added your suggested code and the selction of the Portnum appears to work, but the transition to the main program is stuck.
It does not seem tolike the expression
Private Sub Form_Unload(Cancel As Integer)
		 MSComm1.PortOpen = False
	  End Sub


Can you see what is wrong. the picrurebox comes up ok when I push ok then nothing happens.

This is the full code.


'Option Explicit

Private Sub form_load()
picturbox1.Visible = True
Label1.Caption = "Select Commport" ' a label in picturebox
'button1.Caption = "OK" ' a button in picturebox

End Sub
Private Sub button_click()
pictur1.visable = False
mettersomething Combo1.List(Combo1.ListIndex)
End Sub


Dim Newdata As String   'each imcoming packet is assembled here



Private Sub mettersomething(ByVal portnum As Integer)
		 Form1.Caption = "OE8PCK Digital Wattmeter"
		 With MSComm1
		 
			.CommPort = portnum
			.Handshaking = 2 - comRTS
			.RThreshold = 1
			.RTSEnable = True
			.Settings = "9600,n,8,1"
			.SThreshold = 1
			.PortOpen = True
			' Leave all other settings as default values.
		 End With
		 
		 OutputDisplay.Text = "Infobox" + vbCr + vbLf
		 InformationDisplay.Text = "Databox" + vbCr + vbLf
		 
		 Newdata = ""   'initialize to empty
		
	  End Sub

	  Private Sub Form_Unload(Cancel As Integer)
		 MSComm1.PortOpen = False
	  End Sub

	  Private Sub MSComm1_OnComm()
	  
		 Dim InBuff As String
			
		 Dim I As Integer	   'used to inspect each incoming character
		 Dim theChar As String  'each received character
		 Dim theInfo As String
		 Dim Printout As String
		 
		 Dim Y As Integer
		 Dim z As Integer
		 Dim l As Integer
		 Dim X As String
		 
		 
		 
		 
		 
		 Select Case MSComm1.CommEvent
		 ' Handle each event or error by placing
		 ' code below each case statement.
		 ' Errors
			Case comEventBreak   ' A Break was received.
			Case comEventCDTO	' CD (RLSD) Timeout.
			Case comEventCTSTO   ' CTS Timeout.
			Case comEventDSRTO   ' DSR Timeout.
			Case comEventFrame   ' Framing Error.
			Case comEventOverrun ' Data Lost.
			Case comEventRxOver  ' Receive buffer overflow.
			Case comEventRxParity   ' Parity Error.
			Case comEventTxFull  ' Transmit buffer full.
			Case comEventDCB	 ' Unexpected error retrieving DCB]

		 ' Events
			Case comEvCD   ' Change in the CD line.
			Case comEvCTS  ' Change in the CTS line.
			Case comEvDSR  ' Change in the DSR line.
			Case comEvRing ' Change in the Ring Indicator.
			Case comEvReceive ' Received RThreshold # of chars.
			
				InBuff = MSComm1.Input  'received 1 or more characters
			   
				For I = 1 To Len(InBuff) 'examine each received character in sequence
			
					theChar = Mid$(InBuff, I, 1) 'extract the next character
				   
					If Asc(theChar) = 13 Then 'Look for CR
						theInfo = Mid$(Newdata, 2, 1) 'Loads the second letter in the String "Newdata" in "theInfo" is a (I) or (P)
						
						
						If theInfo = "I" Then
						
								   X = Newdata
								   Y = Len(X)
								   z = Y - 5
								   If z >= 5 Then
											  For l = 1 To Y
											  Newdata = Right(X, z)
											  Next l
								   End If
								   
							
							InformationDisplay.SelLength = 0
							InformationDisplay.SelStart = Len(InformationDisplay.Text)
							InformationDisplay.SelText = Newdata + vbCr + vbLf   'include a CR and LF to separate from next line placed in OutputDisplay
							InformationDisplay.SelLength = 0
							
						ElseIf theInfo = "P" Then
						
								   X = Newdata
								   Y = Len(X)
								   z = Y - 5
								   If z >= 5 Then
											 For l = 1 To Y
											 Newdata = Right(X, z)
											 Next l
								   End If
						
							
							
							OutputDisplay.SelLength = 0
							OutputDisplay.SelStart = Len(OutputDisplay.Text)
							OutputDisplay.SelText = Newdata + vbCr + vbLf   'include a CR and LF to separate from next line placed in OutputDisplay
							OutputDisplay.SelLength = 0
							
						End If
					
						Newdata = "" 'clear NewData so it can assemble the next packet
				
						
					ElseIf Asc(theChar) <> 10 Then  'ignore linefeeds
						
						Newdata = Newdata + theChar 'received a character -- append it to NewData
									
					End If

				Next I
			
			Case comEvSend ' There are SThreshold number of
						   ' characters in the transmit buffer.
			Case comEvEOF  ' An EOF character was found in the
						   ' input stream.
		 End Select

	  End Sub


Was This Post Helpful? 0
  • +
  • -

#4 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Changing a Com port No.

Posted 09 July 2009 - 05:01 AM

now may be the button_click() is wrong button click what about dont like form_unload ? ok i copy paste your code and try it.

1) Dim Newdata As String 'each imcoming packet is assembled here is betwin 2 subs

This post has been edited by NoBrain: 09 July 2009 - 05:08 AM

Was This Post Helpful? 0
  • +
  • -

#5 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Changing a Com port No.

Posted 09 July 2009 - 05:14 AM

now i dont rly have anything to commport so i dont go to Private Sub MSComm1_OnComm() but

this is what i find when copy paste your code and fix it so it hiden the picture box :)



Private Sub button_click()
pictur1.bvisable = False => pictur1.bvisIble = False
mettersomething Combo1.List(Combo1.ListIndex)
End Sub

here is what the fixed code

Option Explicit
Dim Newdata As String   'each imcoming packet is assembled here

Private Sub form_load()
fullcmbbox
picturbox1.Visible = True
Label1.Caption = "Select Commport" ' a label in picturebox
'button1.Caption = "OK" ' a button in picturebox

End Sub
Private Sub button_click()
picturbox1.Visible = False
mettersomething Combo1.List(Combo1.ListIndex)
End Sub






Private Sub mettersomething(ByVal portnum As Integer)
		 Form1.Caption = "OE8PCK Digital Wattmeter"
		 With MSComm1
		 
			.CommPort = portnum
			.Handshaking = 2 - comRTS
			.RThreshold = 1
			.RTSEnable = True
			.Settings = "9600,n,8,1"
			.SThreshold = 1
			.PortOpen = True
			' Leave all other settings as default values.
		 End With
		 
		 OutputDisplay.Text = "Infobox" + vbCr + vbLf
		 InformationDisplay.Text = "Databox" + vbCr + vbLf
		 
		 Newdata = ""   'initialize to empty
		
	  End Sub

	  Private Sub Form_Unload(Cancel As Integer)
		 MSComm1.PortOpen = False
	  End Sub

	  Private Sub MSComm1_OnComm()
	  
		 Dim InBuff As String
			
		 Dim i As Integer	   'used to inspect each incoming character
		 Dim theChar As String  'each received character
		 Dim theInfo As String
		 Dim Printout As String
		 
		 Dim Y As Integer
		 Dim z As Integer
		 Dim l As Integer
		 Dim X As String
		 
		 
		 
		 
		 
		 Select Case MSComm1.CommEvent
		 ' Handle each event or error by placing
		 ' code below each case statement.
		 ' Errors
			Case comEventBreak   ' A Break was received.
			Case comEventCDTO	' CD (RLSD) Timeout.
			Case comEventCTSTO   ' CTS Timeout.
			Case comEventDSRTO   ' DSR Timeout.
			Case comEventFrame   ' Framing Error.
			Case comEventOverrun ' Data Lost.
			Case comEventRxOver  ' Receive buffer overflow.
			Case comEventRxParity   ' Parity Error.
			Case comEventTxFull  ' Transmit buffer full.
			Case comEventDCB	 ' Unexpected error retrieving DCB]

		 ' Events
			Case comEvCD   ' Change in the CD line.
			Case comEvCTS  ' Change in the CTS line.
			Case comEvDSR  ' Change in the DSR line.
			Case comEvRing ' Change in the Ring Indicator.
			Case comEvReceive ' Received RThreshold # of chars.
			
				InBuff = MSComm1.Input  'received 1 or more characters
			   
				For i = 1 To Len(InBuff) 'examine each received character in sequence
			
					theChar = Mid$(InBuff, i, 1) 'extract the next character
				   
					If Asc(theChar) = 13 Then 'Look for CR
						theInfo = Mid$(Newdata, 2, 1) 'Loads the second letter in the String "Newdata" in "theInfo" is a (I) or (P)
						
						
						If theInfo = "I" Then
						
								   X = Newdata
								   Y = Len(X)
								   z = Y - 5
								   If z >= 5 Then
											  For l = 1 To Y
											  Newdata = Right(X, z)
											  Next l
								   End If
								   
							
							InformationDisplay.SelLength = 0
							InformationDisplay.SelStart = Len(InformationDisplay.Text)
							InformationDisplay.SelText = Newdata + vbCr + vbLf   'include a CR and LF to separate from next line placed in OutputDisplay
							InformationDisplay.SelLength = 0
							
						ElseIf theInfo = "P" Then
						
								   X = Newdata
								   Y = Len(X)
								   z = Y - 5
								   If z >= 5 Then
											 For l = 1 To Y
											 Newdata = Right(X, z)
											 Next l
								   End If
						
							
							
							OutputDisplay.SelLength = 0
							OutputDisplay.SelStart = Len(OutputDisplay.Text)
							OutputDisplay.SelText = Newdata + vbCr + vbLf   'include a CR and LF to separate from next line placed in OutputDisplay
							OutputDisplay.SelLength = 0
							
						End If
					
						Newdata = "" 'clear NewData so it can assemble the next packet
				
						
					ElseIf Asc(theChar) <> 10 Then  'ignore linefeeds
						
						Newdata = Newdata + theChar 'received a character -- append it to NewData
									
					End If

				Next i
			
			Case comEvSend ' There are SThreshold number of
						   ' characters in the transmit buffer.
			Case comEvEOF  ' An EOF character was found in the
						   ' input stream.
		 End Select

	  End Sub

	Private Sub fullcmbbox()
	Dim i As Integer
	
	For i = 1 To 20
		Combo1.AddItem i
	Next
	End Sub




remember that the name of your button must be the same as button_click() function

This post has been edited by NoBrain: 09 July 2009 - 05:16 AM

Was This Post Helpful? 0
  • +
  • -

#6 oe8pck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-June 09

Re: Changing a Com port No.

Posted 09 July 2009 - 05:35 AM

If you tell me how I can send you the whole program. :D
Was This Post Helpful? 0
  • +
  • -

#7 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Changing a Com port No.

Posted 09 July 2009 - 05:53 AM

i PM you
Was This Post Helpful? 0
  • +
  • -

#8 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Changing a Com port No.

Posted 09 July 2009 - 06:46 AM

dude first of all you was using a image not a picturebox i fix it :) good luck :P

This post has been edited by NoBrain: 09 July 2009 - 06:47 AM

Was This Post Helpful? 0
  • +
  • -

#9 oe8pck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-June 09

Re: Changing a Com port No.

Posted 09 July 2009 - 07:05 AM

Now it works to the point where the screens are waiting for data from the rs232 and nothing comes. i think that an Integer for the portnum is perhaps no correct. What ekse could I try? I think the code is not seeing the number properly.

Paul OE8PCK
Was This Post Helpful? 0
  • +
  • -

#10 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Changing a Com port No.

Posted 09 July 2009 - 07:07 AM

hmm it works for me as i say i dont got any device to comport so i dont recive any events try debuging the events function (Private Sub MSComm1_OnComm())

This post has been edited by NoBrain: 09 July 2009 - 07:10 AM

Was This Post Helpful? 1
  • +
  • -

#11 oe8pck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-June 09

Re: Changing a Com port No.

Posted 09 July 2009 - 07:33 AM

Something has changed because when I just add a 4 to the Commport and blank out the portnum it still does not receive data. Sorry I don't understand your last message. I cannot find this expression in the code list.

This post has been edited by oe8pck: 09 July 2009 - 07:56 AM

Was This Post Helpful? 0
  • +
  • -

#12 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: Changing a Com port No.

Posted 11 July 2009 - 07:00 PM

so you have done every thing apart from this portion then i will gave you a solution

declare the comport variable as form level not in application level
and make it as public

using option button make textbox there in the form2 (according to you)
now use this code in form2
Option Explicit
'form2 = 1
Dim Comport As Integer

Private Sub Form_Load()
Form2.Caption = "OE8PCK Digital Wattmeter"

'''''''Dim Comport As Integer
End Sub


Public Sub CloseWindow()
   Dim Frm as new Form1

   Load Frm
   Frm.CmPort=val(txtComport.text)
   Unload Form2
   Frm.Show
End Sub



and in form1 code is like this
Public Cmport as integer

Private Sub Form_Load()
		 Form1.Caption = "OE8PCK Digital Wattmeter"
		 With MSComm1
		 
			.CommPort = Cmport
			.Handshaking = 2 - comRTS
			.RThreshold = 1
			.RTSEnable = True
			.Settings = "9600,n,8,1"
			.SThreshold = 1
			.PortOpen = True
			' Leave all other settings as default values.
		 End With
		 
		 OutputDisplay.Text = "Infobox" + vbCr + vbLf
		 InformationDisplay.Text = "Databox" + vbCr + vbLf
		 
		 Newdata = ""   'initialize to empty
	   
	  End 
End sub


and also consider the previous posts when you close the form you should close the comport

hope you got it
Was This Post Helpful? 1
  • +
  • -

#13 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: Changing a Com port No.

Posted 13 July 2009 - 03:29 AM

on the unload event of the form1 close the connection of the commport
Was This Post Helpful? 0
  • +
  • -

#14 ibenhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-August 09

Re: Changing a Com port No.

Posted 08 August 2009 - 09:43 AM

'form1 I added a Command button to trigger the data fuction but you can do this any way you would like.

' From1
Private Sub Command1_Click()
	Dim NumberStr As String
	NumberStr = "0"
	Set MyGetData = Form2
	NumberStr = MyGetData.Enter 'this will call the function in form 2
	Set MyGetDat = Nothing
	Text1.Text = NumberStr
End Sub





'Form2
Dim Comport As Integer
Public Function Enter() As String
	Me.Show vbModal
	Enter = Comport
End Function
Private Sub Option1_Click()
Comport = 1
End Sub
Private Sub Option2_Click()
Comport = 2
End Sub
Private Sub Option3_Click()
Comport = 3
End Sub
Private Sub Option4_Click()
Comport = 4
End Sub
Private Sub Option5_Click()
Comport = 5
End Sub


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1