c# and serial-port communication

Dec 5 2017 2:14 AM
I'm trying to do an application in c #, which sends a text file through a serial port to a machine. all good and beautiful if the machine is put in download mode. if the operator forgets to put the machine on download mode .... here comes the problem, I have a writetimout exception. I would like to use this exception or anything else to resume the process and my program will try to reload the file into the machine. below is my code
 
  1. private void incarca_button_Click(object sender, EventArgs e)  
  2.     {  
  3.         string open_folder = cale_txt.Text.Replace(Environment.NewLine, "");  
  4.         OpenFileDialog theDialog = new OpenFileDialog();  
  5.         theDialog.Title = "Open Text File";  
  6.         theDialog.Filter = "TXT files|*.txt";  
  7.         theDialog.InitialDirectory = open_folder;  
  8.         if (theDialog.ShowDialog() == DialogResult.OK)  
  9.         {  
  10.             var onlyFileName = System.IO.Path.GetFileName(theDialog.FileName);  
  11.             piesa_lbl.Hide();  
  12.             piesa_txt.Hide();  
  13.             SerialPort cnc_com = new SerialPort(com);  
  14.             cnc_com.BaudRate = Int32.Parse(bit);  
  15.             parity = parity.ToString();  
  16.             cnc_com.Parity = (Parity)Enum.Parse(typeof(Parity), parity);  
  17.             cnc_com.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stop_bit);  
  18.             cnc_com.DataBits = Int32.Parse(data_bit);  
  19.             cnc_com.Handshake = (Handshake)Enum.Parse(typeof(Handshake), flow_control);  
  20.             cnc_com.Encoding = Encoding.ASCII;  
  21.             cnc_com.ReadTimeout = 500;  
  22.             cnc_com.WriteTimeout = 500;  
  23.             cnc_com.ReadBufferSize = 1000000;  
  24.             cnc_com.WriteBufferSize = 1000000;  
  25.             //cnc_com.DtrEnable = true;  
  26.             //cnc_com.RtsEnable = true;  
  27.   
  28.             try  
  29.             {  
  30.                 cnc_com.Open();  
  31.             }  
  32.             catch (UnauthorizedAccessException) { }  
  33.             catch (System.IO.IOException) { }  
  34.             catch (ArgumentException) { }  
  35.             using (StreamReader sr = new StreamReader(open_folder + @"\" + onlyFileName))  
  36.             {  
  37.                 while (sr.Peek() >= 0)  
  38.                 {  
  39.                     try  
  40.                     {  
  41.   
  42.                         cnc_com.WriteLine(sr.ReadLine());  
  43.                         //cnc_com.Handshake = (Handshake)Enum.Parse(typeof(Handshake), flow_control);  
  44.                     }  
  45.                     catch(TimeoutException wtout)  
  46.                     {  
  47.                         MessageBox.Show("Masina nu este pregatita\nReincarcati programul");  
  48.                         cnc_com.Close();  
  49.                         cnc_com = null; 
  50.                     }  
  51.                 }  
  52.             }  
  53.             piesa_txt.Show();  
  54.             piesa_lbl.Show();  
  55.             piesa_txt.Focus();  
  56.             //cale_txt.Text = "";  
  57.             cnc_com.DiscardOutBuffer();  
  58.             cnc_com.DiscardInBuffer();  
  59.             cnc_com.Close();  
  60.             cnc_com = null;  
  61.         }  
  62.     }  
I would like this piece of code to be executed only if the file was passed successfully to the machine
 
  1. piesa_txt.Show();  
  2.             piesa_lbl.Show();  
  3.             piesa_txt.Focus();  
  4.             //cale_txt.Text = "";  
  5.             cnc_com.DiscardOutBuffer();  
  6.             cnc_com.DiscardInBuffer();  
  7.             cnc_com.Close();  
  8.             cnc_com = null;  
thank you 

Answers (1)