I have a Netduino and a Netduino Plus that I’ve connected to RS485 transceivers in an attempt to test serial communication between the two. My wiring may well be wrong but I’m just trying to eliminate coding errors first.
The tranceiver requires 3 pins which map to RX, TX and RTS on the netduino I believe. I have connected these to digital pins 2, 3 and 7. I assume this maps to COM2.
The netduino plus attempts to send a few bytes when the button is pressed:
class Program { static SerialPort m_port; static void Main() { m_port = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One); m_port.Handshake = Handshake.RequestToSend; m_port.DataReceived += OnDataReceived; m_port.ErrorReceived += ErrorReceived; m_port.Open(); InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth); button.OnInterrupt += OnButton; Thread.Sleep(Timeout.Infinite); } static void ErrorReceived(object sender, SerialErrorReceivedEventArgs args) { Debug.Print("error"); } static void OnDataReceived(object sender, SerialDataReceivedEventArgs args) { Debug.Print("data"); } static void OnButton(uint data1, uint data2, DateTime time) { if (0 == data2) { var bytes = Encoding.UTF8.GetBytes("hello from netduino plus"); int count = m_port.Write(bytes, 0, bytes.Length); Debug.Assert(bytes.Length == count); m_port.Flush(); } } }
The netduino on the other end simply waits to receive data:
class Program { static SerialPort m_port; static void Main() { m_port = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One); m_port.Handshake = Handshake.RequestToSend; m_port.DataReceived += OnDataReceived; m_port.ErrorReceived += ErrorReceived; m_port.Open(); Thread.Sleep(Timeout.Infinite); } static void ErrorReceived(object sender, SerialErrorReceivedEventArgs args) { Debug.Print("error"); } static void OnDataReceived(object sender, SerialDataReceivedEventArgs args) { Debug.Print("data"); } }
Although the SerialPort.Write call succeeds, data is never received on the remote SerialPort. Am I doing something obviously wrong?
Cheers,
Kenny