Skip to content

Commit 1014f8f

Browse files
committed
datatype & readFrame corrected
1 parent 4fd0ccf commit 1014f8f

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

src/Lin_Interface.cpp

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,36 @@ bool Lin_Interface::readFrame(uint8_t FrameID)
3232
delay(100);
3333

3434
// Break, Sync and ProtectedID will be received --> discard them
35-
int bytes_received = -3;
35+
int bytes_received = -4;
3636
while (HardwareSerial::available())
3737
{
3838
if (bytes_received >= (8 + 1)) // max 8x Data + 1x Checksum
3939
{
4040
// receive max 9 Bytes: 8 Data + 1 Chksum
4141
break;
4242
}
43-
if (bytes_received < 0)
43+
switch (bytes_received)
44+
{
45+
case -4: //??
46+
case -3: // break = 0x00
47+
case -2: // sync = 0x55
48+
case -1: // Protected ID
4449
{
4550
// discard Sync and PID (send by us)
46-
HardwareSerial::read();
47-
// TODO: Sync and PID may to be verified here
51+
uint8_t buffer = HardwareSerial::read();
52+
// Sync and PID may to be verified here
53+
if (buffer == 0x00) { // break
54+
bytes_received = -3;
55+
}
56+
if (buffer == 0x55) { // sync
57+
bytes_received = -2;
58+
}
59+
if (buffer == ProtectedID) { // PID
60+
bytes_received = -1;
61+
}
62+
break;
4863
}
49-
else
50-
{
64+
default: // Data 0...7, Checksum
5165
// Receive and save only Data Byte (send by slave)
5266
LinMessage[bytes_received] = HardwareSerial::read();
5367
}
@@ -58,6 +72,14 @@ bool Lin_Interface::readFrame(uint8_t FrameID)
5872

5973
// erase data in buffer, in case a 9th or 10th Byte was received
6074
HardwareSerial::flush();
75+
while (HardwareSerial::available()) {
76+
HardwareSerial::read();
77+
if (verboseMode > 0)
78+
{
79+
Serial.print("additional byte discarded\n");
80+
}
81+
}
82+
6183
HardwareSerial::end();
6284

6385
// verify Checksum
@@ -88,20 +110,20 @@ bool Lin_Interface::readFrame(uint8_t FrameID)
88110
/// @brief write a complete LIN2.0 frame without request of data to the lin-bus
89111
/// @details write LIN Frame (Break, Synk, PID, Data, Checksum) to the Bus, and hope somebody will read this
90112
/// Checksum Calculations regarding LIN 2.0
91-
/// The data of this frame is 'size' long and incuded in the Lin_Interface::LinMessage[] array
113+
/// The data of this frame is 'dataLen' long and incuded in the Lin_Interface::LinMessage[] array
92114
/// @param FrameID ID of frame (will be converted to protected ID)
93-
/// @param size count of data within the LinMessage array (containing only the data) should be transmitted
94-
void Lin_Interface::writeFrame(uint8_t FrameID, size_t size)
115+
/// @param dataLen count of data within the LinMessage array (containing only the data) should be transmitted
116+
void Lin_Interface::writeFrame(uint8_t FrameID, uint8_t dataLen)
95117
{
96118
uint8_t ProtectedID = getProtectedID(FrameID);
97-
uint8_t cksum = getChecksum(ProtectedID, size);
119+
uint8_t cksum = getChecksum(ProtectedID, dataLen);
98120

99121
// übertragung startet
100122
HardwareSerial::begin(baud, SERIAL_8N1);
101123
writeBreak(); // initiate Frame with a Break
102124
HardwareSerial::write(0x55); // Sync
103125
HardwareSerial::write(ProtectedID); // PID
104-
for (int i = 0; i < size; ++i)
126+
for (uint8_t i = 0; i < dataLen; ++i)
105127
{
106128
HardwareSerial::write(LinMessage[i]); // Message (array from 1..8)
107129
}
@@ -183,16 +205,16 @@ void Lin_Interface::writeFrame(uint8_t FrameID, size_t size)
183205
/// TODO: function needs to be verified
184206
/// send Frame (Break, Synk, PID, Data, Classic-Checksum) to the Bus
185207
/// Checksum Calculations regarding LIN 1.x
186-
void Lin_Interface::writeFrameClassic(uint8_t FrameID, size_t size)
208+
void Lin_Interface::writeFrameClassic(uint8_t FrameID, uint8_t dataLen)
187209
{
188210
uint8_t ProtectedID = getProtectedID(FrameID);
189-
uint8_t cksum = getChecksum(0x00, size);
211+
uint8_t cksum = getChecksum(0x00, dataLen);
190212

191213
HardwareSerial::begin(baud, SERIAL_8N1);
192214
writeBreak(); // initiate Frame with a Break
193215
HardwareSerial::write(0x55); // Sync
194216
HardwareSerial::write(ProtectedID); // ID
195-
for (int i = 0; i < size; ++i)
217+
for (int i = 0; i < dataLen; ++i)
196218
{
197219
HardwareSerial::write(LinMessage[i]); // Message (array from 1..8)
198220
}
@@ -249,9 +271,9 @@ uint8_t Lin_Interface::getProtectedID(uint8_t FrameID)
249271
/// https://microchipdeveloper.com/local--files/lin:specification/LIN-Spec_2.2_Rev_A.PDF
250272
/// 2.8.3 Example of Checksum Calculation
251273
/// @param ProtectedID initial Byte, set to 0x00, when calc Checksum for classic LIN Frame
252-
/// @param size length of Frame (only Data Bytes)
274+
/// @param dataLen length of Frame (only Data Bytes)
253275
/// @returns calculated checksum
254-
uint8_t Lin_Interface::getChecksum(uint8_t ProtectedID, size_t size)
276+
uint8_t Lin_Interface::getChecksum(uint8_t ProtectedID, uint8_t dataLen)
255277
{
256278
uint16_t sum = ProtectedID;
257279
// test FrameID bits for classicChecksum
@@ -263,8 +285,8 @@ uint8_t Lin_Interface::getChecksum(uint8_t ProtectedID, size_t size)
263285
}
264286
// sum up all bytes (including carryover to the high byte)
265287
// ID allready considered
266-
while (size-- > 0)
267-
sum += LinMessage[size];
288+
while (dataLen-- > 0)
289+
sum += LinMessage[dataLen];
268290
// add high byte (carry over) to the low byte
269291
while (sum >> 8)
270292
sum = (sum & 0xFF) + (sum >> 8);

src/Lin_Interface.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ class Lin_Interface : private HardwareSerial
2828

2929
bool readFrame(uint8_t FrameID);
3030

31-
void writeFrame(uint8_t FrameID, size_t size);
32-
void writeFrameClassic(uint8_t FrameID, size_t size);
31+
void writeFrame(uint8_t FrameID, uint8_t datalen);
32+
void writeFrameClassic(uint8_t FrameID, uint8_t datalen);
3333

3434
protected:
3535
uint32_t m_bitCycles;
3636
size_t writeBreak();
3737
uint8_t getProtectedID(uint8_t FrameID);
38-
uint8_t getChecksum(uint8_t ProtectedID, size_t size);
38+
uint8_t getChecksum(uint8_t ProtectedID, uint8_t datalen);
3939
};

0 commit comments

Comments
 (0)