Serial Port Class in VB.net with ScanPal2

Posted on 2010-09-06
Medium Priority
Last Modified: 2013-11-27
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

Question by:milindsaraswala
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Author Comment

ID: 33613314
Any reply for this. Still I don't got anybody suggestion on this.
LVL 13

Expert Comment

ID: 33614504

Accepted Solution

milindsaraswala earned 0 total points
ID: 33616604

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question