简体   繁体   中英

Type conversion in Sending and Receiving a string to Arduino in C#

I am trying to send an SMS from Arduino and GSM module in C# application. I have successfully sent and received the data but the problem lies in my received string which is not in the format I sent. My C# code is:

        string mystr = "012345678900"

            char [] buf = new char[13];
            buf = mystr.ToCharArray();
            int intReturnASCII = 0;
            char charReturnValue = (Char)intReturnASCII;
            currentPort.Open();
            currentPort.Write(buf, 0, 5);
            Thread.Sleep(7500);
            int count = currentPort.BytesToRead;
            string returnMessage = "";
            while (count > 0)
            {
                intReturnASCII = currentPort.ReadByte();
                returnMessage = returnMessage + Convert.ToChar(intReturnASCII);
                count--;
            }
            currentPort.Close();

            str = returnMessage;

And my Arduino Code is:

int ledPin_3 = 13;
byte inputByte_0;
byte inputByte_1;
byte inputByte_2;
byte inputByte_3;
byte inputByte_4;
byte inputByte_5;
byte inputByte_6;
byte inputByte_7;
byte inputByte_8;
byte inputByte_9;
byte inputByte_10;
byte inputByte_11;
byte inputByte_12;

void setup() {

  pinMode(ledPin_3, OUTPUT);
  Serial.begin(9600);
  digitalWrite(ledPin_3, HIGH);
  delay(250);
  digitalWrite(ledPin_3, LOW);
  delay(250);
}

void loop() {

  if (Serial.available() == 13) 
  {
    inputByte_0 = Serial.read();
    delay(100);    
    inputByte_1 = Serial.read();
    delay(100);      
    inputByte_2 = Serial.read();
    delay(100);      
    inputByte_3 = Serial.read();
    delay(100);
    inputByte_4 = Serial.read();
    delay(100); 
    inputByte_5 = Serial.read();
    delay(100);  
    inputByte_6 = Serial.read();
    delay(100);
    inputByte_7 = Serial.read();
    delay(100);
    inputByte_8 = Serial.read();
    delay(100);
    inputByte_9 = Serial.read();
    delay(100);
    inputByte_10 = Serial.read();
    delay(100);
    inputByte_11 = Serial.read();
    delay(100);
    inputByte_12 = Serial.read();
    delay(100);
  }
            delay(1200);
            Serial.print("AT");
            delay(1200);

            int index = 0;
            Serial.println();
            Serial.println("AT+CMGF=1"); 
            delay(100);
            delay(1200);

            Serial.println();
            Serial.print("AT+CMGS=\""); 
            Serial.print(inputByte_0);
            Serial.print(inputByte_1);
            Serial.print(inputByte_2);
            Serial.print(inputByte_3);
            Serial.print(inputByte_4);
            Serial.print(inputByte_5);
            Serial.print(inputByte_6);
            Serial.print(inputByte_7);
            Serial.print(inputByte_8);
            Serial.print(inputByte_9);
            Serial.print(inputByte_10);
            Serial.print(inputByte_11);
            Serial.print(inputByte_12);

            Serial.println("\"");
            delay(1000);
            Serial.print("message from GSM");

            delay(500);

            Serial.write(0x1A);
            Serial.write(0x0D);
            Serial.write(0x0A);



        }

Now the string I received is

AT AT+CMGF=1 AT+CMGS="0000000000000"

But I want

AT AT+CMGF=1 AT+CMGS="012345678900"

How to get the right string back instead of "0000000000000"??

In your c# code you write only 5 characters to the serial port:

 currentPort.Write(buf, 0, 5);

But in arduino you expect 13 bytes:

if (Serial.available() == 13)

This condition will never met, therefore the "if-then" statement is skipped and the variables inputByte_0 - inputByte_12 are not initialiazed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM