Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to read bytes, keep getting "?" (ascii 63) #12

Closed
guiglass opened this issue Jan 14, 2018 · 2 comments
Closed

Trying to read bytes, keep getting "?" (ascii 63) #12

guiglass opened this issue Jan 14, 2018 · 2 comments

Comments

@guiglass
Copy link

guiglass commented Jan 14, 2018

If I send the byte 128 (or anything higher) followed by a newline then I read that into Unity like so:

string message = serialController.ReadSerialMessage ();
print(message[0]);

Outputs: "?" (or ascii 63).

This is just wrong!

@guiglass
Copy link
Author

guiglass commented Jan 14, 2018

Well, I created a solution... Turns out it was to do with serialPort.ReadLine() and the way it converts to string. Not sure why it was adding question marks (completely ridiculous actually). But what I threw together reads in a line of bytes directly instead of using Readline()

SerialThreadLines.cs

    /*OLD WAY
    protected override object ReadFromWire(SerialPort serialPort)
    {
        return serialPort.ReadLine();
    }
    */

    /*NEW WAY*/
    private List<byte> buffer = new List<byte>();
    protected override object ReadFromWire(SerialPort serialPort)
    {
        buffer.Add( (byte)serialPort.ReadByte() );

        // Search for the separator in the buffer
        int index = System.Array.FindIndex<byte>(buffer.ToArray(), 0, buffer.Count, IsSeparator);
        if (index == -1)
            return null;

        buffer.RemoveAt (buffer.Count - 1); //remove CR
	buffer.RemoveAt (buffer.Count - 1); //remove LF

        var output = buffer.ToArray ();
        buffer.Clear ();

        return output;
    }

    private bool IsSeparator(byte aByte)
    {
        return aByte == '\n';
    }

Above could really use some optimization but it's simply conceptual.

@dwilches
Copy link
Owner

Attempting to print an unprintable character (like 128) can yield weird outputs (because they are unprintable). Normally what you do with those characters is to print their ASCII value, not the character itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants