MightyPork / TinyFrame

A simple library for building and parsing data frames for serial interfaces (like UART / RS232)
MIT License
344 stars 111 forks source link

multiple instances with different frame structure? #21

Open asmwarrior opened 4 years ago

asmwarrior commented 4 years ago

Hi, I see in the readme file, it said:

TinyFrame is re-entrant and supports creating multiple instances with the limitation that their structure (field sizes and checksum type) is the same.

In my C++ desktop application, I have use the TinyFrame library for both UART and Socket communication. In Socket, I would like CRC disabled(Because this will save a lot of computation in the target ARM based circuit board) and in UART I would like CRC enabled. Is it possible?

Any workaround about this? For example, maybe, I can create two namespaces, and wrap those .h and .c files in different namespaces? Or, I just have to rename all the C global functions for another frame structure? Thanks.

MightyPork commented 4 years ago

Hi, unfortunately, as you already found, this isn't possible in the current implementation. Frame structure changes the typedefs and there are ifdefs to choose the checksum variant as well.

One could change it to use uint32_t for all checksums etc, and choose the checksum implementation dynamically ... at the cost of code size and performance. I don't want to merge that, but we could have two variants in the repository to choose from, that'd be fine with me. (I also wanted to add https://github.com/MightyPork/TinyFrame/pull/19, that's a very good idea, but the author didn't finish it and it seems abandoned...).

For the record, I used CRC over USB before, despite USB having native checksums, and it helped me track bugs that mangled the data frames. The algorithms are fast, maybe check if the slowdown is really noticeable for you first.

I'm not too familiar with C++, see if you get it working with the namespaces. I'm afraid there's no easy trick here..

asmwarrior commented 4 years ago

Hi, thanks for the reply, and it looks like C++ namespace is a good way to solve this issue.

I have attach the source files, and you can see I can use the two instance of different TinyFrame. And the result is:

print from Uart: 01 80 00 00 04 00 22 d8 ce 55 41 52 54 47 7d
print from Socket: 01 80 00 00 06 00 22 53 6f 63 6b 65 74

The way I use is to paste the content of the TF_Config.h file to the TinyFrame.h. And then I make two TinyFrame.h files and two TinyFrame.cpp files, which have different C++ namespace.

The main.cpp and usage.cpp will use those different kinds of TinyFrame structures.

two-TF.zip

asmwarrior commented 6 months ago

I see a fork for C++ implementation of the TinyFrame which should solve this issue.

marcelnabeck/TinyFramePlusPlus: Object-oriented implementation of TinyFrame in C++. — https://github.com/marcelnabeck/TinyFramePlusPlus