|
-
Feb 4th, 2009, 03:25 PM
#1
Thread Starter
Addicted Member
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,
-
Feb 4th, 2009, 03:52 PM
#2
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
-
Feb 4th, 2009, 04:02 PM
#3
Thread Starter
Addicted Member
Re: How to display a byte other than its ASCII?
 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)?
-
Feb 4th, 2009, 04:04 PM
#4
Re: How to display a byte other than its ASCII?
You mean if you run my code, you get 49 in the textbox?
-
Feb 4th, 2009, 04:14 PM
#5
Thread Starter
Addicted Member
Re: How to display a byte other than its ASCII?
 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.
-
Feb 4th, 2009, 04:35 PM
#6
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
-
Feb 4th, 2009, 04:36 PM
#7
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
 
-
Feb 4th, 2009, 04:37 PM
#8
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
 
-
Feb 4th, 2009, 04:37 PM
#9
Re: How to display a byte other than its ASCII?
serialport write has three methods.
-
Feb 4th, 2009, 04:40 PM
#10
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.
-
Feb 4th, 2009, 04:43 PM
#11
Re: How to display a byte other than its ASCII?
 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!
-
Feb 4th, 2009, 04:59 PM
#12
Thread Starter
Addicted Member
Re: How to display a byte other than its ASCII?
 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!
-
Feb 4th, 2009, 05:45 PM
#13
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
 
-
Feb 4th, 2009, 05:49 PM
#14
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
-
Feb 5th, 2009, 10:42 AM
#15
Thread Starter
Addicted Member
Re: How to display a byte other than its ASCII?
I appreciate everybody's help! It works now.
-
Feb 5th, 2009, 11:30 AM
#16
Re: How to display a byte other than its ASCII?
mark thread resolved (under Thread Tools) and if you want rate posters that helped.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|