NeoSWSerial.h help .available() not working

Hi! I am new to Arduino and I have an important project.

I tried to test by shorting the RX(pin19), TX(pin18), but still the portBARCODE.available() was still not working.

Can you correct my code if there is wrong?

Thank you so much...

Blessed be...

//INCLUDE
#include <NeoSWSerial.h>

//VARIABLE
NeoSWSerial portBARCODE(18,19);


char dataENQ = 0x05;
char dataACK = 0x06;
char recDATA = 0x00;

String stringDATA = "";
String txtSTATS = "";
bool sendSTATS; 

void setup() 
{
  //SETINGS
  pinMode(LED_BUILTIN, OUTPUT);
  
  // initialize serial:
  Serial.begin(9600);
  portBARCODE.begin(9600);

  Serial.println("Arduino Serial Begins...");
}

void loop() 
{
  portBARCODE.write(dataENQ);
  Serial.println("ENQ Sent");
  delay(3000);
}


void serialEvent() 
{
  while (portBARCODE.available())
  {
    recDATA = portBARCODE.read();
    portBARCODE.write(dataACK);
    Serial.println(dataACK);
    digitalWrite(LED_BUILTIN, HIGH);
  }
}

You don't have a need for a software serial implementation. The Mega has 3 additional hardware serial ports and you already are using the pins of one of them.

Use Serial1 for your setup.

I already modified my code based on your advice but still, Serial1.available() is not working.

//INCLUDE

char dataENQ = 0x05;
char dataACK = 0x06;
char recDATA = 0x00;

String stringDATA = "";
String txtSTATS = "";
bool sendSTATS; 

void setup() 
{
  //SETINGS
  pinMode(LED_BUILTIN, OUTPUT);
  
  // initialize serial:
  Serial.begin(9600);
  Serial1.begin(9600);

  Serial.println("Arduino Serial Begins...");
}

void loop() 
{
  Serial1.write(dataENQ);
  Serial.println("ENQ Sent");
  delay(3000);
}


void serialEvent() 
{
  while (Serial1.available())
  {
    recDATA = Serial1.read();
    Serial1.write(dataACK);
    Serial.println(dataACK);
    digitalWrite(LED_BUILTIN, HIGH);
  }
}

:cry:

void serialEvent()
{
  while (Serial1.available())

the serialEvent() function is called when there is data available on Serial
the serial1Event1() function is called when there is data available on Serial1

Note the names of the functions and the serial interfaces they relate to and look carefully at your code
See serialEvent() - Arduino Reference

Having said that, don't bother with the event() functions. Just test in loop() for data being available on the serial interface being used

NeoSWSerial portBARCODE(18,19);

I'm pretty sure it is defined as (RX,TX) not the other way around. You shouldn't be using swSerial anyway as was mentioned, and when you just connect TX to RX i suppose it doesn't really matter which is which. But anyway.

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