Communicate with relay board with "MySerial"

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.

#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);
  }
}

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

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:

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.

Possibly this one

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 ?

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.

The stop bit can be configured using the Serial.begin method, although 1 stop bit is the default in any case.

UKHeliBob:
The stop bit can be configured using the Serial.begin method, although 1 stop bit is the default in any case.
Serial.begin() - Arduino Reference

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.....