1

I am trying to understand how to use the Windows API to communicate with RS232/RS422. I need to interface to some hardware where I don't really have control of the communication protocol, so I am forced to work with this. I need to set the TX + (pin 3) to high (~5 volts), and the TX - (pin 7) to low (~0 Volts).

I know there is

SetCommBreak

But that puts both pins (3,7) to ~5 volts. I need only pin 3 to be 5 volts, and pin 7 to be ~ 0 volts. Is there a doable way to do this? I would love if I could just control these lines like wire, but is there a way to manipulate the baud rate and transmit data to achieve this? Or any sort of solution?

joe
  • 11
  • 2
  • Not the same question.. Reversed.. I am finding both signals are high when it says it isn't supposed to be – joe Aug 14 '18 at 13:08
  • @joe In the other question, you said that calling `SetCommBreak(handle)` does what you are saying that you're trying to do here. – Thomas Jager Aug 14 '18 at 13:11
  • RS232 doesn't even have TX+ and TX- pins. The problem here is that you're using RS422, but Windows offers an _abstraction_ of a port. And in that abstraction, you send bits. You don't put ports in a high or a low state. For that sort of stuff you can use a Raspberry Pi with it's GPIO pins, those are literally General Purpose. – MSalters Aug 14 '18 at 15:09

1 Answers1

1

You can not manipulate the RX and TX pins arbitrarily and without limitations *without writing your own RS485/RS422 protocol is hardcoded on the chip. The reason why you are limited in manipulating the pins when the windows serial driver is loaded ( automatically ) is that the specification of a serial port ( what RS485 / RS 422 is ) requires a specific data structure, i.e. start bits, stop bits, signals like RTS ( ready-to-send ) , ... this required data structure is implemented in the windows serial driver and you can not overwrite it. You can custom your data in a way that the required pin states are produced but your data will always be enveloped in the structure that is required by the serial protocol ( start / stop bits, ... ), cf this graphic from https://en.wikipedia.org/wiki/RS-485 source : https://en.wikipedia.org/wiki/RS-485

in windows you initialize a serial port in a DCB structure ( https://learn.microsoft.com/en-us/windows/desktop/api/winbase/ns-winbase-_dcb )

within this serial port structures the several protocols that run on RS485 / RS422 are defined like MODBus, ...

you can think of a RS-485/RS-422 serial port as a 'smart file'. 'smart' because of the multiple slaves / addresses that can be accessed. if you write a correct address to the file / serial bus you get an answer, i.e. if you write a 23 what means "Slave 23, send me your register values !"

This overlying protocol is what has to be implemented in software. For this you do not need to manipulate single pins except possibly the control lines like RTS, ... and actually you can not do this without writing your own driver.

An example how to implement MODBus protocol is in http://libmodbus.org/documentation/

The following citation shows that the protocols that run over RS-485 are written over the serial port layer :

You cannot easily craft MODBUS messages "by hand", as you would have done with ASCII protocols used on RS232: each MODBUS message ends with a checksum code, computed from the full content of the message. To exchange MODBUS messages, you must therefore use:

either a specific program provided by the device vendor, with a compatible interface;

or a simple RS485 interface with a programming library which encodes and decodes MODBUS messages;

or a smart RS485 interface able to encode and decode by itself the MODBUS messages, such as the Yocto-RS485.

source : http://www.yoctopuce.com/EN/article/a-quick-tutorial-on-rs485-and-modbus

https://social.msdn.microsoft.com/Forums/vstudio/en-US/1751dafb-2fd5-48b8-8c16-08dd95d7db6d/writing-a-string-to-an-rs485-port-vs-2010-c?forum=vcgeneral

ralf htp
  • 9,149
  • 4
  • 22
  • 34