Article Index

Page 254

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600, SERIAL_8N1, 12, 13);
  Serial1.setTimeout(30);
}
void loop() {
  char buffer[100];
  Serial1.write("Hello World\n");
  int len = Serial1.readBytes(buffer, 100);
  buffer[len] = NULL;
  Serial.printf("\nreceived data: %s\n", buffer);
  delay(1000);
}

Page 255

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600, SERIAL_8N1, 12, 13);
  Serial1.setTimeout(30);
}
void loop() {
  char buffer[100];
  Serial1.write("Hello World\n");

  if (Serial1.available()) {
    char byte;
    byte = Serial1.read();
    Serial.print(byte);
  }
  delay(10);
}

 

Page 256

int count;
char buffer[100]={0};
void recieveData(void){
    int nr=Serial1.readBytes(buffer,100);
    buffer[nr]=0;
};
void setup() {
  Serial.begin(9600);
  Serial1.begin(9600, SERIAL_8N1, 12, 13);
  Serial1.setTimeout(5);
  Serial1.onReceive(recieveData);
}

void loop() {
  Serial.print(count++);
  Serial.println(buffer);
  buffer[0]=0;
  Serial1.write("Hello World\n");
  delay(2000);
}

 

Page 258

void setup() { 
  Serial.begin(9600);
  delay(2000);
  Serial.begin(9600);
  Serial1.setTxBufferSize(256);
  Serial1.setRxBufferSize(1024);
  Serial1.begin(9600, SERIAL_8N1, 12, 13);
  Serial1.setTimeout(30);
  int n = 1000;
  char SendData[n];
  for (int i = 0; i < n; i++) {
    SendData[i] = 'A';
  }
  SendData[n - 1] = 0;
  Serial1.flush();
  unsigned long s = micros();
  Serial1.write(SendData, n);
  s = micros() - s;
  Serial.printf("\nt= %f\n", s / 1000.0);
  Serial.flush();
}

void loop() {
  delay(1000);
}

 

Page 259 - created by adding end on page 259 to previous program

void setup() { 
  Serial.begin(9600);
  delay(2000);
  Serial.begin(9600);
  Serial1.setTxBufferSize(256);
  Serial1.setRxBufferSize(1024);
  Serial1.begin(9600, SERIAL_8N1, 12, 13);
  Serial1.setTimeout(30);
  int n = 1000;
  char SendData[n];
  for (int i = 0; i < n; i++) {
    SendData[i] = 'A';
  }
  SendData[n - 1] = 0;
  Serial1.flush();
  unsigned long s = micros();
  Serial1.write(SendData, n);
  s = micros() - s;
  Serial.printf("\nt= %f\n", s / 1000.0);
  Serial.flush();
    char RecData[n+1];
  int nr = Serial1.readBytes(RecData,n+1);
  RecData[nr + 1] = 0;
  Serial.printf("%s\n", RecData);
  Serial.printf("%d    %d\n", nr, sizeof(SendData));
}
void loop() {
  delay(1000);
}

Page 259 

void setup() {
  Serial.begin(9600);
  delay(2000);
  Serial.begin(9600);
  Serial1.setTxBufferSize(2048);
  Serial1.setRxBufferSize(512);
  Serial1.begin(9600, SERIAL_8N1, 12, 13);
  Serial1.setTimeout(30);
  int n = 1000;
  char SendData[n];
  for (int i = 0; i < n; i++) {
    SendData[i] = 'A' + i / 40;;
  }
  SendData[n - 1] = 0;

  Serial1.flush();
  unsigned long s = micros();
  Serial1.write(SendData, n);
  s = micros() - s;
  Serial.printf("t= %f\n", s / 1000.0);
  delay(3000);
  char RecData[n+1];
  int nr = Serial1.readBytes(RecData,n+1);
  RecData[nr + 1] = 0;
  Serial.printf("%s\n", RecData);
  Serial.printf("%d    %d\n", nr, sizeof(SendData));
}


void loop() {
  delay(1000);
}

 

Page  263

void setup() {
Serial.begin(9600);
  delay(2000);
  Serial.begin(9600);
  Serial1.setTxBufferSize(2000);
  Serial1.setRxBufferSize(130);
  Serial1.begin(115200, SERIAL_8N1, 12, 13);
  Serial1.setPins(12, 13, 5, 4);
  Serial1.setHwFlowCtrlMode(UART_HW_FLOWCTRL_CTS_RTS, 10);

  int n = 1000;
  char SendData[n];
  for (int i = 0; i < n; i++) {
    SendData[i] = 'A' + i / 40;
  }
  SendData[n - 1] = 0;
  Serial1.write(SendData, n);

  char RecData[2000];
  int nr = 0;
  int nt = 0;
  Serial1.setTimeout(1);
  do {
    delay(100);
    nt = Serial1.readBytes(&RecData[nr], sizeof(RecData) - nr);
    nr = nr + nt;
    Serial.printf("%d    %d\n", nt, nr);
  } while (nt != 0);

  RecData[nr + 1] = 0;
  Serial.printf("%s\n", &RecData[1]);
  Serial.printf("%d    %d\n", nr, sizeof(SendData));
}

void loop() {
  delay(1000);
}

Page  269

int presence() {
  Serial1.updateBaudRate(9600);
  Serial1.write(0xF0);
  char byte;
  Serial1.readBytes(&byte, 1);
  Serial1.updateBaudRate(115200);
  if (byte == 0xF0) return -1;
  return 0;
}

void writeByte(int byte) {
  char buf[8];
  for (int i = 0; i < 8; i++) {
    if ((byte & 1) == 1) {
      buf[i] = 0xFF;
    } else {
      buf[i] = 0x00;
    }
    byte = byte >> 1;
  };
  Serial1.write(buf, 8);
  Serial1.readBytes(buf, 8);
}

char readByte(void) {
  char buf[8];
  for (int i = 0; i < 8; i++) {
    buf[i] = 0xFF;
  }
  Serial1.write(buf, 8);
  Serial1.readBytes(buf, 8);
  char result = 0;
  for (int i = 0; i < 8; i++) {
    result = result >> 1;
    if (buf[i] == 0xFF) result = result | 0x80;
  }
  return result;
}


void setup() {
  Serial.begin(9600);
  Serial1.begin(115200, SERIAL_8N1, 12, 13);
}

void loop() {
  Serial.printf("\n%d\n", presence());
  writeByte(0xCC);
  writeByte(0x44);
  Serial.printf("%d\n", presence());
  writeByte(0xCC);
  writeByte(0xBE);

  char data[9];
  for (int i = 0; i < 9; i++) {
    data[i] = readByte();
  }

  int t1 = data[0];
  int t2 = data[1];
  int temp1 = (t2 << 8 | t1);
  if (t2 & 0x80) temp1 = temp1 | 0xFFFF0000;
  float temp = temp1 / 16.0;
  Serial.printf("temp =%f\n", temp);
  delay(2000);
}