Connect Weight Balance to Thermal Printer Through Arduino Uno

I'm taking in data from my Mettler Toledo weight balance and I want to print it to my thermal printer. I'm using Adafruit Thermal Printer Library to connect to my thermal printer. I'm using a brandless China thermal printer.

In the "showNewData()" function, I have "printer.println(receivedChars);" to print out the received data.

I encountered a problem where, if this printing command is not in my code, I receive the data perfectly. But if the printing line is in my code, some part of the data is not sent immediately, but combined with the next data I send.

#include "Adafruit_Thermal.h"
#include "SoftwareSerial.h"
#define TX_PIN 6 // Arduino transmit  YELLOW WIRE  labeled RX on printer
#define RX_PIN 5 // Arduino receive   GREEN WIRE   labeled TX on printer
SoftwareSerial mySerial(RX_PIN, TX_PIN); 
Adafruit_Thermal printer(&mySerial);

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
    mySerial.begin(9600);  // Initialize SoftwareSerial
    printer.begin(); 
}

void loop() {
    recvWithEndMarker();
    showNewData();
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
    
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        printer.println(receivedChars);
        newData = false;
    }
}

I read through the original source code of Adafruit Thermal Library and still have no clue on what happened in "printer.println()".

Thanks everyone.

How have you connected things up.

I can see you have a software serial port to communicate with the printer, but I don't see a serial port for communicating with your balance. It looks like you are using the hardware serial port for both standard serial comms to the IDE and to receive data from your balance?

Hi Mark,

You are right. I'm using the hardware serial port (TX/RX).

In short, 9pin RS232 from balance ---> RS232 to TTL/UART converter ---> arduino hardware serial port

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.