Announcement

Collapse
No announcement yet.

Arduino-Serial Problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Arduino-Serial Problem

    I have started working on the xPico-Mux/Arduino interface and have run into an odd behavior. I basically coded the macro from the Mux example into an Arduino sketch. It mostly works fine except in the response to the '1rb~80' command. All the response characters from the Ajax call are received on the Arduino but not the terminating '~'. Then, if I write the '1sb~' command to the xPico from the Arduino, the next byte I read from the xPico is the '~' character.

    Obviously, this causes a problem because I am expecting the '~' to define the end of the Ajax call. I've been able to ignore the problem to this point because I don't care what the content of the Ajax is but going forward, I'm going to need to parse that call to determine what data to forward to the xPico/end user.

    Here is the Arduino code:

    Code:
    boolean cmdError = false;
    
    volatile int lantronixState = 0;
    volatile int lantronixNextStep = 0;
    volatile char ch;
    
    
    int respbyteindex = 0;
    unsigned long respDataTime;
    
    #define LANTRONIX_SETHTTP			10
    #define LANTRONIX_COMMANDRESP		15
    #define LANTRONIX_CONNECTNOTIFY		17
    #define LANTRONIX_WAITCONNECTION	20
    #define LANTRONIX_CMDREADDATA		30
    #define LANTRONIX_RESPREADDATA		35
    #define LANTRONIX_SENDRESPONSE1		40
    #define LANTRONIX_SENDRESPONSE2		45
    #define LANTRONIX_ENDCONNECTION		50
    
    void setup()
    {
      Serial.begin(115200); // Initialize serial port to send and receive at 9600 baud
      Serial1.begin(115200);
    
      while (!Serial) {}
    
      delay(10000);                                          // delay 10 secs to allow xPico Wifi to boot up
      Serial.println("setup complete");
      lantronixState = LANTRONIX_SETHTTP;
    }
    
    void loop()
    {
    	switch (lantronixState)
    	{
    	case LANTRONIX_SETHTTP:
    		{
    			Serial1.print("1h");
    			Serial1.write(13);
    			lantronixState = LANTRONIX_COMMANDRESP;
    			lantronixNextStep = LANTRONIX_CONNECTNOTIFY;
    			respbyteindex = 1;
    			Serial.println("Leave SETHTTP");
    			break;
    		}
    	case LANTRONIX_CONNECTNOTIFY:
    		{
    			Serial1.print("W1r");
    			Serial1.write(13);
    			lantronixState = LANTRONIX_COMMANDRESP;
    			lantronixNextStep = LANTRONIX_WAITCONNECTION;
    			respbyteindex = 1;
    			Serial.println("Leave CON_NOTIFY");
    			break;
    		}
    	case LANTRONIX_COMMANDRESP:
    		{
    			if (Serial1.available())
    			{
    				ch = Serial1.read();
    				Serial.print(ch);
    				if (ch == 'K')
    				{
    					lantronixState = lantronixNextStep;
    				}
    				else if (ch == 'E')
    				{
    					cmdError = true;
    				}
    				else if (cmdError && ch == '\n')
    				{
    					cmdError = false;
    					Serial1.print("1k");
    					Serial1.write(13);
    					lantronixState = LANTRONIX_SETHTTP;
    				}
    			}
    			break;
    		}
    	case LANTRONIX_WAITCONNECTION:
    		{
    			if (Serial1.available())
    			{
    				ch = Serial1.read();
    				Serial.print(ch);
    				if (respbyteindex == 1)
    				{
    					if (ch == '1')
    					{
    						respbyteindex = 2;
    					}
    				}
    				else if (respbyteindex == 2)
    				{
    					if (ch == 'r')
    					{
    						lantronixState = LANTRONIX_CMDREADDATA;
    						Serial.println("Leave WAIT_CON");
    					}
    				}
    			}
    			break;
    		}
    	case LANTRONIX_CMDREADDATA:
    		{
    			Serial1.print("1rb~80");
    			Serial1.write(13);
    			lantronixState = LANTRONIX_COMMANDRESP;
    			lantronixNextStep = LANTRONIX_RESPREADDATA;
    			respDataTime = millis();
    			break;
    		}
    	case LANTRONIX_RESPREADDATA:
    		{
    			if (Serial1.available())
    			{
    				ch = Serial1.read();
    				Serial.print(ch);
    				if (ch == '~' || (millis() - respDataTime > 10))
    				{
    					lantronixState = LANTRONIX_SENDRESPONSE1;
    					Serial.println("Leave RESPREADDATA");
    				}
    				respDataTime = millis();
    			}
    			else if (millis() - respDataTime > 10)
    			{
    				lantronixState = LANTRONIX_SENDRESPONSE1;
    				Serial.println();
    				Serial.println("Leave RESPREADDATA");
    			}
    			break;
    		}
    	case LANTRONIX_SENDRESPONSE1:
    		{
    			Serial1.print("1sb~");
    			Serial1.write(13);
    			lantronixState = LANTRONIX_COMMANDRESP;
    			lantronixNextStep = LANTRONIX_SENDRESPONSE2;
    			Serial.println("Leave SENDRESP1");
    			break;
    		}
    	case LANTRONIX_SENDRESPONSE2:
    		{
    			Serial1.print(random(100));
    			Serial1.print("~");
    			Serial1.write(13);
    			lantronixState = LANTRONIX_COMMANDRESP;
    			lantronixNextStep = LANTRONIX_ENDCONNECTION;
    			Serial.println("Leave SENDRESP2");
    			break;
    		}
    	case LANTRONIX_ENDCONNECTION:
    		{
    			Serial1.print("1e");
    			Serial1.write(13);
    			lantronixState = LANTRONIX_SETHTTP;
    			lantronixNextStep = LANTRONIX_SETHTTP;
    			Serial.println("Leave ENDCON");
    			break;
    		}
    	}
    
    }  // end of loop

  • #2
    Your command requested 80 bytes from the MUX. The mux will send you up to 80 bytes unless you terminate the transfer by sending any byte to the MUX first.
    Your expectation that a single TCP packet will be transferred to you (up to 80 bytes) and then the termination character, is incorrect. The mux will continue to send data to your application until the transfer is expired (length) or you terminate it.

    Comment


    • #3
      Garry,
      Thanks for the quick reply.

      Ok, I can see that but then what is the point of the '~' character?

      Also, I tried setting the command request to 5 (fewer bytes than sent via Ajax) and I still didn't see the '~' character until after I sent a character to the xPico.

      Comment


      • #4
        Interesting - I would have expected you to receive the escape byte once your buffer was exhausted. This will require investigation.

        Comment


        • #5
          Garry,

          I just tested again, setting the buffer to 5 and this time it worked as expected. I've been working on this code for the past couple of days and I must have had something else goofed up.

          Anyway, it would be helpful if the xPico would close the data transfer when the Ajax request was completed. That way, I could use varying length Ajax requests without having to know what the buffer length needed to be before hand.

          Of course, I could put a terminator right in the Ajax request...

          Comment


          • #6
            Yes sir - I have experienced your pain. The only workaround answer I have at the moment - it add a terminating 'tag' or character on the end and parse on the fly.

            Comment

            Working...
            X