Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple ports open simulteanously?

Status
Not open for further replies.

Rich7638

Technical User
Apr 14, 2005
13
0
0
US
I'm using VB6 and I have successfully used MSComm to communicate with a scale. Now I want connect 5 scales and use the ONCOM event for each of them.

I did some searching here and I did find a reference to the MSComm1.index property and setting it to 0. Every time I do so, I get an error at run time - "Wrong number of arguments or invalid property assignment." In addition, I'm not sure how to proceed even if it didn't generate an error.

Any suggestions?
 
Rich,

When you set the index property to 0 you created and array. In your code you will have to use:
Code:
   ...

  MSComm1(0).OnComm

   ...

If you don't use the index for MSComm1 it will give an error that there is the wrong number of arguments.


HyperEngineer
If it ain't broke, it probably needs improvement.
 
You will need 5 MSCOMM controls on your form, one for each scale. I'm assuming you have some sort of add-on card for your PC that will let you hook up to 5 serial inputs to it, such as a USB adapter or the like.

Each MSCOMM control would have the same name, but with a different index. You could even start the index at 1 and go up to 5 so that it's easier to keep track in code.

When your OnComm event fires, the index will show you which scale sent the message.

Does this help?
 
If the scale outputs can be configured to RS422 or RS485, you can hook up a number of scales on the same cable to the one RS422 to RS232 adapter and use the one Comms Computer port.
(Lots of RS422 connection examples on the web)
As Comms ports are becomming obsolete in motherboards, you can get a RS422 to USB adapter and use that, The USB Adapter driver makes it look like a RS232 device to your VB6 software.

If the scales are only RS232 then you can get a RS232 to USB adapter for each scale and USB extenders if not enough ports are available on the computer.

see
 
Thanks for your response everyone. I'll be trying to get this going sometime today. As far as the number of ports go, I'm using a RS232-Ethernet(Wireless) adapter on each scale. The drivers loaded on the PC then make the IP to Com Port association and then everything behaves as if there was an actual port on the PC. It's pretty slick. I can have just 1 PC talking to serveral scales and bar code scanners.

Rich
 
How do you set the index?

Right now, I have two MSComm controls on the form. Is the index set automatically because there are two controls?

When I try to open a port one using this method:
Code:
With MSComm1(0)
   .CommPort = iport
   .Settings = "9600,n,8,1"
   .EOFEnable = True
   .InputLen = 0
   .RThreshold = 1
   .PortOpen = True
End With
I get a "Wrong number of arguments or invalid property" error.

Rich
 
Update of sorts.

When you add multiple MSComm controls to a form, VB names them uniquely. First one is MSComm1, second is MSComm2, etc.

Since that is the case, the only way I could handle the OnComm events was to create one for each control. Once done, I was now able to process multiple scale data transmissions.

Perhaps there is a more elegant method, but I can't seem to get the "index" figured out. Any time I try to set it or read it, I get errors.

Rich
 
>Perhaps there is a more elegant method,

The 'index' method ...
 
You need to create a control array.

The easiest way to do this up front is, drag your first comm control on the form, and name it what you want. Let's say you leave the default name of MSComm1. Then copy/paste that control onto the same form... VB will ask if you want to create a control array; answer "yes". Now, the original control is referenced as MsComm1(0) and the new control is MsComm1(1). Further copy/pastes will get you MsComm1(2), MsComm1(3), and MsComm1(4).

Now when you double-click any of the controls, the OnComm event should look like this:
Code:
Private Sub MSComm1_OnComm(Index As Integer)

End Sub
The index will correspond to the control that received the data.
 

If you want to do it at design time, click on the MSComm control you want to be the first one. Pull up the Properties window. Look for the (Name) property and make it what you want. In this case MSComm1. Then go down the Properties list until you see Index. Make this value 0. Now click on the second MSComm control. Go to the Properties window and find the (Name) property and set its value to MSComm1 just like the first MSComm control. Now go down the Properties list again until you get the Index property. Set its value to 1. Continue on until you have all your MSComm controls set up. Then for coding follow Guitarzan's reply.

HyperEngineer
If it ain't broke, it probably needs improvement.
 
Also, if a control is in a control array, its index property will have a value. If it is not, there won't be a value there.
 
When you are placing the Comms control on the form place the first one the normal way then for the subsequent ones, COPY the icon and paste them on the form.
When you paste the first one it will ask you if you want to creat a control with the same name, Answer yes and indexes will be automatically added to further pasted controls starting with 0 for your first one which you can delete after all controls are placed. (Indexes then become 1 to n)

CODE-

Private Sub MSComm1_OnComm(Index As Integer)
'receive the data into a buffer here then

Select case Index
Case 1
'Port 1 code here

Case 2
'Port 2

Case 3
'Port 3

Case 4
'Port 4

Case 5
'Port 5

End Select
End Sub
 
A little knowledge goes a long way.

Thank you everyone for educating me in the ways of control arrays. It all works great now.

Rich
 
<Indexes then become 1 to n
Not to contradict what Ted is saying, but keep in mind that in our world there is a good deal of confusion as to which arrays are 1-based and which arrays are 0-based. For example, ADO Fields are 0-based and the Status Bar's Panels are 1-based. The choice of which to use is a personal one (if you like, you can make 1 the default base with the statement "Option Base 1" in your code), however, the trend has been to move to 0-based arrays. For example, .Net arrays are 0-based. The idea is that you can always add some sort of constant to the offset in your code if you need one, and this makes for more stylistic consistency in the body of code as a whole.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top