我是netduino的新手所以我有一个简单的问题(或者,它应该很简单)。 我想要做的是通过rs232从我的winform应用程序发送一个整数(字符串)到我的netduino plus 2,然后,我的netduino应该读取该整数并闪烁多次onboard。
我已经阅读了有关该主题的在线教程,并找到了一些可以在我的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 :(得分:0)
我不确定你实际上遇到了什么问题?乍一看你的代码看起来是正确的。
我会首先尝试检查你的硬件,所以你的电脑上的设备管理器中的usb电缆实际设置为COM 1吗?电缆是否连接到netduino中的正确端口?