且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将十进制数据发送到串行端口

更新时间:2023-11-09 12:30:10

有很多可能的原因导致此方法不起作用:
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?