1

Here is my Arduino sketch:

#include <HX711.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
HX711 hx(9, 10, 128, 0.002229);
LiquidCrystal_I2C lcd(0x27,16,2);

void setup() {

  Serial.begin(9600);
  hx.set_offset(-9635);
  lcd.init();                      // initialize the lcd 
  lcd.backlight();
  lcd.begin(16,2);

}
void loop() {
  if(digitalRead(0) == LOW){
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("tare recieved");
    hx.tare();
  }

  double sum = 0;
  for (int i = 0; i < 10; i++) {
    sum += hx.bias_read();
    if(sum < 0) {
      sum = 0.00;
    }

  }
  sum = sum/10;
  lcd.clear();
  lcd.setCursor(4,0);
  lcd.print("Weight");
  lcd.setCursor(5, 1);
  lcd.print(sum);
  lcd.print("g");
  Serial.println(sum);
  delay(10);
 }

Here is an example output as seen on the Arduino Serial Monitor:

150.34
150.39
150.40
150.37
150.38

The Problem is when the data reaches node-serialport the value is split up on multiple lines.

 comport.on("data", function (data) {
    console.log(data.toString());
 }

When i log the data... it comes back like this:

1
50.4
5
150.
49
15
0.47
150.
48
15
0.45

Is there anyway to have Arduino only send the data if it is the full value and not the split up values?

  • There is no such thing as "the full value". Serial works in characters. That's what serial means. It's up to you at the receiving end to combine the individual characters up into full strings. – Majenko Apr 29 '18 at 18:03
2

As explained in Majenko’s comment, the serial link is not packetized: it transmits individual bytes and nothing else. If you need to convey a notion of “data packet”, or “full message”, you will have to devise a way to signal the message boundaries. This is called message “framing”.

Your Arduino is sending ASCII data. In this case, the most common way to frame messages is linewise: each message is a single line, and each line is terminated by an “end of line” sequence. The standard end of line sequences are CR (carriage return, i.e. 0x0d or '\r'), LF (line feed, i.e. 0x0a or '\n'), and the pair CRLF (CR followed by LF, or "\r\n").

Actually you are already doing that: you are using Serial.println(), and this method automatically appends CRLF at the end of whatever it prints. So now you just have to parse that at the other end: you put all the incoming characters into a buffer until you see that incoming CRLF. Then you know you have a full line, which you can report as an entire message.

This procedure is very common and there is a standard Node.js module for it. It once was part of the serialport module, but it seems to have split into a separate module. It is called parser-readline, and you would use it like this:

const Readline = require("parser-readline"),
      parser = comport.pipe(new Readline({ delimiter: "\r\n" }));

parser.on("data", data => console.log(data.toString()));

C.f. the documentation of ReadLineParser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.