python / arduino客户端/服务器通信中的速率限制

时间:2014-04-25 16:48:53

标签: python sockets arduino client-server

我正在尝试让python客户端与arduino服务器通信。 python客户端要求服务器从声纳中进行测量,然后服务器只发送一条确认消息然后接收消息。

客户:

client.py

import socket
import time

while True:
    try:
        client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client_socket.connect(("192.168.0.250", 10220))
        data = "GET\nSONAR\n\n"
        print 'send to server: ' + data
        client_socket.send(data)
        receive = client_socket.recv(2048)
        print receive
        client_socket.close()
        time.sleep(0.1)
    except Exception as msg:
        print msg

和服务器:

server.ino

#include <avr/wdt.h>
#include <SPI.h>
#include <Ethernet.h>
#include <SoftwareSerial.h>

//localClient parameters, for sending data to the server.
byte mac[] = {0x90, 0xA2, 0xDA, 0x0F, 0x03, 0x58};
byte ip[] = {192,168,0,250};
byte server[] = {192, 168, 0, 100};
int serverPort = 8220;

//Server parameters, for acting like a server.
int pollPort = serverPort + 2000;
EthernetServer pollServer = EthernetServer(pollPort);
//char inString[32]; // string for incoming serial data

//sonar stuff
String content_string;
int NUM_SONARS = 1;
int sonarPin[] = {2, 3, 4, 5};
int triggerPin = 6;
int sonarThreshold = 12.0;
int sonarState[] = {0, 0, 0, 0};
long pulse;
int numPulses = 3;
int pulseArray[] = {0,0,0,0,0};
int filteredMode = 0;
float time;

void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac, ip);

  wdt_enable(WDTO_8S);

  pollServer.begin();

  for(int i = 0; i < NUM_SONARS; i++) {
    pinMode(sonarPin[i], INPUT);
  }
  pinMode(triggerPin, OUTPUT);
  digitalWrite(triggerPin, LOW);
  time = 0;
}

void loop() {
  wdt_reset();
  time = millis();

  EthernetClient pollClient = pollServer.available();
  if (pollClient) {
    boolean currentLineIsBlank = true;
    String receivingString = "";
    while (pollClient.connected()) {
      //while the socket is open
      if(pollClient.available()) {
        //and there is something to read on the port, then read the available characters
        char c = pollClient.read();
        receivingString += c;
        if (c == '\n' && currentLineIsBlank) {
          Serial.print("String received -- ");
          Serial.print(receivingString);
          Serial.print("at ");
          Serial.println(time);
          pollClient.println("Received message.");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);

    // parse the incoming data
    String command = split(receivingString,'\n',0);
    String payload = split(receivingString,'\n',1);


    String key = split(payload,'=',0);
    String value = split(payload,'=',1);

    //PARSE THE KEY AND VALUE NOW
    if(command == "GET") {
      //if I received a GET command, send a response to the client.

      if(key == "SONAR") {
        pingSonars();
      }
    }
}

String split(String data, char delimiter, int index) {
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;

  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==delimiter || i==maxIndex){
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }

  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}

void isort(int *a, int n) {
  for (int i = 1; i < n; ++i) {
    int j = a[i];
    int k;
    for (k = i - 1; (k >= 0) && (j < a[k]); k--) {
      a[k + 1] = a[k];
    }
    a[k + 1] = j;
  }
}

int mode(int *x,int n){

  int i = 0;
  int count = 0;
  int maxCount = 0;
  int mode = 0;
  int bimodal;
  int prevCount = 0;
  while(i<(n-1)){
    prevCount=count;
    count=0;
    while(x[i]==x[i+1]){
      count++;
      i++;
    }
    if(count>prevCount&count>maxCount){
      mode=x[i];
      maxCount=count;
      bimodal=0;
    }
    if(count==0){
      i++;
    }
    if(count==maxCount){//If the dataset has 2 or more modes.
      bimodal=1;
    }
    if(mode==0||bimodal==1){//Return the median if there is no mode.
      mode=x[(n/2)];
    }
    return mode;
  }
}

void printArray(int *a, int n) {

  for (int i = 0; i < n; i++)
  {
    Serial.print(a[i], DEC);
    Serial.print(' ');
  }
  Serial.println();
}

void pingSonars() {
  digitalWrite(6, HIGH);
    for(int i = 0; i < NUM_SONARS; i++) {
    for(int j = 0; j < numPulses; j++) {
      pulse = pulseIn(sonarPin[i], HIGH);
      pulseArray[j] = pulse/147; //convert to inches -- 147 uS per inches
      delay(5);
    }
    isort(pulseArray, numPulses);
    filteredMode = mode(pulseArray,numPulses);
    //printArray(pulseArray,numPulses);
    Serial.print("Filtered distance for Sonar ");
    Serial.print(i);
    Serial.print(": ");
    Serial.println(filteredMode);
    if((filteredMode < sonarThreshold) && !sonarState[i]) {
      //if we are closer than the threshold and previously were not, this is a rising edge:
      Serial.print("Sonar ");
      Serial.print(i);
      Serial.println(" triggered!");
      sonarState[i] = 1;
    }
    else if (filteredMode > sonarThreshold && sonarState[i]) {
    //if we are greater than the threshold and previously were, this is a falling edge:
      Serial.print("Sonar ");
      Serial.print(i);
      Serial.println(" falling!");
      sonarState[i] = 0;
    }
  }
}

客户端以大约100毫秒的间隔发送请求,但服务器似乎能够响应慢得多 - 可能每秒2-3次。什么限制了沟通的速度?串行打印输出是否有可能限制它?或者它与我如何在服务器代码中打开/关闭/监听端口有关?

感谢您的帮助!

0 个答案:

没有答案