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.