Barcode won't be send to nextion

Hello all,

I have a problem with my coding i can't get to work.
I'm making a barcode scanner to count the inventory in our stores.
I got everything to work except for the barcode that is read to send to the Nextion.
I see the barcode coming in on the serial monitor, and it is sending the code to the t2.txt, but it doesn't appear on the Nextion.
If i get rid of the barcode coding in the loop and just send a text in the loop it does work.

Is there anyone that knows what I'm doing wrong?

Here is the code:

#include <SoftwareSerial.h>
#include <Nextion.h>
#include <SPI.h>
#include <SD.h>


SoftwareSerial Barcode(5, 6); // RX, TX
NexText t0 = NexText(1, 4, "t0");  // Text box added, so we can read it
NexText t2 = NexText(1, 15, "t2");  // Text box added, so we can read it
NexButton b0 = NexButton(1, 2, "b0");  // Button added
// End of declaring objects

File myFile;

char buffer[100] = {0};  // This is needed only if you are going to receive a text from the display. You can remove it otherwise.
                         // Further on this sketch I do receive text so that's why I created this buffer.

NexTouch *nex_listen_list[] = 
{
  &b0,  // Button added
  NULL  // String terminated
};  // End of touch event list


void b0PushCallback(void *ptr)  // Press event for "Send" button on page 2
{
  memset(buffer, 0, sizeof(buffer));  // Clear the buffer, so we can start using it
  t2.getText(buffer, sizeof(buffer));  // Read the text on the object t0 and store it on the buffer

  myFile = SD.open("test.txt", FILE_WRITE);
  if (myFile) {
    Serial.print("Writing to test.txt...");
  myFile.print(buffer);  // This is the text you want to send to that object and atribute mentioned before.
  Serial.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  Serial.write(0xff);
  Serial.write(0xff);
  myFile.print(",");
  
  memset(buffer, 0, sizeof(buffer));  // Clear the buffer, so we can start using it
  t0.getText(buffer, sizeof(buffer));  // Read the text on the object t0 and store it on the buffer
  myFile.println(buffer);  // This is the text you want to send to that object and atribute mentioned before.
  Serial.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  Serial.write(0xff);
  Serial.write(0xff);
      myFile.close();
              // Send a text to the object called t2:
    Serial.print("t2.txt=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
    Serial.print("\"");  // Since we are sending text, and not a number, we need to send double quote before and after the actual text.
    Serial.print("opgeslagen");  // This is the text you want to send to that object and atribute mentioned before.
    Serial.print("\"");  // Since we are sending text, and not a number, we need to send double quote before and after the actual text.
    Serial.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
    Serial.write(0xff);
    Serial.write(0xff);
    delay (1500);
        // Send a text to the object called t2:
    Serial.print("t2.txt=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
    Serial.print("\"");  // Since we are sending text, and not a number, we need to send double quote before and after the actual text.
    Serial.print("apparaat");  // This is the text you want to send to that object and atribute mentioned before.
    Serial.print("\"");  // Since we are sending text, and not a number, we need to send double quote before and after the actual text.
    Serial.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
    Serial.write(0xff);
    Serial.write(0xff);
  } else {
    // if the file didn't open, print an error:
        // Send a text to the object called t2:
    Serial.print("t2.txt=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
    Serial.print("\"");  // Since we are sending text, and not a number, we need to send double quote before and after the actual text.
    Serial.print("Fout bij opslaan");  // This is the text you want to send to that object and atribute mentioned before.
    Serial.print("\"");  // Since we are sending text, and not a number, we need to send double quote before and after the actual text.
    Serial.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
    Serial.write(0xff);
    Serial.write(0xff);
  }
}

void setup() {  // Put your setup code here, to run once:
  
  Serial.begin(9600);  // Start serial comunication at baud=9600
  Barcode.begin(9600); // set the data rate for the SoftwareSerial port
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  {
  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  // Register the event callback functions of each touch event:
  b0.attachPush(b0PushCallback);  // Button press
  // End of registering the event callback functions
}  // End of setup
}

void loop() {  // Put your main code here, to run repeatedly:

  nexLoop(nex_listen_list);  // Check for any touch event

  if(Barcode.available()) // Check if there is Incoming Data in the Serial Buffer.
  {
//    count = 0; // Reset count to zero
    char myBarcode[80];  // large enough for the biggest barcode you will read
    int idx = 0;
    while (Barcode.available()) // Keep reading Byte by Byte from the Buffer till the Buffer is empty
    {
      char input = Barcode.read(); // Read 1 Byte of data and store it in a character variable
      Serial.print(input); // Print the Byte
      myBarcode[idx++] = input;  // save data
      delay(5); // A small delay
    }
     myBarcode[idx] = '\0';  // terminate string
      delay(5); // A small delay

    Serial.print("t2.txt=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
    Serial.print("\"");  // Since we are sending text, and not a number, we need to send double quote before and after the actual text.
    Serial.print(myBarcode);  // This is the text you want to send to that object and atribute mentioned before.
    Serial.print("\"");  // Since we are sending text, and not a number, we need to send double quote before and after the actual text.
    Serial.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
    Serial.write(0xff);
    Serial.write(0xff);
  }
}  // End of loop

what does "sending the code" mean? Are you trying to send black vertical lines some lines thicker some lines thinner to a textfield?

You should at least post what kind of data you are sending that does not appear on the nextion-textfield and the data that does appear on the nextion-textfield

If you modified the code for that
post the comlete sketch that does

and as a new second code-section post the code that

And as your code is pretty big point your potential helpers to that part of your code that does sending the data towards the nextion-display

best regards Stefan

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