May 18, 2024, 06:31:59 AM

News:

Got VSA?  Want to use your Prop-SX?  Now you can!  See the VSA section of the Library forum for Prop-SX code that works with VSA.


KMTronic RS485 relay board controled by HC-8+

Started by Laytonk, March 11, 2015, 06:42:41 PM

Previous topic - Next topic

Laytonk

Hello, has anybody used a KMTronic RS485 relay board with their HC-8+? If so could you help me with the coding.. Here is a link to the RS485 commands: http://kmtronic.com/kmtronic-rs485-relays-commands.html

Thanks

JonnyMac

I can write a bit of code for you. Do you want to get status back from relay boards, too?

Note that KMTronic board uses 2-wire RS-485. What this means is that you need connect the R+ and T+ terminals together, and the R- and T- terminals together.
Jon McPhalen
EFX-TEK Hollywood Office

Laytonk

Thanks Jon, I have the UART 8 ch relay it has 3 wire conection RX TX and Ground. here is a sample of code for Arduino: http://kmtronic.com/arduino-uart-8-channel-relay-controller-example.html

Code


#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

void setup()
{
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
}

void loop() // run over and over
{

  for(uint8_t i=1; i < 9; i++ ){
      KMtronic_Relay_ON(i);
      delay(50);
  }

    for(uint8_t i=1; i < 9; i++ ){
      KMtronic_Relay_OFF(i);
      delay(50);
  }


  KMtronic_Relay_ON(1);
  delay(500);

  KMtronic_Relay_OFF(1);
  delay(500);

  KMtronic_Relay_ON(8);
  delay(500);

  KMtronic_Relay_OFF(8);
  delay(500);

}

void KMtronic_Relay_ON(uint8_t number)
{
  mySerial.write((uint8_t)0xFF);
  mySerial.write((uint8_t)number);
  mySerial.write((uint8_t)0x01);
}

void KMtronic_Relay_OFF(uint8_t number)
{
  mySerial.write((uint8_t)0xFF);
  mySerial.write((uint8_t)number);
  mySerial.write((uint8_t)0x00);
}

and yes I would like to get status back and be able to clear all relays on start. also if I use this relay can I use other EFX-TEK boards?

JonnyMac

<accent>We don't need no stinking Arduino code!</accent>  ;)

I added a couple methods to the basic HC-8+ template that let you control relays on the RS-485 link. One method is used for on or off control -- it takes three parameters: the board # (1-6), the relay # (1-8), and the status (0 for off, 1 for on). If you want to turn on relay four of board two, you would do it like this:

  set_relay(2, 4, IS_ON)

There are constants in the program called IS_ON and IS_OFF.

To get the status of a relay you would do this:

  if (get_relay(2, 4) == IS_ON)
    ' do something


Again, this is just a template -- you'll need to hook it up and give it a try.

For those that don't want to download the code, here are the methods that handle coms with the relay board. A standard serial driver is connected to the HC-8+ RS-485 port. Being a 2-wire system we have to selectively enable transmitting and receiving.

pub set_relay(board, relay, status)

  if ((board < 1) or (board > 6))                                ' bad board?
    return

  if ((relay < 1) or (relay > 8))                                ' bad relay?
    return

  ' good board & relay -- send command

  set_rs485(TX)                                                  ' set to transmit
  pause(1)                                                       ' let rs485 settle
  rs485.tx($FF)                                                  ' command header
  rs485.tx(((board-1) * 8) + relay)                              ' combine for relay #
  if (status)                                                    ' promote non-zero                                                 
    rs485.tx(IS_ON)                                              '  to IS_ON     
  else
    rs485.tx(IS_OFF)
  rs485.txflush                                                  ' let command finish
  set_rs485(RX)                                                  ' back to receive

 
pub get_relay(board, relay) | buf[2], idx, c

  if ((board < 1) or (board > 6))                                ' bad board?
    return IS_OFF

  if ((relay < 1) or (relay > 8))                                ' bad relay?
    return IS_OFF

  ' good board & relay -- send/receive request

  longfill(@buf, 0, 2)                                           ' clear buffer
   
  set_rs485(TX)                                                  ' set to transmit
  pause(1)                                                       ' let rs485 settle
  rs485.tx($FF)                                                  ' command header
  rs485.tx($A0 + board)                                          ' send board id
  rs485.txflush                                                  ' let command finish
  set_rs485(RX)                                                  ' back to receive

  repeat idx from 0 to 7
    c := rs485.rxtime(250)                                       ' * may require adjusting
    if (c => 0)
      buf.byte[idx] := c
    else
      quit

  return buf.byte[relay-1]                                       ' return relay status


Some will naturally wonder why this looks more invoked than the Schmarschmino code... because this is industrial code (error checking, lets you pass easy parameters instead of calculating the encoded relay #, etc), that example is not.
Jon McPhalen
EFX-TEK Hollywood Office

Laytonk

Hi Jon, thanks for all the help with this, we downloaded the code and ran a test the relay boards rx light illuminates but the program just hangs at that point.. any ideas? we are using a the template you posted for simple prop scene.
Thanks

JonnyMac

I only have one HC-8+ at the moment so I cannot do any real testing. I will be in EFX-TEK headquarters tomorrow night (Transworld is next weekend) so I can set up a second HC-8+ to receive the data to verify.

You'll have to bear with us this week.
Jon McPhalen
EFX-TEK Hollywood Office

Laytonk

Hi, I was revisiting my KMTronic relay but am still having issues, ok I tried the template you sent over and what is happening is, relay board RX light is on but no relays activate. What I am trying to do is,

  repeat                                                                         ' wait for trigger on IN0
      if (ttl_pin == 0)
        quit 

    set_relay(1, 1, IS_ON)                                               ' prop moves
    set_relay(1, 2, IS_ON)                                               ' strobe on
    time.pause(3000)
    set_relay(1, 1, IS_OFF)                                              ' prop moves
    set_relay(1, 2, IS_OFF)                                              ' strobe off
    time.pause(3000)
   
Sorry this should be simple, but I am missing a step

I load   hc-8+_rs485_relay.spin
              │
              ├──jm_prng.spin
              │
              ├──jm_pwm8.spin
              │
              └──jm_fullduplexserial.spin

JonnyMac

While porting my original code to the new HC-8+ template I found that there was a copy-and-paste error in the setup() method -- the term object was getting restarted at 9600 baud (which was supposed to be for the RS-485 object). Give this one a try. Just remember I'm at a disadvantage not having the target hardware you're connecting to.
Jon McPhalen
EFX-TEK Hollywood Office

Laytonk

Hello I tried the new template, however I get a Error box when it gets to an outs.high command, I tried:
repeat
set_relay(2, 4, ON)

just to test but all I get is a flashing RX led on my KMTronic RS485 Relay

I Have it wired R+/T+ to the B connection and the R-/T- to the A connection. Also Dip switch 2 is ON all others are off. If it would help I can send you a Relay Card.

Let me know thanks..

JonnyMac

The outs object was named pwm in that template. I just modified the template to change that object name back to outs. Download the template (dated 2015-12-02 from the sticky post at the top of this forum).
Jon McPhalen
EFX-TEK Hollywood Office