Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

RS485 between two Netduinos


  • Please log in to reply
8 replies to this topic

#1 Kenny Kerr

Kenny Kerr

    Member

  • Members
  • PipPip
  • 17 posts

Posted 24 January 2011 - 06:00 PM

Hi there,

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

#2 bill.french

bill.french

    Advanced Member

  • Members
  • PipPipPip
  • 260 posts
  • LocationPrinceton, NJ

Posted 24 January 2011 - 06:37 PM

check this out: http://forums.netdui...ch__1#entry6890 might have to do with adding the event before the port is open?

#3 bill.french

bill.french

    Advanced Member

  • Members
  • PipPipPip
  • 260 posts
  • LocationPrinceton, NJ

Posted 24 January 2011 - 06:41 PM

You could also test it without events with something like this:

while(true)
{            
while (_COMPORT.BytesToRead > 0)
            {
                byte[] buffer = new byte[1];
                _COMPORT.Read(buffer, 0, 1);
                Debug.Print(buffer[0].ToString());
            }
}


#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 24 January 2011 - 06:43 PM

Hi Kenny, What RS485 transceivers are you using? If using RTS/CTS, you generally need to wire _both_ RTS and CTS. Also, are RX and TX swapped between the two boards (RX1<->TX2 and RX2<->TX1)? Chris

#5 Kenny Kerr

Kenny Kerr

    Member

  • Members
  • PipPip
  • 17 posts

Posted 24 January 2011 - 06:50 PM

What RS485 transceivers are you using? If using RTS/CTS, you generally need to wire _both_ RTS and CTS.


I'm using the LT1785 tranceiver. I thought I didn't need to wire up the CTS but I could be wrong.

Also, are RX and TX swapped between the two boards (RX1<->TX2 and RX2<->TX1)?


I'm not sure what you mean. I tried to wire both board identically: DI to TX (pin 3) and R0 to RX (pin 2).

Cheers,
Kenny

#6 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 26 January 2011 - 08:50 AM

Kenny, the RS485 line is polarized. You should try to swap the wiring between the two LT1785 ICs. There's no damage, but it simply doesn't work. Another good step to do, is to connect a led to the RTS port, so that you've an easy way to see when the transmitter requires the line. That's *NOT* granting the data is flowing correctly, of course! I confirm that the CTS line is not envolved at all for this purpose. FYI, I'm working to do the same thing, but for connect the Netduino+ to a PC. Cheers Mario
Biggest fault of Netduino? It runs by electricity.

#7 Kenny Kerr

Kenny Kerr

    Member

  • Members
  • PipPip
  • 17 posts

Posted 26 January 2011 - 05:04 PM

FYI, I'm working to do the same thing, but for connect the Netduino+ to a PC.


Thanks Mario,

Did you get it to work? From reading through the Atmel docs on their USART and looking at the Micro Framework firmware (native code) it looks like the Micro Framework hardcodes the USART to operate in “normal” mode but we require it to be switched to “rs485” mode to ensure the right RTS behavior. Unfortunately this requires a recompilation of the netduino’s firmware to flip this register value.

Cheers,
Kenny

#8 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 27 January 2011 - 05:33 AM

Hi Kenny. No, I didn't yet. I'd plan to achieve some result on the next week-end. At the moment I must sort out other stuffs *much* more important, because the customer's waiting for! At the moment Netduino is only experimentation in the spare time. I promise to keep you informed. Anyway, about the RTS, I'd avoid to modify the driver for the TX-enabling. I'd prefer to manage "manually" the line... We'll see. Cheers Mario
Biggest fault of Netduino? It runs by electricity.

#9 Kenny Kerr

Kenny Kerr

    Member

  • Members
  • PipPip
  • 17 posts

Posted 27 January 2011 - 01:57 PM

Anyway, about the RTS, I'd avoid to modify the driver for the TX-enabling. I'd prefer to manage "manually" the line...


Hi Mario,

Yes for the netduino it’s probably simplest to let the chip think it’s an RS232 and handle the RTS manually. We’re just going to use native code with our own board, so we can use the RS485 driver.

Cheers,
Kenny




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.