MSCOMM- multiple cycles

how to control comevent cycle

Page 1 of 1

5 Replies - 3288 Views - Last Post: 26 August 2008 - 03:52 PM Rate Topic: -----

#1 caruncles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-July 08

MSCOMM- multiple cycles

Post icon  Posted 19 August 2008 - 01:04 PM

I want to control the comevent cycle to display messages on good reads vs bad reads as is appropriate. However, after a bad read, the program wants to cycle thru twice, so I don't know where to put my messages and color changes. It almost acts like it's polling.
Option Explicit
Public prod As String
Public lot As String
Public scan As String
Public conn As ADODB.Connection
Public rs As ADODB.Recordset
Public cmd As ADODB.Command
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Form_Load()

'Set conn = DataEnvironment1.Connection1
'Set rs = DataEnvironment1.rsCommand1

On Error GoTo commerr
With MSComm1
	MSComm1.CommPort = 1
	MSComm1.PortOpen = True
	MSComm1.Handshaking = comRTS
	MSComm1.RThreshold = 13
	MSComm1.RTSEnable = True
	MSComm1.Settings = "9600,E,7,1"
	MSComm1.InputLen = 0
End With
commerr:
	If Err.Number = 8005 Then
		MSComm1.PortOpen = True
		Resume Next
	End If
	
End Sub
Private Sub MSComm1_OnComm()
scan = ""
lot = ""
prod = ""
txtdesc.Text = ""
txtscandata.Text = ""
txtscandata.BackColor = vbWhite
txtdesc.Visible = False
txtdesc.BackColor = vbWhite

	If MSComm1.CommEvent = comEvReceive Then
	  HandleInput
				
	Else
		txtscandata.Text = "No Data"
		txtscandata.BackColor = vbRed
		Beep
		Beep
		'Beep
		Sleep 2000
	End If
'Sleep 3000
'txtscandata.Text = "Ready to Scan"
'txtscandata.BackColor = vbWhite
'scan = ""
'lot = ""
'prod = ""
'txtdesc.Visible = False
'txtdesc.BackColor = vbWhite
'txtdesc.Text = ""

End Sub
Private Sub Form_Unload(Cancel As Integer)
	MSComm1.PortOpen = False
End Sub
Public Sub HandleInput()
Dim desc As String
Dim noread As String
noread = "NOREAD"

txtscandata.Text = MSComm1.Input
scan = Trim(txtscandata.Text)
scan = Trim(scan)
						
	If Len(Trim(scan)) >= 13 And InStr(scan, noread) = 0 Then
		GoTo gooddata
	Else
		txtscandata.Text = "Bad scan- scan it again!"
		txtscandata.BackColor = vbRed
		Beep
		Beep
		Beep
		Sleep 4000
		GoTo Exsub
	End If

gooddata:
prod = Mid(scan, 1, 6)
lot = Mid(scan, 8, 13)

On Error GoTo Errorexit

Set rs = New ADODB.Recordset
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "pValidateWSScan"
cmd.Parameters.Append cmd.CreateParameter("@prod", adChar, adParamInput, 10, Trim(prod))
cmd.Parameters.Append cmd.CreateParameter("@lot", adChar, adParamInput, 20, Trim(lot))
cmd.Parameters.Append cmd.CreateParameter("@desc", adChar, adParamOutput, 20, Trim(desc))
cmd.Execute

desc = cmd(2)
	
If Trim(desc) = "Invalid Product!" Or Trim(desc) = "" Then
	txtdesc.BackColor = vbRed
	txtdesc.Visible = True
	txtdesc.Text = "Invalid Product!"
	Beep
	Beep
	Beep
	Sleep 2000
	Set cmd = Nothing
Else
	txtscandata.BackColor = vbGreen
	txtdesc.BackColor = vbGreen
	txtdesc.Visible = True
	txtdesc.Text = desc
	Set cmd = Nothing
	Sleep 6000
End If

Errorexit:
	If Err.Number = 94 Then
	 Resume Next
	End If
Exsub:
End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: MSCOMM- multiple cycles

#2 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,599
  • Joined: 17-April 07

Re: MSCOMM- multiple cycles

Posted 19 August 2008 - 05:47 PM

is that gve the correct data for you any time
then use the following
do while  (Len(Trim(scan)) >= 13 And InStr(scan, "noread") = 0 )
if Len(Trim(scan)) >= 13 And InStr(scan, "noread") = 0 Then
 txtscandata.Text = "Bad scan- scan it again!"
 txtscandata.BackColor = vbRed
 Beep
 Beep
 Beep
 Sleep 4000
end if
txtscandata.Text = MSComm1.Input
scan = Trim(txtscandata.Text)
scan = Trim(scan)
loop 

GoTo gooddata




I think you may change your code in any other Event
when a comm port is connected it is always active for you so try your code in another event
Was This Post Helpful? 0
  • +
  • -

#3 caruncles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-July 08

Re: MSCOMM- multiple cycles

Posted 20 August 2008 - 04:43 AM

Thanx! I'll give it a try.
Was This Post Helpful? 0
  • +
  • -

#4 caruncles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-July 08

Re: MSCOMM- multiple cycles

Posted 26 August 2008 - 07:14 AM

I"m not too sharp, but what other event are you refering to? I've tried calling another sub from the ONCOMM sub, but it produces the same results. It always comes back to the calling sub. Even when I turn off InBufferCount or POrtOpen, the program cycles thru the Oncomm event twice.



View Postthava, on 19 Aug, 2008 - 05:47 PM, said:

is that gve the correct data for you any time
then use the following
do while  (Len(Trim(scan)) >= 13 And InStr(scan, "noread") = 0 )
if Len(Trim(scan)) >= 13 And InStr(scan, "noread") = 0 Then
 txtscandata.Text = "Bad scan- scan it again!"
 txtscandata.BackColor = vbRed
 Beep
 Beep
 Beep
 Sleep 4000
end if
txtscandata.Text = MSComm1.Input
scan = Trim(txtscandata.Text)
scan = Trim(scan)
loop 

GoTo gooddata




I think you may change your code in any other Event
when a comm port is connected it is always active for you so try your code in another event

Was This Post Helpful? 0
  • +
  • -

#5 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: MSCOMM- multiple cycles

Posted 26 August 2008 - 08:57 AM

I think what thava is getting at is that there are many possible values that MSComm1.CommEvent could be set to besides an error condition. Here is a table of the possible values for CommEvent:

(At the bottom of the page).
http://www.yes-tele.com/mscomm.html

comEventBreak 	1001 	A Break signal was received.
comEventFrame 	1004 	Framing Error. The hardware detected a framing error.
comEventOverrun 	1006 	Port Overrun. A character was not read from the hardware before the next character arrived and was lost.
comEventRxOver 	1008 	Receive Buffer Overflow. There is no room in the receive buffer.
comEventRxParity 	1009 	Parity Error. The hardware detected a parity error.
comEventTxFull 	1010 	Transmit Buffer Full. The transmit buffer was full while trying to queue a character.
comEventDCB 	1011 	Unexpected error retrieving Device Control Block (DCB) for the port.
Communications events include the following settings:
Constant 	Value 	Description
comEvSend 	1 	There are fewer than Sthreshold number of characters in the transmit buffer.
comEvReceive 	2 	Received Rthreshold number of characters. This event is generated continuously until you use the Input property to remove the data from the receive buffer.
comEvCTS 	3 	Change in Clear To Send line.
comEvDSR 	4 	Change in Data Set Ready line. This event is only fired when DSR changes from 1 to 0.
comEvCD 	5 	Change in Carrier Detect line.
comEvRing 	6 	Ring detected. Some UARTs (universal asynchronous receiver-transmitters) may not support this event.
comEvEOF 	7 	End Of File (ASCII character 26) character received.



What events that you'll actually receive will depend on what settings you're using (especially concerning Hardware flow control).

Also, when you sleep in an event, you will back up the processing of all other events. This may or may not be intentional, but I figured that I'd point that out.
Was This Post Helpful? 0
  • +
  • -

#6 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,599
  • Joined: 17-April 07

Re: MSCOMM- multiple cycles

Posted 26 August 2008 - 03:52 PM

Exactly insane,

i think
try this logic in form key down event for a particular key code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1