C# 응용 프로그램에서 직렬 포트의 arduino로 메시지를 보내려고합니다.Serial.WriteLine ("내 메시지!"); 첨부?
그러나 WriteLine에는 응답하지 않습니다. 결코 끝나지 않습니다. 그리고 arduino의 버퍼에 저장된 내용을 읽을 때 100 번 이상 반복하여 보내고있는 것과 같습니다. C#을 응용 프로그램에
코드 (참조 및 통관에 대한) 아두 이노에
public void testSend()
{
if (mySerialPort.IsOpen)
{
//setup
//mySerialPort.Open();
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.RtsEnable = true;
mySerialPort.WriteTimeout = 500;
try
{
mySerialPort.WriteLine("Sent from my c# app!");
}
catch(TimeoutException)
{
//Console.WriteLine("Timeout while sending data");
}
//mySerialPort.Close();
}
else
{
Console.WriteLine("Port already open!");
}
}
코드
void setup()
{
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial)
{
; // wait for serial port to connect. Needed for native USB
}
}
char* buf = malloc(1024);
int ReciveData()
{
if (Serial.available())
{
// read the incoming bytes:
String temp = Serial.readString();
if (temp.length() > 0)
{
temp.toCharArray(buf, temp.length() + 1);
}
}
}
void loop()
{
Serial.print("Sent from arduino!");
Serial.println(buf);
delay(1000);
ReciveData();
}
}
이 보이는 방법이다. 여기에 4 개의 메시지가 있으며, "Sud from arduino!"라는 문자로 시작됩니다. 내가 그것을 읽을 때. 1 호선과 2 호선 모두 좋지만 C# 응용 프로그램을 시작하면 하와이가됩니다.
Sent from arduino!Sent from serial terminal!
Sent from arduino!Sent from serial terminal!
Sent from arduino!Sent from my c# app!
Sent from my c# app!
Sent from my c# app!
Sent from my c# app!
Sent from my c# app!
Sent from my c# app!
Sent from my c# app!
Sent from my c# app!
Sent from my c# app!
Sent from my c# app!
Sent from my c# app!
Sent from arduino!Sent from my c# app!
Sent from my c# app!
Sent from my c# app!
Sent from my c# app!
Sent from my c# app!
Sent from my c# app!
Sent from my c# app!
Sent from my c# app!
Sent from my c# app!
Sent from my c# app!
Sent from my c# app!
나는 이것이 C#임을 안다. 나는 C# 함수가 어쨌든 보내 줄 것이라고 생각했다. – Jacob
C#은 문자열 터미네이터의 전제를 포함하지 않습니다. 일부는 다른 방법으로 객체의 길이 정보도 포함하므로 종료 문자가 필요하지 않으며 imho는 혼란을 일으 킵니다. 따라서 관리되지 않는 코드의 경우 이진 null 문자가 전송에 추가되어야하므로 명시 적으로 추가해야합니다. – axa
위의 오타 때문에 죄송합니다. 분명히 메모를 수정할 수 없습니다. 스트링스 (#)가 다른 방식으로 작동한다는 의미이고, 비 관리 코드 인 경우 ... – axa