|
Hi Sir,
We start it in same way, I will check from Process object for required value in application, and will let you know, that we have captured required info or not.
Thanks & Regards
|
|
|
|
|
Hi,
I need know how can reading byte using serialport I mean if any device send data in bytes by serialport I need code can be reading byte and display this data in textbox
for examble if micro controller send x=255 this data will send in ascii code how can read this value in C#
Regards
|
|
|
|
|
|
Use SerialPort.Read[^] to get the bytes, and then Bitconverter.ToString to output them to a textbox:
myTextBox.Text += BitConverter.ToString(arrayOfBytesFromSerailPort);
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Hi ,
Thank you for support
I have equation I send data I text for example my_data="ST23.5255"
Iam using
serialport.ReadTo("ST") to Read ST from array text then now I have two value
1- 23.5 this is anolage value from temperature senser
2- 255 this value from anther senser
now my equation how can separated this value from array my_data and display this value in gauge or led array
I mean can be use method in C# convert string to float value and selected index from array because I have two value
Regards
|
|
|
|
|
Read the following three bytes - you will probably have to wait until they are available.
Then use float.Parse or float.TryParse (or their double equivalents) to convert the value.
Then do the same for the next three bytes.
What part of this is difficult?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
The difficult is how can separated the data from string because this array have multi data
for example
string w;
w = Port.ReadTo("ST");
and assume the data send "ST12.5127"
this data 12.5127 will store a text in W now I need convert 12.5 from text to float and convert 127 to float how can separated data from W
Sir can be written the code
Regards
|
|
|
|
|
SerialPort.Read[^] lets you specify how many characters (or bytes) to read...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
hasan hadi wrote: this data 12.5127 will store a text in W now I need convert 12.5 from text to
float and convert 127 to float
Convert.ToFloat("12.51"); You're not going to like this, but converting from one simple type to another simple type should not be an issue.
I suggest you buy a book and go through a lot of tutorials before attempting anything more complex.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi,
Sir what Book mean ?
Regards
|
|
|
|
|
Head First C#[^], or anything similar.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Down vote countered.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
string w = "ST12.5127";
float f1 = float.Parse( w.Substring( 2, 4 ) );
float f2 = float.Parse( w.Substring( 6, 3 ) );
Console.WriteLine( "f1: {0}", f1 );
Console.WriteLine( "f2: {0}", f2 );
modified 2-May-15 1:18am.
|
|
|
|
|
Thank you Sir this is good method
Regrds
|
|
|
|
|
Hi ,
this code is work but I have one problem
the problem in
ledArray1.SetValues(state_data,0,4)
this take Boolean array the c# message say cannot convert from byte[] to bool[] what can be doing?
this code is work
private void button1_Click(object sender, EventArgs e)
{
int baud_rate;
baud_rate = Convert.ToInt32(comboBox2.Text);
MessageBox.Show("" + baud_rate);
Port.Close();
Port = new SerialPort(comboBox1.Text, baud_rate);
Port.DataReceived += new SerialDataReceivedEventHandler(Port_DataReceived);
Port.Open();
button1.BackColor = Color.Lime;
}
string w;
private void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] temp_read;
temp_read = new byte[6];
byte[] state_read;
state_read = new byte[4];
string w;
w = Port.ReadTo("ST");
if (w != string.Empty)
{
temp_read[0] = (byte)w[0];
temp_read[1] = (byte)w[1];
temp_read[2] = (byte)w[2];
temp_read[3] = (byte)w[3];
temp_read[4] = (byte)w[4];
state_read[0] = (byte)w[5];
state_read[1] = (byte)w[6];
state_read[2] = (byte)w[7];
state_read[3] = (byte)w[8];
string ff = Encoding.UTF8.GetString(temp_read, 0, 5);
float kk = Convert.ToSingle(ff);
string ff1 = Encoding.UTF8.GetString(state_read, 0, 4);
float kk1 = Convert.ToSingle(ff1);
thermometer1.Value = kk;
label1.Text = "" + kk1;
}
}
modified 1-May-15 14:27pm.
|
|
|
|
|
Seriously, when Eddie suggested that you need to read a book, he wasn't joking.
You seem to be trying to code by guesswork instead of from knowledge, and that's as likely to succeed in development as it is in driving. To continue the analogy, you seem to be trying to drive across the USA in a 40 ton articulated lorry when you haven't yet mastered riding a bicycle!
It doesn't work - it's clear from your questions that you don't understand the basics enough to do complex code yet. Please, for your own sake: go back and learn the early stuff.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Thank you Sir
For me study course about C# before that but I have some Ambiguity in some point for me I writing the code and is work but I try choice anther method to obtain the same result
look her this my code again Sir I need only choice anther method
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ADC_and_display_value_in_computer
{
public partial class Form1 : Form
{
private string[] Ports;
private byte[] r_data;
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
Ports = SerialPort.GetPortNames();
foreach (string port in Ports)
{
comboBox1.Items.Add(port);
}
}
private void button1_Click(object sender, EventArgs e)
{
int baud_rate;
baud_rate = Convert.ToInt32(comboBox2.Text);
MessageBox.Show("" + baud_rate);
Port.Close();
Port = new SerialPort(comboBox1.Text, baud_rate);
Port.DataReceived += new SerialDataReceivedEventHandler(Port_DataReceived);
Port.Open();
button1.BackColor = Color.Lime;
}
string w;
private void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] temp_read;
temp_read = new byte[6];
byte[] state_read;
state_read = new byte[4];
string w;
w = Port.ReadTo("ST");
if (w != string.Empty)
{
temp_read[0] = (byte)w[0];
temp_read[1] = (byte)w[1];
temp_read[2] = (byte)w[2];
temp_read[3] = (byte)w[3];
temp_read[4] = (byte)w[4];
state_read[0] = (byte)w[5];
state_read[1] = (byte)w[6];
state_read[2] = (byte)w[7];
state_read[3] = (byte)w[8];
string ff = Encoding.UTF8.GetString(temp_read, 0, 5);
float kk = Convert.ToSingle(ff);
string ff1 = Encoding.UTF8.GetString(state_read, 0, 4);
float kk1 = Convert.ToSingle(ff1);
thermometer1.Value = kk;
label1.Text = "" + kk1;
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
button1.Enabled = true;
}
}
}
|
|
|
|
|
thank you for all
now final code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ADC_and_display_value_in_computer
{
public partial class Form1 : Form
{
private string[] Ports;
private byte[] r_data;
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
Ports = SerialPort.GetPortNames();
foreach (string port in Ports)
{
comboBox1.Items.Add(port);
}
}
private void button1_Click(object sender, EventArgs e)
{
int baud_rate;
baud_rate = Convert.ToInt32(comboBox2.Text);
MessageBox.Show("" + baud_rate);
Port.Close();
Port = new SerialPort(comboBox1.Text, baud_rate);
Port.DataReceived += new SerialDataReceivedEventHandler(Port_DataReceived);
Port.Open();
button1.BackColor = Color.Lime;
}
string w;
private void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string w;
w = Port.ReadTo("ST");
if (w != string.Empty)
{
float f1 = float.Parse(w.Substring(0, 5));
float f2 = float.Parse(w.Substring(5, 3));
thermometer1.Value = f1;
label1.Text = "" + f2;
ledArray1.SetValues(sheimy_Converter1.Convert_byte_to_boolarry((byte)f2));
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
button1.Enabled = true;
}
}
}
|
|
|
|
|
Hello,
I am trying to find a code to find all the references of the property defined in a class which is very similar to 'Find All References' function available in Visual Studio environment. But I want to have it in code during run time. How to do it?
e.g
=>Property 1
private int _Salary;
public int Salary()
{
get{return _Salary;}
set{_Salary = value;}
}
=> Property 2
private int _Bonus;
public int Bonus()
{
get{return Salary * 2;}
}
Like in the above example I have two properties Salary & Bonus. Bonus is calculated from salary. As soon as I set a value to Salary, value of Bonus gets updated.
Similarly I want to get reference of all other properties or methods in the class or program which are affected when value of Salary is changed through code.
Conclusion - I want to have a method or function, so that whenever I set a value to some property in a class, I want to have a list of all the other properties(List<Property>) or methods which are affected and in which the changed property is referenced.
Please help me to achieve this.
Siddharth.Shinde
|
|
|
|
|
There is no functionality in the .NET framework that will check all your code to see where it is used. It is part of Visual Studio, and not of the framework.
You could do it yourself; you'd have to loop though all code (yes, means looking inside methods), in either binary or source-form, and make a list of all the places where it is used.
--edit
You're looking for a wrong solution to a known problem. Things might be simpeler if you implement INotifyPropertyChanged[^]
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I think you're on a wrong track. Why do you think you need this? I'm pretty sure there's a better approach to what you're trying to achieve with that.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I have an array in the main class:
static void Main(string[] args)
here is the array
Contact[] contactArray;
contactArray = new Contact[2];
contactArray[0] = contact1;
contactArray[1] = contact2;
Contact is a class of it's own and contact 1 has been initialized in the main class.
I need to access this array from a UI class and iterate through it and display the content
I'm stuck. thanks.
|
|
|
|
|
One way to do this is to create a View Model class, and have all global data accessible via the view model. In the XAML file, you set the DataContext element to a static instance of the View Model class.
You create all UI classes in the View Model class, not in main(). When creating a UI class, one of the parameters to the constructor should be the VM instance.
A Google on "WPF View Model" and "XAML DataContext" should give you plent of examples of this technique.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
|
From time to time, I have faced the same requirement. I found that the simplest solution was to promote the object to class scope and mark it as both static and public. Thereafter, other classes reference it as Program.MyObject.
For example:
static public Contact[] contactArray;
static void Main(string[] args)
contactArray = new Contact[2];
contactArray[0] = contact1;
contactArray[1] = contact2;
In your form, you refer to contactArray as follows.
Contact Contact1 = Progarm.contactArray[0];
|
|
|
|