Click here to Skip to main content
15,799,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written code to send bytes( server time) to serial port at serialport.write method ,
it gives an error saying that input string was not in the correct format.
What I want to do is, to send server time in decimal to serial port so that device connected serial port can read this data.( This device can read only decimal format)
for example: if time is 9 : 26 the output at serial port should read as 57 58 50 54.
I have no idea how to do it so I have started off this way
MSIL
public partial class Form1 : Form
    {
      string output;
      SerialPort _serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);

      public Form1()
      {

       InitializeComponent();
       timer1.Enabled = true;
       _serialPort.Open();
      }

    public string getnettime()
    {
    System.Diagnostics.Process p = new System.Diagnostics.Process();p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.FileName = "net";
    p.StartInfo.Arguments = "time";
    p.Start();
    p.WaitForExit();
    output = p.StandardOutput.ReadToEnd();
    output=(output.Remove(0, 40));
    output = (output.Remove(5, 42));
    return output;
   }
   private void timer1_tick(object sender, EventArgs e)
   {
     //decimal dectime=0;
     try
     {
        getnettime();
        byte[] buffer_2_storetime = new byte[5];
        byte data_2_send = 0;
        data_2_send = Byte.Parse(output);
        buffer_2_storetime[0] = data_2_send;

        _serialPort.Write(buffer_2_storetime, 0, buffer_2_storetime.Length);//write to serial port

     }
     catch (Exception ex)
     {
       MessageBox.Show("An error occurred : " +
            ex.Message, "Data transfer", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }

   }


Can anyone help me
Thanks
Posted
Updated 13-Dec-10 18:48pm
v2
Comments
OriginalGriff 14-Dec-10 5:23am    
Answer updated

There are a lot of possible reasons why this doesn't work:
1) The "net time" command could be returning:
"Could not locate a time-server.

More help is available by typing NET HELPMSG 3912."

2) You could be trimming out the wrong bits of the returned string
3) You could be converting the "output" string to bytes wrong.
4) You could have the communications parameters set wrong.

So: Check things:
1) Log the response to "net time" to either a log file, the console, or to the screen via a message box. Also log your "output" string. If that looks right, move on

2) Log the data you are sending to the communications device in the same way: it should be four characters "9:26" in your example

3) Try replacing your (odd and probably the problem) conversion routine with this:
getnettime();
byte[] buffer_2_storetime = System.Text.Encoding.ASCII.GetBytes(output);
_serialPort.Write(buffer_2_storetime, 0, buffer_2_storetime.Length);//write to serial port
And then send that instead.

4) Check what the communications parameters should be, and double check you have matched them.

It is important to check these in this order - otherwise you won't know what the heck is going on.


"Thanks for taking your time.

net time does returns the server time and i have trimmed that output to be displayed in the textbox and it displays correctly.

still something is wrong at:"

byte[] buffer_2_storetime = System.Text.Encoding.ASCII.GetBytes(output);
_serialPort.Write(buffer_2_storetime, 0, buffer_2_storetime.Length)


Don't answer via the Add Answer button - only you would get an email notification that way. Instead, respond with a comment on my answer so I get an email!

If the value in "output" is displaying in your text box correctly, then I would check your settings - make sure your baud rate, Bpc, parity, etc are all as expected by your device.

Put a breakpoint on the SerialPort.Write and examine the buffer - it should be fine if the "output" string is ok.

Then, check your device is not expecting any leading and/or trailing codes to indicate this is a time code.

Finally, use Hyperterminal or similar to talk directly to the device and type in exactly what you think you are sending. Does the device respond ok?
 
Share this answer
 
v2
What is the actual data that is being sent from your program when the error occurs?

Also, in the statement "if time is 9 : 26 the output at serial port should read as 57 58 50 54.", what is the relationship between these two strings, i.e. how does 57 58 50 54 represent a time of 09:56?
 
Share this answer
 

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