Go Down

Topic: need help speeding up serial communication or a different solution  (Read 1 time) previous topic - next topic

ghlawrence2000

Ok, all I can do is let you try the code I used.

Processing:-
Code: [Select]
int     Serial_SPEED=614400;

/******************************************************************************************************************
 Nothing to see here!!!!!!!
 *****************************************************************************************************************/

import processing.serial.*;
Serial  port = null;
int     capacity;
int     mysize;
int     mytime, mytimeE;
int     tt, tt1, tt2;
int     min;
int     sec;
boolean done = false;
// Wait for line from serial port, with timeout
String readLine() {
  String s;
  int    start = millis();
  do {
    s = port.readStringUntil('\n');
  } while ( (s == null) && ((millis() - start) < 3000));
  return s;
}

void setup() {
  String s;
  size(200, 200); // Dummy window for Serial
  println("Scanning serial ports...");
  for (String portname : Serial.list ()) {
    try {
      port = new Serial(this, portname, Serial_SPEED);
    }
    catch (Exception e) { // Port in use, etc.
      continue;
    }
    print("Trying port " + portname + "...");
    delay(1500);
    if (((s = readLine()) != null) && s.contains("HELLO")) {
      println("OK");
      break;
    } else {
      println();
      port.stop();
      port = null;
    }
  }
  for (int y=0; y<10; y++) {
    while (((s = readLine()) == null) || !s.contains("HELLO")) ;
    int mill=millis();
    int z=0;
    while (z<47999) {
      if (port.available() > 0) {
        char l= port.readChar();
        z+=1;
      }
    }
    int end=millis();
    println("Done z="+z+" time="+(end-mill)+"ms");
    s        = readLine();
    println("Time taken (uS)"+s);
  }
  exit();
}


Arduino:-
Code: [Select]
#define serialport SerialUSB
#define serialspeed 614400

unsigned long timer;
unsigned long timerb = 0;
const int latchPin = 12;
const int analogPin1 = A0;
const int analogPin2 = A1;
const int analogPin3 = A2;
const int analogPin4 = A3;
const int sampsize = 6000;   //6000;

uint16_t myVal[sampsize * 4];

void setup() {
  //delay(2000);//let processing get ready
  REG_ADC_MR = (REG_ADC_MR & 0xFFF0FFFF) | 0x00020000; // speeds up analog read from   http://www.djerickson.com/arduino/
  //now 4 analog reads down to about 20us

  serialport.begin(serialspeed);
  while (!serialport); // <- wait for port to open
  pinMode(latchPin, OUTPUT);
  analogReadResolution(12);
  delayMicroseconds(100);//delay 100us before measuring analog
  serialport.println("HELLO");
}

void loop() {
  for (int i = 0; i < sampsize * 4; i += 4) {
    myVal[i] = analogRead(analogPin1); //4 analog reads take about 8-10us
    myVal[i + 1] = analogRead(analogPin2);
    myVal[i + 2] = analogRead(analogPin3);
    myVal[i + 3] = analogRead(analogPin4);
    delayMicroseconds(89);
  }
  serialport.println("HELLO");
  timer = micros();
  serialport.write((char*)myVal, sampsize * 2 * 4);
  timerb = micros();
  serialport.println(timerb - timer);
  //while (1);
}


Results:-
Quote
Done z=47999 time=5ms
Time taken (uS)6650

Done z=47999 time=4ms
Time taken (uS)6118

Done z=47999 time=6ms
Time taken (uS)6110

Done z=47999 time=6ms
Time taken (uS)5964

Done z=47999 time=6ms
Time taken (uS)5964

Done z=47999 time=6ms
Time taken (uS)6010

Done z=47999 time=6ms
Time taken (uS)6158

Done z=47999 time=6ms
Time taken (uS)6019

Done z=47999 time=6ms
Time taken (uS)6170

Done z=47999 time=6ms
Time taken (uS)5958
Regards,

Graham
UTFT_SdRaw now included in library manager!! ;) High speed image drawing from SD card to UTFT displays for Mega & DUE.
UTFT_GHL - a VASTLY upgraded version of UTFT_CTE. Coming soon to a TFT near you! 8) Shipping April 1 2016!

philipjfry

Hi Graham, That is your arduino code when using the programming port! Do you have your code for when you used the Native port?

I.E. when you used SerialUSB.prints instead of Serial.prints

thank you

ghlawrence2000

Code: [Select]
#define serialport SerialUSB
  serialport.println("HELLO");


Last time I checked, SerialUSB is the native port!!

Code: [Select]
#define serialport Serial
  serialport.println("HELLO");


Would be the programming port no?

Regards,

Graham
UTFT_SdRaw now included in library manager!! ;) High speed image drawing from SD card to UTFT displays for Mega & DUE.
UTFT_GHL - a VASTLY upgraded version of UTFT_CTE. Coming soon to a TFT near you! 8) Shipping April 1 2016!

ghlawrence2000

Oh, I have been playing, you can change a variable to decide how many chunks of 48000 bytes to send now, and get statistics at the end of the run, here is a summary of 5 chunks of 48000 bytes ;) :-
Quote
Total Time taken to transfer 240000 bytes =30590(us)
Bytes per second =7845701.5
MB per second =7.482244
And 50 chunks of 48000 bytes :-
Quote
Total Time taken to transfer 2400000 bytes =302282(us)
Bytes per second =7939606.0
MB per second =7.5717983
No doubt this will upset you even more, this is using my 'secret' technique which everyone tells me 'makes no difference'! Judge for yourself... ;)  :D  :P
5 chunks :-
Quote
Total Time taken to transfer 240000 bytes =26394(us)
Bytes per second =9092976.0
MB per second =8.671738
50 chunks :-
Quote
Total Time taken to transfer 2400000 bytes =264328(us)
Bytes per second =9079628.0
MB per second =8.659008
Regards,

Graham
UTFT_SdRaw now included in library manager!! ;) High speed image drawing from SD card to UTFT displays for Mega & DUE.
UTFT_GHL - a VASTLY upgraded version of UTFT_CTE. Coming soon to a TFT near you! 8) Shipping April 1 2016!

philipjfry

 I am running out of ideas now and still nowhere near 6 ms.. What is your secret technique!!

CrossRoads

int     Serial_SPEED=614400;
unsigned int is only good for 2^16-1, or 65536.

614400 needs long, or unsigned long.
Or 614400UL;
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

ghlawrence2000

UTFT_SdRaw now included in library manager!! ;) High speed image drawing from SD card to UTFT displays for Mega & DUE.
UTFT_GHL - a VASTLY upgraded version of UTFT_CTE. Coming soon to a TFT near you! 8) Shipping April 1 2016!

philipjfry

Code: [Select]

for(int i = 0; i<sampsize*4-1; i=i+4)
    {
      myVals[i] = analogRead(analogPin1);
      myVals[i+1] = analogRead(analogPin2);
      myVals[i+2] = analogRead(analogPin3);
      myVals[i+3] = analogRead(analogPin4);
     
    }
   

    timer = micros();
    Serialport.write((byte*)myVals,48000);
    timerb = micros();

Serialport.println(timerb-timer);

 


from this simple code so just measure the write time
Im getting ~.52 seconds..

oqibidipo

int     Serial_SPEED=614400;
unsigned int is only good for 2^16-1, or 65536.

614400 needs long, or unsigned long.
Or 614400UL;
You seem to be forgetting the context: int is 32 bits on Due.

CrossRoads

Ack, you're right, I totally missed this was the Due forum.

Never mind :smiley-confuse:
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

ghlawrence2000

Im getting ~.52 seconds..
Wowsers!! Well it may not help your situation, but I have heard several people have trouble with the native port, recently someone even said in his experience it was slower than the programming port!! I am at a loss to what the cause may be. Do you have another DUE, another USB->Micro lead, tried a different USB port, unplugged all other USB devices?? What OS are you running? Is your PC half decent? Which version of the Arduino IDE are you using? One of these factors must be the culprit, you just need to track it down.

My secret technique? Get your speed to somewhere approaching my slower speed first! ;) :P

Regards,

Graham
UTFT_SdRaw now included in library manager!! ;) High speed image drawing from SD card to UTFT displays for Mega & DUE.
UTFT_GHL - a VASTLY upgraded version of UTFT_CTE. Coming soon to a TFT near you! 8) Shipping April 1 2016!

Go Up