Communication in VB using mscomm for serial port 1 and 2

K

Thread Starter

Keith

Hi,
I'm a college student that's quite new in VB. I'm more proficient in C, but my project requires VB. I've been looking around at webpages and and reference books and i still have trouble programming. I can get the basics, but i still can'tnderstand the part on using mscomm.ocx in the code. I'm required to acquire data from com port 1 and com port 2 every ever 5 seconds and save it to a file. I'm having trouble creating a file in the first place. I've got the code right(so i think), but there's a object required error msg always popping up. HELP !!! I need to get this project in in 1 week. I've been trying everything. Books that i can access to, surfing the net, asking friends (none knows VB). Please respond. Thanks,

Keith
 
What is the object and when you debug, what line does it highlight (thats the bad code)... Cut and paste it here, so I can hava look.....
 
Thanks !! I found the solution already...sheesh... I missed out on some commands.
But right now I need help in the area of opening a text document, as in exactly the same as double clicking a ***.txt document. I've tried using

NPAD = shell("notepad.exe", true)
AppActivate NPAD
SendKeys "%FO" , true
SendKeys "C:\Leak\" & search & ".txt" , True
SendKeys "{enter}"

And it appears sometimes...after a very long time ... or it just opens an untitled text document.... HELP!!
 
E

Eligio Palcich

Hi Dincer

Excellent site rich with serial port communication information for VB & C++.

Regards

Eligio Palcich
Soft System Sciences


> I think this site will guide u:
> www.ontrak.net/visual.htm
>
> u can save the data by using Access.
>
> Good Luck
> Dincer (Turkiye)
 
D

Dave Swalwell

Hi Kevin,

I think you must be trying to hard, its not too difficult.

Obviously to use the control you must reference it from your project (project>>references) and then you must stick an instance of it on a form.

Use a null modem cable i.e. on a 9-way to 9-way connector pin 2-3, pin 3-2 and pin 5-5 with pins 7 and 8 linked out at both sides (RTS-CTS)

Then you can reference it...


' open the comms port
Public Sub OpenPort()
' if we have any errors, give an error message
On Error GoTo errNoComms

If Not UserForm1.MSComm1.PortOpen Then
With UserForm1.MSComm1
.CommPort = 1 ' could be 2,3,4..)
.Settings = "9600,8,N,1"
.InputLen = 0
.RThreshold = 1
.SThreshold = 1
.InBufferSize = 8192
.OutBufferSize = 8192
.PortOpen = True
End With
End If
Exit Sub

' in case of an error, do this...
errNoComms:
MsgBox "Unable to establish communications" End Sub


' close the comms port and show the current status on the form
Public Sub ClosePort()
' Close the comm port (if its already open)
If UserForm1.MSComm1.PortOpen then
UserForm1.MSComm1.PortOpen = False
End Sub


' Each time a comm event happens then this routine
' will execute
Private Sub MSComm1_OnComm()
Dim InBuff As String

Select Case MSComm1.CommEvent
' Errors
Case comEventBreak ' Break received.
Case comEventCDTO ' CD (RLSD) Timeout.
Case comEventCTSTO ' CTS Timeout.
Case comEventDSRTO ' DSR Timeout.
Case comEventFrame ' Framing Error
Case comEventOverrun ' Data Lost.
Case comEventRxOver ' Rcv buffer ov/flw.
Case comEventRxParity ' Parity Error.
Case comEventTxFull ' Tx buffer full.
Case comEventDCB ' Unex. err.rtrv.DCB

' Events
Case comEvCD ' Change in the CD
Case comEvCTS ' Change in the CTS
Case comEvDSR ' Change in the DSR
Case comEvRing ' Change in the RI.
Case comEvReceive ' Rcvd RThreshold # chr
' RECEIVING DATA PUT CODE HERE
' INBUFF = DATA FROM MSCOMM CONTROL
' INPUT BUFFER
InBuff = MSComm1.Input
Debug.Print InBuff;

Case comEvSend 'SThreshold chars Tx buff
' SENDING DATA PUT CODE HERE
' TRANSFER A STRING OR CHAR TO
' THE OUTPUT BUFFER, MSCOMM CONTROL
' DOES THE REST
Case comEvEOF 'EOF found in input stream
End Select
End Sub
 
A

Alex Pavloff

Look up using ShellExecute in Visual Basic. MSDN KB article #Q170918. That'll do the exact same thing as a double click.

I would consider any use of SendKeys in an application to be a utter last resort.
 
R

Raymundo D. Balderas

Hi there Keith:

Your solution is very simple just do this thing:

PATHFILE = " c:\Leak\"
FILE = search & ".txt"
'
'thinking your choosen file is what contains search
'
NPAD = shell("NotePad.exe" & PATHFILE & FILE,1)

If your "search" file doesn't exist Notepad will
prompt you for create a new one, else it will opening

I wait for your results (offlist)

Don't abuse of Sendkeys,

Best regards:
 
D
>But right now I need help in the area of opening a text document, as in
>exactly the same as double clicking a ***.txt document. I've tried using

>NPAD = shell("notepad.exe", true)
>AppActivate NPAD
>SendKeys "%FO" , true
>SendKeys "C:\Leak\" & search & ".txt" , True
>SendKeys "{enter}"

>And it appears sometimes...after a very long time ... or it just opens an
>untitled text document.... HELP!!

Put the path of the file you wish to open into the Shell command path string:

Shell "notepad.exe c:\Leak\TheFileIWant.txt"

HTH,

David Wooden
Automation and Enterprise Solutions
Omron Electronics LLC
One East Commerce Drive
Schaumburg, IL 60173
Ph.: (847) 884-7034
Fax: (847) 884-9383
[email protected]
 
Keith,
Did you ever find out the ins and outs of Mscomm? I am currently working on a project that requires me to take an analog sample from a third party box that connects to COM1. I must do this only twice in my program, but I have never had to access the COM port before and I am unsure how to do it. If you can give me any pointers, please let me know. Thank You
Jeremy
 
Top