Software Serial code problem

Hello, i'm new to the arduino coding, and my graduation project is based on arduino and 2 modules GPS and GSM, i finished troubleshooting the GSM and GPS in standalone using TTL, both worked perfectly, also interfaced them with the microcontroller using pin 0 and 1 each module alone, now i need to connect both modules to the microcontroller, so im trying to create a new software serial port.

Now im trying to trouble shoot the software serial ports but im not reading any outputs, here is the code im using.

#include <SoftwareSerial.h>

char inbyte = 0;

SoftwareSerial SoftSer(5,4);

void setup(void)
{
  Serial.begin(2400);
  SoftSer.begin(2400);
}
void loop()
{
  if (SoftSer.available()) {
    inbyte = SoftSer.read();
    SoftSer.print(inbyte); 
  }
  

}

Hello, i'm new to the arduino coding, and my graduation project is based on arduino and 2 modules GPS and GSM, i finished troubleshooting the GSM and GPS in standalone using TTL, both worked perfectly, also interfaced them with the microcontroller using pin 0 and 1 each module alone, now i need to connect both modules to the microcontroller, so im trying to create a new software serial port.

Now im trying to trouble shoot the software serial ports but im not reading any outputs, here is the code im using.

#include <SoftwareSerial.h>

char inbyte = 0;

SoftwareSerial SoftSer(5,4);

void setup(void)
{
  Serial.begin(2400);
  SoftSer.begin(2400);
}
void loop()
{
  if (SoftSer.available()) {
    inbyte = SoftSer.read();
    SoftSer.print(inbyte); 
  }
  

}

Hi,
as it stands, your code just loop backs the characters from SoftSer to SoftSer, Serial is not involved and so you get no output. Are you using a Uno? I'm asking because some boards have a limited choice of pins for SoftwareSerial. I also assume that in your circuit pin 5 has the role of RX and pin 4 has the role of TX, is that correct?

[edit] Please do not cross post.

Thanks for the reply, if you mean by cross posting the double posts, it was a mistake the first one.

anyhow yes my board is UNO, also the default or what is called "Hardware serial" is pin 0,1... 4 and 5 is the desired software serial i want to create, i've read that any digital pin should do the job.

Shouldn't my code print the output on the TX? Isn't that the "print" command job? please correct me if i'm wrong.

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

  • Moderator

Why are you using Serial at all? You don't read from it or write to it.

moemzn:
Shouldn't my code print the output on the TX? Isn't that the "print" command job? please correct me if i'm wrong.

The problem is that you don't use Serial (the serial monitor) but just SoftSerial (the modem), both for read() and print(). So the modem is communicating with itself, and even if it worked perfectly you wouldn't see it. What you probably want to do is send commands from the serial monitor to the modem and send responses from the modem to the serial monitor, like this:

loop()
{
  if (Serial.available()) 
  {
    inbyte = Serial.read();
    SoftSer.print(inbyte);
  }
  if (SoftSer.available())
  {
    inbyte = SoftSer.read();
    Serial.print(inbyte);
  }
}

spatula:

moemzn:
Shouldn't my code print the output on the TX? Isn't that the "print" command job? please correct me if i'm wrong.

The problem is that you don't use Serial (the serial monitor) but just SoftSerial (the modem), both for read() and print(). So the modem is communicating with itself, and even if it worked perfectly you wouldn't see it. What you probably want to do is send commands from the serial monitor to the modem and send responses from the modem to the serial monitor, like this:

loop()

{
  if (Serial.available())
  {
    inbyte = Serial.read();
    SoftSer.print(inbyte);
  }
  if (SoftSer.available())
  {
    inbyte = SoftSer.read();
    Serial.print(inbyte);
  }
}

Ah great, i totally miss understood the "Serial" functions, now i'm aware of it, it worked after all, thanks alot!
Just one more question, if i connected both of the devices one to the hardware serial and the other to the software serial, do i need to use the interrupt to receive from one and hold hte other, or can i use both in the same time?
Also if "Serial" declare the serial monitor, how can i define the hardware serial?

Sorry, i should've delete it.

moemzn:
Just one more question, if i connected both of the devices one to the hardware serial and the other to the software serial, do i need to use the interrupt to receive from one and hold hte other, or can i use both in the same time?
Also if "Serial" declare the serial monitor, how can i define the hardware serial?

As for the interrupts, I wouldn't use them in your case because every time you send a command you have to wait for the response and read it (if you don't, the modem will queue its response, so you are going to get it sooner or later). More generally, interrupts are for fast responses to events, but serial communications (both hard and soft) are slow: at 9600 baud it takes slightly more than 1/1000 sec for a byte to be transmitted, which is a lot of time from the point of view of the processor. So there is plenty of time for processing both incoming and outgoing messages in the same loop().

Now that you know that the hardware is working you should look for code examples for sending and receiving entire messages (as strings, not just one character at a time).

As for Serial/HardwareSerial I feel I'm not very good at explaining it but I'll try. On the Uno you have a software class instance named "Serial". It references the hardware serial port of the Uno (the Usb port, and also pins RX and TX). Thus, "Serial" is actually the same as "HardwareSerial" (on the Arduino side), but it doesn't correspond to the serial monitor (which runs on the PC side). As a matter of fact, at the other end of the Usb connection you often have a PC and a serial monitor running on it. The Arduino only knows of its own Serial (same as HardwareSerial), while its association to the PC's serial monitor is only the usual setup.

Short answer: Serial is HardwareSerial; Serial is not serial monitor, it just talks to it.

... do i need to use the interrupt to receive from one and hold hte other, or can i use both in the same time?

Both hardware and software serial already use interrupts to receive data. It is placed in a buffer which you retrieve by polling for "if available".

You've explained it pretty well, i'll have to read more about those commands since the arduino site doesn't declare them clearly, thanks for the answer both, much appreciated, ill do some work and if i need any help ill come back to ask.

EasyTransfer and SoftEasyTransfer allow you to send data as a structure. Structure must be the same on both ends, however you can have two sets of structure so device one sends structure A and device two,three, four, etc. receive structure A. Devices two, three, four, etc, would send structure B and device one would receive structure B. This is somewhat restrictive if you have devices that send out differing structures.