Click here to Skip to main content
15,568,681 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :
C#
  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      /// <summary>
 14      /// Interaction logic for Connection.xaml
 15      /// </summary>
 16      public partial class Connection : Window
 17  
 18      {
 19         public readonly MainWindow win1;
 20  
 21         // private MainWindow parent;
 22          #region variables
 23  
 24          //Richtextbox
 25          FlowDocument mcFlowDoc = new FlowDocument();
 26          Paragraph para = new Paragraph();
 27          //Serial 
 28          SerialPort serial = new SerialPort();
 29  // Here is where the data is send from the weigh indicator to that variable that i display in a textbox its in ASCII
 30          
 31  string recieved_data; 
 32  
 33           
 34  
 35          #endregion
 36  
 37  
 38          public Connection()
 39          {
 40  
 41              InitializeComponent();
 42              InitializeComponent();
 43              //overwite to ensure state
 44              Connect_btn.Content = "Connect";
 45              // this.parent = parent;
 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                  //Sets up serial port
 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                  //Sets button State and Creates function call on data recieved
 69                  Connect_btn.Content = "Disconnect";
 70                  serial.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Recieve);
 71  
 72              }
 73              else
 74              {
 75                  try // just in case serial port is not open could also be acheved using if(serial.IsOpen)
 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              // Collecting the characters received to our 'buffer' (string).
 94              recieved_data = serial.ReadExisting();
 95              Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(WriteData), recieved_data);
 96          }
 97          private void WriteData(string text)
 98          {
 99              // Assign the value of the recieved_data to the RichTextBox.
100              //  para.Inlines.Add(text);
101              // mcFlowDoc.Blocks.Add(para);
102              // Commdata.Document = mcFlowDoc;
103  // Here i get the data from the weigh indicator in ASCII 
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                      // Send the binary data out the port
125                      byte[] hexstring = Encoding.ASCII.GetBytes(data);
126                      //There is a intermitant problem that I came across
127                      //If I write more than one byte in succesion without a 
128                      //delay the PIC i'm communicating with will Crash
129                      //I expect this id due to PC timing issues ad they are
130                      //not directley connected to the COM port the solution
131                      //Is a ver small 1 millisecound delay between chracters
132                      foreach (byte hexval in hexstring)
133                      {
134                          byte[] _hexval = new byte[] { hexval }; // need to convert byte to byte[] to write
135                          serial.Write(_hexval, 0, 1);
136                          Thread.Sleep(1);
137                      }
138                  }
139                  catch (Exception ex)
140                  {
141                      //   para.Inlines.Add("Failed to SEND" + data + "\n" + ex + "\n");
142                      // mcFlowDoc.Blocks.Add(para);
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  }
Posted
Updated 20-Oct-22 2:44am
v2
Comments
Richard MacCutchan 20-Oct-22 9:21am    
You need to find the numeric fields in the text and separate them out. You can then use something like Int32.TryParse Method (System) | Microsoft Learn[^] to convert them to actual integers for calculations.
tarek shawki 21-Oct-22 3:16am    
yes that is exactly what i want but i don't know how to separate the numeric fields from the whole string ( it is happening in >> recieved_data ) so i was thinking maybe an array would help ?? i'm trying to put it in an array to separate them then convert it to integer but i did not figure how to do it yet :D
Richard MacCutchan 21-Oct-22 5:13am    
You have to work that out based on the information returned from the weighing machine. If it is fixed format then you can do it by character position, if it is variable then you need to parse by keywords or number of fields.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900