2016-10-15 2 views
0

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! 

답변

0

이유는 확실하지 않습니다. 하지만 문자열 끝에 \ 0을 추가하려고 시도했을 때 작동했습니다.

 try 
     { 
      mySerialPort.WriteLine("Sent from my c# app! \0"); 
     } 
0

에서 \\는 문자열 터미네이터 문자입니다. 그것 없이는 문자 배열이 끝났음을 알 수 없습니다.

+0

나는 이것이 C#임을 안다. 나는 C# 함수가 어쨌든 보내 줄 것이라고 생각했다. – Jacob

+1

C#은 문자열 터미네이터의 전제를 포함하지 않습니다. 일부는 다른 방법으로 객체의 길이 정보도 포함하므로 종료 문자가 필요하지 않으며 imho는 혼란을 일으 킵니다. 따라서 관리되지 않는 코드의 경우 이진 null 문자가 전송에 추가되어야하므로 명시 적으로 추가해야합니다. – axa

+0

위의 오타 때문에 죄송합니다. 분명히 메모를 수정할 수 없습니다. 스트링스 (#)가 다른 방식으로 작동한다는 의미이고, 비 관리 코드 인 경우 ... – axa

0

mySerialPort.WriteLine 방법을 사용하면 지정된 문자열 + 귀하의 경우 기본값 인 mySerialPort.NewLine 값을 보낼 것 - System.Environment.NewLine을 ("\r\n"이다).

전에 WriteLine (도 ReadLine가)의 사용 - 귀하의 경우, 프로토콜 EOL 문자를 지정

mySerialPort.NewLine = "\0" 

와 쓰기 각각 수동으로 EOL 문자를 추가 할 필요가 없을 것입니다 (그리고에 그냥 Write 이상의 WriteLine의 목적을 놓치지)

관련 문제