I'm sorry, I thought the pinout table that I attached would be enough to understand the connections, but I made a better one now, hope this is what you asked for.
And for the code, I have tried to post is that way, but the system didn't allow me to post saying that the text was longer than the allowed amount of 9000 characters, so let me post the code by parts because the same problem is happening now.
Part1:
#include <SoftwareSerial.h> // For Arduino uno
SoftwareSerial SerialEpcos(19, 18); // RX, TX for arduino uno
//#define SerialEpcos Serial1 // pro micro or leonardo
#define ReceiveTimeOut 5000 // Maximum for waiting answer from EPOS4 in milli second
#define MaxLenFrame 10 // max number of words in frame
byte DataRead[264];
byte ReadOpcode;
byte len;
int pDataRead;
word rxCRC;
unsigned long CommErrorCode;
enum read_status {
RX_DLE,
RX_STX,
RX_OPCODE,
RX_LEN,
RX_DATA,
RX_CRC_LOW,
RX_CRC_HIGH,
RX_DONE,
ESC_OPCODE,
ESC_LEN,
ESC_DATA,
ESC_CRC_LOW,
ESC_CRC_HIGH
}read_st;
/*****************************************************************************************************/
/* CalcFieldCRC : code from EPOS4-Communication-Guide-En.pdf */
/*****************************************************************************************************/
word CalcFieldCRC(word* pDataArray, word numberOfints)
{
word shifter, c;
word carry;
word CRC = 0;
//Calculate pDataArray Word by Word
while(numberOfints--)
{
shifter = 0x8000; //Initialize BitX to Bit15
c = *pDataArray++; //Copy next DataWord to c
do
{
carry = CRC & 0x8000; //Check if Bit15 of CRC is set
CRC <<= 1; //CRC = CRC * 2
if(c & shifter) CRC++; //CRC = CRC + 1, if BitX is set in c
if(carry) CRC ^= 0x1021; //CRC = CRC XOR G(x), if carry is true
shifter >>= 1; //Set BitX to next lower Bit, shifter = shifter/2
} while(shifter);
}
return CRC;
}
inline void SerialEpcosStuffing(byte BYTE)
{
if (BYTE==0x90) SerialEpcos.write(BYTE);
SerialEpcos.write(BYTE);
}
void SendFrame(byte OpCode,word* pDataArray,byte numberOfwords)
{
word CRC;
word pDataCRC[MaxLenFrame+2];
pDataCRC[0] = OpCode | (numberOfwords<<8);
for (int i=0; i<numberOfwords ; i++)
{
pDataCRC[i+1]=pDataArray[i];
}
pDataCRC[numberOfwords+1]=0x0000;
CRC=CalcFieldCRC(pDataCRC, (word)(numberOfwords+2));
SerialEpcos.write(0x90); // DLE=Data Link Escape
SerialEpcos.write(0x02); // STX=Start of Text
SerialEpcosStuffing(OpCode);
SerialEpcosStuffing(numberOfwords);
for (int i=0; i<numberOfwords ; i++)
{
SerialEpcosStuffing(lowByte(pDataArray[i]));
SerialEpcosStuffing(highByte(pDataArray[i]));
}
SerialEpcosStuffing(lowByte(CRC));
SerialEpcosStuffing(highByte(CRC));
}
bool WriteObject(word Index, byte SubIndex,word* pArray)
{
word data[4];
data[0]= 0x01 | (lowByte(Index)<<8); // NodeId=1
data[1]= highByte(Index)|(((word)SubIndex )<< 8);
data[2]=pArray[0];
data[3]=pArray[1];
SendFrame(0x68,data,(byte)4);
return ReadFrame();
}
bool ReadObject(word Index, byte SubIndex)
{
word data[2];
data[0]= 0x01 | (lowByte(Index)<<8); // NodeId=1
data[1]= highByte(Index)|(((word)SubIndex )<< 8);
SendFrame(0x60,data,(byte)2);
return ReadFrame();
}
// For debuging:
void print_rcv_data()
{
//todo check CRC and print data
Serial. print("OPcode: ");
Serial. print(ReadOpcode,HEX);
Serial. print(" len: ");
Serial. print(len);
Serial. print(" Data:");
for (int i=0; i<2*len ; i++)
{
Serial.print(DataRead[i],HEX);
Serial.print(" , ");
}
Serial.print("CRC: ");
Serial.println(rxCRC, HEX);
}
bool ReadFrame()
{
int incomingByte = 0; // for incoming SerialEpcos data
unsigned long timer=millis();
read_st=RX_DLE;
word pDataCRC[MaxLenFrame+2];
CommErrorCode=0;
while ((read_st!=RX_DONE) and (millis()-timer < ReceiveTimeOut))
{
if (SerialEpcos.available() > 0)
{
incomingByte = SerialEpcos.read();
// Serial.print("Read: ");
// Serial.println(incomingByte, HEX);
// Serial.print("read_st: ");
// Serial.println(read_st);