Click here to Skip to main content
Click here to Skip to main content

Digital Thermometer Using C# and a Microcontroller

By , 4 Aug 2009
 
thermometer.png

Introduction

Usually we all have thermometers in our homes but what when it comes to see it digitally on your computer or to keep a record of it on your computer. To provide such an ease, I have created a piece of hardware to detect temperature and to interface it with the computer, so that the temperature can be shown or recorded there.

NOTE: This application requires some hardware to send relevant data to the application through a serial port. Without such a device, it won't work.

Contents

  • Procedural Steps
  • Hardware Details
  • Software Details
    • Configuring Serial Port
    • Receiving Data
    • Sampling Data
    • Creating Graphics
    • Displaying Data

Procedural Steps

Temperature Sensor ----> Atmega16 microcontroller ------> computer Serial Port

(LM35)                                    (sender)                                               (receiver)

Here what I try to explain is that a temperature sensor LM35 detects the temperature and passes an accordingly scaled voltage to microcontroller which converts it into digital data.

This digital data is our temperature reading which is then transmitted to our application via Serial Port on our computer, using Asynchronous Serial Transmission between microcontroller and computer.

lm35.GIF

This is the temperature sensor.

ATMEGA16-pinout.jpg

This our atmega16 microcontroller which creates digital temperature reading and transfers it to the computer. It's an 8-bit microcontroller with 16kb flash memory enough for long programs.

For programming my microcontroller, I used AVR Studio 4 Platform and C language.

The code for microcontroller just includes reading the sensor continuously and transmitting that data to our C# application through a serial port.

The code for the microcontroller is as follows:

#include<avr/io.h>
#include<avr/interrupt.h>
#define FOSC 12000000// Clock Speed
#define F_CPU 12000000ul
#define BAUD 9600
#define MYUBRR (FOSC/16)/BAUD -1
//#include<util/delay.h>

//initialise USART
void USART_Init( unsigned int ubrr)
{
//Set baud rate 
UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;
//Enable receiver and transmitter 
UCSRB = (1<<RXEN)|(1<<TXEN);
//Set frame format: 8data, 2stop bit, NO parity
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}

//Initialise A to D converter
void ADC_Init()
{
//enable adc

ADCSRA |= (1<<ADEN);

//enable interrupts

ADCSRA |= (1<<ADIE);

//set reference selection to Vcc;
//left adjust result
//set voltage selection to bit 7 of portA

ADMUX |= (1<<REFS0) | (1<<ADLAR) | (1<<MUX2) | (1<<MUX1) | (1<<MUX0);

//set prescalar to 128

ADCSRA |= (1<<ADPS0) | (1<<ADPS1) | (1<<ADPS2);// | (1<<ADATE);
}

//Transmit Data
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) )
;
/* Put data into buffer, sends the data */
UDR = data;
}

//Interrupt A to D converter reading
ISR(ADC_vect)
{
unsigned char c;

//variable c stores data from ADCH register

c=ADCH;

USART_Transmit(c);
//next statement starts a new ADC conversion

ADCSRA |= (1<<ADSC);
}

int main( void )
{
USART_Init ( MYUBRR );
ADC_Init();
sei();

//start an adc conversion

ADCSRA |= (1<<ADSC);
while(1);
} 

Monitoring More Than One Sensor at a Time

Note: If you are new to microcontrollers and this is one of your beginning projects, then I suggest you to implement the above code only and work with one sensor. If you think you can, then nothing's better than that and go ahead.

This thing was demanded by a person (I guess Mr. Joel), that's why I am adding it here.

This shows how you can monitor upto 8 temperature sensors and pass their data to your computer.

In ADMUX register of Atmega16 microcontroller, bits MUX4.....MUX0 ( 5 bits) control that data from which PIN of PORT A ( out of 8 pins) will be used for digital conversion, so that it could be transmitted to the computer.

Here our basic idea is that after each conversion, we will keep on changing the PIN from which the reading is to be taken. This way we will read through PIN0 to PIN7 (all 8 one by one) and then again back to PIN0.

Below I show what value of MUX4....MUX0 bits in ADMUX register selects the channel (PIN of PORT A).

MUX4....MUX0 PORTA PIN which will be read
00000 PIN 0
00001 PIN 1
00010 PIN 2
00011 PIN 3
00100 PIN 4
00101 PIN 5
00110 PIN 6
00111 PIN 7

Now to implement it properly in code, we have the idea that as soon as a conversion completes and Interrupt Service Routine (ISR) is called, we will change the PIN for the next conversion. Here is how to do this:

 ISR(ADC_Vect)
{ 
//code for reading and transmitting conversion ( shown above as well)
unsigned char c;
c=ADCH;	
USART_Transmit(c); 

//Now read the value of MUX4,MUX3, MUX2, MUX1 & MUX0 bits of ADMUX register
//and increment it by one if less than 8 else make it 0.

unsigned char d;

d = ADMUX & 0x1F;

if(d < 7) 
{ d = d + 1;
  ADMUX |= d;
}
else
{ ADMUX |= 0;
}

//Now start a new conversion which will read the next PIN now
 ADCSRA |= (1<<ADSC); 
}    

Schematic

The temperature sensor is connected to the microcontroller only.

Many people out here demanded for a schematic, so here I am adding the connection diagrams. Hope this serves the purpose.

Below is the connection diagram of Atmega16 microcontroller with sensor LM35 (on the right). To the left in the image is the Crystal Oscillator, required in case someone wants a higher operating frequency than the internal 1MHz of the microcontroller (it serves the purpose). In case you are satisfied with internal frequency, then don't add this Crystal oscillator.

AtmegaLM35.jpg

Now comes the point of how to connect the hardware to the computer through a serial port. For this, you should be knowing that the computer Serial port adds at around 10v whereas microcontoller operates at around 5v. So for effective communication, we need a level converter (which may convert the ongoing voltages as per the devices on both sides). IC MAX232 serves this purpose, it's a general purpose cheaply available IC with just a simple connection as shown below:

max232.gif

So this was the hardware detailing. Now I would discuss some software part.

Software Details

In this section, I will discuss the code of my application which is used to get temperature readings from serial port and to display it.

By now, the data is available on our computer`s Serial Port, what we all need is an application to retrieve it, which is done here.

Configuring Serial Port

Now what comes first is to configure the Serial Port, i.e., to fix the Baud Rate, Set Parity, number of Data Bits to be received in a single packet of data and number of Stop Bits to be used.

First of all, we create a global System.IO.Ports.SerialPort object port in our class definition, which is then initialised in the load event of the Form.

And also a few variables which hold the values to be set for the properties of the port object.

  • portname holds the name of the serial port. In my computer, it was COM4.
  • voltage holds the reference voltage, which is actually the Vcc voltage of the microcontroller board and is set manually in this application. 4.65 volts here.
  • variable parity holds the Type of parity to be used in serial communication between the computer and the microcontroller, i.e. Even Parity, Odd Parity or No Parity. I have not used any parity.
  • BaudRate holds the value of Baud Rate of serial communication. I settled for a Baud Rate of 9600 bps.
  • StopBits holds the number of stop bits to be used. The possible values are 1, 2, or none. I have used 2 stop bits.
  • And finally databits variable which defines how many data bits are to be received during communication. It's always a good choice to use 8 data bits, as it's a standard byte size, so it becomes much easy to manipulate it.
 public partial class Thermometer : Form
    {
        public Thermometer()
        {
            InitializeComponent();
        }
        
        System.IO.Ports.SerialPort port;
        static public double voltage;
        static public string portname;
        static public Parity parity;
        static public int BaudRate;
        static public StopBits stopbits;
        public int databits;  
    private void Form1_Load(object sender, EventArgs e)
        {
        portname = "COM4";
            parity = Parity.None;
            BaudRate = 9600;
            stopbits = StopBits.Two;
            databits = 8;

            port = new System.IO.Ports.SerialPort(portname);
            port.Parity = parity;
            port.BaudRate = BaudRate;
            port.StopBits = stopbits;
            port.DataBits = databits;
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            port.Open();
    }

These were the serial port settings. The same communication settings are used in the microcontroller, so that communication can take place effectively.

Receiving Data

After the Serial Port is opened by calling port.open() method, the serial port is ready for receiving the data arriving at it.

DataReceived: To receive data at the application, we need to handle DataReceived event of SerialPort class.

port.Read method reads data from COM4 serial port and writes it into a single variable byte array bt.

 void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
        // read one byte data into bt
        port.Read(bt,0,1);
                  // all the code to sample data
    }

Sampling Data

The basic idea behind sampling data is that as the readings are received continuously, we take 100 values and calculate their average value so that a much fairer temperature reading is available. This gives us a single temperature reading to display.

To carry out this calculation, global double variable sum and int variable count are created.

sum adds up subsequent Temperature readings and count counts up the number of reading to see whether they have reached 100 or not.

This code comes under DataReceived event only ahead of the above code:

// the calculation on the right hand side calculates
// the temperature from the read value
sum += Convert.ToDouble(bt[0]) *voltage*100/255;
            
//this counts to 100
count++; 

Now as soon as the counts reaches 100, the calculated sum value is averaged by dividing it by 100. This sampled value is then stored in double temp variable, sum and count are again set to 0 value.

// single temperature reading
temp = sum / 100;
sum = 0;
count = 0; 

Now that we have a temperature reading with us, the next task is to calculate the angle of the arm so that the calculated temp could be displayed on the round meter image.

The angle will be stored in a variable angle. Now before calculating angle, simply give a look at the round meter. Look that the angle between certain values doesn't vary uniformly, that is between 20 - 30, the angle is less as compared to between 30 - 40 and 40 - 50. (See in the above image.)

So before calculating the angle, this needs to be taken care of.

By some analysis, I found some angles beginning from the 50 value on meter as being 0 degree value.

Creating Graphics

Basically creating graphics in this means that whenever a temperature value is obtained, the arm should not just get set to that reading on the meter but it should move there by subsequent rotation (as in some analog meter where the arm moves from initial value to final value).

To achieve this a method named Animate is created, which animates the arm from an initial reading to a final reading. The method takes Currentangle and FinalAngle as arguments.

This method increases the value of variable Currentangle by 1 in each step and keeps on calling itself recursively until the value reaches the FinalAngle value. At each step, the form1.Paint event is raised through code to render the value onto screen.

private void Animate(double Currentangle, double FinalAngle)
	{ 
		// if Final angle is negative then make it positive
		if (FinalAngle < 0)
                FinalAngle += 360;

	// if final angle is greater than 360 degree then reduce it
            if (Currentangle >= 360)
            {

                Currentangle = Currentangle - 360;
            }

	// if current angle is not within +0.5 and -0.5 of
	// the final angle value then execute if block
	
	if (!(Currentangle > FinalAngle-0.5 && Currentangle < FinalAngle + 0.5))
            {

                if (Currentangle > FinalAngle)
                {
			//decrement Currentangle
                    Currentangle -= 1;                    
                }
                else
                {
			//else Increment Current angle
                    Currentangle += 1;                    
                }
              	
                Form1_Paint(this, new PaintEventArgs(surface, DrawingRectangle));
                
                Animate(Currentangle, FinalAngle);
            }
	}

Now with this, all over our code to create animations. Next comes creating graphics on the screen.

Displaying Data

For creating meter on the screen, it is necessary that there should be no flickering of the screen as the graphics are created, so as to ensure that we use Buffered Graphics.

For this, a BufferedGraphics object buff is created and is initialised in the Load event of the form. Also a System.Drawing.Graphics object surface is created which just represents the graphics surface at which the images are drawn.

// create buffered graphics object for area of the form by using this.Bounds

buff = BufferedGraphicsManager.Current.Allocate(this.CreateGraphics(),this.Bounds); 
surface = buff.Graphics;

//ensure high quality graphics to users

surface.PixelOffsetMode = PixelOffsetMode.HighQuality;
surface.SmoothingMode = SmoothingMode.HighQuality;

Now moving onto the final Paint event of the form. Here first of all, we hold the images of the round meter and the meter arm in the Image class objects img and hand.

After this, the meter is drawn on the Form.

The arm is rotated to the calculated angle and is drawn on the screen.

The temperature is drawn on the screen by calling the DrawString method.

The whole of the code is written in a try - catch block and an InvalidOperationException is caught which ensures that if in some case graphics fail to render on the screen, then nothing causes the application to crash.

private void Form1_Paint(object sender, PaintEventArgs e)
{ 
	try
            {                
                Image img = new Bitmap(Properties.Resources.speedometer, this.Size);     
                Image hand = new Bitmap(Properties.Resources.MinuteHand)                
                surface.DrawImageUnscaled(img, new Point(0, 0));                
                surface.TranslateTransform(this.Width / 2f, this.Height / 2f);
                surface.RotateTransform((float)CurrentAngle);                
                surface.DrawImage(hand, new Point(-10, -this.Height / 2 + 40));           
                string stringtemp = displaytemp.ToString();
                stringtemp = stringtemp.Length > 5 ? stringtemp.Remove
		(5, stringtemp.Length - 5) : stringtemp;
                Font fnt = new Font("Arial", 20);
                SizeF siz = surface.MeasureString(stringtemp, fnt);
                surface.ResetTransform();
                

 LinearGradientBrush gd = new LinearGradientBrush(new Point(0,(int)siz.Height + 20), 
	new Point((int)siz.Width,0), Color.Red, Color.Lavender);
                
surface.DrawString(stringtemp, fnt, gd, new PointF(DrawingRectangle.Width / 2 - 
	siz.Width / 2, 70));                

                surface.DrawEllipse(Pens.LightGray, DrawingRectangle);

                surface.Save();
                buff.Render();		
		}
	catch(InvalidOperationException)
	{
		//code to handle exception
	}
}

This finishes all the coding part of the application.

Another interesting project which I have is to switch 230 - 250 volt A.C. home appliances from computer through a C# application.

A pure software application which I also plan to write here is the Isolated Storer. Copy or cut your files and folders and paste it in the GUI provided by this application, and then just delete them from your computer, It will store your data in Isolated Storage area on the hard disk and it will be visible to you through this application only. Copy it from this application and paste it back to your file system. No data will be lost. I plan to write it here as well soon....

Conclusion

With this, I provide the idea about hardware interfacing through serial port using C# applications.

Anyone like me who likes developing hardware for self use would have enjoyed reading this article.

This is the second time I am writing this article as it wasn't liked much earlier due to lack of explanation. I hope things are better this time up.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

HiteshSharma

India India
I am a siebel developer by profession and a C#, ASP.Net, Javascript, C, C++ developer by Hobby. I code mostly for fun and usually code to create utilities and applications to enhance, improve and ease out my work while working on my computer. I try to make my computer a better place to code. I work on electronics as well and like to create hardware which may integrate with my computer. Now a days i code mostly for web and spend more and more of my time on javascript.
 
visit my blog at: http://msphitesh.blogspot.in
Follow on   Google+

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionNice onememberMember 880732810-Apr-12 6:53 
Dear Hitesh,
 
thanx a lot for this tutorial. can you post pulse oximeter based on atmeg16. I am trying to make one but no fruit full result so far. also i want to sand the reading to pc and then display them on web page.
 
thanks
 
Raj
GeneralMy vote of 5memberAbinash Bishoyi30-Mar-12 1:38 
Awsome!!!
 
I mostly work on Arduino or variants, I will share my code for the same soon.
GeneralMy vote of 5memberP1119r1m5-Nov-11 7:23 
Nice!
Questionnegative thermometer [modified]memberunbeliever124-Aug-11 0:58 
how can i read sub-zero value from the LM35 ?
is it even possible ?
 
thanx

modified on Wednesday, August 24, 2011 7:27 AM

Questioncompliments sirmemberbenbas24-Jul-11 6:25 
this is a really enlightening project. i am greatly impressed by the simplicity and accuracy of the logic schemes you have used. congratulations sir
GeneralMy vote of 5memberJF201520-Jan-11 2:03 
Good work. I would've loved it even more if you'd used USB instead of the serial port.
GeneralMy vote of 5memberthatraja9-Dec-10 19:27 
Great work
GeneralRe: My vote of 5memberHiteshSharma13-Dec-10 18:03 
thnx!
QuestionHow to write programme to Micro Controllermemberzain_ali8-Dec-10 1:46 
I want to ask that how can upload program in micro controller.You provide c sample.Which hardware to use to write program to micro controller.can we use atmel micro controller instead.
Thanks.
AnswerRe: How to write programme to Micro ControllermemberHiteshSharma9-Dec-10 3:39 
see this: http://www.fischl.de/usbasp/
here you`ll find how to program a microcontroller. you`ll find all the details to develop the hardware as well if you feel like doing so or buying the same from some vendor.
GeneralPhidgets for Data Inputmemberdmbrider22-Dec-09 10:36 
Check out http://www.phidgets.com/. I have used these little jewels with great success, and they are very easy to program.
GeneralNice articlememberthompsons9-Dec-09 6:25 
Thanks for the nice article.
I was very impressed by the speedometer graghics.
Can you post the GDI code you used to generate this png file?
 
Regards,
Steve.
GeneralRe: Nice articlememberHiteshSharma9-Dec-09 6:52 
The background speedometer & minute hand images were found over web.
I just generated temperature text and adjusted minute hand positions as per temperature
QuestionHow can a microcontroller (89S52,or any) to measure the beats per minute of a pulse circuitmemberMember 407735121-Oct-09 8:57 
I am doing a project to accrurately measure the beats per minute of a pulse circuit connected to P1.0 of a microcontroller.(89S52,or any)
AnswerRe: How can a microcontroller (89S52,or any) to measure the beats per minute of a pulse circuitmemberHiteshSharma22-Oct-09 5:03 
Usually all UC`s have PWM and Timer/counter in them.... read ur UC`s datasheet and get information about them that how to use them through code......
this will solve ur prob.
QuestionHow can i display data from microcontroller to Pc using serialportmemberMember 407735127-Aug-09 9:07 
Hello,pls can someone help me with a code or giudeline on this project am currently working on.
my project is on reading data from a microcontroller (89s52) via serialport to Eight textboxes on a pc GUI using C#,am using Visual studio 2008.Am having problems on how to display the ASCII characters e.g(55,23,12,34,42,33,22,23,N 55,23,12,34,42,33,22,23,N,55,etc)
AnswerRe: How can i display data from microcontroller to Pc using serialportmemberviaducting19-Jun-12 2:44 
Try asking a question about this in the appropriate forum, you will get better replies than flooding an article's comments section with questions.
Questionreading data from a microcontroller (89s52) via serialport [modified]memberMember 407735127-Aug-09 9:06 
Hello,pls can someone help me with a code or giudeline on this project am currently working on.
my project is on reading data from a microcontroller (89s52) via serialport to Eight textboxes on a pc GUI using C#,am using Visual studio 2008.Am having problems on how to display the ASCII characters e.g(55,23,12,34,42,33,22,23,N 55,23,12,34,42,33,22,23,N,55,etc),also be able to store the read data into an access database using C#.
 
modified on Thursday, August 27, 2009 3:16 PM

AnswerRe: reading data from a microcontroller (89s52) via serialportmemberHiteshSharma27-Aug-09 12:00 
through serial port read data into byte arrays and then encode them into strings using any encoding. Once you get data store it in any database or wherever you want.
QuestionRe: reading data from a microcontroller (89s52) via serialportmemberMember 407735129-Aug-09 2:40 
Pls HiteshSharma,am new just a beginner in programing,i hv already Created an instance of System.IO.Ports.SerialPort class,and an avent handler for data received event using serialport.ReadExiting() method but it is displaying all the data in one textbox eg N44,24,55,12,25,22,33,11,N34,54,44,etc.pls can u help me with a code,PLEASE.I hv two projects all reading temperatures from different places,one has four sensors to display the ASCII values on four labels or textboxes and the second micontroller with Eight Temperature sensors displaying on Eight Labels or Textboxes,also storing them in an access database.
Pls HiteshSharma,i would be high grateful to you and this site if u could help me with a code using C#.Thanks for ur anticipated response.
AnswerRe: reading data from a microcontroller (89s52) via serialportmemberHiteshSharma29-Aug-09 3:39 
if your exact problem is reading single byte of data at a time then do this.
port.Read(bt,0,1);
 
here port is the instance of SerialPort class and bt is a byte array.
second argument 0 specifies that begin filling this array from 1st element n
2nd argument of 1 specifies that only one byte of data is to be read.
 
if you want to determine that from which particular sensor the data is coming then you`ll have to program your microcontroller accordingly, so that it may transmit an address byte ( specifying sensor whose data is being sent next) and then the data.
 
for any more queries do post.............
QuestionRe: reading data from a microcontroller (89s52) via serialportmemberMember 407735129-Aug-09 9:18 
Hello HiteshSharma,
i must appreciate your response to this my problem but u know am just a beginner in programming more especially C#,nd i hv tryed so many examples i got on d net but not working.please can u spare some seconds out of ur precious time to help me with some code.i have used hyper terminal on my system and am receiving data from the sensors e.g N55,44,22,12,23,54,23,11,N22,34,22,31,etc,the N stands for a carriage return or begining of a new sample.all i want is to display the data on the four textboxes or 8 textboxes as the case may be using C# in Visual studio 2008.
i hv already programmed the chips one for 4 sensors and the other for 8 sensors.
AnswerRe: reading data from a microcontroller (89s52) via serialportmemberHiteshSharma29-Aug-09 20:48 
using hyper terminal u can just view data n u cant direct it anywhere......
for ur c# application:
i hope from last reply u got that how to read data into a byte array. now to direct it to ur textbox u first need to convert that data into a proper string.
so do this, convert the first byte of the array (into which we are already reading data) into an integer and then convert it into a string.
 
int data=Convert.ToInt32(bt[0]);
textBox1.text=data.ToString();
 
do this for as many textboxes u want.
n if u have more problem please be a bit more precise n clear about it. Its my pleasure if i could be of any help.....
QuestionRe: reading data from a microcontroller (89s52) via serialportmemberMember 407735130-Aug-09 9:44 
okay here is my full the microcontroller chip(89S52)
 
#include
sbit start=p3^3;
sbit end=p3^4;
sbit oe=p3^5;
sbit clock=p3^2;
sbit relay1=P1^3;
sbit relay2=P1^4;
sbit relay3=P1^5;
sbit relay4=P1^6;
unsigned char aray []={'0','1','3','4','5','6','7','8','9'} ;
 
unsigned char temperature;
void delay(){
int k,L;
for(k=0;k<40;k++)
for(i=0;i<80;i++)
}
 
void initSerial()
{
TMOD=0x22;
TH1=0xFD;
SCON=0x50;
TR1=1;
EA=1;
ES=1;
}
void usDelay(int a)
{
TH0=256-(a/1.085);
TR0=1;
ET0=1;
}

void sendChar(unsigned char ch)
{
SBUF=ch;
while(!TI);
TI=0;
}
 
void sendReading(unsigned char val)
{
unsigned char l,m;
l=val%10;
m=(val/10)%10;
sendChar(aray[m]);
sendChar(aray[l]);
sendChar(',');
}
 
void serialRoutine() interrupt 4
{
if(RI==1)
{
value=SBUF;
switch(temperature)
{
case ('a'):
relay1=0;
break;
case ('b'):
relay2=0;
break;
case ('c'):
relay3=0;
break;
case ('d'):
relay4=0;


}
RI=0;
}
}
void main()
{

unsigned char reading;
P1=0;
initSerial();
usDelay(50);
while(1)
{
if(sensor==8)
sensor=0;
if(sensor==0)
send char('N');
p1=(p1&0xf8)+ sensor;
latch();
startConv();
wait();
get();
 
reading=P1;
sendReading(reading);
sensor++

}
}
void latch(){
ale=0;
dalay();
ale=1;
}
void startConv(){
dalay();
start=0;
delay();
start=1;
}
void wait(){
while(end==1);
}
void get(){
delay();
oe=0;
dalay();
oe=1;
}
 
HiteshSharma,this is my complete code for the microcontroller.it has an ADC0808 with 8 temperature sensors(Lm35),monitoring temperature at different places then feeding the data to pc via serial port.the data is to be displayed on eight textboxes.
 

im my c# application,it has eight textboxes or labels.afta creating an instance of the serial port,in my port_DataReceived event handler,i used this code below
 
textbox1.Invoke(new EventHandler(delegate{textbox1.text+=comport.ReadExisting();});
 
and this just continously adds data into one textbox.so my main problem is how to make the received data show on its specific textbox.text
when i run the program i get this on one textbox Confused | :confused: Frown | :(
55,23,12,34,42,33,22,23,N 55,23,12,34,42,33,22,23,N,55,etc but i want them to display on each of the 8 textboxes.text.
 
please i need your expertise help.Thanke for your precious time. Smile | :)
AnswerRe: reading data from a microcontroller (89s52) via serialportmemberHiteshSharma31-Aug-09 4:08 
you get this on one textbox because you are using text property of just one textbox i.e. textbox1.
 
try creating a data_received event handler of your serial port object and in it read the value first rather then doing all in just a single statement.
 
like in the data_received event handler
 
byte[] bt=new bt[1];
comport.Read(bt,0,1);
and then use
double reading=Convert.ToDouble(bt[0]);
textbox1.text= reading.ToString();
textbox2.text= reading.ToString();
textbox3.text= reading.ToString();
.
 
.
textbox8.text= reading.ToString();

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 5 Aug 2009
Article Copyright 2009 by HiteshSharma
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid