将十进制数据发送到串行端口 [英] Send data in decimal to serial port

查看:81
本文介绍了将十进制数据发送到串行端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了代码,通过serialport.write方法将字节(服务器时间)发送到串行端口,
它给出一个错误,指出输入字符串的格式不正确.
我想做的是,将服务器时间(以十进制表示)发送到串行端口,以便连接到串行端口的设备可以读取此数据.(此设备只能读取十进制格式)
例如:如果时间为9:26,则串行端口的输出应为57 58 50 54.
我不知道该怎么做,所以我以这种方式开始了

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

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

推荐答案

有很多可能的原因导致此方法不起作用:
1)净时间"命令可能会返回:
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)您可能会修剪掉返回字符串的错误位
3)您可能将输出"字符串转换为错误的字节.
4)您可能将通讯参数设置为错误.

所以:检查一下:
1)将对网络时间"的响应记录到日志文件,控制台或通过消息框记录到屏幕.还要记录您的输出"字符串.如果看起来正确,请继续

2)以相同的方式记录要发送到通信设备的数据:示例中应为四个字符"9:26"

3)尝试用以下代码替换您的(奇怪的,可能是问题所在的)转换例程:


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

然后将其发送.

4)检查通信参数应该是什么,并再次检查您是否已匹配它们.

请务必按此顺序检查这些内容,否则您将不知道到底发生了什么.


感谢您抽出宝贵的时间.

净时间确实返回服务器时间,并且我已经修剪了要在文本框中显示的输出,并且显示正确.

还是有问题:"

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)



不要通过添加答案"按钮来回答-只有这样您才能收到电子邮件通知.相反,请对我的答案发表评论,以便我收到电子邮件!

如果输出"中的值正确显示在您的文本框中,那么我将检查您的设置-确保您的波特率,Bpc,奇偶校验等都符合设备的期望.

在SerialPort.Write上放置一个断点并检查缓冲区-如果输出"字符串正常,应该没问题.

然后,检查您的设备是否不希望任何前导和/或尾码来表明这是时间码.

最后,使用超级终端或类似工具直接与设备对话,然后键入您认为要发送的内容.设备是否响应正常?



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?


发生错误时,从您的程序发送的实际数据是什么?

另外,在语句如果时间为9:26时,串行端口的输出应读为57 58 5054.",这两个字符串之间的关系是什么,即57 58 50 54如何表示09:56的时间?
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?


这篇关于将十进制数据发送到串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆