473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

getting help with 89v51rd2 serial port data receiving using C

I'm trying to receive data from PC via serial port.
But so far I can't get the data in array of chars (char[]).
Here is the program I've got from my department.

Expand|Select|Wrap|Line Numbers
  1. #include <p89v51rd2.h> // Header file register for P89V51RD2FN
  2. unsigned char Get_Serial(void)
  3. {
  4.     bit Timeout=0;
  5.     unsigned char count=252, Data = 0;
  6.     while((Timeout == 0))
  7.     {
  8.         if(RI == 0)
  9.         count++; //waiting
  10.         else
  11.         {
  12.             Data = SBUF;
  13.             RI=0;
  14.             return(Data);
  15.         }
  16.         if(count <= 2)
  17.         {
  18.             Timeout = 1; //Timeout!!
  19.         }
  20.     }
  21.     return(Data);
  22. }
  23.  
  24. void Send_Serial(unsigned char x)
  25. {
  26.     TI = 0;
  27.     SBUF = x;
  28.     while(TI==0);
  29. }
  30.  
  31. void Serial_initial()
  32. {
  33.     TMOD = 0x21;
  34.     SCON = 0x50;
  35.     TH1 =0xFD;
  36.     TL1 =0xFD;
  37.     TF1 = 0;
  38.     TR1 = 1;
  39.     TI = 0;
  40. }
  41. void main(void) // Main loop
  42. {
  43.     Serial_initial();
  44.     P1 = 0x80;
  45.     while(1) // Infinite loop
  46.     {
  47.         Data = Get_Serial(); // ******** I'm thinking that the data gets in from this line
  48.          //My main loop which I need to apply a string like data.
  49.         }
  50. }
  51.  
If I send a data from PC as "10"
The program will only read '1'
which is not correct.

please give me some advice.
ps. sorry for my English, it's not my primary language.
Mar 15 '12 #1
1 2647
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1. Data = SBUF;
  2.  13.             RI=0;
  3.  14.             return(Data);
  4.  
causes Get_Serial to return after receiving one char. That must be your 1.

I do not see any arrays in this code.

main() needs to receive chars until a deliiter is reached that signals the end of the data. Maybe a newline char like \n.

I would expect code in main like:

Expand|Select|Wrap|Line Numbers
  1. char buffer[100]; //array of 100 chars
  2. etc...
  3. char delimiter= '\n';  //signals end of the data
  4. char data;  //the data received
  5. int i =0;
  6. while ((data =Get_Serial()!= delimiter )
  7. {
  8.   buffer[i] =data; //store data in correct array position
  9.   ++i; //et ready for next char to be received
  10. }
  11. buffer[i] = '\0';  //makes a C-string in the buffer array.
  12. etc...
  13.  
  14. }
The idea is to receive chars one after the other until you reach the end of the data. So sending 123 should result in 123 in the buffer.

You can append a \0 char to the array so the buffer will work with all C string functions and can be printed using printf with a %s formatter.
Mar 15 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: joe bloggs | last post by:
I am writing a mobile application to interface with a legacy system and I am planning to use web services to communicate with this system. The legacy system receives data through a serial port. ...
0
by: sandeepa | last post by:
Hello all I am using the serial port to receive data(7 bytes per second) from the microcontroller,receiving the data as a string and then splitting the string in two,to display 2 different...
8
by: Vivek Menon | last post by:
Hi, I am using a C program to write/read from a serial port. The writing part is working perfectly fine. However, I am not able to read the values correctly and display them. To debug this issue I...
4
by: Petr Jakes | last post by:
I am trying to save data it is comming from the serial port continually for some period. (expect reading from serial port is 100% not a problem) Following is an example of the code I am trying to...
2
by: tclancey | last post by:
Hi. I have a running app that listens to many virtual com ports connected to Ethernet to RS232 coverters. Connected to each multi-port converter is a bar code scanner, (RS232). The app runs...
2
by: pauland80 | last post by:
Hello, My soft passively listen to a device sending +- 300 bytes of data each second. After several hours of work, the soft abruptly stops receiving data without any error, (while the device...
1
by: Narjis | last post by:
Hi everybody? hope y'r all fine I need a small help.. can anyone give me a c++ program that reads the serial port data? (i.e. that data are burned in a microcontroller chip and i want to connect...
2
by: crampio | last post by:
Hello everyone, I generally look at Google and other websites before I post a question, but trust me I still cannot find and answer to this problem. I'm using VB.net. My problem being is that I...
3
by: Ajinkya | last post by:
How can I poll for serial port data using javascript ?
6
by: cnixuser | last post by:
Hello, I have a basic application written which is designed to data over a serial cable and then receive a response back. I am not getting any triggers to my data received event. I have tried...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.