Go Down

Topic: Serial communication problem on Arduiono Nano Every (Read 2591 times) previous topic - next topic

micdzi

Hi Everyone,

I am pretty new here and also with arduino so please be forgiving. I have to boards, i stared with Arduino Leonardo and wrote a program in C# .NET using usb serial port with arduino and sends some binarry data to another device using arduino.

It works perfect when i use Leonardo, but i placed the same code to Arduino Nano Every and it looks like it is not communicating by serial port. I also tried to use build in serial port monitor and send some data, it should replay but it's not.

Can You help please ?

_
Regards Michael.

Juraj

code? can we see the sketch?


micdzi

Sure, Here's the code:
Code: [Select]


void setup(){
  Serial.begin(9600);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(13, OUTPUT);
}

void loop() {
  int pinClk = 8;
  int pinData = 9;
  String dataClk = "10101010101010101110101010101010101110101010101010101110101010101010101110101010101010101110101010101010101110101010101010101111";

  digitalWrite(pinClk, HIGH);
  digitalWrite(pinData, LOW);

  String str = "";
  String data = "";
  char chr;
  while (Serial.available() > 0) {
    chr = Serial.read();
    str += chr;
  }
  
  String command="";
  // COMMAND MASK #CC#DDDD...&
  if((str.substring(0,1)=="#") and (str.substring(3,4)=="#") and (str.indexOf('&')>3)){
     command = str.substring(1,3);
     data = str.substring(4,str.indexOf('&'));
  }else{
     if(command==""){command="0";} else { command="99";}
  }
 
  switch(command.toInt()){
    case 0: break;
    case 1:{
      if(data == "START"){
        Serial.println("#01#STARTED&");
      }
      break;
    }
    case 3:{
      Serial.println("K " +command +" D " +data);
      break;
    }
    case 4:{
      String tonePin = data.substring(0,2);
      String toneMhz = data.substring(3,7);
      String toneSec = data.substring(8,12);

      tone(tonePin.toInt(), toneMhz.toInt());
      delay(toneSec.toInt());
      noTone(tonePin.toInt());
      break;
    }
    case 10:{
      for(int j=0; j<3; j++){
        Serial.println("K " +command +" D " +data);
        for( int i=0; i<128; i++){
          if(dataClk.substring(i,i+1)=="0"){ digitalWrite(pinClk, LOW); } else { digitalWrite(pinClk, HIGH); }
          if(data.substring(i,i+1)=="0"){ digitalWrite(pinData, LOW); } else { digitalWrite(pinData, HIGH); }
          delay(9);
        }
        digitalWrite(pinClk, HIGH);
        digitalWrite(pinData, LOW);
      }
      break;
    }
  }
}


One more thing, after uploading the code there i can see this:
Code: [Select]
avrdude: jtagmkII_initialize(): Cannot locate "flash" and "boot" memories in description

Pins specified in code are acting proper so the code is uploaded successfully.

Juraj

does the other side (C# or Serial Monitor) use 9600 baud?

micdzi

does the other side (C# or Serial Monitor) use 9600 baud?
Yes, it would be too obvious :)
Anyway, like i mentioned before, it works with arduino Leonardo so everythnig is set up properly by side of C# and serial monitor.

I FOUND A SOLUTION !

It looks like it worked before but arduino was treating every string as a char (while loop worked faster than SerialAvailiable was updating) so that't why I haven't received answer from it so i started deeper debuging and discovered as below.

For example, when i sent START it returned
S
T
A
R
T

so i realized that SerialAvailiable is too slow, so i added delay(1) in while loop
Code: [Select]
  while (Serial.available() > 0) {
    chr = Serial.read();
    str += chr;
    delay(1);
  }


And it started to work !

Unfortunately i can send right now max 64 bits in one string, otherwise it will crush.
I tried to set baud rate for example 230400 but it's working the same way.

Mayby soeone have any idea how to fix it in more professional way ? :)

Anyway this is temporary solution, i can split longer strings to few shother than 62 chars (2 bits for \n)


groundFungus


Juraj

Yes, it would be too obvious :)
Anyway, like i mentioned before, it works with arduino Leonardo so everythnig is set up properly by side of C# and serial monitor.
on native USB on Leonardo the baud rate doesn't matter. it would work with different baud rates

sterretje

so i realized that SerialAvailiable is too slow, so i added delay(1) in while loop
...
...
Anyway this is temporary solution, i can split longer strings to few shother than 62 chars (2 bits for \n)
Serial.available is not slow; the problem is that at 9600 baud it takes about 1 ms to transfer a character. So one character is received and you enter the while-loop and read the character. The next character has not completely been received yet so the next iteration of the while-loop will find that the next character is not available yet.

Read the link in reply#5 or the updated version of it.

Note
'\n' is a single byte, not two bits or 2 bytes. "\r\n" would be two bytes. So what is your C# application sending?
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up