Serial Port Class in with ScanPal2

I am really got crazy and I am putting third time question over here but I am not getting proper answer. Please all experts kindly help me. I am using Scanpal2 to read data and as per its documentation as follows

*****  Protocol for receiving transaction data from ScanPal2 terminals  *****

Note : All commands or records sent to or received from the ScanPal2
       terminals should be ended with a return character (0x0d).

1. Send the "READ" command to the ScanPal2 terminal and wait for return.

2. If the terminal returns "ACK", then it is ready to send data to PC.

3. The format of each record received from the terminal is as follows,

   A. The first byte is a sequence count rotated from 0 to 9. It's purpose
      is to ensure the correct order of data transmission.
   B. The last two bytes are the checksum values. The checksum is calculated
      by adding up the sequence count and all the data bytes.
   C. Devide the sum calculated above by 256 will get the last byte checksum
      value. If this value happens to be 13 (the return character: 0x0d),
      change it to 14 (0x0e).
   D. The remaining of the above calculation is the first byte checksum
      value. If it happens to be 13, change it to 14.
   E. Please note that the checksum byte-order for this protocol is different
      to that of downloading lookup files.

4. If the received data is correct, the PC program should return "ACK" to the
   ScanPal2 terminal so that it can send the next record.

5. If the received data is not correct, the PC program should return "NAK" to
   the ScanPal2 terminal so that it can resend the record.

6. If the received data is duplicated (i.e. the sequence count is same as
   previous record), then the PC program should discard this record but still
   return "ACK" to the ScanPal2 terminal so that it can send the next record.

7. Repeat the above procedures until receiving the "OVER" command from the
   ScanPal2 terminal.

But when I write code and send READ to the serial port as per documentation it should send me ACK but it is sending ACK+chr(13)+ FirstRecord and then I can read data one by one untill end of the records but I could not detect OVER some time it is showing OVEROVERor OVER + chr(13) + OVER + chr(13) or some time it is coming with last record.

I don't know to to rectify it.  I feel here in my codes some thread problem is there. Also I wanted to put in text box after reading all it is giving thread error.

I thought there is problem in my Scan pal2 PDT. So i search in internet and I got source code in Delphi,there application is made as per the documentation and it is working perfectly without any problem.   So it means it is problem with my codes only

I am also putting my source code over here kindly help me to remove my error and also let me know why it is happening like that.

Really I am tried of doing all the way. Please help
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class frmScanPalReading
  Inherits System.Windows.Forms.Form

  'Form overrides dispose to clean up the component list.
  <System.Diagnostics.DebuggerNonUserCode()> _
  Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing AndAlso components IsNot Nothing Then
    End If
  End Sub

  'Required by the Windows Form Designer
  Private components As System.ComponentModel.IContainer

  'NOTE: The following procedure is required by the Windows Form Designer
  'It can be modified using the Windows Form Designer.  
  'Do not modify it using the code editor.
  <System.Diagnostics.DebuggerStepThrough()> _
  Private Sub InitializeComponent()
    Me.components = New System.ComponentModel.Container
    Me.txData = New System.Windows.Forms.RichTextBox
    Me.btnExtract = New System.Windows.Forms.Button
    Me.SerialPort = New System.IO.Ports.SerialPort(Me.components)
    Me.txData.Location = New System.Drawing.Point(12, 29)
    Me.txData.Name = "txData"
    Me.txData.Size = New System.Drawing.Size(684, 345)
    Me.txData.TabIndex = 0
    Me.txData.Text = ""
    Me.btnExtract.Location = New System.Drawing.Point(287, 393)
    Me.btnExtract.Name = "btnExtract"
    Me.btnExtract.Size = New System.Drawing.Size(75, 23)
    Me.btnExtract.TabIndex = 1
    Me.btnExtract.Text = "Extract"
    Me.btnExtract.UseVisualStyleBackColor = True
    Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.ClientSize = New System.Drawing.Size(708, 446)
    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
    Me.MaximizeBox = False
    Me.MinimizeBox = False
    Me.Name = "frmScanPalReading"
    Me.Text = "Scan Pal Data Reading"
  End Sub
  Friend WithEvents txData As System.Windows.Forms.RichTextBox
  Friend WithEvents btnExtract As System.Windows.Forms.Button
  Friend WithEvents SerialPort As System.IO.Ports.SerialPort

End Class

Open in new window

Imports System.Threading
Imports System.IO.Ports
Imports System.Text

Public Class frmScanPalReading

  Dim ReadBuffer As String = ""

  Private Sub frmScanPalReading_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
  End Sub

  Private Sub frmScanPalReading_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      With SerialPort
        .PortName = "COM1"
        .Handshake = IO.Ports.Handshake.RequestToSend
        .ReceivedBytesThreshold = 1
        .RtsEnable = True
        .BaudRate = 115200
        .DataBits = 8
        .Parity = IO.Ports.Parity.None
        .StopBits = IO.Ports.StopBits.One

      End With
    Catch ex As Exception
    End Try
  End Sub

  Private Sub btnExtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExtract.Click
  End Sub

  Sub SendCommand(ByVal cmd As String)
    SerialPort.Write(cmd + Chr(13))
  End Sub

  Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
    'This event will Receive the data from the selected COM port..
  End Sub

  Private Sub ReceiveData()
      Dim cmd As String = "", _buffer = ""
      Dim crPOS As Integer

      _buffer = ByteToStr()

      crPOS = InStr(_buffer, Chr(13))
      If crPOS > 1 Then cmd = Microsoft.VisualBasic.Left(_buffer, crPOS - 1)

      If cmd = "ACK" Then
        Do While _buffer <> "OVER" '+ Chr(13)
          _buffer = ByteToStr()
          ReadBuffer = ReadBuffer & _buffer
        MsgBox("Timeout expired!")
      End If
    Catch ex As Exception
    End Try
  End Sub

  Public Function ByteToStr() As String
    Dim strData As String = ""
    Dim bRead, nRead As Integer

    bRead = SerialPort.BytesToRead
    Dim cData(bRead - 1) As Byte

    nRead = SerialPort.Read(cData, 0, bRead)  'Reading the Data

    For Each b As Byte In cData
      If b <> 0 AndAlso b <> 3 AndAlso b <> 10 AndAlso b <> 4 AndAlso b <> 2 AndAlso b <> 16 Then
        strData += Chr(b)
      End If
    Return Trim(strData)
  End Function

End Class

Open in new window

Who is Participating?
milindsaraswalaAuthor Commented:
milindsaraswalaAuthor Commented:
Any reply for this. Still I don't got anybody suggestion on this.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.