com_port name

Electronics Computer Programming Q&A
Post Reply
Michael-love-electronics
Posts: 84
Joined: Tue Nov 26, 2002 1:01 am
Contact:

com_port name

Post by Michael-love-electronics »

Hi,..<p> I want to make a program that controls 8 devices using computer. I want to do it using visual basic 5.0 or 6.0 but I don't know the names of the com_ports(1,2,3,4) in visual basic.<p> So could anyone help.????<p> Thanks very much for consideration
greg123
Posts: 361
Joined: Sat Sep 07, 2002 1:01 am
Location: St. John's NFLD Canada
Contact:

Re: com_port name

Post by greg123 »

Here are a couple replies that i've posted on another site:<p>=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
You can use the MSCOMM component to do this. Add the component from ctrl+T and selecting MScomm Control.<p>You will need to configure your program for the proper baud rate, parity and number of data bits. You can do this in the form load event:<p>Private Sub Form_Load()
Dim Data As Long<p> MSComm1.CommPort = 1 'select your com port
MSComm1.Settings = "9600,n,8,1" 'this must be configured to your specifications
'9600 is the baud rate, n is no parity bits, 8 data bits and 1 stop bit
MSComm1.PortOpen = True 'opens the port
End Sub<p>
You can then output data on the port with:<p>MSComm1.Output = data 'where data contains the data
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
3 Good Links,<p>http://www.freevbcode.com/ShowCode.Asp?ID=2236
http://www.planet-source-code.com/vb/sc ... 2&lngWId=1
http://www.arcelect.com/rs232.htm
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
How is your handshaking setup?<p>You might have to use a Null Modem Cable. The null modem is connected to facilitate the connection of 2 "DTE" or 2 "DCE". The null modem simulated the "handshaking" signals output by a serial connection. The cable wiring is important, here is some info on that:<p>null-mod.html">http://www.loop-back.com/null-mod.html
http://www.nullmodem.com/NullModem.htm<p>Diagrams:<p>http://www.beckwithelectric.com/relays/ ... _fig05.gif
http://goforit.unk.edu/datacomm/images/dc_00oe.gif
http://www.qsl.net/wb3afl/graphics/nullmdm.gif<p>
To ensure it's done correctly, you can buy pre-made null modem cables, as well as connectors for around $1.50. Check this site for info<p>http://www.national-tech.com/catalog/nu ... cables.htm
http://www.trianglecables.com/nulmodad.html<p>Bingie
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
greg123
Posts: 361
Joined: Sat Sep 07, 2002 1:01 am
Location: St. John's NFLD Canada
Contact:

Re: com_port name

Post by greg123 »

Sorry if some of this is redundant, post back if you have other questions:<p>Some Code Examples:<p>'Set the TX/RX parameters for the port<p>Private Sub Form_Load()
Dim Data As Long<p> MSComm1.CommPort = 1 'select your com port
MSComm1.Settings = "9600,n,8,1" 'this must be configured to your specifications
'9600 is the baud rate, n is no parity bits, 8 data bits and 1 stop bit
MSComm1.PortOpen = True 'opens the port
End Sub<p>
'To read data from the port on a mouse click
Private Sub readData_Click()
data = MSComm1.input
End Sub<p>'Or, Use a Timer to Continuously poll the port.
Private Sub Form_Load()
'add this to the above form load
timer1.Interval = 50 'change as you see fit. now set to 50 ms
timer1.enabled = False
End Sub<p>'Then use the command button to activate the timer and read the data<p>Private Sub readData_Click()
timer1.enabled = true
End Sub<p>'Then for the timer<p>Private Sub Time1_timer()
Do
Dummy = DoEvents()
Loop until MScomm1.InbufferCount >=11 'number of bits incoming change as needed
barcode = MSCOMM1.Input 'read the data
timer1.enabled = false 'stop reading data
End Sub <p>Using OnComm,<p>Private Sub MSComm1_OnComm()
Dim buffer As Variant
Select Case MSComm1.CommEvent
Case comEvReceive
buffer = MSComm1.Input
Text1.Text = Text1.Text & buffer
End Select
End Sub<p>=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
http://www.freevbcode.com/ShowCode.Asp?ID=2236
http://www.planet-source-code.com/vb/sc ... 2&lngWId=1
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
A good tutorial on RS232 can be found here:<p>http://www.arcelect.com/rs232.htm<p>You'll need the ActiveX control, MSComm:<p>http://www.ontrak.net/visual.htm
greg123
Posts: 361
Joined: Sat Sep 07, 2002 1:01 am
Location: St. John's NFLD Canada
Contact:

Re: com_port name

Post by greg123 »

And now - to check which COMM ports you have on your system....<p>Private Sub Form_Load()
Dim i As Integer<p> On Error Resume Next
For i = 1 To 8
MSComm1.CommPort = i
MSComm1.PortOpen = True
If Not (Err.Number = 8002) Then 'Msg 8002 = invalid port number
List1.AddItem "COM" & CStr(i)
End If
MSComm1.PortOpen = False
Next

End Sub<p>And To Get a list of all ports, paste the following:<p>Private Type PORT_INFO_2
pPortName As String
pMonitorName As String
pDescription As String
fPortType As Long
Reserved As Long
End Type
Private Type API_PORT_INFO_2
pPortName As Long
pMonitorName As Long
pDescription As Long
fPortType As Long
Reserved As Long
End Type
Private Declare Function EnumPorts Lib "winspool.drv" Alias "EnumPortsA" (ByVal pName As String, ByVal Level As Long, ByVal lpbPorts As Long, ByVal cbBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
Private Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GetProcessHeap Lib "kernel32" () As Long
Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, lpMem As Any) As Long
Dim Ports(0 To 100) As PORT_INFO_2
Public Function TrimStr(strName As String) As String
'Finds a null then trims the string
Dim x As Integer
x = InStr(strName, vbNullChar)
If x > 0 Then TrimStr = Left(strName, x - 1) Else TrimStr = strName
End Function
Public Function LPSTRtoSTRING(ByVal lngPointer As Long) As String
Dim lngLength As Long
'Get number of characters in string
lngLength = lstrlenW(lngPointer) * 2
'Initialize string so we have something to copy the string into
LPSTRtoSTRING = String(lngLength, 0)
'Copy the string
CopyMem ByVal StrPtr(LPSTRtoSTRING), ByVal lngPointer, lngLength
'Convert to Unicode
LPSTRtoSTRING = TrimStr(StrConv(LPSTRtoSTRING, vbUnicode))
End Function
'Use ServerName to specify the name of a Remote Workstation i.e. "//WIN95WKST"
'or leave it blank "" to get the ports of the local Machine
Public Function GetAvailablePorts(ServerName As String) As Long
Dim ret As Long
Dim PortsStruct(0 To 100) As API_PORT_INFO_2
Dim pcbNeeded As Long
Dim pcReturned As Long
Dim TempBuff As Long
Dim i As Integer
'Get the amount of bytes needed to contain the data returned by the API call
ret = EnumPorts(ServerName, 2, TempBuff, 0, pcbNeeded, pcReturned)
'Allocate the Buffer
TempBuff = HeapAlloc(GetProcessHeap(), 0, pcbNeeded)
ret = EnumPorts(ServerName, 2, TempBuff, pcbNeeded, pcbNeeded, pcReturned)
If ret Then
'Convert the returned String Pointer Values to VB String Type
CopyMem PortsStruct(0), ByVal TempBuff, pcbNeeded
For i = 0 To pcReturned - 1
Ports(i).pDescription = LPSTRtoSTRING(PortsStruct(i).pDescription)
Ports(i).pPortName = LPSTRtoSTRING(PortsStruct(i).pPortName)
Ports(i).pMonitorName = LPSTRtoSTRING(PortsStruct(i).pMonitorName)
Ports(i).fPortType = PortsStruct(i).fPortType
Next
End If
GetAvailablePorts = pcReturned
'Free the Heap Space allocated for the Buffer
If TempBuff Then HeapFree GetProcessHeap(), 0, TempBuff
End Function
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Dim NumPorts As Long
Dim i As Integer
'Get the Numbers of Ports in the System
'and Fill the Ports Structure
NumPorts = GetAvailablePorts("")
'Show the available Ports
Me.AutoRedraw = True
For i = 0 To NumPorts - 1
Me.Print Ports(i).pPortName
Next
End Sub
Michael-love-electronics
Posts: 84
Joined: Tue Nov 26, 2002 1:01 am
Contact:

Re: com_port name

Post by Michael-love-electronics »

You are great man :D <p> Thanks very very much :D
Michael-love-electronics
Posts: 84
Joined: Tue Nov 26, 2002 1:01 am
Contact:

Re: com_port name

Post by Michael-love-electronics »

Hi,..<p> Thanks very much for your help, but I have a silly question.<p> Would you please tell me in steps what should I do to make this program control the relay.?????<p> I really appreciate all your help
Post Reply

Who is online

Users browsing this forum: No registered users and 42 guests