Erste Verison mit Kollisions-Vermeidung.
Kollisions-Erkennung sollte auch funktionierten, aber das ist nicht ganz klar. Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -1,19 +1,25 @@
|
||||
#include <SoftwareSerial.h>
|
||||
|
||||
int sTx = 4;
|
||||
int sRx = 5;
|
||||
SoftwareSerial mySerial(sRx,sTx);
|
||||
|
||||
long base = 800;
|
||||
|
||||
int id = 0;
|
||||
int baud = 19200;
|
||||
int enable = 2;
|
||||
char c;
|
||||
int counter = 0;
|
||||
|
||||
void setup(){
|
||||
pinMode(A0,INPUT);
|
||||
id = analogRead(A0)*10;
|
||||
|
||||
pinMode(enable,OUTPUT);
|
||||
mySerial.begin(baud);
|
||||
digitalWrite(enable,LOW);
|
||||
|
||||
pinMode(sTx,OUTPUT);
|
||||
digitalWrite(sTx,LOW);
|
||||
|
||||
pinMode(sRx,INPUT);
|
||||
Serial.begin(115200);
|
||||
Serial.print("Baud rate set to ");
|
||||
Serial.println(baud);
|
||||
@@ -21,23 +27,61 @@ void setup(){
|
||||
Serial.println(id);
|
||||
}
|
||||
|
||||
void printSerial(){
|
||||
while (mySerial.available()){
|
||||
c = mySerial.read();
|
||||
Serial.print(c);
|
||||
bool sendBit(bool bt){
|
||||
digitalWrite(sTx,bt);
|
||||
delayMicroseconds(80);
|
||||
if (digitalRead(sTx) != bt) return false;
|
||||
delayMicroseconds(base-80);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool sendChar(char c){
|
||||
return sendBit(c & 0x01)
|
||||
&& sendBit(c & 0x02)
|
||||
&& sendBit(c & 0x04)
|
||||
&& sendBit(c & 0x08)
|
||||
&& sendBit(c & 0x10)
|
||||
&& sendBit(c & 0x20)
|
||||
&& sendBit(c & 0x40)
|
||||
&& sendBit(c & 0x80);
|
||||
}
|
||||
|
||||
bool sendString(String s){
|
||||
Serial.print("Sending '"+s+"'…");
|
||||
boolean busy = false;
|
||||
for (int i = 0; i<11; i++){
|
||||
if (!digitalRead(sRx)) {
|
||||
if (!busy){
|
||||
Serial.print("busy…");
|
||||
busy = true;
|
||||
}
|
||||
i = 0; // wait for free line
|
||||
}
|
||||
delayMicroseconds(base);
|
||||
}
|
||||
digitalWrite(enable,HIGH);
|
||||
bool success = sendBit(0); // erste Flanke erzeugen
|
||||
if (success) {
|
||||
for (char c : s){
|
||||
if (!sendChar(c)){
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
success = success && sendChar(13);
|
||||
success = success && sendBit(1); // letzte Flanke erzeugen
|
||||
digitalWrite(enable,LOW);
|
||||
Serial.println(success ? "success" : "failed");
|
||||
return success;
|
||||
}
|
||||
|
||||
void loop(){
|
||||
printSerial();
|
||||
delay(id);
|
||||
digitalWrite(enable,HIGH);
|
||||
mySerial.print("Arduino #");
|
||||
mySerial.print(id);
|
||||
mySerial.print(": Counter = ");
|
||||
mySerial.println(counter);
|
||||
mySerial.flush();
|
||||
digitalWrite(enable,LOW);
|
||||
printSerial();
|
||||
counter++;
|
||||
String s = "Hier ist ";
|
||||
s.concat(id);
|
||||
s+=": counter = ";
|
||||
s.concat(counter);
|
||||
while (!sendString(s)) delay(10*base);
|
||||
delay(id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user