COM4

Myn

Myn

Associate
Joined
31 Mar 2011
Posts
73
Location
Bath, UK
After a somewhat ambiguous title:

I've just inherited a program in VB, a language I have no experience in, which apparently opens a serial port to COM4, and listens for incoming data. This data should be being generated by a hastily put-together box which is plugged in via USB. The program, however, isn't finding any.

How can I check whether any data is incoming on the COM4 port?

Thanks in advance
 
I may be missing something here, but is the VB program listening for data (either Ethernet, user input whatever), and then trying to squirt this out the serial port on COM4? Or is the program listening for data coming in on the serial port? Or both?

If it's listening for data on the serial port, you should just be able to open HyperTerminal and connect it to COM4 at the appropriate baud and begin typing away to generate some data. Failing that, maybe look into 'serial sniffer' programs which should allow you to snoop the serial data, much like Wireshark does for Ethernet.
 
The latter, just listening for data.

I'll give HyperTerminal a try when I'm back in the office tomorrow, thanks :)
 
Cool, post back how it goes :)

I love it when you have get a new program dumped on you, either to maintain, or to add new features. Lots of fun and games until you get some traction with how the thing actually hangs together...or doesn't.
 
I'm running Windows 7, so have downloaded HyperTerminal from hilgraeve.net, but am having trouble setting it up to listen for COM4, would you mind if I'm a pain and ask how I might do that? :)
 
Is COM4 a physical serial port on the PC? I'm going to assume not in which case it's a virtual COM port that's created by something.

HyperTerminal doesn't create the COM port, it simply connects to it to allow you to send/receive data.

For example, say you have two physical COM ports, COM1 and COM2. If you connect to COM1 in HyperTerminal, then physically hook up a serial device to the COM1 port of your PC, this would allow you to see data the device sends, or send data to it.

For virtual COM ports (which I'm assuming yours is as most PCs don't have any serial ports at all) it uses the same principle, but instead of the data being sent out the physical COM port, it should be picked up my the program that's listening on this COM port. That's my understanding anyway.

Normally, virtual COM ports are used to allow to you talk to serial devices without having to be physically connected to them to do so.

For example you could have a set up like this with an ethernet network (local or internet) in between, but from HyperTerminal's point of view, it's simply sending data into a COM port:

HyperTerminal --> virtual com port --------> ethernet network ---> some ethernet to serial converter --> serial device

It's the responsibility of the lower layers of how to handle where it's going.

In HyperTerminal you should just be able to go File-->New, give it a name (doesn't matter what), then select COM4 and hit OK. Then just select the correct baud rate settings (although for virtual COM ports, sometimes the baud doesn't actually matter) and you should be all set.
 
Last edited:
In HyperTerminal you should just be able to go File-->New, give it a name (doesn't matter what), then select COM4 and hit OK. Then just select the correct baud rate settings (although for virtual COM ports, sometimes the baud doesn't actually matter) and you should be all set.

This is the part giving me trouble; when I go to File > New, I'm asked to select a name and icon, then asked for an address, port, and a choice of TCP (Winsock) or TCP (SSH). Can't find anything about COM4 :-/

Thanks again for all the help you're giving! You're a lifesaver!
 
I'm on Win XP at work but it should be similar - Have a look in device manager, expand 'Ports (Com & LPT)' and check if a new port appears when you plug in your box. If it does it will show which port number the box is talking to.
It might have been COM4 on the computer of whoever wrote the VB program but your USB ports might be assigned different COM port names so you may need to alter which port the program listens to.
 
This is the part giving me trouble; when I go to File > New, I'm asked to select a name and icon, then asked for an address, port, and a choice of TCP (Winsock) or TCP (SSH). Can't find anything about COM4 :-/

Thanks again for all the help you're giving! You're a lifesaver!

Oh I see. For some reason HyperTerminal can't see the virtual COM port or it's not been created then. As m0rte says, check device manager to see if it shows up there. If it's been created properly, it should show up under the 'Connect using' dropdown menu.

I'm on Win XP at work but it should be similar - Have a look in device manager, expand 'Ports (Com & LPT)' and check if a new port appears when you plug in your box. If it does it will show which port number the box is talking to.
It might have been COM4 on the computer of whoever wrote the VB program but your USB ports might be assigned different COM port names so you may need to alter which port the program listens to.

I'd guess that no COM port is being created as it should show up in HyperTerminal if it's shown in device manager.
 
Took a while as Ports (COM & LPT) wasn't showing, but just got it appearing; unfortunately nothing new appearing when the box is plugged in!
 
Took a while as Ports (COM & LPT) wasn't showing, but just got it appearing; unfortunately nothing new appearing when the box is plugged in!

Well at least you've narrowed it down a bit then. That's the first issue I'd look at, because something has to create the COM port for it to show up in HyperTerminal (or for your VB program to receive data).

Let us know how you get on :)
 
Yeah, I've got one of the Electrical Engineers looking at the box today.

I've been going through the code again and found that it originally tries to open serial port COM4:
Code:
SerialPort1.PortName = TextBox2.Text
Try
   SerialPort1.Open()
   TextBox1.Text = SerialPort1.PortName
Catch ex As Exception
   MsgBox("Caught")
End Try
This always throws an exception, and later on sends data to the port before listening for a response:
Code:
SerialPort1.Write("S")
...
data_a = SerialPort1.ReadByte()

It's just always crashing at SerialPort1.Open(), so is the problem that there is no port to open?

Isn't it great when someone comments code and gives useful variable names?... :)

Thanks again for all the help!
 
I don't know VB but MsgBox("Caught") isn't useful - at the very least change it to include the Exception message. I agree that it will almost certainly be because it can't open COM4 though.

Stupid question time - do you still have COM4 open in Hyperterm? Only one process can have a COM port open at the same time, so Hyperterm will lock any COM port it has open.

Did we establish if COM4 is a physical or a virtual one? If it's virtual then you should be able to virtually connect it to another COM port and test communications that way. If it's a physical COM port I'd suggest getting a null modem cable made up, connect COM4 to a different COM port, open both COM ports up in Hyperterm and test that you can actually send and receive both ways.

Also worth checking what TextBox2.Text is and check it contains the right value for the API.
 
I don't know VB but MsgBox("Caught") isn't useful - at the very least change it to include the Exception message. I agree that it will almost certainly be because it can't open COM4 though.

Stupid question time - do you still have COM4 open in Hyperterm? Only one process can have a COM port open at the same time, so Hyperterm will lock any COM port it has open.

Did we establish if COM4 is a physical or a virtual one? If it's virtual then you should be able to virtually connect it to another COM port and test communications that way. If it's a physical COM port I'd suggest getting a null modem cable made up, connect COM4 to a different COM port, open both COM ports up in Hyperterm and test that you can actually send and receive both ways.

Also worth checking what TextBox2.Text is and check it contains the right value for the API.

I've already changed the exception message, it says Port COM4 does not exist.

HyperTerm never had it open in the first place; it couldn't find it.

I've retrieved a list of all serial port names available in VB; I've had the program take each one from the list and immediately try connecting to it, but each time returns with a message saying the port does not exist.

At the moment, it's only returning one value each time, which is COM5, or variations such as COM53, COM5M, etc.
 
OK, so as I understand you have some sort of USB device that should make a load of COM ports (including COM4) available to the OS? This doesn't appear to be happening as evident by the fact they don't appear in Device Manager and can't be opened with HyperTerm.

Just a thought, you aren't using some sort of lockdown policy that denies access to the Serial Ports? We recently installed Lumension Device Control on our machines we're surprised to find out that all of our serial devices stopped working :p

I suggest trying to get the software working with virtual serial ports on a test rig before worrying about the USB device. We use Eltima Virtual Serial Port Driver: http://www.eltima.com/products/vspdxp/
 
OK, so as I understand you have some sort of USB device that should make a load of COM ports (including COM4) available to the OS? This doesn't appear to be happening as evident by the fact they don't appear in Device Manager and can't be opened with HyperTerm.

Just a thought, you aren't using some sort of lockdown policy that denies access to the Serial Ports? We recently installed Lumension Device Control on our machines we're surprised to find out that all of our serial devices stopped working :p

I suggest trying to get the software working with virtual serial ports on a test rig before worrying about the USB device. We use Eltima Virtual Serial Port Driver: http://www.eltima.com/products/vspdxp/

This is actually a good point. Definitely look into some sort of test rig where you can be sure their creation isn't somehow being blocked.

I've actually had some experience with Eltima's software at work where we also had access to the source code. Whilst it worked for the most part, imo, the code is a mess.

As far as I remember Eltima's software also comes with a trial so you should be able to use it rule in/out whether creation of the port is being blocked.

I've never used VB before, but I presume there must be a way of getting more information of what exception was actually being raised. Some sort of traceback module like in Python maybe?

Edit: Whoops, seemed I missed a few posts and you've already looked into the exception a bit more !
 
Last edited:
Has the test rig you're using got any physical RS232 or virtualised (eg: this USB to Virtual RS232 'box' you’re using; obviously will only show up if plugged in and working - make sure drivers are installed/updated etc) ports ie: are any ports being listed under Device Manager? If a port doesn't physical or virtually exist, then it'll throw an error when you try and open a port; so that could explain that issue.
I'm guessing you're using the GetPortNames() property to return the array of available ports? Seems a bit odd if it is returning port names yet you can't open them. As scorza said, might be worth using a NULL cable with another system (or loopback with two ports on the same test rig and run two instances of the app) and use HyperTerminal or Putty and see if you can parse data between the two as it could potentially be a rig issue.

I'd also check the ports status using IsOpen() (or could use Try/Catch) prior to sending data and I would also output the exception message (ex.Message) for catches; will be of more use to you for debugging.
 
All fixed! Turns out it was a driver issue; I'd been given incorrect drivers for our box. /facepalm

The moment I installed them, I plugged the device in and there it was on COM4.

Thanks for all the help guys!
 
Back
Top Bottom