@@ -22,12 +22,9 @@ bool Lin_Interface::readFrame(uint8_t FrameID)
2222 uint8_t ProtectedID = getProtectedID (FrameID);
2323 bool ChecksumValid = false ;
2424
25- // start transmission
26- HardwareSerial::begin (baud, SERIAL_8N1);
27- writeBreak (); // initiate Frame with a Break
28- HardwareSerial::write (0x55 ); // Sync
29- HardwareSerial::write (ProtectedID); // PID
25+ startTransmission (ProtectedID);
3026 HardwareSerial::flush ();
27+
3128 // wait for available data
3229 delay (100 );
3330
@@ -118,11 +115,8 @@ void Lin_Interface::writeFrame(uint8_t FrameID, uint8_t dataLen)
118115 uint8_t ProtectedID = getProtectedID (FrameID);
119116 uint8_t cksum = getChecksum (ProtectedID, dataLen);
120117
121- // übertragung startet
122- HardwareSerial::begin (baud, SERIAL_8N1);
123- writeBreak (); // initiate Frame with a Break
124- HardwareSerial::write (0x55 ); // Sync
125- HardwareSerial::write (ProtectedID); // PID
118+ startTransmission (ProtectedID);
119+
126120 for (int i = 0 ; i < dataLen; ++i)
127121 {
128122 HardwareSerial::write (LinMessage[i]); // Message (array from 1..8)
@@ -210,10 +204,8 @@ void Lin_Interface::writeFrameClassic(uint8_t FrameID, uint8_t dataLen)
210204 uint8_t ProtectedID = getProtectedID (FrameID);
211205 uint8_t cksum = getChecksum (0x00 , dataLen);
212206
213- HardwareSerial::begin (baud, SERIAL_8N1);
214- writeBreak (); // initiate Frame with a Break
215- HardwareSerial::write (0x55 ); // Sync
216- HardwareSerial::write (ProtectedID); // ID
207+ startTransmission (ProtectedID);
208+
217209 for (int i = 0 ; i < dataLen; ++i)
218210 {
219211 HardwareSerial::write (LinMessage[i]); // Message (array from 1..8)
@@ -226,6 +218,22 @@ void Lin_Interface::writeFrameClassic(uint8_t FrameID, uint8_t dataLen)
226218 HardwareSerial::end ();
227219} // void Lin_Interface::writeFrameClassic
228220
221+ // / Introduce Frame (Start UART, Break, Sync, PID)
222+ void Lin_Interface::startTransmission (uint8_t ProtectedID)
223+ {
224+ // start UART
225+ if (rxPin < 0 && txPin < 0 ) {
226+ // no custom pins are defined
227+ HardwareSerial::begin (baud, SERIAL_8N1);
228+ } else {
229+ HardwareSerial::begin (baud, SERIAL_8N1, rxPin, txPin);
230+ }
231+
232+ writeBreak (); // initiate Frame with a Break
233+ HardwareSerial::write (0x55 ); // Sync
234+ HardwareSerial::write (ProtectedID); // PID
235+ }
236+
229237// / Send a Break for introduction of a Frame
230238// / This is done by sending a Byte (0x00) + Stop Bit by using half baud rate
231239// / @returns if the 0x00 has been send
0 commit comments