1 | using System;
|
2 | using System.Collections.Generic;
|
3 | using System.ComponentModel;
|
4 | using System.Data;
|
5 | using System.Drawing;
|
6 | using System.Linq;
|
7 | using System.Text;
|
8 | using System.Threading.Tasks;
|
9 | using System.Windows.Forms;
|
10 | using System.IO.Ports;
|
11 | using CommandsPD4I;
|
12 |
|
13 |
|
14 | namespace CSharpExample
|
15 | {
|
16 | public partial class CSharpExample : Form
|
17 | {
|
18 | public ComMotorCommands motor1;
|
19 |
|
20 | public CSharpExample()
|
21 | {
|
22 | InitializeComponent();
|
23 |
|
24 | motor1 = new ComMotorCommands();
|
25 | motor1.SetSteps(Convert.ToInt32(numericSchritte.Value));
|
26 | }
|
27 | SerialPort arduino;
|
28 | delegate void InvokeLB(string Data);
|
29 | InvokeLB lbRecievedDelegate;
|
30 | int xPos = 0;
|
31 |
|
32 |
|
33 | private void StartBtn_Click(object sender, EventArgs e)
|
34 | {
|
35 | // Set comm settings for motor 1
|
36 | motor1.SelectedPort = ComPortBox1.Text;
|
37 | motor1.Baudrate = Convert.ToInt32(BaudrateBox1.Text);
|
38 |
|
39 | // Set motor address
|
40 | motor1.MotorAddresse = Convert.ToInt32(Motor1ID.Value);
|
41 | // Set relative positioning mode
|
42 | motor1.SetPositionType(1);
|
43 |
|
44 | // Start travel profile
|
45 | if (motor1.ErrorFlag)
|
46 | {
|
47 | StatusLabel1.Text = "Status 1: " + motor1.ErrorMessageString;
|
48 | }
|
49 | else
|
50 | {
|
51 | StatusLabel1.Text = "Status 1: OK";
|
52 | }
|
53 | }
|
54 |
|
55 | private void StopBtn_Click(object sender, EventArgs e)
|
56 | {
|
57 | // Stop travel profile
|
58 | motor1.StopTravelProfile();
|
59 | }
|
60 |
|
61 | private void timer1_Tick(object sender, EventArgs e)
|
62 | {
|
63 | lblPosition.Text = Convert.ToString(motor1.GetPosition());
|
64 | lblStatus.Text = motor1.ErrorMessageString;
|
65 | // this.chart1.Series["Kraft"].Points.AddXY(xPos, Convert.ToDouble(lblKraft.Text));
|
66 | // xPos++;**strong text**
|
67 | }
|
68 |
|
69 | private void btnHoch_Click(object sender, EventArgs e)
|
70 | {
|
71 | motor1.SetDirection(0);
|
72 | motor1.SetPositionType(1);
|
73 | motor1.StartTravelProfile();
|
74 | }
|
75 |
|
76 | private void btnRunter_Click(object sender, EventArgs e)
|
77 | {
|
78 | motor1.SetDirection(1);
|
79 | motor1.SetPositionType(1);
|
80 | motor1.StartTravelProfile();
|
81 | }
|
82 |
|
83 | private void numericSchritte_ValueChanged(object sender, EventArgs e)
|
84 | {
|
85 | motor1.SetSteps(Convert.ToInt32(numericSchritte.Value));
|
86 | }
|
87 |
|
88 | private void numericGeschwindigkeit_ValueChanged(object sender, EventArgs e)
|
89 | {
|
90 | motor1.SetMaxFrequency(Convert.ToInt32(numericGeschwindigkeit.Value));
|
91 |
|
92 | }
|
93 |
|
94 | private void btnDiagramm_Click(object sender, EventArgs e)
|
95 | {
|
96 | if (timer1.Enabled)
|
97 | {
|
98 | timer1.Stop();
|
99 | }
|
100 | else
|
101 | {
|
102 | timer1.Start();
|
103 | }
|
104 | }
|
105 |
|
106 | private void btnResetDiagramm_Click(object sender, EventArgs e)
|
107 | {
|
108 | this.chart1.Series["Kraft"].Points.Clear();
|
109 | xPos = 0;
|
110 | }
|
111 |
|
112 | private void arduino_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
113 | {
|
114 | string RecievedLine = " ";
|
115 | while (RecievedLine != "")
|
116 | {
|
117 | RecievedLine = arduino.ReadLine();
|
118 | lblKraft.Invoke(lbRecievedDelegate, new object[] { RecievedLine });
|
119 |
|
120 | }
|
121 | }
|
122 |
|
123 | void Invokelabel1(string Data)
|
124 | {
|
125 | label1.Text = Data;
|
126 | this.chart1.Series["Kraft"].Points.AddXY(xPos, Convert.ToDouble(lblKraft.Text));
|
127 | xPos++;
|
128 | }
|
129 |
|
130 | private void btnArduino_Click(object sender, EventArgs e)
|
131 | {
|
132 | //Hier erstellen wir unseren Serialport und legen die Einstellungen fest
|
133 | arduino = new SerialPort("COM7", 9600);
|
134 |
|
135 | if (!arduino.IsOpen)
|
136 | {
|
137 | arduino.Open();
|
138 | if (arduino.IsOpen)
|
139 | {
|
140 | lblArduino.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(200)))), ((int)(((byte)(0)))));
|
141 | lblArduino.Text = "Verbunden mit " + arduino.PortName;
|
142 | }
|
143 |
|
144 | }
|
145 | lbRecievedDelegate = new InvokeLB(Invokelabel1);
|
146 | arduino.DataReceived += new SerialDataReceivedEventHandler(arduino_DataReceived); //DataRecieved Event abonnieren
|
147 | }
|
148 |
|
149 |
|
150 |
|
151 | }
|
152 | }
|