1

I'm currently developing a home automation framework for my apartment. This involves getting JSON over Serial from an Arduino. When the JSON can't be parsed (usually only on startup) I log it as an error.

Today something strange happened though, the broken JSON caused one of my terminals to become weird.

[nodemon] restarting due to changes...
[nodemon] starting `node build/server`
sensor living-room-humidity on serial:arduino-master:42
sensor living-room-temperature on serial:arduino-master:42
sensor living-room-motion on serial:arduino-master:42
sensor living-room-brightness on serial:arduino-master:42
sensor kitchen-humidity on serial:arduino-master:43
sensor kitchen-temperature on serial:arduino-master:43
sensor kitchen-motion on serial:arduino-master:43
sensor kitchen-brightness on serial:arduino-master:43
listening on http://127.0.0.1:50000
serial opened: arduino-master
serial error can't parse JSON:  S≤┼├▒│E⎼⎼⎺⎼: U┼e│⎻ec├ed ├⎺┐e┼
⎽e⎼☃▒┌ d☃⎽c⎺┴e⎼ed: /de┴/├├≤USB▮
/#Y⎼dB▒±dPSX2≤TB☃8AAAA c⎺┼┼ec├ed
/#Y⎼dB▒±dPSX2≤TB☃8AAAA ▒┤├▒e┼├☃c▒├ed
[┼⎺de└⎺┼] ⎼e⎽├▒⎼├☃┼± d┤e ├⎺ c▒▒┼±e⎽↓↓↓
[┼⎺de└⎺┼] ⎽├▒⎼├☃┼± ◆┼⎺de b┤☃┌d/⎽e⎼┴e⎼◆
⎽e┼⎽⎺⎼ ┌☃┴☃┼±↑⎼⎺⎺└↑▒┤└☃d☃├≤ ⎺┼ ⎽e⎼☃▒┌:▒⎼d┤☃┼⎺↑└▒⎽├e⎼:42
⎽e┼⎽⎺⎼ ┌☃┴☃┼±↑⎼⎺⎺└↑├e└⎻e⎼▒├┤⎼e ⎺┼ ⎽e⎼☃▒┌:▒⎼d┤☃┼⎺↑└▒⎽├e⎼:42
⎽e┼⎽⎺⎼ ┌☃┴☃┼±↑⎼⎺⎺└↑└⎺├☃⎺┼ ⎺┼ ⎽e⎼☃▒┌:▒⎼d┤☃┼⎺↑└▒⎽├e⎼:42
⎽e┼⎽⎺⎼ ┌☃┴☃┼±↑⎼⎺⎺└↑b⎼☃±▒├┼e⎽⎽ ⎺┼ ⎽e⎼☃▒┌:▒⎼d┤☃┼⎺↑└▒⎽├e⎼:42
⎽e┼⎽⎺⎼ ┐☃├c▒e┼↑▒┤└☃d☃├≤ ⎺┼ ⎽e⎼☃▒┌:▒⎼d┤☃┼⎺↑└▒⎽├e⎼:43
⎽e┼⎽⎺⎼ ┐☃├c▒e┼↑├e└⎻e⎼▒├┤⎼e ⎺┼ ⎽e⎼☃▒┌:▒⎼d┤☃┼⎺↑└▒⎽├e⎼:43
⎽e┼⎽⎺⎼ ┐☃├c▒e┼↑└⎺├☃⎺┼ ⎺┼ ⎽e⎼☃▒┌:▒⎼d┤☃┼⎺↑└▒⎽├e⎼:43
⎽e┼⎽⎺⎼ ┐☃├c▒e┼↑b⎼☃±▒├┼e⎽⎽ ⎺┼ ⎽e⎼☃▒┌:▒⎼d┤☃┼⎺↑└▒⎽├e⎼:43
┌☃⎽├e┼☃┼± ⎺┼ ▒├├⎻://127↓▮↓▮↓1:5▮▮▮▮
⎽e⎼☃▒┌ ⎺⎻e┼ed: ▒⎼d┤☃┼⎺↑└▒⎽├e⎼
⎽e⎼☃▒┌ d☃⎽c⎺┴e⎼ed: /de┴/├├≤USB▮
⎽e⎼☃▒┌ e⎼⎼⎺⎼ c▒┼'├ ⎻▒⎼⎽e JSON: π"☃d":42←"b⎼☃±▒├┼e⎽⎽":126←"└⎺├☃⎺π"☃d":42←"b⎼☃±▒├┼e⎽⎽":125←"└⎺├☃⎺┼":▮←"├e└⎻e⎼▒├┤⎼e":23↓8▮←"▒┤└☃d☃├≤":29 S≤┼├▒│E⎼⎼⎺⎼: U┼e│⎻ec├ed ├⎺┐e┼ ☃
/#5┌9⎻PA°2P└_└┐c_WAAAA c⎺┼┼ec├ed
/#5┌9⎻PA°2P└_└┐c_WAAAA ▒┤├▒e┼├☃c▒├ed
/#5┌9⎻PA°2P└_└┐c_WAAAA d☃⎽c⎺┼┼ec├ed
/#▒▒73J1G⎺CK↑▒XdVbAAAB c⎺┼┼ec├ed
/#▒▒73J1G⎺CK↑▒XdVbAAAB ▒┤├▒e┼├☃c▒├ed
/#▒▒73J1G⎺CK↑▒XdVbAAAB d☃⎽c⎺┼┼ec├ed
/#├┌7⎺°_▒R6FDON─┐HAAAC c⎺┼┼ec├ed
/#├┌7⎺°_▒R6FDON─┐HAAAC ▒┤├▒e┼├☃c▒├ed
/#├┌7⎺°_▒R6FDON─┐HAAAC d☃⎽c⎺┼┼ec├ed
/#┤_Q┬├XdLW1└C▒Z⎻8AAAD c⎺┼┼ec├ed
/#┤_Q┬├XdLW1└C▒Z⎻8AAAD ▒┤├▒e┼├☃c▒├ed
/#┤_Q┬├XdLW1└C▒Z⎻8AAAD d☃⎽c⎺┼┼ec├ed
/#3☃┐R±°_⎼E⎻☃Q┼K┘▒AAAE c⎺┼┼ec├ed
/#3☃┐R±°_⎼E⎻☃Q┼K┘▒AAAE ▒┤├▒e┼├☃c▒├ed

screenshot

I know that this most likely can be fixed by just restarting that shell, but I still want to understand why this happens and (if possible) prevent it from happening again.

  • If this is considered a true duplicate of that cat binary question, then every off by one error question on stackoverflow should be marked as duplicate too... – Florian Wendelborn Apr 30 '16 at 10:27

2 Answers2

5

That happened because the output you produced included codes that your terminal interface interpreted as control codes.

This is normally resolved with either reset or stty sane.

  • Is there a way to only reset this for the next lines, without clearing the screen? I tried logging the \033c reset character, which resolves the weirdness, but also clears the screen. console.log("\x1Bc"); – Florian Wendelborn Apr 30 '16 at 00:27
  • @Dodekeract: My solution does exactly that. All the previous garbage remains on the screen. – Warren Young Apr 30 '16 at 00:28
  • @WarrenYoung well, I'd need to do that programmatically. If there is a control character that does what you're describing, that'd be a better solution for never running into that problem again. – Florian Wendelborn Apr 30 '16 at 00:30
  • @Dodekeract: Why "programmatically." Just type two control sequences at the terminal. Problem solved. Try it. – Warren Young Apr 30 '16 at 00:31
  • @WarrenYoung the thing is, when the project's code is finished, I'll most likely not look into the terminal for some weeks at a time. That way I won't notice when it breaks and later, when I need to figure some errors out, it'll be nearly impossible to read. – Florian Wendelborn Apr 30 '16 at 00:33
5

Another common remedy for this problem is to type Ctrl-VCtrl-O at the shell prompt. The first puts the shell into "literal" mode so that it won't modify the following character, which is the terminal reset command understood by almost all common terminal types.

You might need to echo this instead, on some terminals.

Warren Young
  • 72,032