Beste allemaal,
Onderstaande post zal worden gemaakt in het Engels. Het heeft betrekking op een stuk programmeer code waar mijn vriendin momenteel mee bezig is, en helaas maar waar is zij de Nederlandse taal niet machtig genoeg om in het Nederlands te posten. Vandaar dus de post in het Engels.
Het staat je vrij om in het Nederlands te reageren, Google translate ftw zeggen we dan, en anders ben ik er uiteraard om enige zaken voor haar te vertalen.
En dan nu de probleemstelling:
What the program should do:
Commumnicate between a PC and a peripheral device through a serial port sending commands:
"M1"
"M0"
"MC"
"MT"
"MS"
"PD"
"CL"
(strings, no CR)
and receive and display response to some of the commands (all except M1 and M0).
First the connection is set up, then after sending "M1" (buttonXtend) every 1s a parallel thread should querry the device for some parameters. The device responds with parameters and the program needs to display them. This stops after sending "M0" (buttonStop).
What the program does:
Sending the commands to the device is working. The device responds to them properly (tested with another program). However, my program does not read from the port properly and after some time 'error: timeout of write operation' occurs. The clock_Elapsed method is called once.
That's my code:
I'll be grateful for any suggestions!
Onderstaande post zal worden gemaakt in het Engels. Het heeft betrekking op een stuk programmeer code waar mijn vriendin momenteel mee bezig is, en helaas maar waar is zij de Nederlandse taal niet machtig genoeg om in het Nederlands te posten. Vandaar dus de post in het Engels.
Het staat je vrij om in het Nederlands te reageren, Google translate ftw zeggen we dan, en anders ben ik er uiteraard om enige zaken voor haar te vertalen.
En dan nu de probleemstelling:
What the program should do:
Commumnicate between a PC and a peripheral device through a serial port sending commands:
"M1"
"M0"
"MC"
"MT"
"MS"
"PD"
"CL"
(strings, no CR)
and receive and display response to some of the commands (all except M1 and M0).
First the connection is set up, then after sending "M1" (buttonXtend) every 1s a parallel thread should querry the device for some parameters. The device responds with parameters and the program needs to display them. This stops after sending "M0" (buttonStop).
What the program does:
Sending the commands to the device is working. The device responds to them properly (tested with another program). However, my program does not read from the port properly and after some time 'error: timeout of write operation' occurs. The clock_Elapsed method is called once.
That's my code:
C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
| using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO.Ports; using System.Threading; using System.Timers; namespace Endoprosthesis { public partial class Form1 : Form { //% allocate components private static SerialPort port; System.Timers.Timer clock; delegate void SetTextCallback(string text, Label field); public Form1() { InitializeComponent(); //% create an instance of serial port port = new SerialPort(); //%timer to prompt for status updates every second clock = new System.Timers.Timer(1000); //% setup not to stop counting after first occurance clock.AutoReset = true; //% making sure the garbage collector will not kill it too soon GC.KeepAlive(clock); //% handling for the event of elapsed second clock.Elapsed += new ElapsedEventHandler(clock_Elapsed); } void clock_Elapsed(object sender, ElapsedEventArgs e) { string value; port.Write("CL"); System.Threading.Thread.Sleep(20); value = port.ReadExisting(); setText(value, length); port.Write("MT"); System.Threading.Thread.Sleep(20); value = port.ReadExisting(); setText(value, temperature); port.Write("MC"); System.Threading.Thread.Sleep(20); value = port.ReadExisting(); setText(value, current); port.Write("MS"); System.Threading.Thread.Sleep(20); value = port.ReadExisting(); setText(value, speed); } private void buttonXtend_Click(object sender, EventArgs e) { port.Write("M1"); clock.Start(); } private void buttonStop_Click(object sender, EventArgs e) { port.Write("M0"); clock.Stop(); } private void setText(string text, Label field) { if (field.InvokeRequired) { SetTextCallback d = new SetTextCallback(setText); this.Invoke(d, new object[] { text, field }); } else { field.Text = text; } } private void buttonClose_Click(object sender, EventArgs e) { clock.Stop(); clock.Dispose(); if(port.IsOpen) port.Close(); this.Close(); } private void buttonClear_Click(object sender, EventArgs e) { length.Text = temperature.Text = current.Text = speed.Text = "--"; } private void buttonConnect_Click(object sender, EventArgs e) { //% port configuration port.PortName = textBox2.Text; port.BaudRate = 38400; port.Parity = Parity.None; port.DataBits = 8; port.StopBits = StopBits.One; //% selecting handshake method if (radioButton1.Checked) port.Handshake = Handshake.None; else if (radioButton2.Checked) port.Handshake = Handshake.RequestToSend; port.WriteTimeout = 500; port.Open(); if (!port.IsOpen) MessageBox.Show("Error", "Cannot open" + port.PortName + "port", MessageBoxButtons.OK, MessageBoxIcon.Error); port.Write("PD"); System.Threading.Thread.Sleep(20); textBox1.Text = port.ReadExisting(); } private void buttonDisconnect_Click(object sender, EventArgs e) { if (port.IsOpen) port.Close(); } } } |
I'll be grateful for any suggestions!
[ Voor 0% gewijzigd door RobIII op 26-01-2011 18:44 . Reden: Syntax highlighting FTW \O/ ]
You have to be careful if you don't know where you are going because you might not get there...