Arduino Mega 2560 Serial2
Napsal: 06 led 2019, 09:15
Dobrý den potřeboval bych prosím překopat tento sketch ze software serial na serial2 na Arduinu. Nevím jak přesně pracuje čtení z portu2 a poslaní do portu0 (PC). přepsaní myserial na serial2 nepomohlo, hlásí to chybu CRC 0/0, samozřejmě jsem smazal ze sketche tu knihovnu atf. Děkuji moc Petr
#include <SoftwareSerial.h>;
SoftwareSerial mySerial(A3, A4); // prvni TX , druhy - RX
byte cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
unsigned char response[9];
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
void loop()
{
mySerial.write(cmd, 9);
memset(response, 0, 9);
mySerial.readBytes(response, 9);
int i;
byte crc = 0;
for (i = 1; i < 8; i++) crc+=response;
crc = 255 - crc;
crc++;
if ( !(response[0] == 0xFF && response[1] == 0x86 && response[8] == crc) ) {
Serial.println("CRC error: " + String(crc) + " / "+ String(response[8]));
} else {
unsigned int responseHigh = (unsigned int) response[2];
unsigned int responseLow = (unsigned int) response[3];
unsigned int ppm = (256*responseHigh) + responseLow;
int x = ppm;
float y = x/1000.00;
unsigned int teplota = (unsigned int) response[4];
int a = teplota;
float b = a-40;
Serial.print ("koncentrace CO2: ");
Serial.print(ppm);
Serial.print(" PPM ");
Serial.print(y);
Serial.println("%");
Serial.print("teplota: ");
Serial.print(b);
Serial.println(" stupnu Celsia");
}
delay(5000);
}
#include <SoftwareSerial.h>;
SoftwareSerial mySerial(A3, A4); // prvni TX , druhy - RX
byte cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
unsigned char response[9];
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
void loop()
{
mySerial.write(cmd, 9);
memset(response, 0, 9);
mySerial.readBytes(response, 9);
int i;
byte crc = 0;
for (i = 1; i < 8; i++) crc+=response;
crc = 255 - crc;
crc++;
if ( !(response[0] == 0xFF && response[1] == 0x86 && response[8] == crc) ) {
Serial.println("CRC error: " + String(crc) + " / "+ String(response[8]));
} else {
unsigned int responseHigh = (unsigned int) response[2];
unsigned int responseLow = (unsigned int) response[3];
unsigned int ppm = (256*responseHigh) + responseLow;
int x = ppm;
float y = x/1000.00;
unsigned int teplota = (unsigned int) response[4];
int a = teplota;
float b = a-40;
Serial.print ("koncentrace CO2: ");
Serial.print(ppm);
Serial.print(" PPM ");
Serial.print(y);
Serial.println("%");
Serial.print("teplota: ");
Serial.print(b);
Serial.println(" stupnu Celsia");
}
delay(5000);
}