You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.0 KiB
61 lines
1.0 KiB
// Für Controllino könnte man benutzen: sRx = 20, sTx = 21, enable = 53 |
|
|
|
int sTx = 2; |
|
int sRx = 3; |
|
|
|
long base = 100; |
|
long rst = base*10; // reset |
|
boolean bt = false; |
|
long dif = 0; |
|
long lt = 0; |
|
long now = 0; |
|
char rcv = 0; |
|
int idx = 0; |
|
String message; |
|
void setup(){ |
|
pinMode(sRx,INPUT); |
|
pinMode(sTx,OUTPUT); |
|
attachInterrupt(digitalPinToInterrupt(sRx),change485,CHANGE); |
|
Serial.begin(115200); |
|
Serial.println("Receiver ready"); |
|
} |
|
|
|
void reset485(long dif){ |
|
rcv = 0; |
|
idx = -1; |
|
message.reserve(32); |
|
} |
|
void change485(){ |
|
bool bt = digitalRead(sRx); |
|
now = micros(); |
|
dif = now - lt; |
|
lt = now; |
|
if (dif > rst){ |
|
reset485(dif); |
|
} else { |
|
handle485(!bt,dif); |
|
} |
|
} |
|
|
|
void handle485(boolean bt, long dif){ |
|
long count = dif/base; |
|
for (int i=0; i<count; i++) push485(bt); |
|
} |
|
|
|
void push485(boolean bt){ |
|
if (idx>=0) rcv |= bt<<idx; |
|
idx++; |
|
|
|
if (idx == 8){ |
|
if (rcv == 13) { |
|
Serial.println(message); |
|
message = ""; |
|
} else { |
|
message+=rcv; |
|
} |
|
idx =0; |
|
rcv =0; |
|
} |
|
} |
|
|
|
void loop(){}
|
|
|