Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Have code writen where it reads the content of the file, when it reads the file it writes to a certain point of the text file, current line and then fails. I have tried to remove the port.close but no luck in getting the file to write correctly to the com port. The file saves and has everything in it. I am writing to a cisco switch, so also I would like to be able to verify the prompt on the switch and if not in enable mode, have it send the correct command. So I would be looking for a > or # at the end of the line returned to the script.

C#
   private void Submitbtn_Click(object sender, EventArgs e)
    {
        String Hostname = Clientnametbx.Text + Locationcodelbx.Text;
        String Client = Clientnametbx.Text;
        String Hostfile = ((@"c:\clients\" + Client) + (@"\" + Hostname + ".txt"));

        if (Textfilerbtn.Checked)
        {
      //Opens the .TXT File to allow copy to the system//
            Configuration();
            System.Diagnostics.Process.Start((@"c:\clients\" + Client) + (@"\" + Hostname + ".txt"));
        }
        if (Commportrbtn.Checked)
            // Writes configurations to the serial port on the computer//
        {
            Configuration();
            string Portconfiguration = "com" + CommPortbtn.Text;
            SerialPort port = new SerialPort(Portconfiguration, 9600, Parity.None, 8, StopBits.One);

            // Open the port for communications//
            port.Open();

            //Write Configuration file to System
            using (StreamReader config = new StreamReader(Hostfile))
            {
              port.Write(File.OpenText(Hostfile).ReadToEnd());
             }

            //Close the port//
            port.Close();
            System.Diagnostics.Process.Start((@"c:\clients\" + Client) + (@"\" + Hostname + ".txt"));

            MessageBox.Show("Please review the system configuration on the system");
        }
        else
        {
        }
        }
   }
}
Posted
Updated 30-Oct-12 12:56pm
v3
Comments
Sergey Alexandrovich Kryukov 30-Oct-12 19:04pm    
Well, bad code. In particular, there are no situations when hard-coded path names could be used, ever. Look at "c:\clients". How do you think the user even had the "C:" volume? Nothing guarantees that. And all those string concatenations... Why would you start the external process? It is usually a bad thing...

Now, if you read the file to the end and write it to the port, it means you write all those "\r" and "\n". Are you sure you really need it. Other than that, it could be anything. When you do such things, the half of what's going on is on the opposite side of your RS-232 cable...
--SA

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