Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

RS232 with VBA

Posted on 2003-11-04
15
Medium Priority
?
2,955 Views
Last Modified: 2008-08-27
Didn't know whether to put this in Languages or Database, so lets start here.

I am (attempting to !) write an application which communicates with a connected instrument via RS232. The problem I am having is that I cannot find any method of using VBA to control the RS232 ports. Can anyone help me with this?

I have done extensive searching on the web and can only find various methods using mscomm.ocx which does not come as part of Access.

I am sure there is a way but don't know what it is.

Please help.
0
Comment
Question by:wheatpark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 
LVL 1

Accepted Solution

by:
L30 earned 500 total points
ID: 9680053
ummm, what is RS232? MS Access has a lot of features you can inlude in it. You have to look at the references. If you are looking at messing around with ports in MS Access I reccomend you look at Windows Sockets. There's a lot you can do there. The reference to the Windows sockets can be included on any MS Access forms. The name of the file is

mswinsck.ocx and usually found on C:\Windows\System32 or C:\Winnt\System32 for Pro editions

the winsock control will give you unlimited flexibility and power as far as ports go

If you need more info then provide more details
cheers
0
 
LVL 18

Assisted Solution

by:bonjour-aut
bonjour-aut earned 500 total points
ID: 9680312
have a look at : http://virtualserialport.com/products/seriallogger/

if you evaluate your time, you will not be cheaper off, i think
at least you can try the sofware for free

regards, Franz
0
 
LVL 18

Assisted Solution

by:lludden
lludden earned 500 total points
ID: 9680408
Writing serial I/O routines is not for the faint of heart.  Check out http://www.gleaf.com/CommX/CommXInfo1.asp for one of the best controls for doing serial I/O
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 54

Assisted Solution

by:nico5038
nico5038 earned 500 total points
ID: 9680879
You'll have to use the OPEN statement to get a buffer linked to the device.
When it's connected to the COM1 port you can use e.g.:
    Open "COM1" For Output As #1
    Print #1, Chr$(7)
    Close #1
Like stated in:
http://www.experts-exchange.com/Databases/MS_Access/Q_12024419.html?query=com1&searchType=topic

Nic;o)
0
 

Author Comment

by:wheatpark
ID: 9685202
Thanks so far.
The equipment connected to the serial port is really simple. Each control string sent is only 6 bytes long. The communication format is fixed at 9600 baud, 8 data bits, 1 stop bit and no parity.

The mentioned OCX's look interesting (but perhaps a little too involved for me). The website for the gleaf variant does not work correctly (I cannot click the 'Own It Now' button).

What I need to do is as follows:-

Open the com port (COM1) with the above comm setting.

Write the 6 bytes to the com port

Read back 5 bytes from the connected equipment (the acknowledgement)

Close the COM port.

This sounds so simple and yet I am struggling to do it.

Thanks all for your help so far but this is still not solved.
0
 

Author Comment

by:wheatpark
ID: 9686270
Ok, I have found in my C:\Winnt\System32 the MSCOMM32.OCX. This seems to be a big step towards solving the problem. My code is as follows:-

Private Sub btnPSUOff_Click()
    Dim SendChar(5) As Byte
    If Not SPComms.PortOpen Then   ' Open the comm port if not already open
      SPComms.PortOpen = True
    End If
    If Not SPComms.PortOpen Then
      MsgBox "cannot open comm port "
      End
    End If
   
    SendChar(0) = 6
    SendChar(1) = 1
    SendChar(2) = 31
    SendChar(3) = 14
    SendChar(4) = 1
    SendChar(5) = 105
   
    SPComms.Output = SendChar()

    SPComms.PortOpen = False        'Close Port
End Sub


This is called from a button press on a form which includes the Microsoft Comm Control (from the ocx file above). My intention is to send six Hex bytes to the attached instrument: #06#01#1F#0E#01#69
This works perfectly well using advanced serial port monitor.

Note also, I have another button which does the following code (although I have set these values as defaults inside the control):-

Private Sub btnInitialise_Click()
    SPComms.CommPort = 1        'Set to use COM1
    SPComms.DTREnable = True    'Don't think this matters
    SPComms.Handshaking = 0     'No Handshaking
    '0="No Handshaking"
    '1="XOn/XOff handshaking"
    '2="Request-to-send/clear-to-send handshaking."
    '3="Request-to-send, XOn/XOff handshaking."
    SPComms.Settings = "9600,n,8,1"     '9600 Baud, No Parity, 8 Data bits, 1 Stop Bit
End Sub
0
 
LVL 54

Expert Comment

by:nico5038
ID: 9687977
Sounds good, as always a lot of trail and error, but I see you did it well !
What's remaining ?

Nic;o)
0
 

Author Comment

by:wheatpark
ID: 9688024
I'm sorry but in reading it again, my last post wasn't clear.

It should send the six bytes to COM1 to the attached instrument (which does work using 'Advance Serial Port Monitor' however, this does not give the desired result using my code above.

I am sure that once I crack this, everything else I need to do will be incredibly simple.

I appreciate your help.
0
 
LVL 54

Expert Comment

by:nico5038
ID: 9688241
Did you try to send:
    dim strSendChar as string
    strSendChar= chr(6)&chr(1)&chr(31)&chr(14)&chr(1)&chr(105)
    SPComms.Output = strSendChar

Nic;o)
0
 

Author Comment

by:wheatpark
ID: 9692614
Unfortunately I did. Reading a reference I found at

http://www.yes-tele.com/mscomm.html

tells me "To send text data using the Output property, you must specify a Variant that contains a string. To send binary data, you must pass a Variant which contains a byte array to the Output property."

The above code was the closest I could get to it.
0
 

Author Comment

by:wheatpark
ID: 9692709
Note, have just found some excellent help here:

http://msdn.microsoft.com/library/en-us/comm98/html/vbobjComm.asp

But this still does not solve the problem.
0
 
LVL 54

Expert Comment

by:nico5038
ID: 9692781
Hmm, did you also look into:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconusingcommunicationscontrol.asp

In "the old days" only sending the characters to the output device was enough, but perhaps also a trailing vbCRLF is needed to generate a linefeed and show the device the data is complete...

Nic;o)
0
 

Author Comment

by:wheatpark
ID: 9692844
Unfortunately sending vbCR or vbLF or vbCRLF has no effect.

Much information, all of little help.

I think my next check will be to connect the output of this PC to another running 'Advance Serial Port Monitor' so I can see exactly what is being put out. However, my lead is at home and I won't be able to do this until tomorrow.

I'll let you know what this tells me.

Thanks
0
 
LVL 32

Expert Comment

by:jadedata
ID: 10022351
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: L30 {http:#9680053} & bonjour-aut {http:#9680312} & lludden {http:#9680408} & nico5038 {http:#9680879}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jadedata
EE Cleanup Volunteer
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question