|
-
Nov 12th, 2007, 10:44 AM
#1
Thread Starter
Frenzied Member
Encoding.GetBytes()
Anybody have any experience with this function? I am trying to read data from a serial port with SerialPort.ReadExisting(). When I look at my port monitor tool I see the correct data. But when I read with the SerialPort function and convert the result into a byte array, it seems like half of my bytes are incorrect. Here is my code:
Code:
Public Sub DataReceived( _
ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles m_oSerialPort.DataReceived
Dim strResult As String
Dim strHexString As String
Dim baReturnPacket() As Byte
strResult = m_oSerialPort.ReadExisting()
Dim encoding As New System.Text.ASCIIEncoding()
baReturnPacket = encoding.GetBytes(strResult)
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Nov 12th, 2007, 10:56 AM
#2
Re: Encoding.GetBytes()
Since it's a serial port, why not just use Read, and pass in the byte array as an argument. That would skip a semi-costly encoding and decoding step.
However, if you want to do it that way, have you looked at strResult to confirm that it is ok at that point?
I have used that function without any problem before, and nothing you have looks incorrect to me, so I would be curious to know whether the string looked right.
One other thing I might mention, though it shouldn't matter. I haven't used that function in over a year, but I don't recall making the object. I think I did something along the lines of:
baRet = system.text.asciiencoding.encoding.GetBytes(strResult)
though I would have to go back and review an old program to be certain.
My usual boring signature: Nothing
 
-
Nov 12th, 2007, 10:59 AM
#3
Re: Encoding.GetBytes()
Are you sure the string is ASCII encoding? This may be causing the issue if it is not actually in ASCII.
-
Nov 12th, 2007, 11:10 AM
#4
Thread Starter
Frenzied Member
Re: Encoding.GetBytes()
 Originally Posted by Shaggy Hiker
Since it's a serial port, why not just use Read, and pass in the byte array as an argument. That would skip a semi-costly encoding and decoding step.
However, if you want to do it that way, have you looked at strResult to confirm that it is ok at that point?
I have used that function without any problem before, and nothing you have looks incorrect to me, so I would be curious to know whether the string looked right.
One other thing I might mention, though it shouldn't matter. I haven't used that function in over a year, but I don't recall making the object. I think I did something along the lines of:
baRet = system.text.asciiencoding.encoding.GetBytes(strResult)
though I would have to go back and review an old program to be certain.
Don't I need to know how many bytes there are in order to use SerialPort.Read()? That's why I used ReadExisting, gives me whatever is there in the buffer which I don't know the size for. Honestly haven't used this class much though so maybe there is a better way.
Edit: Ok I see a BytesToread field, lemme play with that and Read. Thanks.
Last edited by SeanGrebey; Nov 12th, 2007 at 11:23 AM.
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
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
|