Results 1 to 16 of 16

Thread: How to display a byte other than its ASCII?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    135

    How to display a byte other than its ASCII?

    Hi,

    I read parameters from my ECU thru RS232 and display them on TextBox. But the display is ASCII instead of itself. E.g. I read 0x01 and store it in DataReceived, and set

    Dim DataRecevied As Byte

    TextBox1.Text = DataReceived

    The value appeared in TextBox1 is 49 (0x31), which is ASCII of 0x01. How to display 01 there?

    Thanks,

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: How to display a byte other than its ASCII?

    Are you sure the value you are getting from the RS232 is 0x01?

    If you just try this simple code, you will see setting a textbox.text value, to a byte that contains 0x01 does in fact show 1 in the textbox, not 49

    Code:
            Dim DataRecevied As Byte = &H1
            TextBox1.Text = DataRecevied
    in VB, 0x01 is wrtten as &H1

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    135

    Re: How to display a byte other than its ASCII?

    Quote Originally Posted by kleinma
    Are you sure the value you are getting from the RS232 is 0x01?

    If you just try this simple code, you will see setting a textbox.text value, to a byte that contains 0x01 does in fact show 1 in the textbox, not 49

    Code:
            Dim DataRecevied As Byte = &H1
            TextBox1.Text = DataRecevied
    in VB, 0x01 is wrtten as &H1

    Thanks. But I got the same issue as before. BTW how to set if the variable is an array DataReceived(11,4)?

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: How to display a byte other than its ASCII?

    You mean if you run my code, you get 49 in the textbox?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    135

    Re: How to display a byte other than its ASCII?

    Quote Originally Posted by kleinma
    You mean if you run my code, you get 49 in the textbox?
    Yes. The simple test was to use DecimalValue(0, 0) declared as Short. I typed 1 in TextBox1 and store it to DecimalVlaue(0,0) as follows. Then I sent it using SerialPort.Write().

    DecimalValue(0, 0) = Convert.ToInt16(TextBox1.Text)
    SerialPort.Write(DecimalValue(0, 0))

    By shorting Rx and Tx of RS232, I got the value I sent out and store it as follows :

    DataReceived(0, 0) = Convert.ToByte(buffer(0))
    TextBox1.Text = DataReceived(0, 0)

    Because the code you told didn't work for array. I changed my code without array. It still show 49 other than 1 in textbox.

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

    Re: How to display a byte other than its ASCII?

    Code:
            Dim buff() As Byte = New Byte() {1}
            SerialPort1.Write(buff, 0, 1)
    
    
    
        Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Dim numBytes As Integer = SerialPort1.BytesToRead 'get number of bytes to read
            Dim buff() As Byte = New Byte(numBytes - 1) {} 'create a buffer
            numBytes = SerialPort1.Read(buff, 0, numBytes) 'read available bytes
            For x As Integer = 0 To buff.Length - 1 'show bytes read as hex
                Debug.WriteLine(buff(x).ToString("X")) 'show as hex
            Next
        End Sub
    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

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

    Re: How to display a byte other than its ASCII?

    That doesn't sound like what Kleinma suggested. You say you typed 1 in a textbox, but he set 1 to a variable. You are seeing the ascii for the text character "1", not the numeral 1 converted to a string.

    put a breakpoint on the write, and take a look at what is in DecimalValue(0,0). It should be 1.

    By the way, SerialPort.Write has only one version that takes a single argument, and that argument is a string. I would say that the code is implicitly converting DecimalValue(0,0) to a string and sending that, rather than sending the value. This would only be possible if you have Option Strict Off. Turn Option Strict ON, deal with the errors that arise, and this problem will have been clarified.
    My usual boring signature: Nothing

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

    Re: How to display a byte other than its ASCII?

    I figured I'd be a bit slow, and I was wondering where dbasnett was, since this was a serial issue. However, he did the same thing Kleinma did, which is write a byte rather than a string.
    My usual boring signature: Nothing

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

    Re: How to display a byte other than its ASCII?

    serialport write has three methods.
    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

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: How to display a byte other than its ASCII?

    I would suggest turning option strict on, which will probably force more explict accurate conversions of the datatypes, instead of hoping the runtime converts them to what you want.

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

    Re: How to display a byte other than its ASCII?

    Quote Originally Posted by kleinma
    I would suggest turning option strict on, which will probably force more explict accurate conversions of the datatypes, instead of hoping the runtime converts them to what you want.
    amen to that!
    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

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    135

    Re: How to display a byte other than its ASCII?

    Quote Originally Posted by dbasnett
    Code:
            Dim buff() As Byte = New Byte() {1}
            SerialPort1.Write(buff, 0, 1)
    
    
    
        Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Dim numBytes As Integer = SerialPort1.BytesToRead 'get number of bytes to read
            Dim buff() As Byte = New Byte(numBytes - 1) {} 'create a buffer
            numBytes = SerialPort1.Read(buff, 0, numBytes) 'read available bytes
            For x As Integer = 0 To buff.Length - 1 'show bytes read as hex
                Debug.WriteLine(buff(x).ToString("X")) 'show as hex
            Next
        End Sub
    Thanks. It does show its hex value but is ASCII hex. E.g. if I send 1, it shows 31. For one digit, I can convert it by minus 0x30. However for two digits it only shows the first one. E.g. if I type 21, it shows 32 (ASCII Hex value of the 1st digit 2).

    Any other solutions?

    I appreciate it!

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

    Re: How to display a byte other than its ASCII?

    If you are just sending things that have been typed into a textbox, then they are strings, so leave them as strings. Once they have been transmitted, then you can take the string and convert in to an integer with Integer.TryParse or something like that. Right now, you seem to be fighting to get a string (which happens to represent a number, but is still just a string, since it comes from a textbox) into some other form prior to transmission. Leave the conversion until afterwards.
    My usual boring signature: Nothing

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

    Re: How to display a byte other than its ASCII?

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'i have the port open and a loop back
            Debug.WriteLine("")
            Dim buff() As Byte = New Byte() {0}
            For x As Integer = 1 To 16
                buff(0) = CByte(x)
                SerialPort1.Write(buff, 0, 1)
            Next x
        End Sub
        Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Dim numBytes As Integer = SerialPort1.BytesToRead 'get number of bytes to read
            Dim buff() As Byte = New Byte(numBytes - 1) {} 'create a buffer
            numBytes = SerialPort1.Read(buff, 0, numBytes) 'read available bytes
            For x As Integer = 0 To buff.Length - 1 'show bytes read as hex
                'when button 1 is clicked the debug output is
                '0102030405060708090A0B0C0D0E0F10
                'from the statement that follows
                Debug.Write(buff(x).ToString("X").PadLeft(2, "0"c)) 'show as hex
                ' looks like hex to me
            Next
        End Sub
    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

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    135

    Re: How to display a byte other than its ASCII?

    I appreciate everybody's help! It works now.

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

    Re: How to display a byte other than its ASCII?

    mark thread resolved (under Thread Tools) and if you want rate posters that helped.
    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

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