Solved

Serial Port Class in VB.net with ScanPal2

Posted on 2010-09-06
3
834 Views
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
      components.Dispose()
    End If
    MyBase.Dispose(disposing)
  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.SuspendLayout()
    '
    'txData
    '
    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 = ""
    '
    'btnExtract
    '
    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
    '
    'frmScanPalReading
    '
    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.Controls.Add(Me.btnExtract)
    Me.Controls.Add(Me.txData)
    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
    Me.MaximizeBox = False
    Me.MinimizeBox = False
    Me.Name = "frmScanPalReading"
    Me.Text = "Scan Pal Data Reading"
    Me.ResumeLayout(False)
  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
    SerialPort.Close()
  End Sub

  Private Sub frmScanPalReading_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try
      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

        .Open()
      End With
    Catch ex As Exception
      MsgBox(ex.Message)
    End Try
  End Sub

  Private Sub btnExtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExtract.Click
    SendCommand("READ")
  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..
    ReceiveData()
  End Sub

  Private Sub ReceiveData()
    Try
      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)
          SendCommand("ACK")
          _buffer = ByteToStr()
          ReadBuffer = ReadBuffer & _buffer
        Loop
      Else
        MsgBox("Timeout expired!")
      End If
    Catch ex As Exception
      MsgBox(ex.Message)
    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
    Next
    Return Trim(strData)
  End Function

End Class

Open in new window

0
Comment
Question by:milindsaraswala
  • 2
3 Comments
 
LVL 5

Author Comment

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

Expert Comment

by:game-master
ID: 33614504
0
 
LVL 5

Accepted Solution

by:
milindsaraswala earned 0 total points
ID: 33616604
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now