Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

need help vb6 mscomm's serial interface

Not open for further replies.


New Member
i need ur help urgently, friends

i am now making a project.

now i come across one vb problem.

i can't control mscomm's port i mean ( open and close )

i can make it with C but i prefer cos i wanna my project excellent interface.

i am now using xp and i also test it with Win(Me) but doesn't work, too.i mean even to open the port.

well my project is to send the ASCII characters from one computer to the other stuff with wireless.

i have already made system and now i am to test it with software.

here my software for access point and access client.

pls help me out if any bug u see cos i am so new to vb.

Access Point

Dim k As Integer
Dim binary As String

Private Sub Command1_Click()
Dim word As String
Dim i As Integer, j As Integer
Dim cha As String
Dim deci As Integer

Dim re As Integer
word = Trim(Text1.Text)
For i = 1 To i = Len(word)
cha = Right$(word, Len(word) - i + 1)
deci = AscW(cha)
Do While deci > 0
re = deci Mod 2
binary = binary & CStr(re)
deci = deci / 2
binary = Trim$(binary)
For j = 1 To j = 8 - Len(binary)
binary = binary & "0"
MSComm1.DTREnable = True
Timer1.Enabled = True
Do While Timer1.Enabled = True
MSComm1.DTREnable = False
End Sub

Private Sub Form_Load()
k = 1
MSComm1.PortOpen = True

End Sub

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

Private Sub Timer1_Timer()

If AscB(CInt(Right$(binary, k))) = 49 Then
MSComm1.RTSEnable = True
Else: MSComm1.RTSEnable = False

End If
k = k + 1
If k > 8 Then
k = 1
Timer1.Enabled = False
End If

End Sub

Private Sub Timer2_Timer()
Form2.Caption = Right$(Form2.Caption, Len(Form2.Caption) - 1) & Left$(Form2.Caption, 1)

End Sub

Access Client

Dim bi As String
Dim k As Integer
Dim deci As Integer
Dim word As String

Public Function ConvertBinaryToDecimal(BinVal As String) As String
Dim iVal#, temp#, i%, Length%

Length = Len(BinVal)
For i = 0 To Length - 1
temp = CInt(Mid(BinVal, Length - i, 1))
iVal = iVal + (temp * (2 ^ i))
Next i
ConvertBinaryToDecimal = iVal
End Function

Private Sub Form_Load()
MSComm1.PortOpen = True

Timer1.Enabled = True
k = 1
bi = ""

End Sub

Private Sub Timer1_Timer()
If MSComm1.DSRHolding = True Then
Timer2.Enabled = True
Else: Timer2.Enabled = False
End If

End Sub

Private Sub Timer2_Timer()
If (MSComm1.CommEvent = comEvCTS) Then
bi = bi & "1"
Else: bi = bi & "0"
End If
k = k + 1
If k > 8 Then
k = 1
deci = ConvertBinaryToDecimal(Trim$(bi))
word = Chr(deci)
Label1.Caption = Label1.Caption + word
End If

End Sub

Private Sub Timer3_Timer()
Form2.Caption = Right$(Form2.Caption, Len(Form2.Caption) - 1) & Left$(Form2.Caption, 1)

End Sub
Have you set MSCOMMs CommPort property to a free port. Sometimes it happens that you are trying to access a port that is currently being used by other software. Set it and try again. It might work.

In Win XP ports cannot be accesed because of security reasons. There are many similar hardware-acess issues in XP. Search for a work-around in Google.
did you set the port to use ?
MSComm1.CommPort = ??

did you set the data speed/format
MSComm1.Settings = ??
For example 9600, N, 8, 1 for 9600 baud, 8 databits, 1 stopbit and No parity

MSComm should work with any windows, also XP. The serial port api is available in winXP and fully functional. It's the parallel port that is blocked in XP/NT
i have also tried with port 1 and 2 too.
but still don't work too.
if i use the portopen = true error is port is already open
and if i cancel out that statement and put portopen = false error is port is not open.

now what should i do
but i can still measure volt from port.
i am so tired abt it
pls pls help me out
This seems really strange..... :roll:

Can you list out all the design-time properties of MSCOMM that you have set?

Also, I see two places where you have tried to set PortOpen =TRUE. Is that the code of a single form or are you using two different forms? Is that a MDI interface?

May be the two instances of MSCOMM are trying to open port one by one and that is causing error.
.... (scratching head)...
I tought I've already posted one reply to this...

bobo, if you want to PROGRAM for RS232, first you have to
learn how to USE serial communication with other programs
(you definitely need to learn how to use terminal emulator).
Don't panic, it's not hard. I could give you ready fish
but instead I will try to teach you how to fish.
What you need are three things:

You need some HARDWARE so get or make a crossover cable.
RS232 crossover cable is often called "null-modem cable".
the simplest pinout is
and both connectors should be DB9 female.
In case you need handshaking you can try something more
Note that handshaking is usually NOT essential to develop
application. You can do pretty much everything and once
you are happy with it just change handshaking property
from 'NONE' to whatever you need. The important thing is
to get the crossover cable (check internet for more pinouts
if you want to get really confused hehe...)

You need some SOFTWARE to test connection, ports and hardware
(that's the cable we just mentioned). Since you are lucky guy,
you already have one (or if it's not installed already, just
put the windows CD in you PC and install it).
It's called Hyperterminal and you should be able to find it
in your programs/accessories/communication
Use the cable to connect two COM ports (they can be on same PC
it really doesn't matter). Start one terminal and assign one
COM port to it (Com1 for example). Start the other terminal
and assign the other COM port (COM2 for example).
Make sure they both use same settings (no handhaking if you use
simple cable). In the properties, select "windows keys" and under
ASCII setup check everything except "force 7-bit".
Try it out. Typing in one terminal will make same text appear in
the other window as well. That's good, it means everything is ok.
Play with this until you are REALLY familiar with it.
See what happens when you setup different baud rate, if both
terminals are using same COM port, try changing port settings
while connected (port used by application) etc.

If you got the first two parts working fine, you might be ready
for PROGRAMMING. Now that you know how to use terminal, you can
use it to monitor what your program sends out and to send data
to your program.

And some TIPS before you jump into programming:

VB (actually mscomm32.ocx) cannot use baud rates higher than
19200. If you need speeds higher than that, you will need some
3rd party component.

Do not open com port when application loads.
Make sure you can change port number and port settings BEFORE
opening com port. (you were opening port in Form_Load and then
trying to change port settings in Command1_Click).

The simplest way to create RS232 application using VB is to:
- start new project
- press Ctrl+T to add mscomm
- drop one mscomm on your form and change RTreshhold from "0" to "1"
- create "Connect" and "Diconnect" buttons
- create indicator "Connected"
- create text box or whatever to change port number (1,2,3,4...)
- create text box with port properties (such as "9600,8,n,1")
so you can change it on fly.
- create two larger text boxes ("Send" amd "Receive") and make
sure that at least the "Receive" box is multiline (change property).
- make sure that whatever comes in is added to "Receive" box.
You will need to buffer it because reading from COM port clears
ports buffer (just read it into string and add content of the string
to your "Receive" box).
- make sure that "Receive" box has scrollbars or at least provide a way
to clear it (simple "Clear" button will do).
- create "Send Data" button that will send whatever is in "Send"
text box (if the port is open).
- This is pretty much it. You are only couple of years late to get
rich by making something like ICQ.
Do you have a Palm or PDA cradle? Or Do you have ActiveSync? Normally those programs that run the cradle usage open up the serial port unless you strictly tell them that you don't want to connect to the serial port.

I would got to the task manager and start closing programs one by one until you find the one that has the serial port open....

The error ocurrs because another device has the serial port already open, and your application can't close a port that your application hasn't open.

Good Luck

Not open for further replies.

Latest threads