5

I think I missed something in my code.

Below is my code in C#

public Form1()
    {
        InitializeComponent();
        serialPort1.PortName = "COM2";
        serialPort1.BaudRate = 9600;
        serialPort1.Parity = Parity.None;
        serialPort1.DataBits = 8;
        serialPort1.StopBits = StopBits.One;
        serialPort1.Handshake = Handshake.RequestToSend;
        serialPort1.DtrEnable = true;
        serialPort1.RtsEnable = true;
        serialPort1.NewLine = System.Environment.NewLine;

    }

    private void btnSend_Click(object sender, EventArgs e)
    {
        serialPort1.Open();

    }

    private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        try
        {                
            string num = "+639952006630\n";
            serialPort1.Write(num);        

            string message = "Your child arrived at our school at " + DateTime.Now + ".";
            serialPort1.Write(message);                
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }                        
    }

Below is my code in Arduino for sending data using GSM module SIM800L

    #include <SoftwareSerial.h>

//SIM800 TX is connected to Arduino D8
#define SIM800_TX_PIN 8

//SIM800 RX is connected to Arduino D7
#define SIM800_RX_PIN 7

//Create software serial object to communicate with SIM800
SoftwareSerial serialSIM800(SIM800_TX_PIN,SIM800_RX_PIN);

void setup() {
  //Begin serial comunication with Arduino and Arduino IDE (Serial Monitor)
  Serial.begin(9600);
  while(!Serial);

  //Being serial communication with Arduino and SIM800
  serialSIM800.begin(9600);
  delay(1000);

  //Set SMS format to ASCII
  serialSIM800.write("AT+CMGF=1\r\n");
  delay(1000);

  //getting the number  
  char remoteNum[20];  // telephone number to send sms
  readSerial(remoteNum);



  //Send new SMS command and message number      
  serialSIM800.print("AT+CMGS=\"");
  serialSIM800.print(remoteNum);
  serialSIM800.print("\"\r\n");  
  delay(1000);

   // getting sms text  
  char txtMsg[200];
  readSerial(txtMsg); 

  //Send SMS content
  serialSIM800.print(txtMsg);  
  delay(1000);

  //Send Ctrl+Z / ESC to denote SMS message is complete
  serialSIM800.write((char)26);
  delay(1000);

  Serial.println("SMS Sent!");
}

 /*
  Read input serial
 */
char readSerial(char result[]) {
  int i = 0;
  while (1) {
    while (Serial.available() > 0) {
      char inChar = Serial.read();
      if (inChar == '\n') {
        result[i] = '\0';
        Serial.flush();
        return 0;
      }
      if (inChar != '\r') {
        result[i] = inChar;
        i++;
      }
    }
  }
}
void loop() {
}

My confusion/question here is

Whenever I test it using Serial Monitor in Arduino, the code in Arduino sends message to the cellular number successful. But when I use a form in Visual Studio using C#, nothing happens. There are also no errors that appear. I tried F11 also to know if I am missing an error, still I see nothing. But the application does not send SMS to the number.

Help from you guys is very much appreciated. Thank you in advance.

Saucy Goat
  • 1,587
  • 1
  • 11
  • 32

2 Answers2

0

The solution is as obvious as simple. You have all your routines in the Arduino setup which runs only once. So sending one sms from Arduino directly works exactly 1 time.
When connecting via c code, the Arduino is already booted and in the EMPTY loop. So the chance to hit the one time run code is zero.
Put your routines especially

   readSerial(remoteNum);

in the loop and it should work as expected.

Codebreaker007
  • 2,911
  • 1
  • 10
  • 22
0

The first thing you should do before sending the commands with your Visual Studio is that the serialport is correctly configured, you simply have the serial port closed loop, everything you send should receive, If you do not receive anything you have a serial port configuration problem

DarioG
  • 83
  • 7