[英]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. 我正在尝试从C#应用程序中的Arduino和GSM模块发送短信。 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:
我的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;
And my Arduino Code is: 我的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);
}
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"?? 如何获取正确的字符串而不是“ 0000000000000”?
In your c# code you write only 5 characters to the serial port: 在您的C#代码中,您仅向串行端口写入5个字符:
currentPort.Write(buf, 0, 5);
But in arduino you expect 13 bytes: 但是在arduino中,您希望有13个字节:
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. 此条件永远不会满足,因此将跳过“ if-then”语句,并且不会初始化变量inputByte_0-inputByte_12。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.