Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Hardware VB.NET
Hi all,
 
I've develop a code which send and read through comport..i develop the utilities using windows form. It all perfect running in the code. But why in the world when I apply the code in my actual program which using WPF is not able to complete the reading ACK from hardware? I'm able to write ENQ to comport but it got timeout. I really2 need advise on this. Below are my code (sample from original):-
 
Private Const strDLE As String = "16"
Private Const strSTX As String = "2"
Private Const strETX As String = "3"
Private Const strENQ As String = "5"
Private Const strACK As String = "6"
Private Const strNAK As String = "21"
 
Private Const lngACK1TimeOut As Long = 5L '10L
Private Const lngACK2TimeOut As Long = 5L '10L
Private Const lngENQTimeOut As Long = 20L '10L
Private Const lngETXTimeOut As Long = 5L '10L

Public Function Initialize() As Boolean
Dim strMsg As String
Dim bCont As Boolean = True
Dim strResp As String
Dim strRespAscii As String
Dim strCmd As String
Dim Cmd() As Byte
Dim lngCurrent As Long
Dim lngNow As Long
Dim strAsciiTmp As String = String.Empty
Dim strAsciiTmp2 As String = String.Empty
Dim strFullAsciiTmp As String = String.Empty
Dim strTmp As String = String.Empty
Dim strTmpHex As String = String.Empty
Dim strDataCondition As String = String.Empty
Dim strTmpCnt As String = String.Empty
Dim RespData2() As Byte
Dim Idx As Integer = 0
 
Try
  objComPort.DiscardOutBuffer()
  objComPort.DiscardInBuffer()
 
  strResp = String.Empty
  strRespAscii = String.Empty
 
  objComPort.Write(cmdENQ, 0, cmdENQ.Length)
  bCont = True
  lngCurrent = DateTime.Now.Ticks
     Do While (objComPort.BytesToRead <= 0)
       lngNow = DateTime.Now.Ticks
       If (lngNow > (lngCurrent + (lngACK1TimeOut * lngTickCount))) Then
          bCont = False
          Exit Do
       End If
     Loop
     'Mark 1
     If (Not bCont) Then
       strMsg = "Error - Waiting ACK Timeout"
       Return False
     End If
 
     If (objComPort.BytesToRead > 0) And (bCont = True) Then
       Do While (objComPort.BytesToRead > 0)
          If (objComPort.ReadByte.ToString = strDLE) Then
             If (objComPort.ReadByte.ToString = strACK) Then
                    strMsg = "Recv: (ACK) 10 06"
                Exit Do
             End If
          End If
       Loop
     End If
Catch ex As Exception
 
End Try
End Function
 
See the comment "Mark 1". The module unable to operate. But with the same code in Windows Form I run instantly, it never have a problem. The process through completely. This problem has burden my head for a days. I don't know why when it loop in "objComPort.BytesToRead" it return false. How in the world this happen in the same code but different environment (VB.Net Windows Form & VB.Net WPF) whether it affect the DateTime.Now.Ticks or whatsoever..
 
Help me on this
Posted 2-May-13 3:15am
Comments
CPallini at 2-May-13 8:24am
   
You don't show us lngTickCount value.
Mike Meinz at 2-May-13 8:24am
   
It looks like many people have had problems with serial communications and WPF. See MSDN Search for serial port wpf. Maybe there is something in one of these search results that could help you.
Luiey Ichigo at 2-May-13 21:48pm
   
it start since objComport.Write where at ReadBytes is 0 while the windows form is showing 2 when i breakpoint in it.why?
Luiey Ichigo at 11-May-13 13:28pm
   
Sorry guys...my bad,it because the Parity that should be even but I read 0 from INI files..

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



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 2 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100