dcsimg
Results 1 to 7 of 7

Thread: [RESOLVED] Serial Port will not send values greater than 0x3F????

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    68

    Resolved [RESOLVED] Serial Port will not send values greater than 0x3F????

    Hi, I am having a very frustrating and seemingly simple problem to fix.

    When sending data over a serial port, the receiving is not receiving values over 0x3F. I believe this is due to encoding. reading online, it appears the standard encoding does not allow values over '?'.

    I am trying to send hex values. Not strings.

    My understanding is that this can be done using something like:
    Public Shared array_to_send(999) As Byte
    mySerialPort.Write(array_to_send, 0, array_to_send.Count)

    " Public Sub CommPortSetup()

    'Always prevent program crash
    Try

    With mySerialPort
    .PortName = cboCOM.Text
    .BaudRate = 256000 '57600 '115200 '921600 460800 115200 256000
    .DataBits = 8
    .Parity = Parity.None
    .StopBits = StopBits.One
    .Handshake = Handshake.None
    .DtrEnable = True 'This allows us to open and close the port as many times as we want.
    .WriteTimeout = 400 'Do not get stuck trying to send old data if device did not recieve it (not connected?)
    .ReadTimeout = 400
    .ReceivedBytesThreshold = 1000 'interrupt when we have received the entire packet structure and have the data to update the display with
    .ReadBufferSize = 2000
    .Encoding = System.Text.Encoding.GetEncoding(1252) '1252 '28591
    End With

    AddHandler mySerialPort.DataReceived, AddressOf DataReceivedHandler

    mySerialPort.Open()
    Catch ex As Exception
    'MsgBox(ex.Message) 'Ignore failed connect attempts
    End Try

    End Sub"

    Above is how I am initializing my port. I have another app on this computer that is pinging back and forth with this app.

    I tried to set the encoding to what people noted online but nothing is working.

    Any thoughts?

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    68

    Re: Serial Port will not send values greater than 0x3F????

    Wow....I was being an idiot. I set the encoding on one application (sending) but not the receiving....sorry!

  3. #3
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: Serial Port will not send values greater than 0x3F????

    The serial port uses ASCII as its default encoding. ASCII has only 128 characters which are mapped to code points 0 to 127. So it's values over 0x7F that are not allowed.

    You see the ? because the ASCIIEncoding Class uses a replacement fallback mechanism whereby byte values 128 to 255 are decoded to the ? character, and characters not in the ASCII character set are encoded as byte value 0x3F (the ASCII code point for the ? character).

    In your code, you are using the SerialPort.Write method that writes a Byte array. This method is not affected by the Text Encoding, so if you are seeing ? characters popping up, then you are likely sending using binary data methods, but receiving using character data methods, which is bad ;-)

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,911

    Re: Serial Port will not send values greater than 0x3F????

    Perhaps mark the thread resolved so Inferrd wouldn't have suggested at the end of post 3 what you already determined in post #2.
    Although the additional information about the question mark being ASCII 0x3F and being replaced for the above ASCII range byte values was good for explaining what you were seeing on the receiving end. I would have though a lot of values greater than 0x3F would have been getting through since the capital letters start at 0x41 and the lower case letters start at 0x61, which all should have made it through the receiving decoding.

  5. #5
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: Serial Port will not send values greater than 0x3F????

    Quote Originally Posted by passel View Post
    Perhaps mark the thread resolved so Inferrd wouldn't have suggested at the end of post 3 what you already determined in post #2.
    In post #2, Daniel states that setting the encoding on the receiving end is what fixed his problem. That would indicate that while the sending end is sending byte data with Write(Byte[], Int32, Int32) (as stated in post #1) the receiving end is using one of the character based read methods: Read(Char[], Int32, Int32), ReadChar(), ReadExisting(), ReadLine(), ReadTo(String).

    My point is that if you are sending bytes with Write(Byte[], Int32, Int32), then you should not be using a character based read method or playing with text encodings. You should use one of the corresponding binary read methods Read(Byte[], Int32, Int32) or ReadByte()

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    68

    Re: Serial Port will not send values greater than 0x3F????

    Thank you for the replies. Both methods of writing via the serial port actually work when using that encoding style. But yes, I am using Write(Byte[], Int32, Int32) and same for read. Marking as resolved now.

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,266

    Re: Serial Port will not send values greater than 0x3F????

    Quote Originally Posted by Inferrd View Post
    In post #2, Daniel states that setting the encoding on the receiving end is what fixed his problem. That would indicate that while the sending end is sending byte data with Write(Byte[], Int32, Int32) (as stated in post #1) the receiving end is using one of the character based read methods: Read(Char[], Int32, Int32), ReadChar(), ReadExisting(), ReadLine(), ReadTo(String).

    My point is that if you are sending bytes with Write(Byte[], Int32, Int32), then you should not be using a character based read method or playing with text encodings. You should use one of the corresponding binary read methods Read(Byte[], Int32, Int32) or ReadByte()
    I agree with this.

    my $0.02...

    My experience has been to only use Read(Byte[], Int32, Int32) and Write(Byte[], Int32, Int32) methods, even if the data is characters. In the case of character(string) based data I use the appropriate System.Text encoding to convert to/from bytes at both ends.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width