簡體   English   中英

在C#中向Arduino發送和接收字符串中的類型轉換

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

我正在嘗試從C#應用程序中的Arduino和GSM模塊發送短信。 我已經成功發送和接收了數據,但是問題出在我接收的字符串中,該字符串不是我發送的格式。 我的C#代碼是:

        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;

我的Arduino代碼是:

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);



        }

現在我收到的字符串是

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

但我想要

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

如何獲取正確的字符串而不是“ 0000000000000”?

在您的C#代碼中,您僅向串行端口寫入5個字符:

 currentPort.Write(buf, 0, 5);

但是在arduino中,您希望有13個字節:

if (Serial.available() == 13)

此條件永遠不會滿足,因此將跳過“ if-then”語句,並且不會初始化變量inputByte_0-inputByte_12。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM