Click here to Skip to main content
15,303,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
run time error 8018:
operation valid only when the port is open.

when debuggin it says Mscomm1.output = 6
im a newbie in vb..
my code...
Dim hwdc As Long
Dim startcap As Boolean

Private Sub Command10_Click()
Dim sdata2 As Long ' Holds our incoming data
MSComm1.Input = sdata2
Label1.DataField = CLng(sdata2)
End Sub

Private Sub MSComm1_OnComm()
Dim sData As String ' Holds our incoming data
Dim lHighByte As Long   ' Holds HighByte value
Dim lLowByte As Long    ' Holds LowByte value
Dim lByte As Long       ' Holds the Byte result

' If comEvReceive Event then get data and display
If MSComm1.CommEvent = comEvReceive Then
   sData = MSComm1.Input ' Get data
    lHighByte = Asc(Mid$(sData, 1, 1)) ' get 1st byte
     lLowByte = Asc(Mid$(sData, 2, 1))  ' Get 2nd byte
   lByte = JoinHighLow(lHighByte, lLowByte)
    Label1.Caption = CStr(lByte)
    DrawScale lByte
End If

' Fire Rx Event Every Two Bytes
MSComm1.RThreshold = 2

' When Inputting Data, Input 2 Bytes at a time
MSComm1.InputLen = 2

' 2400 Baud, No Parity, 8 Data Bits, 1 Stop Bit
MSComm1.Settings = "2400,N,8,1"

' Make sure DTR line is low to prevent Stamp reset
MSComm1.DTREnable = False

'  Use COM9
MSComm1.CommPort = 1

' Open the port
MSComm1.PortOpen = True

End Sub
Private Function JoinHighLow(lHigh As Long, lLow As Long) As Long
JoinHighLow = (lHigh * &H100) Or lLow
End Function
Private Sub DrawScale(lVal As Long)
Picture1.Line (0, 0)-(lVal, Picture1.ScaleHeight), vb3DShadow, BF
End Sub
Private Sub Command11_click()
Dim PinNumber As Variant
PinNumber = Text2.DataField
MSComm1.Output = Chr$(PinNumber) 'Chr$(255) &
End Sub
Private Sub Command2_click()
MSComm1.Output = Chr$(3)
End Sub
Private Sub Command3_Click()
MSComm1.Output = 4
End Sub
Private Sub Command1_click()
MSComm1.Output = 1
End Sub
Private Sub Command4_Click()
MSComm1.Output = 2
End Sub
Private Sub Command5_Click()
MSComm1.Output = 9
End Sub
Private Sub Command6_Click()
MSComm1.Output = 6
End Sub
Private Sub Command7_Click()
MSComm1.Output = 7
End Sub
Private Sub Command8_Click()
MSComm1.Output = 8
End Sub
Private Sub Command9_Click()
MSComm1.Output = 5
End Sub
Private Sub cmdCapture_Click()
Dim temp As Long
  hwdc = capCreateCaptureWindow("NeV0 Vision System", ws_child Or ws_visible, 0, 0, 640, 480, Picture1.hWnd, 0)
  If (hwdc <> 0) Then
    temp = SendMessage(hwdc, wm_cap_driver_connect, 0, 0)
    temp = SendMessage(hwdc, wm_cap_set_preview, 1, 0)
    temp = SendMessage(hwdc, WM_CAP_SET_PREVIEWRATE, 30, 0)
    startcap = True
    MsgBox ("No Webcam found")
  End If
End Sub
Private Sub cmdClose_Click()
Dim temp As Long
If startcap = True Then
temp = SendMessage(hwdc, WM_CAP_DRIVER_DISCONNECT, 0&, 0&)
startcap = False
End If
End Sub
Private Sub cmdVideoFormat_Click()
 Dim temp As Long
 If startcap = True Then
  temp = SendMessage(hwdc, WM_CAP_DLG_VIDEOFORMAT, 0&, 0&)
End If
End Sub
Updated 13-Mar-12 17:19pm
ZurdoDev 13-Mar-12 15:45pm
That is a lot of code to look at but from the error it sounds like you are trying to do something with the port, like read, while it is closed. The error should tell you what line and you should be able to narrow it down quickly.
nevin pillai 13-Mar-12 15:55pm
Private Sub Command6_Click()
MSComm1.Output = 6
End Sub

the mscomm1.output is the problem

1 solution

hat's usually caused by something else using the port, so your program threw an error when it tried to open the port. Though error arises at:

Private Sub Command6_Click() 
  MSComm1.Output = 6 
End Sub

Root cause for this problem would be the external interaction using COM component or SendMessage method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900