5

I want to communicate between my PC and some controller boards.

The expectation is that the PC will send an identifier of the board on RS-485 and then it should receive the answer from the board.

When I try to receive the response, I receive the wrong data.

Here is my code:

public void set()
    {

        SerialPort sp = new SerialPort("COM1");
        sp.Open();
        if (sp.IsOpen)
        {
            byte[] id = new byte[]{0,0,0,0,0,0,0,0,0,0};
            byte[] rec = new byte[540];
            while (!end)
            {
                sp.Write(id,0,id.Length);

                sp.Read(rec,0,rec.Length);

                //do some with rec
                //WORKING
                //do soem with rec

            }
        }
        sp.Close();
    }

It works if I am using RS-232, but not when I am using RS-485.

UPDATE :

It is RS-485 2 wire.(http://en.wikipedia.org/wiki/RS-485)

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
M.Movaffagh
  • 1,284
  • 5
  • 20
  • 33
  • what type of RS-485 interface are you using? A normal PC has no such interface... – Daan Timmer May 21 '12 at 17:05
  • @DaanTimmer : you are correct i use uport 1150 a product from moxa – M.Movaffagh May 21 '12 at 17:33
  • @Ericj. : i know it is wrong.it is from a sensor. – M.Movaffagh May 21 '12 at 17:34
  • Did you check if the baud rate, parity, flow control, etc that you are using on your program are the same ones that are being used on your boards? – Felipe May 21 '12 at 17:35
  • @Komyg : yes.baud rate is correct and no parity (RS-485). – M.Movaffagh May 21 '12 at 17:39
  • As far as the virtual serial port interface is concerned, RS-485 and RS-232 are identical. Check your settings on the hardware. – Brad May 21 '12 at 17:45
  • @brad you are correct but there is half duplex communication in this and it seems we have no parity and no hardware handshake. – M.Movaffagh May 21 '12 at 17:57
  • 1
    I think this is more hardware related than software related. Check the documentation of your RS-485 hardware and check your signal wires using a Logic Analyzer or Oscilloscope. – Daan Timmer May 21 '12 at 18:19
  • I want to do something like this and I too will use moxa 1150. Can you send data over 485 without changing the hardware setting from Device Settings to RS-485 2W? (We use the latter option for 422) – theaob Aug 02 '13 at 12:29

1 Answers1

4

I found the problem.

 sp.Read(rec,0,rec.Length);

Read is a non-blocking method so it reads the buffer but does not wait for all of the bytes. So you need to use the return value of this function which returns an integer with a number of bytes that it could read.

I am using this:

int read = 0;
int shouldRead = readData1.Length;
int len;
while (read < shouldRead )
{
    len = serialport.Read(buffer, 0, readData1.Length);
    if (len == 0)
         continue;
    Array.Copy(buffer, 0, readData1, read, len);
    read += len;
    Thread.Sleep(20);
}
GEOCHET
  • 21,119
  • 15
  • 74
  • 98
M.Movaffagh
  • 1,284
  • 5
  • 20
  • 33