Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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):-

VB
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
Comments
CPallini 2-May-13 8:24am    
You don't show us lngTickCount value.
Mike Meinz 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 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 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)



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