E
EAB1977
Hi guys,
The code below works about 90% of the time, but for the other 10% it
does not. What the below code does is it pulls data from a gram scale
and inserts it in the active cell. I am not very knowledgeable about
the com control, so any advice will be greatly appreciated.
Sub GetInput()
ActiveCell.Value = GetWeight()
End Sub
Public Function GetWeight() As Double
Dim sInput As String, PauseTime, Start, Finish
On Error GoTo ErrorHandler
If frmComm.comExcel.InBufferCount > 0 Then
frmComm.comExcel.InBufferCount = 0
End If
ActiveCell.Value = "Waiting for Stable..."
frmComm.comExcel.CommPort = 1
frmComm.comExcel.Settings = "9600,N,7,2"
frmComm.comExcel.PortOpen = True
frmComm.comExcel.Output = "1S" & Chr$(13) 'Print on stable
frmComm.comExcel.Output = "P" & Chr$(13) 'Display data
Do
DoEvents
Loop Until frmComm.comExcel.InBufferCount >= 18
sInput = frmComm.comExcel.Input
Dim x As Integer
Dim intLength As Integer
Dim strTemp As String
Dim strTemp1 As String
intLength = Len(sInput)
For x = 1 To intLength
strTemp = Mid(sInput, x, 1)
Select Case strTemp
Case 0 To 9
strTemp1 = strTemp1 + strTemp
Case "."
strTemp1 = strTemp1 + strTemp
End Select
Next x
GetWeight = strTemp1
frmComm.comExcel.PortOpen = False
'GetWeight = Val(Mid$(sInput, 1))
Exit Function
ErrorHandler:
If Err.Number = 8005 Then
Exit Function
Else
MsgBox Err.Description
End If
End Function
The code below works about 90% of the time, but for the other 10% it
does not. What the below code does is it pulls data from a gram scale
and inserts it in the active cell. I am not very knowledgeable about
the com control, so any advice will be greatly appreciated.
Sub GetInput()
ActiveCell.Value = GetWeight()
End Sub
Public Function GetWeight() As Double
Dim sInput As String, PauseTime, Start, Finish
On Error GoTo ErrorHandler
If frmComm.comExcel.InBufferCount > 0 Then
frmComm.comExcel.InBufferCount = 0
End If
ActiveCell.Value = "Waiting for Stable..."
frmComm.comExcel.CommPort = 1
frmComm.comExcel.Settings = "9600,N,7,2"
frmComm.comExcel.PortOpen = True
frmComm.comExcel.Output = "1S" & Chr$(13) 'Print on stable
frmComm.comExcel.Output = "P" & Chr$(13) 'Display data
Do
DoEvents
Loop Until frmComm.comExcel.InBufferCount >= 18
sInput = frmComm.comExcel.Input
Dim x As Integer
Dim intLength As Integer
Dim strTemp As String
Dim strTemp1 As String
intLength = Len(sInput)
For x = 1 To intLength
strTemp = Mid(sInput, x, 1)
Select Case strTemp
Case 0 To 9
strTemp1 = strTemp1 + strTemp
Case "."
strTemp1 = strTemp1 + strTemp
End Select
Next x
GetWeight = strTemp1
frmComm.comExcel.PortOpen = False
'GetWeight = Val(Mid$(sInput, 1))
Exit Function
ErrorHandler:
If Err.Number = 8005 Then
Exit Function
Else
MsgBox Err.Description
End If
End Function