RS485 Example

I’ve connected two spiders together and send a string from to the other.

I get a data received event on Spider two, but it doesn’t have any data, just the event.

Is there an example that more illustrates using the RS485, I’ve tried every variant of the UART example and RS485 example, but no luck, just an event no data.

I’m sure it’s something I am over-looking, but is not obvious right now.

Thanks Robbie

It should just be read and write.
You can look at the RtuModbusInterface class in my Modbus lib on code share:
https://www.ghielectronics.com/community/codeshare/entry/880

Since RS485 is a bus and not a direct connection you also MUST NOT cross the wires: a goes to a, b goes to b.

Thank You,
I’ll make sure i have it wired correctly

Hello Robbiecat07

I’m not sure if this will help or not, but when I first started playing with RS232 / 485 (Quick note, the difference between 232 & 485 is simply an on /off signal vs a differential signal… the protocols are identical past the HW translation) with these boards last year, we found an interesting scenario where after opening up the ports, we had to pause the thread for a few milliseconds before continuing in order to get data.

I apologize I can’t remember the exact scenario that was causing this, but I do remember trying to assist a programmer to solve the problem, and that was one of the issues we found. It also depends on how your trying to read the data, are you reading it one byte at a time, or are you using the read string function?

What language are you doing this in?

Michael

Sorry for stealing thread, but i have similar problem.
I try to communicate with ADR-R network analyser, and get timeout exceptions (fez spider).
Baud Rate and other settings are correct.
There are no A and B marks on ADR, but + and -.
I connected B to +, and A to -.

What i’m afraid is note from manual
" The entire message has to be sent with no interruptions, and if there is a pause lasting more than a transmission time of 1.5 characters the receiver has to recognise the incomplete message condition and assume that the following byte is the start of a new message."

I established successful communication with some other modbus devices.

Is it possible, that netmf is too slow for some “sensitive” modbus devices, so that things like timeout and events with no data happen? Could be transmission time of 1.5 character problem?

I haven’t analyzed it, but I assume that if I send a message with a single call to Serial.Write(), that it is sent without delays between the bytes.
If this is not the case, then that’s a huge problem in the UART driver.
But since I also assume that the sending part is implemented in native code, I don’t think that this should be a problem.

Yes, it makes sense.

I’m not sure if this would help, but last summer as I ran into problems with the Serial routine, I never had any issues with the Serial.Write function, but with the Read Line. If you read the communication on a byte by byte basis, it allows you to manual control what is considered a message.

Basically as bytes hit the serial port, I dump them into a buffer Array, and track how many bytes are in it. When a specified number of bytes are received (or timeout, or delimeter ect), I’d verify the CRC, (which makes it difficult to use a delimeter with the Read.Line function), then call a reinitialize to the array to all “0”, and reset the Index to 0. I apologize if it’s a bit crude, but it is functional, and doesn’t suffer from some of the limitations of the Read Line function,

I also suspect that I should have a loop included within the Try to keep reading until bytestoread=0 (to handle the condition where interrupts are disabled in another routine, and data builds up on the serial port), but given my application it’s been functional, and haven’t had time to do a cleanup.

Public Sub DataReceivedHandler(ByVal sender As Gadgeteer.Interfaces.Serial, ByVal data As IO.Ports.SerialData)
If rs232.serialPort.BytesToRead() > 32 Then 'Reset the Serial port, because unexpected data is in the buffer, as no command it will ever receive is greater than 32 bytes
rs232.serialPort.Close()
rs232.serialPort.Open()
intRecievedIndex = 0
Debug.Print(“bytes to read greater than 16”)
End If

        Try
            If rs232.serialPort.BytesToRead > 0 Then
                bytRecievedData(intRecievedIndex) = CByte(rs232.serialPort.ReadByte)
                intRecievedIndex = intRecievedIndex + 1
            End If

        Catch ex As Exception
            Debug.Print("Serial Error")
        End Try

’ Code needs to be added here to decide when a full message has been received & reset the array index.

    End Sub

All, please remember that I’m not a professional programmer, but somebody who basically moves on as soon as I get a solution, and this does work.

My testing results:

  1. Read Holding Register (3) works fine
  2. Read Input Regsister (4) doesn’t work. I tried Reinhard library, and also netduino modbus library. Both works well for 0x03, but not for 0x04 function code.

With Serial.Read 6 bytes are received, and error testing buffer[1] & 0x80 succed, so timeout condition is met.


if ((desiredLength > 0 || n == 0) && DateTime.Now > tOut)
            {
               // timeout
               break;
            }
            if (_Serial.BytesToRead > 0)
            {
               if (desiredLength > 0)
               {
                  n += _Serial.Read(buffer, n, desiredLength - n);
               }
               else
               {
                  n += _Serial.Read(buffer, n, buffer.Length - n);
               }
               // a delay of more than 1.5 chars means end of telegram /////, but since this is a extreme short time, we extend it by factor 2
               nextRead = DateTime.Now.Ticks + 6 * _HalfCharLength;
            }
            if (!errorChecked && n >= 2)
            {
               errorChecked = true;
               if ((buffer[1] & 0x80) != 0)
               {
                  // modbus error, so desired length is 5
                  desiredLength = 5;
               }
            }

  1. With Windows App for Modbus testing everything works ok (code 0x03 and 0x04)

Is really something wrong on native side, becouse both different libraries gives same results?