Pages: [1]   Go Down
Author Topic: Communicate with relay board with "MySerial"  (Read 586 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I bought a relay board from conrad which has 8 relays which Im trying to control using my arduino one. In the user manual for this relay board it says that each command should exist of 4 bytes with following frame structure:

Byte0: Command, I get to choose between one out of 8 commands
Byte1: Board address, I only have one board so it should be a one
Byte2: Data, information about which relay should be turned on/off
Byte3: Check sum (XOR from Byte0, Byte1 and Byte2)

It seems fairly simple but I can't get it to work. First of all I do not know how to create the check sum so I just ignore it and send in a bunch of zeros, but the relay board also requires a stop bit which I do not know how to create.

Can anyone help get this board running.

This is my code so far.
Code:
#include "Arduino.h"
#include "SoftwareSerial.h"

#define rxPin 12
#define txPin 13

SoftwareSerial MySerial(rxPin, txPin); // RX, TX
long incomingByte = 0;

byte byte0 = B00000000;
byte byte1 = B00000001;
byte byte2 = B10100100;
byte byte3 = B00000000;

byte ByteArray[] = { byte0, byte1, byte2, byte3 };

void setup()                   
{
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  Serial.begin(9600);
  MySerial.begin(19200);
}

void loop()                     
{
  MySerial.write(ByteArray, 4);
  Serial.println("Hello world!"); 

  delay(1000);
 
  if (MySerial.available() > 0)
  {
    incomingByte = MySerial.read(); // read the incoming byte:               
    Serial.print("I received: "); // say what you got:
    Serial.println(incomingByte, BIN);

    delay(1000);
  }
}
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4013
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you post a link to the board that you are using ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Faraday Member
**
Karma: 80
Posts: 3785
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Byte3: Check sum (XOR from Byte0, Byte1 and Byte2)

I do not know how to create the check sum

XOR (^) and the other bitwise operators are explained here.

Here's a sketch which XORs your 3 bytes:

Code:
void setup()
{
  Serial.begin(9600);
 
byte byte0 = B00000000;
byte byte1 = B00000001;
byte byte2 = B10100100;
byte byte3 = byte0^byte1^byte2;

  Serial.println(byte0,BIN);
  Serial.println(byte1,BIN);
  Serial.println(byte2,BIN);
  Serial.println(byte3,BIN); //should be 10100101 for these bytes
}

void loop(){}

A stop bit is an extra bit on the end of data, in this case that would be a 9th bit. Presumably that means adding a 9th bit to those bytes. That might mean you have to size-up from a byte to an int and stick an extra bit on the end.

So for example your current byte1, 00000001 would become 0000000000000011 ... but there are probably better ways of doing this.

Post a link, as UKHB suggested: I can't find an 8-relay board on their site.
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4013
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Possibly this one
http://www.conrad-uk.com/ce/en/product/197730/C-Control-8-Channel-PC-Relay-Board-RS232-Serial-230Vac-16A

The user manual has a section on control of the relays that looks like what was posted
http://www.produktinfo.conrad.com/datenblaetter/175000-199999/197730-an-01-ml-8_K_RELAISKARTE_PC230V_AC16A_de_en_fr_nl.pdf

You can't just ignore the checksum.  Without the correct checksum the board will not work as it will assume that there has been an error in the serial transmission.  What do you get back from the board when sending your commands ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Faraday Member
**
Karma: 80
Posts: 3785
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure how you would take care of the stop bit: in the case of RS232 that's taken care of by the sending device knowing what to do, outside of the user's application, within the comms intelligence.
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4013
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The stop bit can be configured using the Serial.begin method, although 1 stop bit is the default in any case.
http://arduino.cc/en/Serial/Begin
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Faraday Member
**
Karma: 80
Posts: 3785
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The stop bit can be configured using the Serial.begin method, although 1 stop bit is the default in any case.
http://arduino.cc/en/Serial/Begin

Ah.... I was looking for a command like Serial.stopbits! Didn't think of looking inside Serial.begin.

So there you go omegaman, that should be you sorted.....
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Pages: [1]   Go Up
Jump to: