Results 1 to 4 of 4

Thread: Encoding.GetBytes()

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    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.

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

    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

  3. #3
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Encoding.GetBytes()

    Are you sure the string is ASCII encoding? This may be causing the issue if it is not actually in ASCII.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Encoding.GetBytes()

    Quote 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
  •  



Click Here to Expand Forum to Full Width