通过串口发送文本文件 [英] Send text file over serial port

查看:143
本文介绍了通过串口发送文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我正在尝试使用C#通过串行端口将文本文件发送到调制解调器.我尝试了以下操作,但看不到任何数据写入串行端口.

我的方法正确吗?以下是我的端口设置.我使用的是波特率115200,并且已根据调制解调器的指定对硬件进行流量控制.

port.StopBits = StopBits.One;
port.Parity = Parity.None;
port.Encoding = System.Text.Encoding.GetEncoding(1252);
port.DtrEnable = true;
port.RtsEnable = true;
port.Handshake = Handshake.RequestToSend;

Hi Guys

I am trying to send a text file to a modem over the serial port using C#. I have tried the following but not seeing any data being written to the serial port.

Is my approach correct? Below are my port settings. I am using baud rate 115200, with hardware flow control on , as specified by the modem.

port.StopBits = StopBits.One;
port.Parity = Parity.None;
port.Encoding = System.Text.Encoding.GetEncoding(1252);
port.DtrEnable=true;
port.RtsEnable = true;
port.Handshake = Handshake.RequestToSend;

StreamReader readtext = new StreamReader("filename.txt");
string contents = readtext.ReadToEnd();
readtext.Close();
readtext.Dispose();
byte[] WriteBuffer=new byte[contents.Length];
ASCIIEncoding enc = new System.Text.ASCIIEncoding();
WriteBuffer = enc.GetBytes(contents);
port.Write(WriteBuffer, 0,contents.Length );

推荐答案

大大简化了代码:
You can simplify your code quite a bit:
byte[] writeBuffer = File.ReadAllBytes("filename.txt");
port.Write(writeBuffer, 0, writeBuffer.Length);


http://msdn.microsoft.com/en-us/library/system. io.file.readallbytes.aspx [ ^ ]
但这对从串行端口输出的数据没有任何影响.
1)您确定没有数据在写吗?为什么?你怎么检查的?
2)如果没有任何数据,则:
2a)端口是否连接到物理串行端口,例如COM1?您是否正在检查COM1,并确保可以使用终端程序(超级终端或类似程序)对其进行写操作并与调制解调器对话?
2b)您配置了握手功能吗?是的,请将其关闭(默认情况下处于关闭状态)
2c)您是否启用了RTS和DTR?试试吧.

如果这样做没有帮助,请编辑您的问题并发布用于端口创建和初始化的代码片段-当我们了解更多信息时,我们可能会提供进一步的帮助.

感谢格里夫,我可以使用串行端口监视工具查看从串行端口发送和接收的内容.是的,我已经用超级终端等测试了COM9.我用流控制设置更新了我的问题.请注意,调制解调器指定了硬件流量控制."

好的!因此,您实际上什么都没有发出,但是您可以使用Hyperterminal进行传输.尝试禁用握手-调制解调器可以指定它,但是如果它不用于测试,那就没关系-您可以在传输某些数据时重新启用它.可能是您和调制解调器之间存在电缆连接-如果您没有RTS/CTS和DTR/DTE对,则启用握手功能只会禁用端口传输.


http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes.aspx[^]
But it won''t make any difference to data coming out of the serial port.
1) Are you sure no data is being written? Why? How have you checked?
2) If you don''t have any data, then:
2a) Is the port connected to a physical serial port, such as COM1? Are you checking COM1, and have you made sure that you can write to it and talk to the modem with a terminal program (hyperterminal or similar)?
2b) Have you configured Handshaking? Is so, turn it off (it is off by default)
2c) Have you enabled RTS and DTR? Try it.

If this doesn''t help, edit your question and post the code fragments you use for port create and initialize - we may be able to help further when we have more info.

"Thanks Griff, I can see what is being sent and received from the serial port , using a serial port monitoring tool. Yes I have tested COM9 with hyperterminal etc. I have updated my question with the flow control settings. Note the modem specifies hardware flow control."

Good! So you genuinely have nothing coming out, but you can transmit with Hyperterminal. Try disabling the Handshake - the Modem may specify it but if it ain''t there for testing it doesn''t matter too much - you can re-enable it when some data is being transferred. It may be cabling between you and the modem - if you don''t have the RTS/CTS and DTR/DTE pairs enabling handshake will just disable the port transmit.


这篇关于通过串口发送文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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