|
see above edited post. yes data comes eventually.
|
|
|
|
|
Check my comments in the code below:
public void ThreadMain()
{
int i = 0;
while (port.IsOpen)
{
try
{
var b1 = port.ReadByte();
if (b1 == ReadSignal.STARTByte)
{
b1 = port.ReadByte();
...
if (b1 == ReadSignal.STOPByte)
{
RSignal.isValid = true;
}
}
if (RSignal.isValid)
{
RSignal.ExtractLocatoins();
if (DataReceived != null)
DataReceived.Invoke(RSignal);
}
}
catch (Exception ex)
{
}
}
}
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
if the STOPByte is read there just poor out the data until new pack of correct data comes.(until STARTByte be found). also notice that I'm sending data in turn so if one pack of data is received then if no noise all data packs can be received in turn.
|
|
|
|
|
Yes you are right.
If the full data comes eventually, then maybe there is no problem at all.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
so what is the cause of the interrupt? is it relative to system(pc) or to serial port or my code? the interrupt (not sending data for some millisecond) can be seen by comwizard , also I must send data for a motor and also the motor shows such interrupt.
|
|
|
|
|
I'm sorry, I do not know.
Are you sure your send data is full every time?
I'm not there, so I can't see everything you see.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
yes. I' work around.
Richard Andrew x64,thanks for your attentions.
|
|
|
|
|
hello
I want to depict a chart dynamically and every 3 millisecond a point is send to the chart .
when I run my program about 8sec it will be executed very more than 8sec( about 3 mins or more).(i used wpftoolkit charting, dynamic data display , ... but none of them works well).
also when I want to depict 6000 point in offline mode(storing data in an array and at last depicting it on a chart) ,yet there is a such problem.
Matlab depicts these data very rapidly .
is there any way to depict these data in c#?
|
|
|
|
|
Matlab doesn't depict them in three milliseconds either. It may be quick, but it's not realtime. There's no realtime software under Windows, as Windows isn't a realtime OS. It's the OS that determines how many processortime you get.
khomeyni wrote: is there any way to depict these data in c#? Yes; and can be relatively quick too. Draw your graph once, and instead of redrawing when a new datapoint appears, scroll everything a single pixel and add the new point to the right.
Having a complete table in memory and charting the complete table every iteration will never be fast - not even if you use assembly.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I've this simple code but it takes about 20sec to be depicted!
private void button1_Click(object sender, RoutedEventArgs e)
{
ObservableCollection<ChartItem> InputItems = new ObservableCollection<ChartItem>();
for (int i = 0; i < 6000; i++)
{
InputItems.Add(new ChartItem(i, i));
}
InputLine.ItemsSource = InputItems;
}
}
public class ChartItem : INotifyPropertyChanged
{
public ChartItem(double t, double v)
{
time = t;
myVar = v;
}
private double time;
public double Time
{
get { return time; }
set
{
time = value;
OnPropertyChanged("Time");
}
}
private double myVar;
public double Value
{
get { return myVar; }
set
{
myVar = value;
OnPropertyChanged("Value");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
with using :
System.Windows.Controls.DataVisualization.Toolkit
and
System.Windows.Controls.DataVisualization.Toolkit
|
|
|
|
|
If you're "looking" for faster code - you won't find it. It's something that needs to be written.
You plot the graph yourself, and update the graph with new info instead of redrawing it completely.
Human eyes aren't realtime either; you'd need 24 frames/second to get it to look convincingly realtime. Although Windows might delay your app (one a single-core machine, a virusscanner and some automatic updates would do the trick), it should be doable to update a picture in a reasonable time.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
there is one another question: why can Matlab depict these charts so faster than c# ?
|
|
|
|
|
I have used this tutorial to make a chat:
http://www.youtube.com/watch?v=BDVfpPq3weo[^]
It works on the same computer but not between two.
The problem seem so be that I need more than one thread.
I get Cross-thread operation not valid.
|
|
|
|
|
No, the problem is the reverse of that: "Cross-thread operation not valid" means that you are executing code on one thread that can only be executed on a different thread: normally, this occurs when you try to update a control from a different thread to that from which it was created (which must be the UI thread), either in a BackgoundWorker, a Thread instance or in the handler of a communications class that uses threading to handle it's events (the SerialPort does this for example).
Check your code: you may just have to start invoking the control instead of accessing it directly. For example:
private void AddNewTab(string tabName)
{
if (InvokeRequired)
{
Invoke(new MethodInvoker(delegate { AddNewTab(tabName); }));
}
else
{
TabPage tp = new TabPage(tabName);
myTabControl.TabPages.Add(tp);
}
}
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Not sure I understand...Could you show how to change my code?
This is my code:
private void Form1_Load(object sender, EventArgs e)
{
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
txtLocalIP.Text = GetLocalIP();
txtRemoteIP.Text = GetLocalIP();
}
private string GetLocalIP()
{
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
return ip.ToString();
}
return "127.0.0.1";
}
private void MessageCallBack(IAsyncResult aResult)
{
try
{
byte[] receivedData = new byte[1500];
receivedData = (byte[])aResult.AsyncState;
ASCIIEncoding aEncoding = new ASCIIEncoding();
string receivedMessage = aEncoding.GetString(receivedData);
listMessage.Items.Add("Friend: " + receivedMessage);
buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void btnConnect_Click(object sender, EventArgs e)
{
epLocal = new IPEndPoint(IPAddress.Parse(txtLocalIP.Text), Convert.ToInt32(txtLocalPort.Text));
sck.Bind(epLocal);
epRemote = new IPEndPoint(IPAddress.Parse(txtRemoteIP.Text), Convert.ToInt32(txtRemotePort.Text));
sck.Connect(epRemote);
buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
}
private void btnSend_Click(object sender, EventArgs e)
{
ASCIIEncoding aEncoding = new ASCIIEncoding();
byte[] sendingMessage = new byte[1500];
sendingMessage = aEncoding.GetBytes(txtMessage.Text);
sck.Send(sendingMessage);
listMessage.Items.Add("Me: " + txtMessage.Text);
txtMessage.Text = "";
}
|
|
|
|
|
Where do you get the error? Which line?
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Ok... The other guy whose thread I can´t se now wrote:
I'm guessing that's the line that's causing the problem. The callback method will be called on a background thread; you will need to use
InvokeRequired
and
Invoke
to get back to the UI thread when you want to access the form's controls.
Invoke((Action<object>)listMessage.Items.Add, "Friend: " + receivedMessage);
I Think he was right about the line. Tried to do change the line accordingly but got an error:
int System.Windows.Forms.ListBox.ObjectCollection.Add(object)' has the wrong return type
|
|
|
|
|
larsp777 wrote: int System.Windows.Forms.ListBox.ObjectCollection.Add(object)' has the wrong return type
Whoops - didn't notice that the method returned an int . The delegate type will need to be a Func<object, int> instead of an Action<object> .
Invoke((Func<object, int>)listMessage.Items.Add, "Friend: " + receivedMessage);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Thanks, Richard, it worked!
Well, thank you both for helping me out!
|
|
|
|
|
Do you know if there is a simple way for a third person can listen in what the others are Writing. (only has to listen to the sender)
I am making an application where you should be able to simulate someone eavesdropping the conversation.
|
|
|
|
|
If all the computers are on the same LAN, you could use the broadcast address so that any computer can pick up the messages:
Broadcasting Using Socket-Oriented Approach[^]
If they're on different networks, or you don't want the overhead associated with broadcasting, then you'll need to use multicasting:
IP Multicasting in C#[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks! It will probebly be on the same LAN.
|
|
|
|
|
The error occurs when I try to send a message. It occurs on both computers.
When I send from one computer the error shows on the other.
|
|
|
|
|
larsp777 wrote: listMessage.Items.Add("Friend: " + receivedMessage);
I'm guessing that's the line that's causing the problem. The callback method will be called on a background thread; you will need to use InvokeRequired and Invoke to get back to the UI thread when you want to access the form's controls.
Invoke((Func<object, int>)listMessage.Items.Add, "Friend: " + receivedMessage);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|