Git Product home page Git Product logo

Comments (2)

ws1088 avatar ws1088 commented on June 12, 2024

@PeterStaev This is probably similar to #16, 1 is a valid JSON string. What I have seen and done in other python libraries is to look for at least open curly brace first. If yes, then attempt to parse JSON. If no, then print it out. You can do fancier logic to look for things like {"m":

from lego-spikeprime-mindstorms-vscode.

ws1088 avatar ws1088 commented on June 12, 2024

@PeterStaev A very "hacky" solution to show the cause of the problem.

constructor(location: string, logger: Logger) {
        this._serialPort = new SerialPort(location, {
            baudRate: 115200,
            autoOpen: false,
        });
        this._serialPort.setDefaultEncoding("utf-8");
        this._parser = this._serialPort.pipe(new SerialPort.parsers.Readline({ delimiter: "\r" }));
        this._parser.on("data", (data: string) => {
            let json: { [key: string]: any };

            let print_data = false;

            try {
                if ((data.length >= 1 && data.charAt(0) === "{") ||
                    (data.length >= 2 && data.charAt(0) === "\n" && data.charAt(1) === "{")) {
                    json = JSON.parse(data);
                }
                else {
                    print_data = true;
                }
            }
            catch (e) {
                print_data = true;
            }

            if (print_data) {
                // 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.

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.