Click here to Skip to main content
15,909,498 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello all,
i have problem to get numeric data in variabel (float, int or double) from serialport received in C#.
can all help me for that ???
Posted
Comments
Zoltán Zörgő 3-Jun-12 8:22am    
How is the data put in the serial stream? What format is used: text, bcd, little endian, big endian...?
Sergey Alexandrovich Kryukov 4-Jun-12 0:05am    
What problem?
--SA

See MSDN: SerialPort Class[^] for an example on serial port handling in C#.

There is no data protocol defined on serial port - this is pure bit stream with some rudimentary synchronization and error detection (start/stop bits, parity). You are not exposed to that in C#. What you get is a byte stream.

You need to specify how the sender/receiver encode data, e.g. how an integer is to be interpreted:
- size of the int (1,2,4 bytes)?
- endian-ness (little/big endian)?
- signed/unsigned?
- ...

In addition to that, you may also have some other data than int only.

I assume you have some "protocol" specification that tells what messages are transfered over the serial line.

Once you have that specification, you write a message parser. Please provide that specification (or part of it) so that we can give more specific advise.

Cheers
Andi
 
Share this answer
 
Comments
tetuko99 3-Jun-12 10:19am    
can you give me some example for me..!!
Andreas Gieriet 3-Jun-12 10:54am    
Example on what? Tell me what your problem is? E.g. what do you have as a setup? What device do you talk to over the serial line?

BTW: what means "..!!" - are you desperate? Any homework assignment?
tetuko99 3-Jun-12 11:48am    
yes you allright,,
i can't explain my problem clearly.
this my problem all,,
i have a homework from my teacher
this jobs is,,
how to show data temperatur, humidity and count from microcontroller to PC.
and then using GUI (graphic user interface) for show all data.

i'am sent data from microcontroller to PC using this script
printf ("%02d %03d %04d \n", temp, hum, count);

and on my GUI i recived all data using script
string data = serialport1.ReadExsiting();
and then i'am success for recive all data with string format and can viewed on textbox using this script
richTextBox1.AppendText(data);

but the problem is,, how i can divided data string into 3 data INTEGER ( temperatur, humidity, and count) ?????????

thanks,,
Zoltán Zörgő 3-Jun-12 15:36pm    
See my updated answer.
Andreas Gieriet 3-Jun-12 15:59pm    
Now, it's clear. See Zoltán's solution #2.
The C printf(...) creates a text of three integers in a row, separated by a space (plus a trailing space(?).
So you get a string on the C# side like 25 085 0001 . This is best split by taking space as separator and then try to parse as int numbers.
Cheers
Andi
No example will help you, if you don't know what you try to read.
Use SerialPort.Read [^] method to read as many bytes as you need for your datatype. Than, use BitConverter[^] class to convert the bytes read to the data type you need. But this is only the general approach. A serial protocol might be more complicated than that.

Update:

If you have a text protocol as you exposed finally, you need something like this:
C#
string[] values = data.Split(' ');
int temperature = int.Parse(values[0]);
int humidity = int.Parse(values[1]); 
int count = int.Parse(values[2]);

Or you can use this one: scanf replacement for .net[^]
 
Share this answer
 
v2
Comments
Andreas Gieriet 3-Jun-12 15:55pm    
My 5.
Andi
Zoltán Zörgő 3-Jun-12 16:07pm    
Thanks
Sergey Alexandrovich Kryukov 4-Jun-12 0:06am    
My 5.
--SA
Zoltán Zörgő 4-Jun-12 4:43am    
Thanks
tetuko99 4-Jun-12 10:56am    
thank for your attention,,
but that very complicated for me.
can you give me a solution for my project work.!
i must send data integer temperature-humidity-count every 10ms to the PC with serialPORT rs232 communication.
i just try send all data with script printf("@ %02d %03d %04d \r, temp,hum, count) ;
delay_ms(10);

now i dont know how My GUI in C# can receive data completed every 10mS.???
please help me...

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900