atwist
asked on
MSComm Port Already Open
I am getting run-time error '8005' which is Port Already Open. The mscomm1 port is not open when it reads the first if statement so it is entering it and then when it tries to open it, the program errors out saying its already open. Whats the deal?
My code is as follows:
If MSComm1.PortOpen = False Then
MSComm1.CommPort = 1
Baud = "9600"
Parity = "N"
Data = "8"
Stopbit = "1"
MSComm1.Settings = Baud & "," & Parity & "," & Data & "," & Stopbit
MSComm1.PortOpen = True
End If
My code is as follows:
If MSComm1.PortOpen = False Then
MSComm1.CommPort = 1
Baud = "9600"
Parity = "N"
Data = "8"
Stopbit = "1"
MSComm1.Settings = Baud & "," & Parity & "," & Data & "," & Stopbit
MSComm1.PortOpen = True
End If
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I agree with Eniac- I use hyperterminal alot when I am building serial apps and sometimes forget to disconnect the hyperterminal connection when I run my app.
I usually declare another instance of the MScomm control in a .bas mod
Public Serial As MSComm ''''Serial port driver - MSComm.OCX SP2
Then in form_load set this instance to the form's instance
Set Serial = MSComm1
I add something like this to the BAS module
Public Function SerialOpen(Optional statusOnly = False) As Boolean
On Error GoTo SerialOpenError
Dim i As Integer, inbuff As String
If statusOnly Then SerialOpen = Serial.PortOpen: Exit Function
Serial.PortOpen = True
'inbuff = Serial.Input ''I flush the port's input buffer whenever I open it
SerialOpen = True
'MainForm.tmWD.Enabled = True ''I use a watchdog timer that I create elsewhere in code
SerialOpenError_Exit:
SerialOpen = True: Exit Function
SerialOpenError:
If Serial.PortOpen then Resume Next
SerialOpen = False
End Function
I usually declare another instance of the MScomm control in a .bas mod
Public Serial As MSComm ''''Serial port driver - MSComm.OCX SP2
Then in form_load set this instance to the form's instance
Set Serial = MSComm1
I add something like this to the BAS module
Public Function SerialOpen(Optional statusOnly = False) As Boolean
On Error GoTo SerialOpenError
Dim i As Integer, inbuff As String
If statusOnly Then SerialOpen = Serial.PortOpen: Exit Function
Serial.PortOpen = True
'inbuff = Serial.Input ''I flush the port's input buffer whenever I open it
SerialOpen = True
'MainForm.tmWD.Enabled = True ''I use a watchdog timer that I create elsewhere in code
SerialOpenError_Exit:
SerialOpen = True: Exit Function
SerialOpenError:
If Serial.PortOpen then Resume Next
SerialOpen = False
End Function
ASKER
True it could be another program being opened but I was getting the error since I was checking the port before I told it what port to check.
Since the port is not opened by the MsComm1 the MsComm1.PortOpen will return false, but when it will try to open the port 1, it will see that it has been opened by some other control or application (for example modem could be opened by windows dial up)
You must use error handling routines to cacth that error and give a reasonable message