Click here to Skip to main content
15,884,388 members
Home / Discussions / C#
   

C#

 
GeneralRe: Change Cursor Mouse for an image in application but when close app return to the initial state Pin
Eddy Vluggen9-Mar-17 4:20
professionalEddy Vluggen9-Mar-17 4:20 
GeneralRe: Change Cursor Mouse for an image in application but when close app return to the initial state Pin
Member 130142789-Mar-17 6:39
Member 130142789-Mar-17 6:39 
GeneralRe: Change Cursor Mouse for an image in application but when close app return to the initial state Pin
Eddy Vluggen9-Mar-17 9:33
professionalEddy Vluggen9-Mar-17 9:33 
GeneralRe: Change Cursor Mouse for an image in application but when close app return to the initial state Pin
Member 1301427810-Mar-17 0:50
Member 1301427810-Mar-17 0:50 
GeneralRe: Change Cursor Mouse for an image in application but when close app return to the initial state Pin
Eddy Vluggen10-Mar-17 2:47
professionalEddy Vluggen10-Mar-17 2:47 
GeneralRe: Change Cursor Mouse for an image in application but when close app return to the initial state Pin
Member 1301427813-Mar-17 6:21
Member 1301427813-Mar-17 6:21 
Question.NET core, standart and desktop app (using VS2017) (trying to replace PCL) Pin
Super Lloyd8-Mar-17 4:04
Super Lloyd8-Mar-17 4:04 
QuestionPlotting serial port data using zedgraph (data vs time) Pin
Ram _Varman8-Mar-17 1:51
Ram _Varman8-Mar-17 1:51 
I'm trying to plot the data read from a serial port using zedgraph. I'm still learing to code so I couldn't deduce why the plot does not work. Please have a look at the code and advice.
To open and read from a serial port, I use a combox and a couple of buttons (and then later I try to save it to a text file).
The plot does not update! I can't quite guess why. Please tell me if there's mistakes in my approach or the code.

C#
//zedgraph
namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        string t;
        SerialPort sp;

Thread m_thread;
bool m_running = false;
ManualResetEvent m_event = new ManualResetEvent(true);
bool m_pause = false;
private GraphPane myPane;

public Form1()
{
    InitializeComponent();
    Control.CheckForIllegalCrossThreadCalls = false;
    // User can already search for ports when the constructor of the FORM1 is calling 
    // And let the user search ports again with a click
    // Searching for ports function

    SearchPorts();

    CreateZedGraph(); //error : Severity    Code    Description Project File    Line    Suppression State
                      //Error CS7036  There is no argument given that corresponds to the required formal parameter 
                      //'w' of 'Form1.DrawPoint(ZedGraphControl, int, PointPair)'
}
// start button
private void btnStart_Click(object sender, EventArgs e)
{
    if (m_thread == null || m_thread.IsAlive == false)
    {
        ClearGraph();
        m_thread = new Thread(Process);
        m_thread.Start();
    }
}
void Process()
{       
    PointPair point = new PointPair();
    btnStart.Enabled = false;
    btnStop.Enabled = true;
    m_running = true;
    while (m_running == true)
    {
        m_event.WaitOne();

        point.Y = Convert.ToDouble(serialPort1);
        point.X++;  //time instance of measurement??
        DrawPoint(zed1, point);
        ssData.Value = point.Y.ToString();
        RefresheZedGraphs(zed1);
        Thread.Sleep(700);
    }
    btnStart.Enabled = true;
}

private void CreateZedGraph(object sender, SerialDataReceivedEventArgs e, ZedGraphControl zgc)
{
    myPane = zgc.GraphPane;
    // axes stuff
    myPane.Title.Text = "FRDM-KW40z serial Test";
    myPane.XAxis.Title.Text = "Time";
    myPane.YAxis.Title.Text = "Voltage";
    myPane.XAxis.MajorGrid.IsVisible = true;
    myPane.YAxis.MajorGrid.IsVisible = true;
    myPane.XAxis.MinorGrid.IsVisible = true;
    myPane.YAxis.MinorGrid.IsVisible = true;

    // data from serial port

    PointPairList list = new PointPairList();
    zed1.GraphPane.AddCurve("Test", list, Color.Red);

}
//serial port data collection
private void button2_Click(object sender, EventArgs e)
    {
        comboBox1.Items.Clear();
        SearchPorts();
    }
    void SearchPorts()
    {
        string[] ports = SerialPort.GetPortNames();
        foreach (string port in ports)
        {
            comboBox1.Items.Add(port);
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        // Catch exception if it will be thrown so the user will see it in a message box
        OpenCloseSerial();
    }      
    void OpenCloseSerial()
    {
        try
        {
            if (sp == null || sp.IsOpen == false)
            {
                t = comboBox1.Text.ToString();
                sErial(t);
                button3.Text = "Close Serial port"; // button text
            }
            else
            {
                sp.Close();
                button3.Text = "Connect and wait for inputs";   // button text

            }
        }
        catch (Exception err)   // catching error message
        {
            MessageBox.Show(err.Message);   // displaying error message
        }           
    }

    void sErial(string Port_name)
    {
        try
        {
            sp = new SerialPort(Port_name, 115200, Parity.None, 8, StopBits.One);   // serial port parameters
            sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            sp.Open();
        }
        catch (Exception err)
        {
            throw (new SystemException(err.Message));
        }
    }
private  void DataReceivedHandler(object sender,SerialDataReceivedEventArgs e)
    {

        // This below line is not need , sp is global (belongs to the class!!)
        //SerialPort sp = (SerialPort)sender;
        if (e.EventType == SerialData.Chars)
        {
            if (sp.IsOpen)
            {
                string w = sp.ReadExisting();
                if (w != String.Empty)
                {
                    Invoke(new Action(() => Control.Update(w)));
                }
            }
        }
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (sp == null || sp.IsOpen == false)
        {
            OpenCloseSerial();
        }
    }


I get an error at : Invoke(new Action(() => Control.Update(w))); when trying to update the graph so that I can save after that.

I again have an error at: DrawPoint(zed1, point);

Thank you all for your time. Good day.

Cheers, Ram.
AnswerRe: Plotting serial port data using zedgraph (data vs time) Pin
Garth J Lancaster8-Mar-17 2:15
professionalGarth J Lancaster8-Mar-17 2:15 
GeneralRe: Plotting serial port data using zedgraph (data vs time) Pin
Ram _Varman8-Mar-17 2:17
Ram _Varman8-Mar-17 2:17 
AnswerRe: Plotting serial port data using zedgraph (data vs time) Pin
Gerry Schmitz8-Mar-17 6:39
mveGerry Schmitz8-Mar-17 6:39 
GeneralRe: Plotting serial port data using zedgraph (data vs time) Pin
Ram _Varman9-Mar-17 2:59
Ram _Varman9-Mar-17 2:59 
GeneralRe: Plotting serial port data using zedgraph (data vs time) Pin
Gerry Schmitz9-Mar-17 6:08
mveGerry Schmitz9-Mar-17 6:08 
Questionreferencing a dll available in a different folder from a Windows Service without copying the dll Pin
govindarajan k8-Mar-17 0:24
govindarajan k8-Mar-17 0:24 
AnswerRe: referencing a dll available in a different folder from a Windows Service without copying the dll Pin
Pete O'Hanlon8-Mar-17 0:40
mvePete O'Hanlon8-Mar-17 0:40 
AnswerRe: referencing a dll available in a different folder from a Windows Service without copying the dll Pin
Alan N8-Mar-17 3:58
Alan N8-Mar-17 3:58 
GeneralRe: referencing a dll available in a different folder from a Windows Service without copying the dll Pin
govindarajan k9-Mar-17 0:27
govindarajan k9-Mar-17 0:27 
QuestionLINQ error string matching? Pin
Member 28240516-Mar-17 4:41
Member 28240516-Mar-17 4:41 
AnswerRe: LINQ error string matching? Pin
Richard Deeming6-Mar-17 5:36
mveRichard Deeming6-Mar-17 5:36 
GeneralRe: LINQ error string matching? Pin
Member 28240516-Mar-17 20:28
Member 28240516-Mar-17 20:28 
QuestionWorking with .resx Files / C#, WinForms [Closed] Pin
HobbyProggy5-Mar-17 21:44
professionalHobbyProggy5-Mar-17 21:44 
AnswerRe: Working with .resx Files / C#, WinForms Pin
Gerry Schmitz6-Mar-17 9:42
mveGerry Schmitz6-Mar-17 9:42 
GeneralRe: Working with .resx Files / C#, WinForms Pin
HobbyProggy6-Mar-17 19:27
professionalHobbyProggy6-Mar-17 19:27 
GeneralRe: Working with .resx Files / C#, WinForms Pin
Gerry Schmitz6-Mar-17 20:08
mveGerry Schmitz6-Mar-17 20:08 
GeneralRe: Working with .resx Files / C#, WinForms Pin
HobbyProggy6-Mar-17 20:24
professionalHobbyProggy6-Mar-17 20:24 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.