01-08-2024 02:18 AM
@constructionworker wrote:
I think the '''\r'', '\n'' used here may be causing a problem because '\n' is used as the termination character of ''VISA Configure serial port'' in the labview.
How can we do this in labview?
Of course the \r\n is the problem. You try to send binary data which can contain this bytes anywhere in the data and then use these bytes also as termination character. Big NO-NO! You overload the meaning of those bytes with two entirely different things. The receiver has no reliable way of detecting what is data and where is your end of message without a lot of complex parsing, conditional logic and use of the Bytes at Serial Port only aggravates the problem.
In communication you have three types of messages:
1) (textual) messages with a special termination character that is guaranteed to not occur in the message itself
2) binary fixed size messages
3) fixed size header containing information about the length and type of the message that immediately follows
Anything else is going to cause you and whoever you burden with dealing with that protocol only a lot of grief and cursing.
So choose your poison from these 3 types and properly implement it. Don't mix and match various characteristics. Especially the use of binary data together with a termination character is a very bad choice.
01-09-2024 10:52 AM - edited 01-09-2024 11:11 AM
Thanks for your descriptive answer.You made a good observation.
@rolfk wrote:
Of course the \r\n is the problem. You try to send binary data which can contain this bytes anywhere in the data and then use these bytes also as termination character. Big NO-NO! You overload the meaning of those bytes with two entirely different things. The receiver has no reliable way of detecting what is data and where is your end of message without a lot of complex parsing, conditional logic and use of the Bytes at Serial Port only aggravates the problem.
bool sendPktRFD900(uint8_t *data, uint8_t len) { for (int i = 0; i < len; i++) { radioPort.write(data[i]); radioPort.flush(); } radioPort.println(); radioPort.flush(); // set flag radioFnctn = TXmode; TX = true; return true; }
I couldn't determine what change I could make in the code.
How can I add a fixed-size header that contains information about the length and type of the message immediately following it, and I think I should use it as the serial configure port termination character?
01-09-2024 12:35 PM - edited 01-09-2024 12:37 PM
For instance something like this
#define HEADER_MAGIC_1 0x12
#define HEADER_MAGIC_2 0x34
#define HEADER_MAGIC_3 0x56
bool sendPktRFD900(uint8_t *data, uint8_t len)
{
// Send 4 byte header
radioPort.write(HEADER_MAGIC_1);
radioPort.write(HEADER_MAGIC_2);
radioPort.write(HEADER_MAGIC_3);
radioPort.write(len);
// Send actual data
radioPort.write(buf, len);
// Flush, but that should not really be necessary
radioPort.flush();
// set flag
radioFnctn = TXmode;
TX = true;
return true;
}
- You don't need to send each byte separately, the Serial port has a function to send an entire byte array.
- You definitely should not flush the port after every byte, in fact flushing should not really be needed, but if you insists you can call it once at the end of the message.
In LabVIEW first disable termination character entirely.
Read 4 bytes, check that the first three are 0x12, 0x34, 0x56. If so use the 4th as indication how many bytes to read and do so, rinse and repeat.
01-10-2024 02:20 PM - edited 01-10-2024 02:21 PM
rolfk
Thank you very much.
As always, you diagnosed the problem directly 🙂 Your exact answer really helped me a lot.
I have added the code below for those who want it and encounter the same problem. The entire code is as follows. In this way, I achieved my goal. But perhaps some more improvements can be made in the code.
01-11-2024 12:02 AM
The full code is as follows.
01-11-2024 06:42 AM
Your architecture scares me. Using local variables to pass data to other loops is a major red flag. Learn to use Queues to pass the data from one loop to the next. A queue will make sure every data sample is processed as well as not use up CPU since it can sleep while waiting for data to come in.
01-11-2024 11:10 AM
@crossrulz wrote:
Your architecture scares me.
I fully agree. In addition, everything is extremely convoluted with way too many sequence structures and Rube Goldberg constructs, complicated data dependencies, way too many local variables, and way too much duplicate code. I think done correctly, the entire code would shrink to 20% and fit on a postcard.
01-11-2024 12:05 PM
@altenbach wrote:
@crossrulz wrote:
Your architecture scares me.
I fully agree. In addition, everything is extremely convoluted with way too many sequence structures and Rube Goldberg constructs, complicated data dependencies, way too many local variables, and way too much duplicate code. I think done correctly, the entire code would shrink to 20% and fit on a postcard.
With the new zoom feature, ALL code can fit on a post card. 😉
01-11-2024 12:30 PM
@billko wrote:
@altenbach wrote:
@crossrulz wrote:
Your architecture scares me.
I fully agree. In addition, everything is extremely convoluted with way too many sequence structures and Rube Goldberg constructs, complicated data dependencies, way too many local variables, and way too much duplicate code. I think done correctly, the entire code would shrink to 20% and fit on a postcard.
With the new zoom feature, ALL code can fit on a post card. 😉
Not quite, there is a limit to how much you can zoom out (25%). This code will need 2 post cards when fully zoomed out as is. If cleaned up, I agree with Christian that the whole code could potentially fit in 1/4 the size (locals take a lot of space, case structures layers could be removed, a lot of comparisons would be easily removed by wiring the numeric straight to the case structure, etc.).
01-12-2024 12:17 AM
@crossrulz wrote:
Your architecture scares me. Using local variables to pass data to other loops is a major red flag. Learn to use Queues to pass the data from one loop to the next. A queue will make sure every data sample is processed as well as not use up CPU since it can sleep while waiting for data to come in.
Thanks for your opinion.
Yes, I think your observation was good. Indeed, CPU usage is high when the program is running. For this, it is absolutely necessary to use Queue.
for Rube goldberg code;You are right that the code takes up a lot of space. Actually, I wanted to fix this, but I couldn't decide how to do it. My brain is tired to edit this right now.Maybe someone willing to help can help 🙂