Char displayed as 'boxes' in textbox?!

nescartel

Member
Joined
Jun 10, 2007
Messages
22
Programming Experience
3-5
Hello everyone, :)

New forum member here. had this weird problem, and can't seem to find the solution anywhere! :p I am using VB .net 2005 on an XP based laptop. I have this controller-based hardware connected to the serial port of my laptop. My serial port is only using 3 pins, transmit, receive, and signal ground. I am transmitting hexadecimal bytes contained in an array to the hardware and expects to get response from the hardware.

If let's say I transmit 15, 4, 17, 6, (in hexadecimal) sequentially; the hardware will execute 'system reset'. I managed to do this just fine by using;

VB.NET:
TxBuffer = New String() {Chr(&H15), Chr(&H4), Chr(&H17), Chr(&H6)}

the hardware DID reset on this event :), but if I want to monitor the data sent through serial port by putting the transmitted data in a textbox as below;

VB.NET:
TextBox1.Text = TextBox1.Text & " " & TxBuffer(i)

instead of the "15, 4, 17, 6" that I transmitted, the textbox just displays 4 'boxes'! :confused: i tried editing the transmitted data by deleting the 'char' typecast from the above code, turning it into;

VB.NET:
TxBuffer = New String() {&H15, &H4, &H17, &H6}

by using this, i can display the transmitted data in textbox very well, but the hardware did not reset :(. why is this so? i want the user to be able to display the transmitted text correctly, how can i do this?

here is my complete code. it is very short, resides on only 1 form that has one textbox to display the transmitted data, and one button to trigger the transmission of data to serial port. please advise, thank you!

VB.NET:
Public Class Form1

    Dim TxBuffer() As String    'Telegram message to send
    Dim TxBufferSize As Integer 'Size of dynamic array

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Open()
    End Sub

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        SerialPort1.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TxBuffer = New String() {Chr(&H15), Chr(&H4), Chr(&H17), Chr(&H6)} 'reset OK, but display squares
        'TxBuffer = New String() {&H15, &H4, &H17, &H6}                     'reset not OK , but display OK

        TxBufferSize = UBound(TxBuffer) 'Get array size
        TransmitData(TxBuffer,TxBufferSize)
    End Sub


End Class
 
Last edited:
instead of the "15, 4, 17, 6" that I transmitted

erm.. no, you didnt transmit "15, 4, 17, 6" because that, in hex, is:

&H22 &H31 &H35 &H20 &H2C &H20 &H34 &H2C &H20 &H31 &H37 &H2C &H20 &H36 &H22

A character of hex code 4, is not the numerical digit "4". "4" is &H34, or 52 in decimal

Chr(52) = "4"

Now, can you see why your textbox shows just control characters (boxes) ?
 
hurmm... :confused: i'm still confused here...
the thing is, the code;

VB.NET:
TxBuffer = New String() {Chr(&H15), Chr(&H4), Chr(&H17), Chr(&H6)}

is doing exactly the right thing as i wanted,
but somehow i cannot display it in textbox.
why is it displaying squares instead of the data as above?
what is it that i did not understand?
would you be so kind as to point it out for me? :eek:

thank you in advance... :D
 
the character A is 65 is decimal, or &H41 in hex


This means:

TxBuffer = New String() {Chr(&H41), Chr(&H41), Chr(&H41), Chr(&H41)}

When shown in a text box, will be AAAA


The hex number 15, when converted to a character IS NOT the string "15". Chr(&H15) IS NOT A PRINTABLE CHARACTER


I dont know how else to explain this to you: An array of 4 characters that are not printable is NOT THE SAME THING as a string of "15, 4, 7, 6"

Look at this table:
table.gif



Those are the ascii codes for characters.. If you have a number, 65, (&H41) and you convert it to a character, it becomes A
If you have a number, 21 (&H15) and you convert it to a character, it becoems NAK (according to that table, an ascii control character probably) IT DOESNT BECOME A STRING OF THE CHARACTER "1" (which is character Chr(&H31) ) followed by "5" (which is character code Chr(&H35) )

I cant really think of any other way I can put this.. You cant convert a byte array (what youre sending to the device) directly to a string, and turn all the bytes within, into a numerical representation, because there isnt a mapping like that

If you want to convert byte &H4E into the string "4E" then you have to call .ToString("X") on it. Trying to convert it into a char will result in it becoming the character "N" because N's number in the ascii table is 4E
 
owh, now i get it!
no wonder it's displaying boxes,
those are actually NOT ACKNOWLEDGE, END OF TRANSMISSION, etc,
all are non printed characters! :p

thank you for your generous explanation cjard! :)
found a workaround for that problem,
instead of transmitting the data and try to display the ascii data,
i split the data into 2 behavior,
one to be displayed on textbox,
and another one to be converted into ascii and transmitted.

thank you so much! :D:D:D
 
Back
Top