mscomm vb6 --> vs2005

I have a very simple vb6 program I want to convert to VS2005. MSComm is nolonger available and I don't understand how to receive data without the mscomm_OnComm() event.

Please advise.

Also, can this be implemented so that the code is exposed to MSAccess 2000 ? Ultimately I need to communicate to a piece of Lab equipment connected to the serial port using a production application written in MSAccess.

Thanks

*VB6 Code*

Dim Buffer As String
Private Sub MSComm1_OnComm()
If Me.MSComm1.CommEvent = 2 Then
Buffer$ = Me.MSComm1.Input
Me.Text1.Text = Me.Text1.Text & Buffer$
End If
End Sub

Private Sub cmdSend_Click()
On Error GoTo errorHandler
If Me.MSComm1.PortOpen = True Then
Me.MSComm1.PortOpen = False
End If
Select Case Trim(Me.Combo1.Text)
Case "Com1", "Com2", "Com3", "Com4"
Me.MSComm1.CommPort = Right(Trim(Me.Combo1.Text), 1)
Case Else
Me.MSComm1.CommPort = Trim(Me.Combo1.Text)
End Select

Me.MSComm1.Settings = Me.Combo3 + "," + Left(Trim(Me.Combo5.Text), 1) + "," + Trim(Me.Combo4.Text) + "," + Trim(Me.Combo6.Text)
Select Case Me.Combo7
Case "None"
Me.MSComm1.Handshaking = comNone
Case "Hardware"
Me.MSComm1.Handshaking = comRTS
Case Else
Me.MSComm1.Handshaking = comXOnXoff
End Select
Me.MSComm1.RThreshold = 1 ' Activates the OnComm event
Me.MSComm1.PortOpen = True
Me.MSComm1.Output = Chr(13)
Me.MSComm1.Output = Trim(Me.Combo2.Text) + Chr(13)
exitNormal:
On Error Resume Next
Exit Sub
errorHandler:
Buffer$ = CStr(Err.Number) + " " + Err.Description
If Me.MSComm1.PortOpen = True Then
Me.MSComm1.PortOpen = False
End If
Resume exitNormal
End Sub

Private Sub cmdReset_Click()
Text1.Text = ""
If Me.MSComm1.PortOpen = True Then
Me.MSComm1.PortOpen = False
End If
End Sub

Private Sub cmdEnd_Click()
End
End Sub

Private Sub Form_Load()
Combo1.AddItem "Com1"
Combo1.AddItem "Com2"
Combo2.AddItem "Calibrate"
Combo2.AddItem "Read"
Combo2.AddItem "Screen"
Combo2.AddItem "Version"
Combo2.AddItem "Ping"
Combo2.AddItem "Time"
Combo2.AddItem "Date"
Combo2.AddItem "Help"
Combo3.AddItem "115200"
Combo3.AddItem "38000"
Combo3.AddItem "19200"
Combo3.AddItem "9600"
Combo4.AddItem "8"
Combo4.AddItem "7"
Combo4.AddItem "67"
Combo4.AddItem "5"
Combo5.AddItem "Even"
Combo5.AddItem "Odd"
Combo5.AddItem "None"
Combo5.AddItem "Mark"
Combo5.AddItem "Space"
Combo6.AddItem "1"
Combo6.AddItem "1.5"
Combo6.AddItem "2"
Combo7.AddItem "Hardware"
Combo7.AddItem "Xon / Xoff"
Combo7.AddItem "None"
End Sub

[3027 byte] By [tegrovesjr] at [2007-12-17]
# 1
The new SerialPort class is a replacement for the MSComm control. Here's some code that can replace most of what you had

Dim WithEvents serial As New System.IO.Ports.SerialPort
Dim buffer As String
Private Sub serial_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serial.DataReceived
If e.EventType <> IO.Ports.SerialData.Chars Then Exit Sub
buffer = serial.ReadExisting
Me.Text1.Text &= buffer
End Sub
Private Sub cmdSend_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdSend.Click
Try
If Not serial.IsOpen Then serial.Open()
'it seems the combo box will be ComX
serial.PortName = Me.Combo1.Text.Trim
serial.BaudRate = Integer.Parse(Me.Combo3.Text)
'this assumes that the combo box has the full names of each parity
'eg: Even, Mark, None
serial.Parity = [Enum].Parse(GetType(IO.Ports.Parity), Me.Combo5.Text)
'you will need to determine that value from the combo box
serial.StopBits = IO.Ports.StopBits.One
serial.DataBits = Integer.Parse(Me.Combo4.Text)
Select Case Me.Combo7.SelectedItem.Text
Case "None"
serial.Handshake = IO.Ports.Handshake.None
Case "Hardware"
serial.Handshake = IO.Ports.Handshake.RequestToSend
Case Else
serial.Handshake = IO.Ports.Handshake.XOnXOff
End Select
serial.Open()
serial.NewLine = Convert.ToChar(13)
serial.WriteLine("")
serial.WriteLine(Me.Combo2.Text.Trim)
Catch ex As Exception
buffer = ex.Message
If serial.IsOpen Then serial.Close()
End Try
End Sub
Private Sub cmdReset_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdReset.Click
Text1.Text = ""
If serial.IsOpen Then serial.Close()
End Sub
Private Sub cmdEnd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdEnd.Click
Me.Close()
End Sub
Private Sub Form_Load()
'you can add the items at design time to the combo boxes
'for easier use, you may consider naming the combo boxes more
'descriptively
End Sub

hgen_banks at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
How do you handle the 'CROSS THREAD EXCEPTION' in the above code?
RARiedel at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

Hello

I am interested in the follow up to this question:

"Also, can this be implemented so that the code is exposed to MSAccess 2000 ? Ultimately I need to communicate to a piece of Lab equipment connected to the serial port using a production application written in MSAccess."

I am currently working on an MSAccess 2000 database that communicates to a piece of Lab equipment using MSComm. I am currently using a timer on a form to poll the input buffer before reading and processing the output from the meter. I was able to set up a VB6 routine to use the OnComm event to collect data from this meter, but I cannot get the OnComm event to fire in MSAccess 2000. Could you offer any help with this? Thank you for your help!!

VB6 code that works:

Private Sub cmdTrigger_Click()
MSComm1.Output = "MEAS?" & vbCr
End Sub

Private Sub Form_Load()
Form1.Caption = "App1"
'set up port
With MSComm1
.CommPort = 1
.Handshaking = 0
.InputLen = 0
.RThreshold = 16
.RTSEnable = True
.Settings = "9600,n,8,1"
.SThreshold = 1
.PortOpen = True
' Leave all other settings as default values.
'set up some meter settings
.Output = "REMS;OHMS;RANGE 4;TRIGGER 1" & vbCr
End With
Text1.Text = ""
End Sub

Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub

Private Sub MSComm1_OnComm()
'MsgBox "EVENTS? " & MSComm1.CommEvent
Select Case MSComm1.CommEvent
Case comEvReceive ' Received RThreshold # of chars.
Text1.Text = MSComm1.Input
'MsgBox "got it"
End Select

End Sub

VBA Code that does not work:

This connects to the meter sets it up but the trigger command will trigger the meter causing it to send back a reading, but the OnComm events are never called.

Option Compare Database
Public MSComm1 As MSComm

Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive ' Received RThreshold # of chars.
Me!txtSystemStatus.Value = MSComm1.Input
MsgBox "got it"
End Select
End Sub

Private Sub ActiveXCtl7_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive ' Received RThreshold # of chars.
Me!txtSystemStatus.Value = MSComm1.Input
MsgBox "got it"
End Select
End Sub

Private Sub Form_Open(Cancel As Integer)
Me!txtSystemStatus.Value = "Form Open"
Set MSComm1 = New MSComm

MSComm1.CommPort = 1
MSComm1.Handshaking = 0
MSComm1.InputLen = 0
MSComm1.RThreshold = 16
MSComm1.RTSEnable = True
MSComm1.Settings = "9600,n,8,1"
MSComm1.SThreshold = 1
MSComm1.PortOpen = True
MSComm1.Output = "REMS;OHMS;RANGE 4;TRIGGER 1" & vbCr

Me!txtSystemStatus.Value = "Meter Ready"
End Sub

Sub cmdTrigger_Click()
'sent the command to meter to take reading
MSComm1.Output = "MEAS?" & vbCr
End Sub

Jstrick45 at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
I would like to know the solution for Access(VBA) serial connection. Did you receive one?
jvallier at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic General...