Now, I have tried to adapt this code for the SC16IS752 with two UART ports. If I delete or comment out the "readSerial()" section, it sends the data and the Arduino on the UART receives it. But if I put the "readSerial()" section in that should receive the data from the Arduino on the UART of the SC16IS752, I get only this on the serial monitor:
***********Data available***********
=====Available data:64
=====Available data:64
***********Data available***********
=====Available data:64
=====Available data:64
***********Data available***********
=====Available data:64
=====Available data:64
***********Data available***********
=====Available data:64
=====Available data:64
***********Data available***********
=====Available data:64
=====Available data:64
***********Data available***********
=====Available data:64
=====Available data:64
***********Data available***********
=====Available data:64
=====Available data:64
***********Data available***********
=====Available data:64
I have used this code for the master with the SC16IS752 connected with I2C:
/*Arduino 1 has the SC16IS752 connected on SDA/SCL
*TX/RX of the SC16IS750 is connected to Arduino 2
*VCC/GND
*I2C of the SC16IS750 is connected to VCC
*A0 and A1 of the SC16IS750 is connected as shown in the address table below
*/
#include <Wire.h>
#include <SC16IS752.h>
SC16IS752 i2cuart = SC16IS752(SC16IS750_PROTOCOL_I2C,SC16IS750_ADDRESS_AA); /*Address table: A1 A0 Address
VDD VDD 0x90 => AA
VDD GND 0x92 => AB
VDD SCL 0x94 => AC
VDD SDA 0x96 => AD
GND VDD 0x98 => BA
GND GND 0x9A => BB
GND SCL 0x9C => BC
GND SDA 0x9E => BD
SCL VDD 0xA0 => CA
SCL GND 0xA2 => CB
SCL SCL 0xA4 => CC
SCL SDA 0xA6 => CD
SDA VDD 0xA8 => DA
SDA GND 0xAA => DB
SDA SCL 0xAC => DC
SDA SDA 0xAE => DD
*/
const byte bufferSize {10};
char serialBuffer[bufferSize+1] {""};
char myData[bufferSize];
char myData1[bufferSize];
char myData2[bufferSize];
char var;
char m;
int E = 0;
int F = 0;
int G = 0;
int H = 0;
int BUT = 0;
static byte i = 0;
static byte j = 0;
static byte k = 0;
int y = 0;
void setup (void)
{
i2cuart.begin(38400, 38400); //UART A, UART B
Serial.begin(38400);
}
void loop (void)
{
readSerial();
Serial.print("E = ");
Serial.println(E);
Serial.print("F = ");
Serial.println(F);
Serial.print("G = ");
Serial.println(G);
Serial.print("H = ");
Serial.println(H);
Serial.print("BUT = ");
Serial.println(BUT);
command("A0");
command("B55");
command("C180");
command("D125");
command("@12");
}
void command(String value) {
memset(serialBuffer, 0, sizeof serialBuffer);
sendToSlaveI2C(serialBuffer, value);
}
void sendToSlaveI2C (char *const buffer, String command)
{
command.toCharArray(buffer, bufferSize);
const size_t zeichenAnzahl {strlen(buffer)};
Serial.print("laenge: "); Serial.println(zeichenAnzahl);
if(strchr("ABC", buffer[0]) != NULL){
for (byte i = 0; i < zeichenAnzahl; i++)
{
i2cuart.write(SC16IS752_CHANNEL_A, buffer[i]);
Serial.print("sent: ");
Serial.println(buffer[i]);
}
i2cuart.write(SC16IS752_CHANNEL_A, '\n');
}
else if(strchr("D@", buffer[0]) != NULL){
for (byte i = 0; i < zeichenAnzahl; i++)
{
i2cuart.write(SC16IS752_CHANNEL_B, buffer[i]);
Serial.print("sent: ");
Serial.println(buffer[i]);
}
i2cuart.write(SC16IS752_CHANNEL_B, '\n');
}
}
void readSerial(){
while(i2cuart.available(SC16IS752_CHANNEL_A)) {
m = i2cuart.read(SC16IS752_CHANNEL_A);
receiveData();
}
while(i2cuart.available(SC16IS752_CHANNEL_B)) {
m = i2cuart.read(SC16IS752_CHANNEL_B);
receiveData();
}
}
void receiveData(){
if(m != '\n') {
myData[i++] = m;
}
else if(m == '\n') {
i = 0;
while(i <= strlen(myData) ) {
if(isdigit(myData[i]) == true) {
myData1[j] = myData[i];
j++;
i++;
}
else {
myData2[k] = myData[i];
k++;
i++;
}
}
memset(myData, 0, sizeof myData);
j = 0;
k = 0;
i = 0;
y = atoi(myData1); //making the integer part
var = myData2[0];
memset(myData1, 0, sizeof myData1);
memset(myData2, 0, sizeof myData2);
switch(var) {
case 'E' : E = y;
break;
case 'F' : F = y;
break;
case 'G' : G = y;
break;
case 'H' : H = y;
break;
case '#' : BUT = y;
break;
}
}
}
And this code for the slave that is connected with the RX/TX:
/*Arduino 2 has the SC16IS750 connected on TX/RX
*/
const byte bufferSize {10};
char serialBuffer[bufferSize+1] {""};
char myData[bufferSize];
char myData1[bufferSize];
char myData2[bufferSize];
char var;
int A = 0;
int B = 0;
int C = 0;
int D = 0;
int BUT = 0;
static byte i = 0;
static byte j = 0;
static byte k = 0;
int y = 0;
void setup()
{
Serial.begin(38400);
}
void loop() {
readSerial();
Serial.print("A = ");
Serial.println(A);
Serial.print("B = ");
Serial.println(B);
Serial.print("C = ");
Serial.println(C);
Serial.print("D = ");
Serial.println(D);
Serial.print("BUT = ");
Serial.println(BUT);
command("E10");
command("F5");
command("G147");
command("H251");
command("#33");
}
void command(String value) {
memset(serialBuffer, 0, sizeof serialBuffer);
sendToUART(serialBuffer, value);
}
void sendToUART (char *const buffer, String command)
{
command.toCharArray(buffer, bufferSize);
const size_t zeichenAnzahl {strlen(buffer)};
for (byte i = 0; i < zeichenAnzahl; i++)
{
Serial.write(buffer[i]);
}
Serial.write('\n');
}
void readSerial(){
while(Serial.available()) {
char m = Serial.read();
if(m != '\n') {
myData[i++] = m;
}
else if(m == '\n') {
//Serial.print("myData: ");
//Serial.println(myData);
i = 0;
while(i <= strlen(myData) ) {
if(isdigit(myData[i]) == true) {
myData1[j] = myData[i];
j++;
i++;
}
else {
myData2[k] = myData[i];
k++;
i++;
}
}
memset(myData, 0, sizeof myData);
j = 0;
k = 0;
i = 0;
//Serial.print("myData1: ");
//Serial.println(myData1);
//Serial.print("myData2: ");
//Serial.println(myData2);
y = atoi(myData1); //making the integer part
var = myData2[0];
//Serial.println(var);
//Serial.println(y);
memset(myData1, 0, sizeof myData1);
memset(myData2, 0, sizeof myData2);
switch(var) {
case 'A' : A = y;
break;
case 'B' : B = y;
break;
case 'C' : C = y;
break;
case 'D' : D = y;
break;
case '@' : BUT = y;
break;
}
}
}
}
What is wrong with the master code?