vb.net serial IO problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gene.james@gmail.com

    #1

    vb.net serial IO problem


    One of those cases where the problem is probably right under my nose
    but I can't see it. This serial test code fails to send the right data:

    Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles ButtonCAM1.Clic k
    Dim TestData() As Byte = {&HFE, &H11, &H11, &HFF}
    Using KeyerCom As IO.Ports.Serial Port = _
    My.Computer.Por ts.OpenSerialPo rt(KeyerPortNam e, 38400,
    IO.Ports.Parity .None, 8, IO.Ports.StopBi ts.One)
    KeyerCom.DtrEna ble = True
    KeyerCom.Write( TestData, 0, 4)
    End Using
    End Sub

    Checking with a serial monitor, I find that it actually sends code
    sends a NUL (&H00) after every byte sent. So the stream actually sent
    (as hex)is: FE 00 11 00 11 00 FF 00

    Any ideas?

  • Chris Dunaway

    #2
    Re: vb.net serial IO problem

    gene.james@gmai l.com wrote:
    One of those cases where the problem is probably right under my nose
    but I can't see it. This serial test code fails to send the right data:
    >
    Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles ButtonCAM1.Clic k
    Dim TestData() As Byte = {&HFE, &H11, &H11, &HFF}
    Using KeyerCom As IO.Ports.Serial Port = _
    My.Computer.Por ts.OpenSerialPo rt(KeyerPortNam e, 38400,
    IO.Ports.Parity .None, 8, IO.Ports.StopBi ts.One)
    KeyerCom.DtrEna ble = True
    KeyerCom.Write( TestData, 0, 4)
    End Using
    End Sub
    >
    Checking with a serial monitor, I find that it actually sends code
    sends a NUL (&H00) after every byte sent. So the stream actually sent
    (as hex)is: FE 00 11 00 11 00 FF 00
    >
    Any ideas?
    It sounds like an encoding issue. You could try using the Encoding
    class to get a string using a different encoding:

    Dim s As String = Encoding.UTF8.G etString(TestDa ta)
    KeyerCom.Write( s)

    I'm not sure if this will solve your problem, though.

    Comment

    • gene.james@gmail.com

      #3
      Re: vb.net serial IO problem


      Chris Dunaway wrote:
      It sounds like an encoding issue. You could try using the Encoding
      class to get a string using a different encoding:
      >
      Dim s As String = Encoding.UTF8.G etString(TestDa ta)
      KeyerCom.Write( s)
      Been there. My data is declared bytes because if I encode as UNICODE
      UTF8 or ASCII, output for bytes >7F is translated to multi byte strings.

      Comment

      • Chris Dunaway

        #4
        Re: vb.net serial IO problem


        gene.james@gmai l.com wrote:
        Chris Dunaway wrote:
        It sounds like an encoding issue. You could try using the Encoding
        class to get a string using a different encoding:

        Dim s As String = Encoding.UTF8.G etString(TestDa ta)
        KeyerCom.Write( s)
        >
        Been there. My data is declared bytes because if I encode as UNICODE
        UTF8 or ASCII, output for bytes >7F is translated to multi byte strings.
        Have you tried Encoding.Defaul t?

        Comment

        • Stephany Young

          #5
          Re: vb.net serial IO problem

          Because SerialPort uses ASCIIEncoding by default, and you have not set any
          other Encoding, I would expect your transmission to be translated to {&H3F,
          &H11, &H11, &H3F}. That is with all bytes above &H7F (127) translated to
          &H3F (63 or ?). This behaviour is clearly stated in the documentation.


          <gene.james@gma il.comwrote in message
          news:1159886328 .517173.97600@b 28g2000cwb.goog legroups.com...
          >
          One of those cases where the problem is probably right under my nose
          but I can't see it. This serial test code fails to send the right data:
          >
          Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
          System.EventArg s) Handles ButtonCAM1.Clic k
          Dim TestData() As Byte = {&HFE, &H11, &H11, &HFF}
          Using KeyerCom As IO.Ports.Serial Port = _
          My.Computer.Por ts.OpenSerialPo rt(KeyerPortNam e, 38400,
          IO.Ports.Parity .None, 8, IO.Ports.StopBi ts.One)
          KeyerCom.DtrEna ble = True
          KeyerCom.Write( TestData, 0, 4)
          End Using
          End Sub
          >
          Checking with a serial monitor, I find that it actually sends code
          sends a NUL (&H00) after every byte sent. So the stream actually sent
          (as hex)is: FE 00 11 00 11 00 FF 00
          >
          Any ideas?
          >

          Comment

          • gene.james@gmail.com

            #6
            Re: vb.net serial IO problem


            Stephany Young wrote:
            Because SerialPort uses ASCIIEncoding by default, and you have not set any
            other Encoding, I would expect your transmission to be translated to {&H3F,
            &H11, &H11, &H3F}. That is with all bytes above &H7F (127) translated to
            &H3F (63 or ?). This behaviour is clearly stated in the documentation.
            You are correct. That is almost the behavior if I send a string it
            actually sends 3F 00 11 00 11 00 3F 00. If I say xxx.write (Chr(&FF)) I
            will get 3F 00 out of the serial port.
            I am sending a byte array and so should not be getting any UNICODE
            translation. I am puzzled by the incusion of the NUL after each
            character.

            I might think there's an anomaly with my serial monitor, but I have
            another program that sends these strings and it shows the right data on
            the monitor.

            Comment

            • Dick Grier

              #7
              Re: vb.net serial IO problem

              Hi,

              I send binary data frequently, and haven't seen this problem. Are you sure
              that the code that you've posted is accurate. I added a button the the
              VS2005Terminal example code on my web site as follows:

              Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
              System.EventArg s) Handles Button2.Click

              Dim TestData() As Byte = {&HFE, &H11, &H11, &HFF}

              'Dim TestData(3) As Byte

              'TestData(0) = &HFE

              'TestData(1) = &H11

              'TestData(2) = &H11

              'TestData(3) = &HFF

              SerialPort.Writ e(TestData, 0, 4)

              End Sub


              The array initializer code AND the explicit assignment code sent the correct
              data (verified by viewing it on a serial data analyzer). Encoding applies
              to String data only, not arrays of type Byte -- so, it should work "as
              advertised" unless there is something far from my experience. If you send
              me email, I can forward the example that I fiddled for this test.

              Dick

              --
              Richard Grier, MVP
              Hard & Software
              Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
              Edition,
              ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
              2006.
              See www.hardandsoftware.net for details and contact information.


              Comment

              • Dick Grier

                #8
                Re: vb.net serial IO problem

                Actually, I suspect that you have a receiving/viewing problem. If you are
                attempting to assign receive data to a String (don't do this!) using
                ReadExisting, your code will not work. Instead, you must assign binary data
                to an array of type Byte -- using the Read method.

                --
                Richard Grier, MVP
                Hard & Software
                Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
                Edition,
                ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
                2006.
                See www.hardandsoftware.net for details and contact information.


                Comment

                • gene.james@gmail.com

                  #9
                  Re: vb.net serial IO problem


                  Dick Grier wrote:
                  Actually, I suspect that you have a receiving/viewing problem. If you are
                  attempting to assign receive data to a String (don't do this!) using
                  ReadExisting, your code will not work. Instead, you must assign binary data
                  to an array of type Byte -- using the Read method.
                  >
                  Thanks. You're right, the code is fine. My serial monitor is not!

                  My serial monitor program (COMMSNIFFER 1.0.32) WAS tested and working
                  fine weeks ago ... on an XP box. I was monitoring, however, with a
                  WinME machine and there the COMMSNIFFER shows that extra NUL. Thinking
                  I was crazy, I retested COMMSNIFFER on the XP box today and it works
                  fine there. And so does my code.

                  Thanks very much for your kind help.
                  -Gene

                  Comment

                  • Dick Grier

                    #10
                    Re: vb.net serial IO problem

                    Hi,

                    Try my VirtualNullMode m/DataMonitor program (software downloads on my
                    homepage) and see if this works on your trouble machine.

                    Dick

                    --
                    Richard Grier, MVP
                    Hard & Software
                    Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
                    Edition,
                    ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
                    2006.
                    See www.hardandsoftware.net for details and contact information.


                    Comment

                    Working...