Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact
A1VBCode Forums
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      

Home » Classic Visual Basic (VB 6 or earlier) » General Visual Basic » Monitoring MSComm1.CommEvent


Monitoring MSComm1.CommEventExpand / Collapse
Author
Message
Posted 3/20/2010 12:47:46 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: 3/29/2010 6:53:03 AM
Posts: 1, Visits: 6
Hello,
I'm using MSComm.CommEvent to monitor event of my COM port.
First I open port1 and get value 105
then I short circuit pin 7 (RTS) and 8 (CTS) and then It returns event value "3"
But when I release (disconnect) pin 7 and 8, it still remains value "3"
Why I does not return back to the original value "105"
Does anyone know the code,
help me pls

************
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.RTSEnable = True
MSComm1.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
MSComm1.RTSEnable = False
End Sub
Private Sub MSComm1_OnComm()
Debug.Print MSComm1.CommEvent
Text1.Text = MSComm1.CommEvent
End Sub
************

duchoang
Post #29010
Posted 3/21/2010 1:09:02 AM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: 10/18/2012 11:40:00 AM
Posts: 548, Visits: 2,556
This code toggles the RTS line which is jumped to the CTS line. It's garanteed to work.

By the way comEvCTS only returns a change in the CTS line. It can't tell you if it's high or low, which is why I didn't use it in the top example.

Option Explicit

Private Sub Form_Load()
   MSComm1.PortOpen = True
End Sub

Private Sub Command1_Click()
   MSComm1.RTSEnable = Not MSComm1.RTSEnable = True
End Sub

Private Sub MSComm1_OnComm()
   If MSComm1.CTSHolding = True Then
      Me.Caption = "CTS Line Is High"
   Else
      Me.Caption = "CTS Line Is Low"
   End If
End Sub

If you want to use comEvCTS you can write it this way but it's not as reliable or clean as my first example.

Private Sub Form_Load()
   MSComm1.PortOpen = True
End Sub

Private Sub Command1_Click()
   MSComm1.RTSEnable = Not MSComm1.RTSEnable = True
End Sub

Private Sub MSComm1_OnComm()
   Static intEvCtr As Integer
   If MSComm1.CommEvent = comEvCTS Then intEvCtr = intEvCtr + 1
   If MSComm1.CommEvent = comEvCTS And intEvCtr = 1 Then
      Me.Caption = "CTS Line Went High"
   End If
   If MSComm1.CommEvent = comEvCTS And intEvCtr = 2 Then
      Me.Caption = "CTS Line Went Low"
      intEvCtr = 0
   End If
End Sub


________________________________________________________________ 

"So much to learn. So little time to do it. Wise men know it's later than one thinks"!

Mark's Syntax.Zip    Pause Sub

I don't answer programming questions via PMs. That's what the forum is for! 

Post #29020
Posted 3/21/2010 1:13:56 AM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: 10/18/2012 11:40:00 AM
Posts: 548, Visits: 2,556
After giving this more thought I think this is the best way to go because it tests for both comEvCTS as well as the state of CTSHolding.

Private Sub Form_Load()
   MSComm1.PortOpen = True
End Sub

Option Explicit

Private Sub Form_Load()
   MSComm1.PortOpen = True
End Sub

Private Sub Command1_Click()
   MSComm1.RTSEnable = Not MSComm1.RTSEnable = True
End Sub

Private Sub MSComm1_OnComm()
   If MSComm1.CommEvent = comEvCTS And MSComm1.CTSHolding = True Then
      Me.Caption = "CTS Line is High"
   End If
   If MSComm1.CommEvent = comEvCTS And MSComm1.CTSHolding = False Then
      Me.Caption = "CTS Line is Low"
   End If
End Sub




________________________________________________________________ 

"So much to learn. So little time to do it. Wise men know it's later than one thinks"!

Mark's Syntax.Zip    Pause Sub

I don't answer programming questions via PMs. That's what the forum is for! 

Post #29021
« Prev Topic | Next Topic »


Reading This TopicExpand / Collapse
Active Users: 1 (1 guest, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: Admin, Mod, Mark, Keithuk

PermissionsExpand / Collapse

All times are GMT -5:00, Time now is 7:15am