C++ 下的 RS-232 混淆
给定代码有什么问题?为什么当我们通过 d-9 连接器连接 2 号针和 2 号针短接时,没有显示 rs232 的输出。 3在那?
#include <bios.h>
#include <conio.h>
#define COM1 0
#define DATA_READY 0x100
#define SETTINGS ( 0x80 | 0x02 | 0x00 | 0x00)
int main(void)
{
int in, out, status;
bioscom(0, SETTINGS, COM1); /*initialize the port*/
cprintf("Data sent to you: ");
while (1)
{
status = bioscom(3, 0, COM1); /*wait until get a data*/
if (status & DATA_READY)
if ((out = bioscom(2, 0, COM1) & 0x7F) != 0) /*input a data*/
putch(out);
if (kbhit())
{
if ((in = getch()) == 27) /* ASCII of Esc*/
break;
bioscom(1, in, COM1); /*output a data*/
}
}
return 0;
}
What's the problem in given code? Why it is not showing the output for rs232 when we connect it by the d-9 connector with the short of pin number 2 & 3 in that?
#include <bios.h>
#include <conio.h>
#define COM1 0
#define DATA_READY 0x100
#define SETTINGS ( 0x80 | 0x02 | 0x00 | 0x00)
int main(void)
{
int in, out, status;
bioscom(0, SETTINGS, COM1); /*initialize the port*/
cprintf("Data sent to you: ");
while (1)
{
status = bioscom(3, 0, COM1); /*wait until get a data*/
if (status & DATA_READY)
if ((out = bioscom(2, 0, COM1) & 0x7F) != 0) /*input a data*/
putch(out);
if (kbhit())
{
if ((in = getch()) == 27) /* ASCII of Esc*/
break;
bioscom(1, in, COM1); /*output a data*/
}
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
嗯,代码看起来没问题。您是否确实正确连接了插头中的其余引脚,请参阅 serial 和 引脚连接。
Well, the code looks alright. Have you really connected the remaining pins correctly in the plug, see serial and pin connections.
从您的代码中没有任何明显的原因可以看出。在处理硬件/软件时检查您的所有基础。以下 Microsoft 文章使用 _bios_serialcom (来自 bios.h)进行了不同的实现,这可能是一个很好的参考点。
http://support.microsoft.com/kb/39501
有关下一步操作的建议:
我还建议使用为波特率、奇偶校验(例如_COM_NOPARITY)预定义的常量替换文字(例如0x08),以使代码在您的问题中更具可读性。
检查 Com 端口是否确实打开,因为它是上面的代码示例中未检查的假设。
检查 Com 端口是否实际打开
还要检查 DB9 的引脚连接。要连接两台计算机/设备,您需要将调制解调器调零,例如另一端的引脚 2 到引脚 3,以及信号接地。确保您禁用/不查找 DTR。
如果其他计算机/设备已设置,那么我建议首先运行超级终端(程序->附件->通信)并连接到您的 COM 1 并检查您是否可以看到来自其他设备的字符。如果不是,则很可能与您的电缆有关。
希望有帮助。
Nothing obvious stands out from your code as the cause. Check all your bases as you are dealing with hardware/software. The following Microsoft article has a different implementation using _bios_serialcom (from bios.h) which might be a good reference point for you.
http://support.microsoft.com/kb/39501
Suggestions for where to go from here:
I would also suggest replacing the literals (eg 0x08) using the constants predefined for Baud rate, Parity (eg _COM_NOPARITY) to make the code more readable in your question.
Check that the Com port is actually open, as its a assumption which is unchecked in your code example above.
Also check up on the pin connections for the DB9. To connect two computers/devices you will need to null modem it, eg pin 2 to pin 3 at the other end, plus the Signal Ground. Make sure you are disabling/not looking for DTR.
If the other computer/device is setup then I would suggest first running HyperTerminal (Programs->Accessories->Communication) and connect to your COM 1 and check you can see characters from the other device. If not its most likely related to your cable.
Hope that helps.
在检查代码之前,请务必使用终端程序检查串行通信。我对 Windows 环境没有太多经验,但在 Linux 中,您可以使用 Cutecom 或 gtkterm 等程序,在其中可以从串行端口发送/接收数据。我们在 Linux 中广泛使用这些程序进行串行通信,它们非常适合调试串行端口接口(也包括硬件和软件)的潜在问题。因此,在怀疑您的代码之前,请先使用终端仿真器程序进行检查。
Before checking with your code always check your serial communication with a terminal program. I don't have much experience with Windows environment but in Linux you have programs like cutecom or gtkterm where you can send/receive data from serial port. We extensively used these programs for serial communication in Linux, they are great for debugging potential problems with serial port interface (both h/w & s/w as well). So, before suspecting your code check with a terminal emulator program.
嘿,我不是 Win32 方面的专家,但使用另一篇文章作为来源似乎更容易(之前提到的文章看起来已经过时了):
http://msdn.microsoft.com/en-us/library/ms810467
这也很旧,日期约为 1995 年,但看起来仍然有效。 NT 体系结构在授予对硬件的访问权限时非常严格,例如,要将字节发送到 PC 并行端口,需要依赖开源开发人员编写的解决方法 dll。
Hey, I am not an expert on Win32, but it seems to be easier to use another article as a source (the one mentioned here before looks outdated):
http://msdn.microsoft.com/en-us/library/ms810467
This is old too, dated around 1995, but it looks still effective. The NT architecture is very restrictive when it comes to grant access to hardware, for example, to send bytes to a PC paralell port one needs to rely on workarounds dll written by open source developers.
我从您关于“pin 2 & 3”的评论中假设您已经连接了环回电缆,因此您希望在屏幕上看到您键入的任何内容(并在断开电缆时停止这样做) 。
我认为代码中存在错误:
if (kbhit())
位于if (status & DATA_READY)
的内部。这意味着您只检查键盘是否有一些输入准备好从串行端口接收 - 但实际上不会,因为您还没有向它发送任何内容!尝试移动测试,看看是否有所改善。
(这里有一些 类似的串行端口代码,其中放置
if (kbhit( ))
在DATA_READY
检查之外进行测试,它并不声称有效,但提供了一些证据表明这可能是问题的根源...)I'm assuming from your comment about "pin 2 & 3" that you've connected a loopback cable, so you're expecting to see anything you type come up on the screen (and stop doing so when you disconnect the cable).
I think there's a bug in the code: the
if (kbhit())
is inside theif (status & DATA_READY)
.That means you only check the keyboard if there is some input ready to receive from the serial port - which there won't be, because you haven't sent it anything yet! Try moving the test and see if it improves matters.
(Here is some similar serial port code that puts the
if (kbhit())
test outside theDATA_READY
check. It doesn't claim to work, but provides some evidence that this might be the source of the problem...)