Serial Communication in VB6 - 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

Related

"User-defined type not defined" Error

Hi im trying to create a simple program involving an object and a class within
VB 6.0.
The error message i get is: "User-defined type not defined"
The highlighted code that VB suspects is "Dim Bob As Ball"
My defined class is as follows:
Dim Bob As Object
Public Sub Ball()
Dim Circlex As Integer
Dim Circley As Integer
Public Sub makeBall()
Circlex = 3000
Circley = 3000
End Sub
Private Sub moveBall()
Circle (Circlex, Circley), 200
End Sub
End Sub
My code for the only form in my project is:
Private Sub Command1_Click()
Command1.Visible = False
Command1.Enabled = False
vbalProgressBar1.Visible = True
Timer1.Enabled = True
Beep
End Sub
Private Sub Form_Load()
Form1.Width = 6000
Form1.Height = 6000
Dim Bob As Ball
Dim Bob As New Ball
End Sub
Private Sub Form_Unload(Cancel As Integer)
If MsgBox("Are you sure you want to be a quitter?!"
, vbYesNo,"Quit?") = vbYes Then
Unload Me
Set Form1 = Nothing
Else
Cancel = 1
End If
End Sub
Private Sub Timer1_Timer()
Bob = moveBall(Circlex, Circley)
End Sub
Im not sure why the suspected line of code is incorrect, but any help would be appreciated!
VB6 doesn't use code style that you coded.
You have to follow these method to make code work with your intention.
1. make classmodule
2. set its name 'Ball'
3. paste this code on classmodule
Dim Circlex As Integer
Dim Circley As Integer
Public Sub makeBall()
Circlex = 3000
Circley = 3000
End Sub
Private Sub moveBall()
Circle (Circlex, Circley), 200
End Sub
paste code on your form
Private Sub Command1_Click()
Command1.Visible = False
Command1.Enabled = False
vbalProgressBar1.Visible = True
Timer1.Enabled = True
Beep
End Sub
Private Sub Form_Load()
Form1.Width = 6000
Form1.Height = 6000
Dim Bob As New Ball
End Sub
Private Sub Form_Unload(Cancel As Integer)
If MsgBox("Are you sure you want to be a quitter?!"
, vbYesNo,"Quit?") = vbYes Then
Unload Me
Set Form1 = Nothing
Else
Cancel = 1
End If
End Sub
Private Sub Timer1_Timer()
Bob.moveBall(Circlex, Circley)
End Sub
Additionally, VB6 doesn't support style like
Sub A()
Sub B()
End Sub
End Sub

Access a new created line from another sub

I have some problems accessing informations from a new created element.This is my code
Private Sub c1_Click()
Refresh
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled Then
c1.Caption = "Stop"
Else
c1.Caption = "Start"
End If
a = l.X2 - l.X1
choice = a
End Sub
Private Sub Form_Load()
Dim l As Line
Set l = Controls.Add("VB.Line", "l", Me)
With l
.X1 = 2760
.Y1 = 3000
.X2 = 5640
.Y2 = 3000
.Visible = True
.BorderStyle = 1
.BorderWidth = 2
.BorderColor = vbRed
End With
End Sub
I get an error when i click the button:"Object required".On the form i can see the line but i can't access it from another sub.Where is the problem?
Your Line variable l is private to the Form_Load procedure and goes out of scope.
Move Dim l As Line from Form_Load to the top of your form's code module (outside of any method or function). This will make it available to any method or procedure on your form:
Dim l As Line
Private Sub c1_Click()
Refresh
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled Then
c1.Caption = "Stop"
Else
c1.Caption = "Start"
End If
a = l.X2 - l.X1
choice = a
End Sub
Private Sub Form_Load()
Set l = Controls.Add("VB.Line", "l", Me)
With l
.X1 = 2760
.Y1 = 3000
.X2 = 5640
.Y2 = 3000
.Visible = True
.BorderStyle = 1
.BorderWidth = 2
.BorderColor = vbRed
End With
End Sub

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

visual basic 6.0 comm port code error

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

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