visual basic 6.0 comm port code error - vb6

I am actually trying to get data from port1 but the error Invalid port number is generated.
The code is:
Private Sub Command1_Click()
MsgBox ("The port is open " & MSComm1.PortOpen)
If (MSComm1.PortOpen = False) Then
MSComm1.PortOpen = True
End If
Command1.Enabled = False
Command2.Enabled = True
End Sub
Private Sub Command2_Click()
If (MSComm1.PortOpen = True) Then
MSComm1.PortOpen = False
End If
Command1.Enabled = True
Command2.Enabled = False
End Sub
Private Sub Form_Load()
With MSComm1
.CommPort = 1
.RThreshold = 1
.RTSEnable = True
.Settings = "9600,N,8,1"
.InputLen = 127
.SThreshold = 1
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
If (MSComm1.PortOpen = True) Then
MSComm1.PortOpen = False
End If
End Sub
Private Sub MSComm1_OnComm()
Dim Buffer As String
Select Case MSComm1.CommEvent
Case comEvReceive
'Text1.Text = " "
Buffer = MSComm1.Input
Text1.Text = Text1.Text & Buffer
End Select
End Sub

Try different COM port. A number between 1 and 16 is acceptable.
' Open the serial port
MSComm1.CommPort = 2
MSComm1.Settings = "9600,N,8,1"
MSComm1.PortOpen = True
"The CommPort property sets which serial port to open. Assuming that a modem is connected to COM2, the above example sets the value to 2 (COM2) and connects to the modem. You can set the CommPort property value to any number between 1 and 16 (the default is 1). If, however, you set this value to a COM port that does not exist for the system on which your application is run, an error will be generated."
Sauce: dx.eng.uiowa.edu/eedesign/MScomm.doc

have a look at the code i posted in the following answer :
search for available com ports
running that code will give you a list of available com ports
use it in your code, and let your code select a com port from that list

Related

Microsoft VS - Access

I'm having a problem with access-VS it's ways Variable not defined can anyone help me?
This is the code that i'm working on: (am a newbie)
Option Compare Database
Option Explicit
Private Sub btnLogin_Click()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("tbl1Employees", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='" & Me.txtUserNAme & "'"
If rs.NoMatch = True Then
Me.lblWrongUser.Visible = True
Me.txtUserNAme.SetFocus
Exit Sub
End If
Me.lblWrongUser.Visible = False
If rs!Password <> Nz(Me.txtPassword, "") Then
Me.lblWrongPass.Visible = True
Me.txtPassword.SetFocus
Exit Sub
End If
Me.lblWrongPass.Visible = False
DoCmd.OpenForm "frmMenu"
DoCmd.Close acForm, Me.Name
End Sub

Serial Communication in VB6

I am new with vb6 and communication app, I try to run a example that transmitting and receiving data between two serial ports ( I install two serial ports by com0com soft).
In this example, I want User to type charracters in textbox1, then click command button, and charracters in textbox1 display in textbox2.
Private Sub Command1_Click()
com1.Output = Text1.Text
End Sub
Private Sub Form_Load()
com1.CommPort = 1
com1.Settings = "9600,n,8,1"
com1.PortOpen = True
com2.CommPort = 2
com2.Settings = "9600,n,8,1"
com2.PortOpen = True
Text1.Text = ""
Text2.Text = ""
End Sub
Private Sub com2_OnComm()
If com2.CommEvent = comEvReceive Then
Text2.Text = Text2.Text + com2.Input
End If
End Sub
I screened form in the example and setting in com0com port, I don't have enought reputation to post image, so I upload this to flickr.
http://farm8.staticflickr.com/7627/16740707238_6b1d9ec3ab_b.jpg
But when I try to run this example by type charracters in textbox1 and click button, nothing happen.
So what am I doing wrong?. If so, how can I do to get data from com1 port to com2port? Or any advice for anything!
thanks for reading !
I think you need to look a little harder at the documentation.
Here's a minimal example where COM3/COM4 are the looped ports on my machine:
Option Explicit
Private Sub Command1_Click()
If Len(Text1.Text) > 0 Then
MSComm1.Output = Text1.Text
Text1.Text = vbNullString
End If
Text1.SetFocus
End Sub
Private Sub Form_Load()
With MSComm1
.CommPort = 3
.Settings = "256000,n,8,1"
.Handshaking = comNone
.SThreshold = 0 'No events after send completions.
.RThreshold = 0 'No events after receive completions.
.PortOpen = True
End With
With MSComm2
.CommPort = 4
.Settings = "256000,n,8,1"
.EOFEnable = False
.Handshaking = comNone
.InputMode = comInputModeText
.RThreshold = 1 'Event for each character received. Terribly
'inefficient but if char-by-char events are
'required there isn't much choice.
.SThreshold = 0 'No events after send completions.
.PortOpen = True
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
MSComm2.PortOpen = False
End Sub
Private Sub MSComm2_OnComm()
With MSComm2
If .CommEvent = comEvReceive Then
.InputLen = 0
Text2.SelStart = &H7FFF
Text2.SelText = .Input
End If
End With
End Sub

Sending data to device via MS Comm VB6

I need to send a file to some electronic device and execute it.
I couldn't find any information online regarding MS Comms and I didn't find Documentation on the Microsoft (https://msdn.microsoft.com/en-us/library/aa231237(v=vs.60).aspx) any useful :
' Send Byte array data
MSComm1.Output = Out
Would be great if you guys could give me some pointers and help me to solve my problem. The problem that I am experiencing is an infinite loop at Loop Until MSComm1.OutBufferCount = 0, when I return "MSComm1.OutBufferCount" between Do and Loop "MSComm1.OutBufferCount" is 0 and files dont seem to be sent over to the device.
Closest function I got to at the present moment is below:
Function SendFile(tmp$)
Dim temp$
Dim hsend, bsize, LF&
' Open file
Open tmp$ For Binary Access Read As #2
' Check size on Mscomm1 OutBuffer
bsize = MSComm1.OutBufferSize
' Check file length
LF& = LOF(2)
' This code makes tiny pieces of data (Buffer sized)
' And send's it
Do Until EOF(2)
If LF& - Loc(2) <= bsize Then
bsize = LF& - Loc(2) + 1
End If
' Make room for some data
temp$ = Space$(bsize)
' Put the data piece in the Temp$ string
Get #2, , temp$
MSComm1.Output = temp$
Do
' Wait until the buffer is empty
Loop Until MSComm1.OutBufferCount = 0
Loop
' close file
Close #2
End Function
Have a look at the RThreshold and SThreshold properties
Below is a simple example project :
'1 form with :
' 1 label control : name=Label1
' 1 textbox control : name=Text1
' 1 command button : name=Command1
Option Explicit
Private Sub Command1_Click()
'send command
MSComm1.Output = Text1.Text & vbCr
End Sub
Private Sub Form_Load()
'config mscomm control and open connection
With MSComm1
.Settings = "9600,N,8,1"
.RThreshold = 1
.SThreshold = 0
.CommPort = 1
.PortOpen = True
End With 'MSComm1
End Sub
Private Sub Form_Resize()
'position controls
Dim sngWidth As Single, sngHeight As Single
Dim sngCmdWidth As Single, sngCmdHeight As Single
Dim sngTxtWidth As Single
Dim sngLblHeight As Single
sngWidth = ScaleWidth
sngHeight = ScaleHeight
sngCmdWidth = 1215
sngCmdHeight = 495
sngLblHeight = sngHeight - sngCmdHeight
sngTxtWidth = sngWidth - sngCmdWidth
Label1.Move 0, 0, sngWidth, sngLblHeight
Text1.Move 0, sngLblHeight, sngTxtWidth, sngCmdHeight
Command1.Move sngTxtWidth, sngLblHeight, sngCmdWidth, sngCmdHeight
End Sub
Private Sub MSComm1_OnComm()
'process received data
Dim strInput As String
Select Case MSComm1.CommEvent
Case comEvReceive
strInput = MSComm1.Input
Label1.Caption = Label1.Caption & strInput
End Select
End Sub
In Command1_Click I add a carriage return to the command from Text1 as most devices require the command to be finished by that
In MSComm1_OnComm I just print the received data to the label, but you might want to add the received data to a global variable, and then process the contents of that variable, as all data might not be received at once

I want to send data and receive data as string if possible or at least as integer

I am trying to send and receive data through mscomm port, but am receiving datatype mismatch.so what should I do to avoid this error, I need to send data as integer and receive it as string if possible ,minimum I should get it as integer datatype. The following is my code, please help me to solve this problem.
The send and receive code is working when run independently.
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
If (MSComm1.PortOpen = False) Then
MSComm1.PortOpen = True
End If
Command1.Enabled = False
Command2.Enabled = True
Text3.Text = "COM1, Baud - 9600, Databit - 8, Parity - None, Stopbit - 1....CONNECTED." & Text3.Text
End Sub
Private Sub Command2_Click()
If (MSComm1.PortOpen = True) Then
MSComm1.PortOpen = False
End If
Command1.Enabled = True
Command2.Enabled = False
Text3.Text = "DISCONNECTED" & Text3.Text
End Sub
Private Sub Command3_Click()
Text1.Text = " "
Text2.Text = " "
Text3.Text = "CLEARED" & Text3.Text
End Sub
Private Sub Command4_Click()
MSComm1.Output = Text2.Text
Text3.Text = "SENDING" & Text3.Text
End Sub
Private Sub Command5_Click()
Text3.Text = " "
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.DTREnable = True
MSComm1.Handshaking = comRTS
MSComm1.InBufferSize = 2
MSComm1.RThreshold = MSComm1.InBufferSize
MSComm1.RTSEnable = True
MSComm1.InputLen = 2
MSComm1.InputMode = comInputModeText
MSComm1.NullDiscard = True
MSComm1.OutBufferSize = 2
MSComm1.SThreshold = MSComm1.OutBufferSize
MSComm1.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
If (MSComm1.PortOpen = True) Then
MSComm1.PortOpen = False
End If
End Sub
Private Sub MSComm1_OnComm()
Dim Buffer As String
Select Case MSComm1.CommEvent
Case comEvReceive
'Text1.Text = " "
Buffer = Cstr(MSComm1.Input)
Text1.Text = Buffer
End Select
End Sub
I think the main problem is that you are sending data asynchroniously without any attempt to transmission control. Since there are multiple characters to be transmitted you need to tell the receiver where a new transmission start to let it find the right offset.
In your case you say you only want to send values in the range 0..100. By using a single byte values in the range 0 to 255 can be addressed so sending a single byte per value to send is sufficient in that case and for a single character no transnission control will be necessary.
Here an example where the number is converted to a single byte, send and reconverted to a number:
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub cmdOpen_Click()
If (MSComm1.PortOpen = False) Then
MSComm1.PortOpen = True
End If
cmdOpen.Enabled = False
cmdClose.Enabled = True
txtState.Text = "COM1, Baud - 9600, Databit - 8, Parity - None, Stopbit - 1....CONNECTED." & txtState.Text
End Sub
Private Sub cmdClose_Click()
If (MSComm1.PortOpen = True) Then
MSComm1.PortOpen = False
End If
cmdOpen.Enabled = True
cmdClose.Enabled = False
txtState.Text = "DISCONNECTED" & txtState.Text
End Sub
Private Sub cmdClear_Click()
txtReceived.Text = " "
txtSend.Text = " "
txtState.Text = "CLEARED" & txtState.Text
End Sub
Private Sub cmdSend_Click()
Dim Number As Byte
'some checking - needed depending on source of data
If Not IsNumeric(txtSend.Text) Then
MsgBox "only numbers (from 0 to 255)"
Exit Sub
End If
If Val(txtSend.Text) < 0 Or Val(txtSend.Text) > 255 Then
MsgBox "out of range (from 0 to 255)"
Exit Sub
End If
Number = CByte(Val(txtSend.Text))
MSComm1.Output = Chr(Number)
txtState.Text = "SENDING" & txtState.Text
End Sub
Private Sub cmdClearState_Click()
txtState.Text = " "
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.DTREnable = True
MSComm1.Handshaking = comNone 'comRTS i only got a 3-wire connection - so no handshaking
MSComm1.InBufferSize = 1024 'not 2 we don't want a buffer overflow THAT fast
MSComm1.RThreshold = 1 'Raise OnComm-Event if 1 character is in Rx-buffer
MSComm1.RTSEnable = True
MSComm1.InputLen = 1 'get one character at a time
MSComm1.InputMode = comInputModeText 'the Binary mode never worked out for me - even when transmitting binary data
MSComm1.NullDiscard = False 'also NULL-Characters will be received
MSComm1.OutBufferSize = 512 'again we don't want overflow
MSComm1.SThreshold = 0 'don't hold back data - blow them out directly
MSComm1.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
If (MSComm1.PortOpen = True) Then
MSComm1.PortOpen = False
End If
End Sub
Private Sub MSComm1_OnComm()
Dim Buffer As String
Select Case MSComm1.CommEvent
Case comEvReceive
'txtReceived.Text = " "
Buffer = MSComm1.Input
txtReceived.Text = Asc(Buffer)
txtState.Text = "RX" + txtState.Text
End Select
End Sub

How might I retrieve the port number of a USB device that is connected to a USB port?

How might I retrieve the port number of a USB device that is connected to a USB port, using VB6?
I can already get the USB Device name, let's say:
\\?\USB#Vid_0801&Pid_2250#7&91e2848&0&1#{4d36e978-e325-11ce-bfc1-08002be10318}
How do I automatically detect what port number is assigned to it?
I am making a program that will automatically determine the port number of a specific device attached to any of the USB port and start communicating on it automatically.
to view the available comports you can use :
'1 form with :
' 1 combobox : name=Combo1
' 1 mscomm control : name=MSComm1
Option Explicit
Public Enum PortAttr
PortFree = 0
PortInUse = 1
PortUnknown = 2
End Enum
Private Sub Form_Load()
'show available ports
ShowPorts
End Sub
Private Sub ShowPorts()
Dim intIndex As Integer
Dim intPort As Integer
Dim intFree As Integer
On Error GoTo ErrorFound
With MSComm1
If .PortOpen Then .PortOpen = False
intPort = .CommPort
Combo1.Clear
Combo1.AddItem "--- Not Used ---"
Combo1.ItemData(0) = -2 'not possible
Combo1.AddItem "---- In Use ----"
Combo1.ItemData(1) = -2 'not possible
intFree = 0
For intIndex = 1 To 10
Select Case CheckPort(intIndex)
Case PortFree
intFree = intFree + 1
Combo1.AddItem "Com" & CStr(intIndex), intFree
Combo1.ItemData(intFree) = intIndex
Case PortInUse
Combo1.AddItem "Com" & CStr(intIndex)
End Select
Next intIndex
If .PortOpen Then .PortOpen = False
.CommPort = intPort
If CheckPort(intPort) = PortFree Then
If .PortOpen = False Then .PortOpen = True
End If
End With 'MSComm1
Exit Sub
ErrorFound:
MsgBox Err.Description, vbCritical, "Error " & CStr(Err.Number)
On Error GoTo 0
End Sub
Private Function CheckPort(intPort As Integer) As PortAttr
On Error GoTo ErrorFound
With MSComm1
If .PortOpen Then .PortOpen = False
.CommPort = intPort
.PortOpen = True
CheckPort = PortFree
If .PortOpen = False Then .PortOpen = True
End With 'MSComm1
Exit Function
ErrorFound:
Select Case Err.Number
Case 8002 'port doesnt exist
CheckPort = PortUnknown
Case 8005 'port already in use
CheckPort = PortInUse
End Select
On Error GoTo 0
End Function
in that list you can mark the ports which you already know, so the others should be the port from the usb device
pay attention : when the usb device is connected through another usb port, the converted rs232 port might be a different one as well
This guy has some useful scripts:
http://todbot.com/blog/2012/03/02/listcomports-windows-command-line-tool-for-usb-to-serial/

Resources