Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit 9263e6f

Browse files
authored
manchoz_fix_rs485 (#52)
* Fix endTrasmission timings * Fix RS485 Full Duplex support * Update RS485 example * Update supported architectures
1 parent 5785ea2 commit 9263e6f

File tree

4 files changed

+47
-39
lines changed

4 files changed

+47
-39
lines changed

examples/RS485_fullduplex/RS485_fullduplex.ino

+42-36
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,49 @@ using namespace machinecontrol;
1919

2020
unsigned long counter = 0;
2121

22-
void setup() {
23-
24-
Serial.begin(9600);
25-
while (!Serial) {
26-
; // wait for serial port to connect.
27-
}
28-
Serial.println("Start RS485 initialization");
29-
30-
// Initialize the serial interface, enable the
31-
// RS485 on SP335ECR1 and set as full duplex
32-
comm_protocols.rs485.begin(9600);
33-
comm_protocols.rs485.enable = 1;
34-
comm_protocols.rs485.sel_485 = 1;
35-
comm_protocols.rs485.half_duplex = 0;
36-
37-
Serial.println("Initialization done!");
22+
void setup()
23+
{
24+
25+
Serial.begin(115200);
26+
while (!Serial) {
27+
; // wait for serial port to connect.
28+
}
29+
delay(1000);
30+
Serial.println("Start RS485 initialization");
31+
32+
comm_protocols.rs485.begin(115200);
33+
comm_protocols.rs485.enable = 1; // SDHN_N
34+
comm_protocols.rs485.sel_485 = 1; // RS485_RS232_N
35+
comm_protocols.rs485.half_duplex = 0; // HALF_FULL_N
36+
comm_protocols.rs485.receive(); // RE_N
37+
comm_protocols.rs485.fd_tx_term = 1; // FD_TX_TERM - 120 Ohm Y-Z termination enabled when both TERM and FD_TX_TERM are high
38+
comm_protocols.rs485.term = 1; // TERM - 120 Ohm A-B termination enabled when high
39+
40+
Serial.println("Initialization done!");
3841
}
3942

43+
constexpr unsigned long sendInterval { 1000 };
44+
unsigned long sendNow { 0 };
45+
46+
constexpr unsigned long halfFullInterval { 5000 };
47+
unsigned long halfFull { 0 };
48+
byte halfFullStatus { 0 };
49+
50+
void loop()
51+
{
52+
while (comm_protocols.rs485.available())
53+
Serial.write(comm_protocols.rs485.read());
54+
55+
if (millis() > sendNow) {
56+
comm_protocols.rs485.noReceive();
57+
comm_protocols.rs485.beginTransmission();
58+
59+
comm_protocols.rs485.print("hello ");
60+
comm_protocols.rs485.println(counter++);
61+
62+
comm_protocols.rs485.endTransmission();
63+
comm_protocols.rs485.receive();
4064

41-
void loop() {
42-
// Call receive(); sets the flux control pins properly
43-
// and allows receiving data if available
44-
comm_protocols.rs485.receive();
45-
if (comm_protocols.rs485.available()) {
46-
Serial.print("read byte: ");
47-
Serial.write(comm_protocols.rs485.read());
48-
Serial.println();
49-
}
50-
// Call beginTransmission(); sets the flux control pins properly
51-
// and allows starting a trasnsmission.
52-
// If instead of a string, you want
53-
// to send bytes, use the API write();
54-
comm_protocols.rs485.beginTransmission();
55-
comm_protocols.rs485.print("hello ");
56-
comm_protocols.rs485.println(counter);
57-
comm_protocols.rs485.endTransmission();
58-
counter++;
59-
60-
delay(1000);
65+
sendNow = millis() + sendInterval;
66+
}
6167
}

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ sentence=Arduino Library for Arduino Machine Control
66
paragraph=
77
category=Communication
88
url=https://github.com/arduino-libraries/MachineControl
9-
architectures=mbed
9+
architectures=mbed, mbed_portenta
1010
includes=MachineControl.h

src/utility/RS485/RS485.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ void RS485Class::begin(unsigned long baudrate, uint16_t config)
5050

5151
_transmisionBegun = false;
5252

53+
half_duplex.output();
54+
5355
_serial->begin(baudrate, config);
5456
}
5557

@@ -123,7 +125,7 @@ void RS485Class::endTransmission()
123125
_serial->flush();
124126

125127
if (_dePin != NC) {
126-
delayMicroseconds(50);
128+
delayMicroseconds(500);
127129
digitalWrite(_dePin, LOW);
128130
}
129131

src/utility/RS485/RS485.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class RS485Class : public HardwareSerial {
5050

5151
void setPins(PinName txPin, PinName dePin, PinName rePin);
5252

53-
mbed::DigitalOut half_duplex = mbed::DigitalOut(PA_9);
53+
mbed::DigitalInOut half_duplex = mbed::DigitalInOut(PA_9);
5454
mbed::DigitalOut sel_485 = mbed::DigitalOut(PA_10);
5555
mbed::DigitalOut slew_n = mbed::DigitalOut(PG_14);
5656
mbed::DigitalOut fd_tx_term = mbed::DigitalOut(PI_15);

0 commit comments

Comments
 (0)