Arduino Serial Communication To Visual Studio C#

hy guys

I am working on an university project, and I’m looking for some guidance on a problem I am encountering. Basically, I am trying to read data via a receiver radio connected to an Arduino Mega. The data is being transmitted via a transmitter radio connected to my laptop.

The telemetry are synced (solid green light and same baud rate of 9600) and I know there is some communication. I used lcd to see what receiver get.

The setup is as follows: The transmitter radio is connected to my laptop via USB cable. There, I can use any serial port emulator to write data for transmitting. This works without any issue. On the other side, I have the receiver radio connected to my Arduino board. Radio RX and TX goes to Arduino RX and TX, respectively. If i use Serial0 it work but if i use Serial1 it not work anymore, however, as I transmit data via the transmitter, the receiver does not respond. Thus, the output from the Arduino is always -1.

Here is the code I’m using or the receiver radio:

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>

//SoftwareSerial ss(1, 0);

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
String commandString = "";

int led1Pin = 19;
int led2Pin = 20;
int led3Pin = 21;

boolean isConnected = false;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 


void setup() {
  
  Serial.begin(9600);
  Serial1.begin(57600);
  pinMode(led1Pin,OUTPUT);
  pinMode(led2Pin,OUTPUT);
  pinMode(led3Pin,OUTPUT);
  initDisplay();
}

void loop() {

Serial1.print(random(0,100));

if(stringComplete)
{
  stringComplete = false;
  getCommand();
  
  if(commandString.equals("STAR"))
  {
    lcd.clear();
  }
  if(commandString.equals("STOP"))
  {
    turnLedOff(led1Pin);
    turnLedOff(led2Pin);
    turnLedOff(led3Pin);
    lcd.clear();
    lcd.print("Ready to connect");    
  }
  else if(commandString.equals("TEXT"))
  {
    String text = getTextToPrint();
    printText(text);
  }
  else if(commandString.equals("LED1"))
  {
    boolean LedState = getLedState();
    if(LedState == true)
    {
      turnLedOn(led1Pin);
    }else
    {
      turnLedOff(led1Pin);
    }   
  }
    else if(commandString.equals("LED2"))
  {
    boolean LedState = getLedState();
    if(LedState == true)
    {
      turnLedOn(led2Pin);
    }else
    {
      turnLedOff(led2Pin);
    }   
  }
    else if(commandString.equals("LED3"))
  {
    boolean LedState = getLedState();
    if(LedState == true)
    {
      turnLedOn(led3Pin);
    }else
    {
      turnLedOff(led3Pin);
    }   
  }
  
  inputString = "";
}

}

void initDisplay()
{
  lcd.begin(16, 2);
  lcd.print("Ready to connect");
}

boolean getLedState()
{
  boolean state = false;
  if(inputString.substring(5,7).equals("ON"))
  {
    state = true;
  }else
  {
    state = false;
  }
  return state;
}

void getCommand()
{
  if(inputString.length()>0)
  {
     commandString = inputString.substring(1,5);
  }
}

void turnLedOn(int pin)
{
  digitalWrite(pin,HIGH);
}

void turnLedOff(int pin)
{
  digitalWrite(pin,LOW);
}


String getTextToPrint()
{
  String value = inputString.substring(5,inputString.length()-2);
  return value;
}

void printText(String text)
{
  lcd.clear();
  lcd.setCursor(0,0);
    if(text.length()<16)
    {
      lcd.print(text);
    }else
    {
      lcd.print(text.substring(0,16));
      lcd.setCursor(0,1);
      lcd.print(text.substring(16,32));
      
    }
}

void serialEvent() {
  while (Serial1.available()) {
    // get the new byte:
    char inChar = (char)Serial1.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

Plis Help Me !!

If the only difference in the working and non-working code is your choice of UART, you probably have a hardware problem. Double check your serial connections to Serial1 on the Mega.

Duplicate post