Git Product home page Git Product logo

Comments (7)

PeterStaev avatar PeterStaev commented on June 12, 2024 1

@ws1088 I dont think \n is an option. It might fix the "simple" print option but from what i have seen JSON from the RPC are separated by \r so I prefer to have correct working JSON RPC than to make "simple" print not print empty lines :)

from lego-spikeprime-mindstorms-vscode.

PeterStaev avatar PeterStaev commented on June 12, 2024

@ws1088 , I've noticed the different behaviors, but sadly not much can be done since there are cases where I get the value with a new line inside (not sure why) and in some cases there is no ending new line. That's why I decided to leave the extra new line just so it does not end on the same line as the previous print.

from lego-spikeprime-mindstorms-vscode.

ws1088 avatar ws1088 commented on June 12, 2024

@PeterStaev It's most likely because of how we are splitting the lines while reading the serial communications:

        this._parser = this._serialPort.pipe(new SerialPort.parsers.Readline({ delimiter: "\r" }));

imagine we are parsing the content in the serial buffer:

line_1\r\nline2\r\nline3\r\n

and if we split the above by \r then will will get the following

line_1
\nline2
\nline3
\n   <- this one might be a valid JSON so we will not see this?

we should be splitting by \n and removing trailing \r (or splitted by \r\n if possible), so that we will get:

line_1
line_2
line_3

and add '\r\n' to each.

if we want to handle print('hello', ends='') we might need to do something more...

from lego-spikeprime-mindstorms-vscode.

ws1088 avatar ws1088 commented on June 12, 2024

@PeterStaev If my theory is right (haven't debug into it), that \n is a valid JSON, we can make sure data have more than 3 characters and the first non-space character is { (see #19). That way last \n can be printed.

from lego-spikeprime-mindstorms-vscode.

PeterStaev avatar PeterStaev commented on June 12, 2024

\n is not a valid JSON:
image

from lego-spikeprime-mindstorms-vscode.

ws1088 avatar ws1088 commented on June 12, 2024

@PeterStaev ok, how about this?

        this._parser.on("data", (data: string) => {
            let json: { [key: string]: any };

            try {
                json = JSON.parse(data);
            }
            catch (e) {
                // When data cannot be JSON parsed we re probably getting text from user's `print` command so we log it
                if (data.length > 0 && data.charAt(0) === "\n") {
                    logger?.info(data.substring(1).replace(/\n/gi, "\n\r"));
                } else {
                    logger?.info(data.replace(/\n/gi, "\n\r"));                
                }
                logger?.info("\n\r");
                return;
            }

from lego-spikeprime-mindstorms-vscode.

PeterStaev avatar PeterStaev commented on June 12, 2024

@ws1088 I will review this and the other print problem (#19), but for the time being I consider those a nice to have and i want to avoid "hacks" that might break the main JSON RPC communication. In the end if users want to have better printing they can always use the LEGO provided one which shouldn't have such problems.

from lego-spikeprime-mindstorms-vscode.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.