EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / Cannot Receive 4digit Integer value from serial port

Cannot Receive 4digit Integer value from serial port

|

Microcontroller › 8051 › Cannot Receive 4digit Integer value from serial port

  • This topic has 10 replies, 2 voices, and was last updated 11 years, 4 months ago by Bakhtiar.
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • December 6, 2014 at 10:17 am #3339
    Bakhtiar
    Participant

    Dear Concern,

     

    I have send 1 digit value from serial port and it received and work but when i am sending 4 digit integer value at a time from serial port it cannot reveive the value it receive only 1 digit value. Can any one help me.

    Thanks in advanced.

     

    Bakhtiar

    December 7, 2014 at 7:24 pm #12400
    Ashutosh Bhatt
    Participant

    from serial port each and every letter or digit is received as ASCII value that is also saparately

    that means if you want to send 4 digit value as 3425 then serial port will send 4 ASCII values

    33h, 34h, 32h and 35h

    you have to store all four values in an array (or table)

    then you have to write a program to convert all 4 ASCII values into digits and finally combine them into single value

    December 10, 2014 at 9:17 am #12408
    Bakhtiar
    Participant

    Thanks a lot about your reply.

     

     

    Ok if i use array then my program is receive using serial first time data here is my code.

     

    void compare()
    {

    if(strcmp(msg,”1234″)==0)
    {
     
         lcdcmd(0x01);
         lcddata(‘B’);
        
       }
    if (strcmp(msg,”4321″)==0)
    {

     
         lcdcmd(0x01);
         lcddata(‘C’);
            

       }
    }

     

     

    void recieve() interrupt 4
    {

    int i;
    while (RI != 1) {;}

    msg[i++] = SBUF;
    RI = 0;    

            compare();
        
     
     }

     

     

    for example: after running the code it take any one of the input

     

    if input “1234”

     

    then it worked.

     

    but at the same time when i input

     

    “4321”

     

    it will not take.

     

    please help me

     

    December 12, 2014 at 11:11 am #12416
    Ashutosh Bhatt
    Participant

    you have not initialized i as i=0

    also after getting all four codes you have to again reset i to 0

    December 12, 2014 at 1:23 pm #12419
    Bakhtiar
    Participant

    Thanks a lot sir,

     

    Your instruction is finally give me solution.I have appled and its done successfull.

     

    I reset the “i”  and it worked.

     

     

    Thanks again Sir.

     

     

     

    December 17, 2014 at 5:57 pm #12422
    Ashutosh Bhatt
    Participant

    good

    now post your working code your project details

    so that other can get ideas

    December 18, 2014 at 4:11 pm #12431
    Bakhtiar
    Participant

    It work fine but lettle problem i faceing, this is

    the define string compare is ok for string compare but if i again press the same keys the
    it will take only first digit for example

    #include<reg51.h>
    #include<string.h>
    #define dataport P2
    int  i=0;
    unsigned char msg[20];
    void lcdcmd(unsigned char);
    void lcddata(unsigned char);

    void compare()
    {

    if(strcmp(msg,”1234″)==0)
    {
         
         lcdcmd(0x01);
         lcddata(‘B’);
        
         i=0;
        
       }
     if (strcmp(msg,”4321″)==0)
    {

     
         lcdcmd(0x01);
         lcddata(‘C’);
        
         i=0;
            }
    }

    void recieve() interrupt 4
    {

    while (RI != 1) {;}

    msg[i++] = SBUF;
        RI = 0;

            compare();
        
             
     }

    void main()
    {
    while(1);
    }
    void lcdcmd(unsigned char value)
    {   dataport=value;
          rs=0;
          rw=0;
          en=0;
          delay(1);
          en=1;
    }

    void lcddata(unsigned char value2)
    {  dataport=value2;
          rs=1;
          rw=0;
          en=0;
          delay(1);
          en=1;
    }

    if again press “1” then the first compare block will display ‘B’ it will
    not take “234”
    but if i press another digit block that is “4321” it will take that one and display ‘C’
     again if i press first 4 digit then
    it will display ‘B’

    it will still not empty the fist digit block
    but take second digit block again it will take first digit block so on.

    December 21, 2014 at 6:29 pm #12442
    Ashutosh Bhatt
    Participant

    in your compare() function

    after first condition if(….)

    next condition must be else if (….)

    not just if

    December 22, 2014 at 5:36 pm #12445
    Bakhtiar
    Participant

    still it will take the first block value ,i mean the array value is not empty.

     

    if i press another block of value then previous array value will empty.

     

    for example

     

    first time

     

    when press “1234” then lcd display

     

    if i again press only “1” of that block that time also lcd display.But i want if i again press “1234” second time then lcd will display.

     

     

    after your advice i change my code in below way.

     

    void compare()
    {

    if(strcmp(msg,”1234″)==0)
    {
         
         lcdcmd(0x01);
         lcddata(‘B’);
        
         i=0;
        
       }
    else if (strcmp(msg,”4321″)==0)
    {

     
         lcdcmd(0x01);
         lcddata(‘C’);
        
         i=0;
            }
    }

    void recieve() interrupt 4
    {

    while (RI != 1) {;}

    msg[i++] = SBUF;
        RI = 0;

            compare();
        
             
     }

     

    but still face problem

    December 24, 2014 at 10:17 am #12454
    Ashutosh Bhatt
    Participant

    your compare function should be only called after you receive all digits like 4 or 5 etc, 

    you are calling compare function everytime when you receive any byte

    December 24, 2014 at 11:13 am #12455
    Bakhtiar
    Participant

    Thanks your valuable answer.

     

    Please ,

    Can you tell how can i do this or any example. Then it will be very helpfull for me.

     

     

    Thanks

     

     

  • Author
    Posts
Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Getting into an LED bulb April 21, 2026
  • understanding of resonance in time domain April 21, 2026
  • Beginner Questions About CNC Machines – G-code, Control Systems & Accuracy April 21, 2026
  • A Must-Watch Video Showing Dangerous Construction of Cheap Lithium-Ion Cells April 21, 2026
  • S1MJ ? April 20, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise