2013-05-29 64 views
1

我是netduino的新手,所以我有一個簡單的問題(或者它應該很簡單)。 我想做的是從我的winform應用程序發送一個整數(字符串)從我的winform應用程序到我的netduino加2,然後,我的netduino應該讀取該整數,並在板上多次閃爍。與PC的Netduino通信

我已閱讀過該主題的在線教程,並找到了一些應該提供我的PC和Netduino之間通信的示例。

是的,我確實收到了迴音。即使斷開我的netduino並將其隱藏在我的口袋裏,我也會收到回聲:)。 對於我對該小工具的理解非常多。

如何通過rs232電纜將信息發送至我的Netduino,以便他可以閱讀,理解並採取相應措施?

有一個代碼,直接從網頁:

對於NETDUINO:

using System; 
using System.Net; 
using System.Net.Sockets; 
using System.Threading; 
using Microsoft.SPOT; 
using Microsoft.SPOT.Hardware; 
using SecretLabs.NETMF.Hardware; 
using SecretLabs.NETMF.Hardware.Netduino; 
using System.IO.Ports; 

namespace NetduinoApplication1 
{ 
public class Program 
{ 
    static SerialPort serial; 

    public static void Main() 
    { 
     // initialize the serial port for COM1 (using D0 & D1) 
     serial = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One); 
     // open the serial-port, so we can send & receive data 
     serial.Open(); 
     // add an event-handler for handling incoming data 
     serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived); 
     OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); 
     for (int i = 0; i < 3; i++) 
     { 
      led.Write(true); // turn on the LED 
      Thread.Sleep(250); // sleep for 250ms 
      led.Write(false); // turn off the LED 
      Thread.Sleep(250); // sleep for 250ms 

     } 

     // wait forever... 
     Thread.Sleep(Timeout.Infinite); 
    } 

    static void serial_DataReceived(object sender, SerialDataReceivedEventArgs e) 
    { 

     // create a single byte array 
     byte[] bytes = new byte[1]; 

     // as long as there is data waiting to be read 
     while (serial.BytesToRead > 0) 
     { 
      // read a single byte 
      serial.Read(bytes, 0, bytes.Length); 
      // send the same byte back 

      serial.Write(bytes, 0, bytes.Length); 
      OutputPort led1 = new OutputPort(Pins.ONBOARD_LED, false); 
      led1.Write(true); // turn on the LED 
      Thread.Sleep(250); // sleep for 250ms 
      led1.Write(false); // turn off the LED 
      Thread.Sleep(250); // sleep for 250ms 

     } 

    } 

} 
} 

而對於我的控制檯代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO.Ports; 

namespace ConsoleRSS 
{ 
class Program 
{ 
    static SerialPort serial; 
    static void Main(string[] args) 
    { 
     // provide some usage information 
     System.Console.WriteLine("enter some text and hit ENTER."); 
     System.Console.WriteLine("enter 'x' and hit ENTER to exit."); 
     System.Console.WriteLine(); 

     // initialize the serial port for COM3 (could be other port, depends on system) 
     serial = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One); 
     // open the serial-port, so we can send & receive data 
     serial.Open(); 
     // add an event-handler for handling incoming data 
     serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived); 

     // this will hold each line entered 
     string line = string.Empty; 

     // as long as an x is not entered 
     while (line.ToLowerInvariant() != "x") 
     { 
      // read a single line from the console 
      line = System.Console.ReadLine(); 

      // convert the line to bytes 
      byte[] utf8Bytes = System.Text.Encoding.UTF8.GetBytes(line); 

      // send the bytes over the serial-port 
      serial.Write(utf8Bytes, 0, utf8Bytes.Length); 
     } 
    } 
    static void serial_DataReceived(object sender, SerialDataReceivedEventArgs e) 
    { 
     // wait a little for the buffer to fill 
     System.Threading.Thread.Sleep(100); 

     // create an array for the incoming bytes 
     byte[] bytes = new byte[serial.BytesToRead]; 
     // read the bytes 
     serial.Read(bytes, 0, bytes.Length); 
     // convert the bytes into a string 
     string line = System.Text.Encoding.UTF8.GetString(bytes); 

     // write the received bytes, as a string, to the console 
     System.Console.WriteLine("echo: " + line); 
     System.Console.WriteLine(); 
    } 

} 
} 
+0

你可以顯示一些你嘗試過的東西的代碼嗎?這是你嘗試過的例子嗎? http://www.instructables.com/id/Serial-Port-Programming-With-NET/ – tay10r

+0

也許你和這篇文章的作者:http://stackoverflow.com/questions/16623915/how-to-send- a-signal-to-run-a-machine-using-netduino should talk :)很相似的問題...... – user2019047

回答

0

我不知道是什麼問題,你實際上是用這個嗎?乍一看你的代碼看起來是正確的。

我會試着先檢查你的硬件,那麼USB設備管理器中的USB連接線實際上是否設置爲你的電腦?電纜是否連接到netduino中的正確端口?