📜 ⬆️ ⬇️

How to connect a bunch of old RS232 equipment via USB without registration and sms (STM32 + USB-HID)

Instead of intro


As usual, I propose to do strange - try to connect several old RS232 devices through a single USB port using blue electrical tape and ingenuity. The article will not be big, rather, it’s a description of what is where to get it and why to do it all.



What for?


It is necessary that this happens when a certain special hardware complex consisting of separate devices and which performs some kind of uniform functionality, suddenly begins to be modified. Of course, you can try to find a set of equipment for newer, but in real life it is very rarely used. Begin to modify what is. Sometimes thoughtfully, but more often how it goes.

As a rule, the "brains" of such a complex is a computer with 100500 RS232 outputs. From examples I can give the place of the cashier in a supermarket, ATMs and the like. I faced the first case.
')


These computers do not shine with power, but differ in space cost. Naturally, it ceases to meet the requirements of modern technologies and many have the idea to replace them with a regular PC and get decent power at an adequate price, but it quickly turns out that RS232 on new PCs has become extinct as a class. Now this interface has become highly specialized. Accordingly, it is necessary either to independently screw a bunch of RS232 or to search for a ready-made special solution.

Of course, you can replace the equipment itself, but if you look at how much a stationary laser scanner of a decent company costs and multiply this price by the number of them, you will change your mind.

People are not dedicated to the topic immediately happily buying a bundle of Chinese USBtoRS232 adapters, and then everything goes very sadly. Better not to do this. The second option is to buy a specialized expansion card with a bunch of RS232. This option is better and has the right to life, but also has its drawbacks. For example, the cost and problems with firewood if not used by Windows or the wrong version. Also not unimportant factor is the availability in the future, as something fails and the park may expand in the future. Then it turns out that a particular model is no longer being produced or is not being decided in a specific locality, etc. In general, tying yourself to a particular device is always dangerous, especially if you can not do this.



We try to do something


It may seem strange that primitive RS232 devices are so difficult and expensive to connect in a normal way if in fact there are usually simple protocols and a primitive physical layer. And all because usually such hardware systems are used in commercial income areas and the purchase of equipment at such prices is justified, and the equipment itself has already passed into the category of special. Special equipment = special issue price.

However, all this does not interfere with trying to collect your budget bike. Bonus we get the opportunity to change the behavior of such a kind of RS232 multiplexer and completely circumvent the problem of writing USB drivers. HID profile is supported almost everywhere.

Once I read an excellent article by the author RaJa Before that, I was interested in USB, but I didn’t get to practice. I had several cheap debugging boards, Chinese Blue Pill clones on the Stm32f103c8t6 microcontroller. The story itself and the idea of ​​creating this board is very interesting, it is worth asking.



This microcontroller is different in that it has three UART and USB hardware support. This is something that may be of interest to us in the context of our idea, but in general the microcontroller on this board runs at 72Mhz and, according to its characteristics, any Arduino of a similar form factor will break. But the most important advantage is the widespread prevalence of this board. I do not know of simpler and cheaper ways to touch the “iron” USB.

By adding three cheap RS232toUART MAX2323 converters and a little “crumbling” you can put together a kind of 3xRS232 <=> USB interface converter.

In my case, it was necessary to connect three RS232 devices to the Raspberry Pi 3. If you use regular RS232 <=> USB adapters, you end up with a lot of identical devices in the system that don’t understand what’s connected and it’s all pretty buggy, but it looks more sad.

If you drop a bit in the direction of which USB to UART chips are available everywhere, then you will find out that there are a lot of fakes. I think it is not necessary to explain how it all then behaves with the original drivers. And no one can guarantee you that even branded adapters will not suddenly buy the left batch of microcircuits. Cheaper production, it happens everywhere.

As a result, I came up with something like this simple switching scheme that can even be portrayed in the Arduino style:



I broke off a resistor that pulls D + up to power and made this tightening controlled by a transistor. She is responsible for identifying the device on the USB bus.

Pinout RS232 connectors in the program ( Fritzing ) where I threw a strange scheme, but I think it would be easy for anyone to find it on the Internet, contacts are involved as usual 2,3 and 5. And it will be even more convenient to use UART to RS232 converter boards which already have an RS232 connector .



Low level


I wrote the code for the microcontroller and debugged it with the help of the EmBitz IDE (to be honest, I was surprised how easy it was to get this IDE, especially after dancing with a tambourine around CooCox.). Based on the draft of the article above. This is the first example that I have earned immediately after casting.

I added work with three UARTs and changed the structure of HID reports so that the exchange with the PC was always 64 bytes with a checksum (crc8).

I tried to auto-reconnect the device if the USB connection did not work correctly. I do not pretend to a super algorithm, I must admit that I am not a USB specialist. A separate transistor, shown in the diagram, controlled by a separate pin B5 pulls one of the USB signal lines to the power supply which simulates switching on the device and the host initializes the device. If the attempt is not successful then a reconnection occurs.

It was noted that in Linux (Raspbian) the percentage of unsuccessful connections is significantly less than in Win 10, perhaps this result was due to my local technical conditions.

The general algorithm is similar to a primitive router, taking a packet via USB, we look at which UART it is designed for and send it in the opposite direction in the same way. There is a bit of processing of the packets themselves, but this applies to the specific RS232 equipment that I connected. These were: Datecs dpd-201 customer screen, Datalogic Magellan 8300 stationary laser barcode scanner and Digi DS890 scale.

In the photo above, the test sample of the device contains another DC-DC power converter on the MP2307 chip.

This is necessary in order to power the device from a voltage of 10-24 V (input) and to be able to connect a customer display that is powered by the same voltage. After DC-DC we convert the converter to 3.3V for all other modules. For this display, the “telephone” connector is also installed instead of the RS232, I did not want to re-solder the stock plug.

The project is here .

High level


The second part of the software is examples and tests compiled into a messy Java project written by IDE IDEA. It is assumed that the work with the device is integrated into the high-level software using various wrappers for working with the USB stack, depending on the language on the court, this software is written. Now it is difficult to find such a PL so that such wrappers do not exist for it. Separately for the Old Believers, I note that java and usb are compatible if prepared correctly, this is proved by practice and is used in a commercial project.

In the process of testing, it turned out that work in Linux and Windows with USB HID is somewhat different, the work was debugged through two libraries usb4java and hid4java. Work through the latter is used in Linux (Raspberry Pi 3).

The difference is that in Windows, you can directly access the USB device even if it is registered as a HID and write / read its endpoints. In Linux, you have to work with a hid device. That is, the standard hid driver is installed and everything, work only with it please. Work in this way is a little slower than directly, but it is also directly possible if you convince the system not to install drivers. This is real.

As promised, I do not stretch the article and do not provide a detailed description of the code, those few people who are interested can see the projects and play live, and I think it would be more useful for the rest to take note that there is such a decision and resort to deeper study if necessary.

Conclusion


The presented device is just one of the examples of how you can quite easily join the process of creating native USB devices and finally stop using adapters.

Do not forget to add RaJa to the author's article , which explains on the fingers how to touch the USB and keep the desire to understand further.

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


All Articles