在 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 没有被初始化。
我正在尝试从 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 没有被初始化。