RS-232 mess in C ++

What is the problem in the given code? Why doesn't it show the output for rs232 when we connect it to the d-9 connector with pin number 2 and 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; } 
+4
source share
5 answers

Well, the code looks good. You really plugged the rest of the pins correctly in the plug, see serial and .

+1
source

Nothing obvious stands out from your code as a reason. Check all your databases as you are dealing with hardware / software. The following Microsoft article has another implementation using _bios_serialcom (from bios.h), which might be a good starting point for you.

http://support.microsoft.com/kb/39501

Suggestions on where to go from here:

  • I would also suggest replacing literals (e.g. 0x08) with constants predefined for baud rate, parity (e.g. _COM_NOPARITY) to make the code more readable in your question.

  • Make sure that the Com port is indeed open, as its assumption, which is not indicated in the above code example.

  • Also check DB9 pin connections. To connect two computers / devices, you will need a null modem, for example, pin 2 to pin 3 at the other end, plus signal ground. Make sure you turn off / not looking for DTR.

  • If another computer / device is configured, I suggest that you first start HyperTerminal (Programs-> Accessories-> Communication) and connect to your COM 1 and verify that you can see characters from another device. If this is not most likely connected to your cable.

Hope this helps.

0
source

Before checking the code, always check your serial connection with the terminal program. I don't have much experience with the Windows environment, but on Linux you have programs like cutecom or gtkterm where you can send and receive data from the serial port. We have widely used these programs for serial communication on Linux, they are great for debugging potential serial port interface issues (both h / w and s / w). Therefore, before you suspect your code, check the program of the terminal emulator.

0
source

Hey, I'm not an expert on Win32, but it seems easier to use another article as a source (the one mentioned here before it looks deprecated):

http://msdn.microsoft.com/en-us/library/ms810467

It is also old, dated around 1995, but it still looks effective. The NT architecture is very limited when it comes to providing access to hardware, for example, to send bytes to the paralell port for a PC. You must rely on dll workarounds written by open source developers.

0
source

I assume from your comment on "pin 2 and 3" that you plugged in a loopback cable, so you expect to see anything you type on the screen (and stop doing this when you unplug the cable).

I think there is an error in the code: if (kbhit()) is inside if (status & DATA_READY) .

This means that you are only checking the keyboard if there is any input ready to receive from the serial port, which will not be there, because you have not sent anything yet! Try moving the test and see if it improves it.

(Here is some similar serial port code that puts the if (kbhit()) test outside of the DATA_READY check. It does not work, but it gives some evidence that this may be the source of the problem ...)

0
source

Source: https://habr.com/ru/post/1300275/


All Articles