Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem in serial receive 18f4550
14-11-2015, 08:37 AM,
#1
Problem in serial receive 18f4550
hello, i have problem in serial reception data my code is given below and serial window file  attached. i used pic18f4550 pinguino board.please tell what need to do. whats wrong in it???? n why ???????????

my code is

void setup() {
 Serial.begin(9600);
}

void loop() {
 Serial.print("pinguino");
}


Attached Files Thumbnail(s)
   
Reply
14-11-2015, 02:54 PM, (This post was last modified: 14-11-2015, 02:55 PM by pingotg.)
#2
RE: Problem in serial receive 18f4550
I think you need printf not print

(If print exists it likely needs at least 2 args.)

John
Reply
16-11-2015, 11:21 AM,
#3
RE: Problem in serial receive 18f4550
thanks for reply;

i got problem in my hardware. There is problem in Max232 TTL Level, So that i got Wrong data.

serial.print("Pinguino");

is working correctly.

we should check hardware, before complain about the code Idea
Reply
17-11-2015, 10:13 AM,
#4
RE: Problem in serial receive 18f4550
The wiki says serial.print( some string ) is not supported.

John
Reply
28-01-2016, 06:39 AM,
#5
RE: Problem in serial receive 18f4550
hello;
      i want to read serial data like string "turnon" , "turnoff". i got only first character read "t". how can we read whole string serialy.



char val; // variable to receive data from the serial port

void setup()
{
 pinMode(USERLED, OUTPUT);  // pin where on-board LED is connected as OUTPUT
 Serial.begin(9600);        // start serial communication at 9600bps
}


void loop()
{



 while ( Serial.available() )       // if data is available to read
   val = Serial.read();         // read it and store it in 'val'




 if ( val == 'turnon' )     {
  // if 'h' was received
   digitalWrite(USERLED, HIGH);  // turn ON the LED

 }        

   if ( val == 'turnoff' )    {           // if 'l' was received
   digitalWrite(USERLED, LOW);   // turn OFF the led
 
   }

 if ( val == 'a' )     {
  // if 'h' was received
   digitalWrite(USERLED, HIGH);  // turn ON the LED

 }        

   if ( val == 'b' ){           // if 'l' was received
   digitalWrite(USERLED, LOW);   // turn OFF the led
 
   }
   
 delay(100);                    // wait 100ms for next reading

}
Reply
28-01-2016, 04:09 PM, (This post was last modified: 28-01-2016, 08:05 PM by gtcbreizh.)
#6
RE: Problem in serial receive 18f4550
Hello !
You declare:
char val; // so val can receive only 1 character !
Then you think by :
val = Serial.read()
you can store in val strings as "turnon" or "turnoff". Perforce it does not work !!!

I prepare the following code which allows to enter string in the val array using the serialgetstring function.

It works for a single character 'a' or 'b' or a string as "turnon" or "turnoff" . 

For the led I changed USERLED into u8 ledpin=0 because on my board USERLED does not work.

new code :
// VERY IMPORTANT : terminate your string input by a "return" key

u8 val[10];
u8 ledpin=0;

void Serialgetstring(u8 *buffer)
{
u8 c;
u8 i = 0;

do {
while (!(Serial.available()));
c = Serial.read();
if (c != '\r') buffer[i++] = c;
} while (c != '\r');
buffer[i] = '\0';
}

void setup()
{
pinMode(ledpin, OUTPUT); // pin where on-board LED is connected as OUTPUT
Serial.begin(9600); // start serial communication at 9600bps

}

void loop()
{
Serialgetstring(val); // read a string and store it in 'val'
Serial.printf(" ptr=%s\n\r",val);

if ( strcmp( val, "turnon" ) == 0 ) {
digitalWrite(ledpin, HIGH); // turn ON the LED
}

if ( strcmp( val, "turnoff" ) == 0 ) {
digitalWrite(ledpin, LOW); // turn OFF the led
}

if ( val[0] == 'a' ) {
digitalWrite(ledpin, HIGH); // turn ON the LED
}

if ( val[0] == 'b' ){
digitalWrite(ledpin, LOW); // turn OFF the led
}

delay(100); // wait 100ms for next reading
}
Reply
29-01-2016, 02:17 PM,
#7
RE: Problem in serial receive 18f4550
yes...its working now, just we need to put there 

Serial.flush();

at end to erase past data and ready for next.  Idea

now my android app working with pinguino.

thank you very much.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)