No announcement yet.

Example code for RS485 board

  • Filter
  • Time
  • Show
Clear All
new posts

  • Example code for RS485 board


    I'm using the PES-022-002 expansion board to try to control a RS485 device. I found this project ( that is similar to what I want to do but I can't find example code anywhere so I can apply it to my project. Does anyone have this code? Or similar code to control a RS485 device?

    Thank you!

  • #2
    You can follow the document for UART:

    When you do the setting for UART, you just need to set flowctrl to 3 to enable TxDE flow control for RS485: pid_ioctl($pid, "set flowctrl 3");
    You can refer to this:


    • #3

      I have been looking at that documentation today and I believe setting the flow control to 3 has helped. I am able to open $pid and the TX and TXDE LED on my RS485 board are blinking when I send data but I'm not sure what format to send it in. The device I'm attempting to control uses Pelco-D and I know the commands need to be 7 bytes. How do I get this to my RS485 board so it correctly passes along those commands to my device?

      I've tried
      $moveLeft = array(255, 1, 0, 4, 30, 0, 35);
      $moveLeft = "255, 1, 0, 4, 30, 0, 35\r\n";
      And a couple other variations but none seem to work.

      My code looks something like this:
      $pid = pid_open("/mmap/uart0");
      pid_ioctl($pid, "set baud 2400");
      pid_ioctl($pid, "set parity 0");
      pid_ioctl($pid, "set data 8");
      pid_ioctl($pid, "set stop 1");
      pid_ioctl($pid, "set flowctrl 3");
      $moveLeft = array(255, 1, 0, 4, 30, 0, 35);
      pid_write($pid, $left);
      Hopefully that gives you a better understanding of my problem.

      Thank you!


      • #4
        You can use the hexadecimal number. For example, I convert 7 byte 255, 1, 0, 4, 30, 0, 35 to hexadecimal numbers:
        $moveLeft = "\xFF\x01\x00\x04\x1E\x00\x23\r\n";
        pid_write($pid, $moveLeft);
        Please make sure that \r\n is necessary or not


        • #5
          Yes that worked! Thank you very much Khanh!


          • Khanh
            Khanh commented
            Editing a comment
            You're welcome!