|
yes . the data received is what is sent on the other COM port.
Quote: The DataReceived function is invoked any time there is data available, not just when the data is complete.
So it is conceivable that it could be raised after each byte is received, hence the greater frequency.
so how can I change it to be raised every 3 millisecond? I want to create a match between sending and receiving. for example when I depict the chart of both send and receive in realtime, for comparison of data ,the data must be matched.
|
|
|
|
|
One thing you could try is to read from the port using the ReadLine function. This only returns after it reads a newline sequence.
Then, in your send function, use the WriteLine function, which will append a newline sequence to each packet.
If this does not suit your requirements, then you'll have to implement your own stream scheme to buffer the received data until it is complete, and only then process it.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I just wanted to also mention, you can wrap the SerialPort class in a Stream [^]class to make it easier to read and write.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
another problem I've with this code and writing to port:
when I see the data which I'm sending in Comwizard ,sometimes I see an interrupt in the data received by comwizard. it is randomly and when I increase the frequency of sending(3 ms to 10ms ...) it appears in longer time and less than before. I checked all things but I cannot find any mistake in my code . is it possible to be relative to serial port class and functions of c#?can you suggest what is the mistake?
|
|
|
|
|
When you say you increase the frequency from 3ms to 10ms, I don't understand because that sounds like you are decreasing the frequency?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
yes . excuse me. I'm decreasing it.
and this is the code for reading bytes:
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)
{
}
}
}
and code for writing:
public void SendCommand(SerialPort port,byte[] command)
{
if (port != null && port.IsOpen)
{
port.Write(command, 0, command.Length);
}
else
{
MessageBox.Show("port is not open!");
}
}
|
|
|
|
|
Are you using the ReadLine and Writeline functions?
It may be that Comwizard is not waiting for the newline sequence to return the data it sees?
Even though there is an interrupt in the data, does the full data come through eventually?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
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
|
|
|
|