سلام
اینکار شدنی هست
شما باید ببینید خود برد آردوینویی که دارید چه تعداد رو به صورت سخت افزاری پشتیبانی میکنه
بیشتر از اون رو هم میتونید به صورت نرم افزاری پیاده کنید
فقط اگر بیش از دو یوآرت نرم افزاری داشتید باید برای دریافت دیتا روی هر کدوم از uart های نرم افرازی تابع listen رو فراخوانی کنید .
این یه کدی هست که من قبلا نوشتم و در اون از دو یوآرت نرم افزاری استفاده شده
میتونید از این کمک بگیرید
#include <SoftwareSerial.h>
#include "ELMduino.h"
/// DEFINES /////////////////////////////////////////////////////////
#define ELM_PORT SSerial_1
#define ELM_PORT_rxPin 12
#define ELM_PORT_txPin 11
#define HMI_PORT SSerial_2
#define HMI_PORT_rxPin A1
#define HMI_PORT_txPin A0
#define BUZZ_Pin 10
#define DBG_PORT Serial
/// DISTANCE METER ////////////////////////////////////////////////
bool DM_CONNECTED = true;
#define SRF_LFT_rx 4 // Trigger
#define SRF_LFT_tx 5 // Echo
#define SRF_RGT_rx 6 // Trigger
#define SRF_RGT_tx 7 // Echo
float temp_In_C = 20.0; // Can enter actual air temp here for maximum accuracy
float speed_Of_Sound; // Calculated speed of sound based on air temp
float distance_Per_uSec;
long duration;
/// @ELM ////////////////////////////////////////////////////////
const bool DEBUG = true;
const int TIMEOUT = 2000;
bool ELM_CONNECTED = false;
ELM327 myELM327;
typedef enum { ENG_RPM, SPEED, COOLANT } obd_pid_states;
obd_pid_states obd_state = ENG_RPM;
float rpm = 0;
int32_t kph = 0;
float coolant = 0;
SoftwareSerial ELM_PORT(ELM_PORT_rxPin, ELM_PORT_txPin);
/// HMI /////////////////////////////////////////////////////////
char buff[100];
SoftwareSerial HMI_PORT(HMI_PORT_rxPin, HMI_PORT_txPin);
/// @OTHER ////////////////////////////////////////////////////////
long out_speed;
long out_rpm;
long out_temp;
long out_distance_l;
long out_distance_r;
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
// Define pin modes for TX and RX
pinMode(ELM_PORT_rxPin, INPUT);
pinMode(ELM_PORT_txPin, OUTPUT);
pinMode(HMI_PORT_rxPin, INPUT);
pinMode(HMI_PORT_txPin, OUTPUT);
pinMode(BUZZ_Pin, OUTPUT);
DBG_PORT.begin(115200);
ELM_PORT.begin(9600);
HMI_PORT.begin(9600);
randomSeed(analogRead(5));
pinMode(LED_BUILTIN, OUTPUT);
pinMode(SRF_LFT_tx, OUTPUT);
pinMode(SRF_LFT_rx, INPUT);
pinMode(SRF_RGT_tx, OUTPUT);
pinMode(SRF_RGT_rx, INPUT);
digitalWrite(BUZZ_Pin, LOW);
speed_Of_Sound = 331.1 + (0.606 * temp_In_C);
distance_Per_uSec = speed_Of_Sound / 10000.0;
DBG_PORT.println("Attempting to connect to ELM327...");
ELM_PORT.listen();
if (!myELM327.begin(ELM_PORT, DEBUG, TIMEOUT)) {
ELM_CONNECTED = false;
DBG_PORT.println("Couldn't connect to OBD scanner");
} else {
ELM_CONNECTED = true;
DBG_PORT.println("Connected to ELM327");
}
}
void loop() {
//out_speed = random(120);
//out_rpm = random(7000);
//out_temp = random(150);
//out_distance_l = random(110);
//out_distance_r = random(110);
if (DM_CONNECTED) {
out_distance_l = read_distance(SRF_LFT_tx, SRF_LFT_rx);
out_distance_r = read_distance(SRF_RGT_tx, SRF_RGT_rx);
}
if (ELM_CONNECTED) {
ELM_PORT.listen();
read_ELM();
out_speed = (long)kph;
out_rpm = (long)rpm;
out_temp = (long)coolant;
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval || true) {
// save the last time you blinked the LED
previousMillis = currentMillis;
sprintf(buff, "Car Data: speed:%ld,rpm:%ld,temp:%ld , %ld , %ld", out_speed, out_rpm, out_temp, out_distance_l, out_distance_r);
DBG_PORT.println(buff);
sprintf(buff, "%ld,%ld,%ld,%ld,%ld,0", out_speed, out_rpm, out_temp, out_distance_l, out_distance_r, 0);
HMI_PORT.println(buff);
DBG_PORT.println("Data Sent");
}
}
long read_distance(int tx_pin, int rx_pin) {
digitalWrite(tx_pin, HIGH);
delayMicroseconds(20);
digitalWrite(tx_pin, LOW);
duration = pulseIn(rx_pin, HIGH);
return ((duration / 2) * distance_Per_uSec);
}
void read_ELM() {
switch (obd_state) {
case ENG_RPM: {
float lrpm = myELM327.rpm();
if (myELM327.nb_rx_state == ELM_SUCCESS) {
rpm = lrpm;
DBG_PORT.print("rpm:");
DBG_PORT.println(rpm);
obd_state = SPEED;
} else if (myELM327.nb_rx_state != ELM_GETTING_MSG) {
myELM327.printError();
obd_state = SPEED;
}
break;
}
case SPEED: {
int32_t lkph = myELM327.kph();
if (myELM327.nb_rx_state == ELM_SUCCESS) {
kph = lkph;
DBG_PORT.print("kph:");
DBG_PORT.println(kph);
obd_state = COOLANT;
} else if (myELM327.nb_rx_state != ELM_GETTING_MSG) {
myELM327.printError();
obd_state = COOLANT;
}
break;
}
case COOLANT: {
float lcoolant = myELM327.engineCoolantTemp();
if (myELM327.nb_rx_state == ELM_SUCCESS) {
coolant = lcoolant;
DBG_PORT.print("cool:");
DBG_PORT.println(coolant);
obd_state = ENG_RPM;
} else if (myELM327.nb_rx_state != ELM_GETTING_MSG) {
myELM327.printError();
obd_state = ENG_RPM;
}
break;
}
}
}