示例#1
0
void loop() {

	static boolean packetActive = false;
	static uint8_t index = 0;
	static boolean readingReturn = false;
	static uint8_t rindex = 0;

	if (!client.connected()) {
		// try to connect to a new client
		client = server.available();
	} else {
		// read data from the connected client
		if (client.available() > 0) {
			char c = client.read();

			if (packetActive) {
				commandBuffer[index] = c;
				commandBuffer[++index] = 0;
				if (c == EOP) {
					comHandler();
					packetActive = false;
				}
			}

			else if (c == SOP) {
				index = 0;
				commandBuffer[index] = c;
				commandBuffer[++index] = 0;
				packetActive = true;
			}

			if (returnReady) {
				client.println(returnBuffer);
				server.println(returnBuffer);
				Serial.println(returnBuffer);
				returnReady = false;
			}
		}
	}

	if (Serial.available() && !returnReady) {
		char s = Serial.read();

		if (s == SOP) {
			readingReturn = true;
			rindex = 0;
		}
		if (readingReturn) {
			returnBuffer[rindex] = s;
			returnBuffer[++rindex] = 0;

			if (s == EOP) {
				returnReady = true;
			}
		}
	}
}
示例#2
0
int main()
{
  char buffer[1000];
  HO820ComHandler comHandler( "/dev/ttyUSB0" );

  comHandler.SendCommand( "OP1" );
  comHandler.ReceiveString( buffer );

  std::cout << "BUFFER: " << buffer << std::endl; /////////////////////////////////

  sleep( 1 );

  comHandler.SendCommand( "SU1:12.34" );
  comHandler.ReceiveString( buffer );
  
  std::cout << "BUFFER: " << buffer << std::endl; /////////////////////////////////
}