Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MSComm1 stops "normal comm" and label requires click

Status
Not open for further replies.

norason

Programmer
Jan 28, 2009
139
US
I have two problems:

1. In adding an MSComm1 to this existing code, I have apparently stopped whatever code was controlling the serial port. I have tried to:
Call getCommPort
to get the original communications restarted, but I get:

ambiguous name detected: getCommPort

If I knew how to use the existing comm system without MSComm1, I'd to that, but I think that will be way beyond my current capabilities.

'FLOW DATA
Private Sub Form_Load()
With MSComm1
' First we initialize a few properties.
' Set the COM port number.
.CommPort = 1
' The communication settings. The values are:
' baud rate, parity, data bits, stop bits.
.Settings = "9600,n,8,1"
' Recieve and send thresholds, described below.
.RThreshold = 1
.SThreshold = 1
' Open COM1 and begin communications.
.PortOpen = True
' Send the string "some data" to the output buffer.
.Output = "464c4f57"
' Get the current contents of the input buffer.
If (MSComm1.InBufferCount > 0) Then
flowdatastr = MSComm1.Input
Else
flowdatastr = ""
End If
End With
Close MSComm1
Call getCommPort

End Sub

This is the top of the serialcomm(serialcomm.bas) form:

Option Explicit

Public Sub getCommPort()




My second problem is displaying the data I get from MSComm1 without clicking the label1 box. The correct data comes up, but I don't want to click the box to get it.

Public flowdatastr As String

Private Sub Label1_Click()

End Sub

Thanks,

Gary





 
I seems to me that I should be re-initializing this code somehow to restart the 'normal' comm system.

This is the serialcomm(serialcomm.bas) module:

Option Explicit

Public Sub getCommPort()

' Initialize DOM port
Dim tmpOptP As Integer
Dim tmpOptV As Integer
Dim tmpOptD As Integer ' default startup pump/com port
Dim tmpOptC As String ' Comm port setting for all pumps
Dim tmpOptBaud As Integer

On Error GoTo Error

' Get changeable COMM option
tmpOptP = GetSetting("M450DVPump GUI", "Options", "Com", -1)
tmpOptV = GetSetting("M450DVPump GUI", "Options", "DCom", -1)
tmpOptD = GetSetting("M450DVPump GUI", "Options", "DftPmp", -1)
tmpOptC = GetSetting("M450DVPump GUI", "Options", "DftCom", "")
tmpOptBaud = GetSetting("M450DVPump GUI", "Options", "Baud", -1)

If tmpOptP = -1 Or tmpOptV = -1 Or tmpOptD = -1 Or tmpOptC = "" Or tmpOptBaud = -1 Then
' Registry entry does not exist, ask user
fmSerialConfig.Show vbModal
Else
' Now get the setting and save to registry
PMP_COMM_PORT = GetSetting("M450DVPump GUI", "Options", "Com", -1) ' Pump COMM Port
DV_COMM_PORT = GetSetting("M450DVPump GUI", "Options", "DCom", -1) ' DV COMM Port
DFT_PMP = GetSetting("M450DVPump GUI", "Options", "DftPmp", -1) ' Default Pump Number
DFT_COMM = GetSetting("M450DVPump GUI", "Options", "DftCom", "") ' Default Pump's COMM Port
COMM_BAUD = GetSetting("M450DVPump GUI", "Options", "Baud", -1) ' Baud Rate
End If

Exit Sub

Error:

Call MsgBox(GetResString(COMMPORTERR), vbCritical + vbOK, GetResString(COMMERR))
If GetSetting("M450DVPump GUI", "Options", "Com", -1) <> -1 Then
DeleteSetting "M450DVPump GUI", "Options", "Com"
End If
If GetSetting("M450DVPump GUI", "Options", "DCom", -1) <> -1 Then
DeleteSetting "M450DVPump GUI", "Options", "DCom"
End If
If GetSetting("M450DVPump GUI", "Options", "DftPmp", -1) <> -1 Then
DeleteSetting "M450DVPump GUI", "Options", "DftPmp"
End If
If GetSetting("M450DVPump GUI", "Options", "DftCom", -1) <> -1 Then
DeleteSetting "M450DVPump GUI", "Options", "DftCom"
End If
If GetSetting("M450DVPump GUI", "Options", "Baud", -1) <> -1 Then
DeleteSetting "M450DVPump GUI", "Options", "Baud"
End If

End Sub

Public Function QueryAddress(formObj As Form, guiObj As Object) As String

Dim tmpObj As UserPropertyBag
Dim tmpStr As String

' Set the object
Set tmpObj = formObj.UserPropertyBag1(guiObj.Tag)
guiObj.Font.Charset = CharSetID
' Set global query address for use in the BuildCommand routine
tmpObj.OldValue = 0

' Get the address
tmpStr = DecodeMsg(formObj, guiObj, SerialMsgSend(formObj, BuildCommand(formObj, guiObj), PUMP_COMM))
If IsNumeric(tmpStr) Then
guiObj.Text = tmpStr
tmpObj.OldValue = tmpStr
' StaticCombo(BUS_ADDRESS).ListIndex = CInt(tmpStr)
Else
'guiObj.Text = CommonTextBox(BUS_ADDRESS).Text
' StaticCombo(BUS_ADDRESS).ListIndex = 0 ' In the About Tab
End If

' Remove the object
Set tmpObj = Nothing

QueryAddress = guiObj.Text

End Function

Public Function initCOM(formObj As Form) As Boolean
Dim COMwasOpen As Boolean
Dim tmpBaud As String

On Error GoTo COMMPORT_ERROR

' Assume RS232 Communication is working
StopRS232 = False

' Get COMM Port from Registry
Call getCommPort

' Check if port open okay (valid port) or fail (invalid port)
' Pump Port
formObj.Comm1.CommPort = PMP_COMM_PORT + 1
If formObj.Comm1.PortOpen = False Then
formObj.Comm1.PortOpen = True
End If
' Now close port
formObj.Comm1.PortOpen = False
' Digital Valve Port
formObj.Comm2.CommPort = DV_COMM_PORT + 1
If formObj.Comm2.PortOpen = False Then
formObj.Comm2.PortOpen = True
End If
' Now close port
formObj.Comm2.PortOpen = False

' Baud
Select Case COMM_BAUD
Case 0
tmpBaud = "9600"
Case 1
tmpBaud = "19200"
Case 2
tmpBaud = "38400"
Case 3
tmpBaud = "57600"
Case 4
tmpBaud = "115200"
Case Else
tmpBaud = "9600"
End Select

' Set the COMM Port Settings
With formObj
' Close Port first if openned
If .Comm1.PortOpen = True Then
COMwasOpen = True
.Comm1.PortOpen = False
End If
If .Comm2.PortOpen = True Then
COMwasOpen = True
.Comm2.PortOpen = False
End If

.Comm1.CommPort = PMP_COMM_PORT + 1
'.Comm1.Settings = tmpBaud + "," + COMM_PARITY + "," + COMM_DATA + "," + COMM_STOP
.Comm1.Settings = tmpBaud + ",n,8,1"

.Comm2.CommPort = DV_COMM_PORT + 1
'.Comm2.Settings = tmpBaud + "," + COMM_PARITY + "," + COMM_DATA + "," + COMM_STOP
.Comm2.Settings = tmpBaud + ",n,8,1"
End With

initCOM = True

Exit Function

COMMPORT_ERROR:

Call MsgBox(GetResString(COMMPORTERR), vbCritical + vbOK, GetResString(COMMERR))
If GetSetting("M450DVPump GUI", "Options", "Com", -1) <> -1 Then
DeleteSetting "M450DVPump GUI", "Options", "Com"
End If
If GetSetting("M450DVPump GUI", "Options", "DCom", -1) <> -1 Then
DeleteSetting "M450DVPump GUI", "Options", "DCom"
End If
If GetSetting("M450DVPump GUI", "Options", "DftPmp", -1) <> -1 Then
DeleteSetting "M450DVPump GUI", "Options", "DftPmp"
End If
If GetSetting("M450DVPump GUI", "Options", "DftCom", -1) <> -1 Then
DeleteSetting "M450DVPump GUI", "Options", "DftCom"
End If
If GetSetting("M450DVPump GUI", "Options", "Baud", -1) <> -1 Then
DeleteSetting "M450DVPump GUI", "Options", "Baud"
End If

initCOM = False

End Function

Public Sub ReleaseCOM(commObj As MSComm)
On Error Resume Next
'With fmMain
' Close COM port if open
If commObj.PortOpen = True Then
commObj.PortOpen = False
End If
'End With
End Sub

Thanks,

Gary

 
Well, part of the problem is that you are checking the port for incoming data immediately after sending data out. Your code executes much faster than the data can be received and answered, so you get nothing.

Instead of manually checking the port for data in the buffer in your form load event, you should use the MSComm1.DataReceived event to see when data is available. Make sure you are getting all of the data ( based on what is being sent back ) and then displaying it in your label.
 
Thanks for the help!

I got it running - both problems are fixed. Here's the code:

Private Sub Form_Load()
With MSComm1
'.CommPort = 1
'.Settings = "9600,n,8,1"
.RThreshold = 1
.SThreshold = 1
.PortOpen = True
.InputLen = 0
.Output = "464c4f57"
Do
DoEvents
flowdatastr = flowdatastr & MSComm1.Input
Loop Until InStr(flowdatastr, "" & vbCr)
.PortOpen = False
End With
If flowdatastr = "312e323334" + vbCr Then
'Label1.Caption = "312e323334"
MsgBox flowdatastr
Else
Label1.Caption = "No Joy"
End If
End Sub

I fixed the second problem by putting:

Label1.Caption = flowdatastr

in the Form Load.

Life is good!

Thanks,

Gary

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top