Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need to send a string to the serial COM3: port using VBA.

558 views
Skip to first unread message

Rick

unread,
Apr 8, 2003, 4:24:01 AM4/8/03
to
I need to send a string to the COM3: port using Visual Basic. I'll start
the process with an OnClick event procedure but I'm not sure how to address
the serial port. I'd prefer the command line used the default modem port
(whatever that maybe) but if I have to refer to the port by name, that's ok
too.

The string will be (for example) "ATDT3037571234"

Any assistance would be GREATLY appreciated.

Rick C


Dirk Goldgar

unread,
Apr 8, 2003, 11:33:37 AM4/8/03
to
"Rick" <NOSPAMf...@msn.comNOSPAM> wrote in message
news:OdWU4ga$CHA....@TK2MSFTNGP11.phx.gbl...

Your best bet would probably be an ActiveX control designed for this
purpose, but I'm not familiar with what's available. Tony Toews has a
page of links on his site, I believe; poke around at
http://www.granite.ab.ca/accsmstr.htm .

Failing that, I think you can open and write to the port directly as a
binary file using standard Basic I/O statements. You might try
something like this:

Dim intFileNo As Integer

intFileNo = FreeFile()

Open "COM3:" For Binary As #intFileNo

Put intFileNo, , "ATDT3037571234"

Close #intFileNo

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

Adrian Jansen

unread,
Apr 8, 2003, 6:23:47 PM4/8/03
to
You can no longer write to a port as though it was a file - those days are
gone with DOS.

You need to put an MSComm control on your form - from the list of ActiveX
controls, then set up its properties, and send it the string you need.

Something like:

If MSComm1.PortOpen = False Then

MSComm1.CommPort = "Com3"
MSComm1.Settings = "9600,n,8,1" 'adjust speed as appropriate
'most modern modems will auto-baud to whatever speed you set
MSComm1.Handshaking = comNone

MSComm1.SThreshold = 0
MSComm1.InputLen = 0
MSComm1.RThreshold = 1 'event on every input char
MSComm1.PortOpen = True

End If


MSComm1.Output = "ATDT3037571234" & vbCrLf

This example comes from VB5 - where there is a good help file on the use of
the control.
Its not simple to detect which port has a modem on it, but you could try
opening each port in turn, and sending just an "ATI" string. Most modems
will reply with at least an "ATI" and some sort of ID. Of course other
devices may also echo just the ATI, and you still dont know for sure you
have a modem.

--
Regards,

Adrian Jansen
J & K MicroSystems
Microcomputer solutions for industrial control
"Dirk Goldgar" <d...@NOdataSPAMgnostics.com> wrote in message
news:OrBZjPe$CHA....@TK2MSFTNGP11.phx.gbl...

Dirk Goldgar

unread,
Apr 9, 2003, 12:32:26 PM4/9/03
to
I believe you are mistaken. Certainly you can write to LPT1: as a
file, or at least you could with Win95 -- I can't at this moment
guarantee that you can with the WinNT derivatives. Because it's been
a while since I tried this, and because I'm not sure that I've seen it
done with COM ports (though Googling returns suggestions that it
works), I'll test it out and report back. Unfortunately, I don't have
anything attached to COM or LPT ports on my own machine.

One thing you certainly can't do is *read* from a COM or LPT port as
if it were a file.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

"Adrian Jansen" <q...@noqqwhere.com> wrote in message
news:3e9350e7$1...@duster.adelaide.on.net...

Dirk Goldgar

unread,
Apr 9, 2003, 12:39:45 PM4/9/03
to
Assuming this does work, it occurs to me you may need to send a CR/LF
to the modem following the command string itself. In that case, you
might have to replace this:

> Put intFileNo, , "ATDT3037571234"

with this:

Put intFileNo, , "ATDT3037571234" & vbCrLf

or this

Print intFileNo, "ATDT3037571234"

If it turns out that this doesn't work, you might try the code posted
in MS Knowledgebase article 148857, "ACC: How to Dial a Phone Number
from MS Access 95/97":

http://support.microsoft.com/default.aspx?scid=kb;en-us;148857

Or, of course, use an ActiveX control.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

"Dirk Goldgar" <d...@NOdataSPAMgnostics.com> wrote in message
news:OrBZjPe$CHA....@TK2MSFTNGP11.phx.gbl...
>

Adrian Jansen

unread,
Apr 9, 2003, 7:19:13 PM4/9/03
to
Interesting, I just tried this in A2002 with VBA on a Win2000 machine and it
does work:

open "Com1:9600,n,8,1" for output as #1

print #1,"Test message" & vbcrlf

close #1

but it sent a pile of garbage, until I opened a standard comms package
Hyperteminal ), sent some strings, and then closed Hyperterm. After that I
could send from VBA. Presumably it doesnt fully set up the comms port.

I thought I tried this some time ago from VisualBasic5, and it didnt work at
all - just complained that you couldnt do that ( although on a different
machine running Win95 ) , but I just tried that too, and it works under
Win2000.

So yes, just open the port, get the settings correct, and print to it, works
like a charm !

Thanks Dirk for pointing this out, its certainly much easier for quick jobs
than the MSComm activeX control. Input may be a different story, altough I
might be temped just to try

Dim ln as string

LineInput #1,ln

to see what happens.

--
Regards,

Adrian Jansen
J & K MicroSystems
Microcomputer solutions for industrial control

"Dirk Goldgar" <d...@NOdataSPAMgnostics.com> wrote in message

news:eYxAJZr$CHA....@TK2MSFTNGP12.phx.gbl...

Dirk Goldgar

unread,
Apr 10, 2003, 2:58:14 AM4/10/03
to
"Adrian Jansen" <q...@noqqwhere.com> wrote in message
news:3e94...@duster.adelaide.on.net...

Well, thanks in return, Adrian, for saving me the trouble of checking
it out myself. I thought it would work, but you had made me
uncertain. I think I saw it used before for sending a BEL character
to open the till on a cash register.

> Input may be a different story, altough I
> might be temped just to try
>
> Dim ln as string
>
> LineInput #1,ln
>
> to see what happens.

Nahhh, it'll never work ... I don't think ... but try it and let us
know. :-)

> --
> Regards,
>
> Adrian Jansen
> J & K MicroSystems
> Microcomputer solutions for industrial control

And to you, Adrian.

0 new messages