Am I complicating things?
I am archiving my code to talk from the 8051 microphone to a peripheral device via UART. The peripheral device responds to commands from the host and can only respond to one command at a time. This is a simple send and receive protocol. (tx1, rx1, tx2, rx2, tx3, rx3) Each TX message ends with CR, each response ends with>. I cannot send a new message until I receive a reply to the last one. Responses can also echo the original TX message at the beginning if I enable this option (but this causes more traffic)
Message example:
Or with the echo option ...
- TX: hello
- RX: Hello \ rWorld! >
Option A A function such as getHello will consist of sending and receiving. The parallel ISR procedure will collect the incoming bytes and throw a flag when it receives the '>' character.
char* getHello(char * buf){ sendMsg("Hello\r"); delay(10ms); //wait a little bit //wait for receive to come in or timeout to occur while(!receiveFlag || !timeoutFlag); //thrown by ISR receiveMsg(buf); //parse the message and do some other stuff return buf; }
Pros:
- Everything is contained in one function.
- Easier to debug
Minuses:
- This function is blocked and may freeze if the peripheral device never answers, so you need to time out.
- Messages cannot be received out of order, must be sequentially (i.e. tx1, rx1, tx2, rx2, tx3, rx3)
Option B A parallel approach is applied. Two separate functions are created. one to send the message, and one that will be the tops when receiving a response from the ISR.
void sendHello(){ sendMsg("Hello\r"); //do some other stuff if needed } char* receiveMsg(char * buf){ //figure out from echo print what the tx message was //use a switch statement to decide which response parser to call switch(txMessage){ //pseudo code case "Hello": receiveMsg(buf); //parse the message and do some other stuff break; } return buf; }
Pros:
- It can handle concurrent messages that fail because it relies on echo printing the tx message to figure out how to parse it. (i.e. tx1, tx2, tx3, rx1, rx2, rx3)
Minuses:
- quite difficult to debug
- generates multiple threads
- a lot of extra code
- not worth it, as the messages will definitely be back in order
Now I am doing option B, but as the project continues, I begin to feel that it is becoming too complicated. I'm curious what you guys think.
Thanks!
source share