I can't seem to get Serial Communication with USB working. To clarify my problem, here is the code on my netduino 3
public class Program { static SerialPort _serial; private static Lcd _lcd; public static void Main() { _serial = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One); _serial.Open(); _serial.DataReceived += _serial_DataReceived; var lcdProvider = new GpioLcdTransferProvider(Pins.GPIO_PIN_D12, // RS Pins.GPIO_PIN_D11, // ENABLE Pins.GPIO_PIN_D5, // DB4 Pins.GPIO_PIN_D4, // DB5 Pins.GPIO_PIN_D3, // DB6 Pins.GPIO_PIN_D2); // DB7 _lcd = new Lcd(lcdProvider); _lcd.Begin(20, 2); Thread.Sleep(100); _lcd.Clear(); _lcd.SetCursorPosition(0, 1); Thread.Sleep(100); _lcd.Write("Initing..."); Thread.Sleep(100); _lcd.Clear(); _lcd.SetCursorPosition(0, 1); _lcd.Write("Waiting..."); Thread.Sleep(500); while (true) { _lcd.SetCursorPosition(0, 0); _lcd.Clear(); _lcd.Write(_serial.IsOpen.ToString()); Thread.Sleep(1000); } //Thread.Sleep(Timeout.Infinite); } private static void _serial_DataReceived(object sender, SerialDataReceivedEventArgs e) { _lcd.Clear(); _lcd.Write("Connected"); // 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); string data = Convert.ToBase64String(bytes); _lcd.Write(data); } } }
and this is the code on the PC application that's running:
public partial class MainWindow : Window { private SerialPort serial; public MainWindow() { InitializeComponent(); AssettoCorsa assettoCorsa = new AssettoCorsa(); assettoCorsa.PhysicsUpdated += AssettoCorsa_PhysicsUpdated; serial = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); serial.DataReceived += Serial_DataReceived; serial.Open(); assettoCorsa.Start(); } private 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(); } private void AssettoCorsa_PhysicsUpdated(object sender, PhysicsEventArgs e) { Console.WriteLine(e.Physics.Gear + @"-------" + e.Physics.SpeedKmh); if (serial.IsOpen) { serial.WriteLine(e.Physics.SpeedKmh.ToString(CultureInfo.InvariantCulture)); } } }
But when I send data, nothing happens on the Netduino. The ports are open though