Results 1 to 9 of 9

Thread: SerialPort.ReadChar() takes hex returns decimal

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    5

    SerialPort.ReadChar() takes hex returns decimal

    Hi there,

    I am working on a simple vb project to read data from a PC serial port and write that data to a file.

    The data is ascii sent in Hex format. I used MySerialPort.ReadChar() to read data and MyStreamWriter.Write(MySerialPort.ReadChar()) to write it to a file.

    I copied my code below, sorry about the format.

    The trouble is: MySerialPort.ReadChar() acts the same as MySerialPort.ReadByte() , both takes the Hex data input and returns data in decimal format.
    I tried to use Convert.ToChar(MySerialPort.ReadChar()) to covert decimal to char, but it's too slow.
    I tried decimal.ToChar(MySerialPort.ReadChar()), but VB doesn't recognize decimal.ToChar, even after I installed .NET4.

    Did I do something wrong? how to make MySerialPort.ReadChar() to read Hex data and return char?

    Please help..

    nx2ms


    Entire code is here: sorry about the format.
    ******************************************************

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


    Public Class Form2

    Dim WithEvents ComPort As SerialPort =
    New SerialPort("COM3", 9600, Parity.Even, 8, StopBits.One)


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If ComPort.IsOpen = False Then ComPort.Open()
    MessageBox.Show(ComPort.BaudRate & ComPort.PortName & ComPort.Parity & ComPort.StopBits)
    End Sub

    Private Sub ComPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles ComPort.DataReceived


    Dim path As String = "C:\Users\blu\My Documents\work\Projects\VB_Uart\Uart_Test\test8.txt"

    Dim objWriter As New System.IO.StreamWriter(path, True)

    'objWriter.WriteLine(Hex(ComPort.ReadByte()))
    objWriter.WriteLine(ComPort.ReadChar())
    'objWriter.WriteLine(ComPort.ReadByte())
    'objWriter.Write(Convert.ToChar(ComPort.ReadByte()))
    objWriter.Close()


    End Sub


    End Class

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: SerialPort.ReadChar() takes hex returns decimal

    "The data is ascii sent in Hex format." means what exactly? Hex is an Ascii interpretation of ones and zeros stored in memory. The data is actually stored and sent in binary, how we interpret the binary is up to us.

    Code:
            Dim c As Char = "Z"c
            Dim a As Byte = Convert.ToByte(c) 'get c as it is stored numerically
            Debug.WriteLine(c)
            Debug.WriteLine(a.ToString) 'show the decimal value
            Debug.WriteLine(a.ToString("X").PadLeft(2, "0"c)) 'how c looks in memory as hex
            Debug.WriteLine(Convert.ToString(a, 2).PadLeft(8, "0"c)) 'how c looks in memory as binary
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    5

    Re: SerialPort.ReadChar() takes hex returns decimal

    You are right Hex part is not that critical.

    The data is made of 0's and 1's, is intended to be interpreted as Ascii.

    Do you have any suggestions as why MySerialPort.ReadChar() behaves same as MySerialPort.ReadByte(), both return data in decimal format?

    Thanks.

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: SerialPort.ReadChar() takes hex returns decimal

    Quote Originally Posted by nx2ms View Post
    You are right Hex part is not that critical.

    The data is made of 0's and 1's, is intended to be interpreted as Ascii.

    Do you have any suggestions as why MySerialPort.ReadChar() behaves same as MySerialPort.ReadByte(), both return data in decimal format?

    Thanks.
    Then interpret the data as ascii. I'd use .Read.

    Code:
            Dim foobar() As Byte = New Byte() {90} 'what you received from the rs232 port
            Dim c As String = System.Text.ASCIIEncoding.ASCII.GetChars(foobar, 0, foobar.Length)
    
    'or
    
            Dim foobar As Integer = 90  'what you received from the rs232 port
            Dim c As Char = Chr(foobar)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: SerialPort.ReadChar() takes hex returns decimal

    A character IS a byte fundamentally. Technically, we now have two byte characters in some encodings, but not for serial purposes. A byte is always binary. It's up to you whether you want to see it as decimal, hex, or interpret it into an ASCII character. All three of those are different visual representations of the same eight bits. The bits aren't any different just because you choose to interpret them as a decimal number or a string character. Having said that, I should also add that you want to keep them as numbers as long as possible, because once you put them into a string or character datatype, they are slower to work with.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    5

    Re: SerialPort.ReadChar() takes hex returns decimal

    Thanks for the reply. I understand what you guys try to say. I tried Chr() in my code, it works well.

    My issue here is the speed. When I used putty to receive data, it can take 8x more data than using VB SerialPort.ReadChar(), with Chr(dec), or Convert.ToChar(dec), those two are about same speed. I originally thought the decimal to char conversion slows down the data transmission. Then I realized MySerialPort.ReadByte() runs about same speed as well.

    So maybe batch processing i.e. reading from a buffer would improve more? Any suggestions?

    Thanks for your help, I really appreciate.

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    5

    Re: SerialPort.ReadChar() takes hex returns decimal

    Thanks guys, I used SerialPort.ReadLine(), it's now cooking with gas... great!!

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: SerialPort.ReadChar() takes hex returns decimal

    Yeah, reading into a buffer should boost your speed. ReadByte is going to have to get each byte sequentially, which is ok, but when you fill a buffer, the compiler can use some memory copying techniques that should greatly boost your performance. Fill a buffer whenever that option appears viable, and don't convert things into strings until the last possible moment. If you get a byte array buffer, leave it as a byte array, and work with it as a byte array, as long as you possibly can. Manipulating strings is slower than working with bytes, as a general rule.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    5

    Re: SerialPort.ReadChar() takes hex returns decimal

    I see, in data format is easier to process. Now I just use StreamWriter.WriteLine(SerialPort.ReadLine()) everything is ASCII, VB may treat it as a string. Anyway to read a buffer as data?

    Thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width