Hello Guys , i'm new to programming and developing application so please take it easy one me if you please .
i'm developing application that will connect to a weigh indicator via RS232 serial port , i have established a connection between the application and the weigh indicator and i get the data from the weigh indicator to a textbox and it goes like : ST,GS,+ 8.8kg >
ST : Stable
GS : Gross Weight
+ sign
space 8.8 which is the weight
KG
the problem is all that data is in ASCII and i need to convert it to INT so i can get first weight then second weight then subtracting the 2 weighs will give us the net weight which i need to print a weigh ticket .
so i was thinking maybe converting it first to byte array then choosing from the array the bytes related to only the weight and convert it to integer ?? or can i convert it to integer without changing it first to byte of array ??
i need the values converted to be the same as the weigh indicator just changing the type of data from ASCII to INT .
your help is much appreciated
What I have tried:
Have a look at my code :
1 using System;
2 using System.IO.Ports;
3 using System.Text;
4 using System.Threading;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Data;
8 using System.Windows.Documents;
9 using System.Windows.Threading;
10
11 namespace WpfApp4
12 {
13 public partial class Connection : Window
17
18 {
19 public readonly MainWindow win1;
20
21
22 #region variables
23
24
25 FlowDocument mcFlowDoc = new FlowDocument();
26 Paragraph para = new Paragraph();
27
28 SerialPort serial = new SerialPort();
29
30
31 string recieved_data;
32
33
34
35 #endregion
36
37
38 public Connection()
39 {
40
41 InitializeComponent();
42 InitializeComponent();
43
44 Connect_btn.Content = "Connect";
45
46 win1 = new MainWindow();
47
48 }
49
50 private void Connect_Comms(object sender, RoutedEventArgs e)
51 {
52
53
54
55 if (Connect_btn.Content == "Connect")
56 {
57
58 serial.PortName = Comm_Port_Names.Text;
59 serial.BaudRate = Convert.ToInt32(Baud_Rates.Text);
60 serial.Handshake = System.IO.Ports.Handshake.None;
61 serial.Parity = (Parity)Enum.Parse(typeof(Parity), Parity_1.Text);
62 serial.DataBits = Convert.ToInt32(Data_Bits.Text);
63 serial.StopBits = (StopBits)Enum.Parse(typeof(StopBits), Stop_Bits.Text);
64 serial.ReadTimeout = 200;
65 serial.WriteTimeout = 50;
66 serial.Open();
67
68
69 Connect_btn.Content = "Disconnect";
70 serial.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Recieve);
71
72 }
73 else
74 {
75 try
76 {
77 serial.Close();
78 Connect_btn.Content = "Connect";
79 }
80 catch
81 {
82 }
83 }
84
85
86 }
87
88 #region Recieving
89
90 private delegate void UpdateUiTextDelegate(string text);
91 private void Recieve(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
92 {
93
94 recieved_data = serial.ReadExisting();
95 Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(WriteData), recieved_data);
96 }
97 private void WriteData(string text)
98 {
99
100
101
102
103
104 Comm_Data = recieved_data;
105
106 }
107
108 #endregion
109
110
111 #region Sending
112
113 private void Send_Data(object sender, RoutedEventArgs e)
114 {
115 SerialCmdSend(SerialData.Text);
116 SerialData.Text = "";
117 }
118 public void SerialCmdSend(string data)
119 {
120 if (serial.IsOpen)
121 {
122 try
123 {
124
125 byte[] hexstring = Encoding.ASCII.GetBytes(data);
126
127
128
129
130
131
132 foreach (byte hexval in hexstring)
133 {
134 byte[] _hexval = new byte[] { hexval };
135 serial.Write(_hexval, 0, 1);
136 Thread.Sleep(1);
137 }
138 }
139 catch (Exception ex)
140 {
141
142
143 Comm_Data = recieved_data;
144 }
145 }
146 else
147 {
148 }
149 }
150
151
152
153 #endregion
154 public string Comm_Data
155 {
156 get { return Comm_data.Text; }
157 set { Comm_data.Text = value; }
158 }
159
160 private void Comm_Port_Names_SelectionChanged(object sender, SelectionChangedEventArgs e)
161 {
162
163 }
164
165 private void Comm_data_TextChanged(object sender, TextChangedEventArgs e)
166 {
167
168
169 win1.Comm_Data1 = this.Comm_data.Text;
170
171
172
173
174
175 }
176
177 private void Button_Click(object sender, RoutedEventArgs e)
178 {
179 Connection objConnection = new Connection();
180 this.Visibility = Visibility.Hidden;
181 win1.Show();
182
183 }
184 }
185
186 }