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

  • Tutorial

Instead of joining

As usual, I propose to do something strange - try to connect several old RS232 devices through one USB port using blue tape and ingenuity. The article will not be large, but rather a description of where to get it and why to do all this at all.

What for?

This is necessary when a certain special hardware complex, consisting of separate devices and which performs some unified functionality, suddenly begins to modify. Of course, you can try to find a set of equipment for a newer one, but in real life this is very rarely done. They begin to modify what is. Sometimes thoughtfully, but more often as it happens.

As a rule, the “brains” of such a complex is a computer with 100500 RS232 outputs. I can cite examples of a cashier’s place 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 people get the idea to replace them with a regular PC and get decent power at an adequate price, but it quickly becomes clear that RS232 on new PCs died out as a class. Now this interface has become highly specialized. Accordingly, you need to either screw up a bunch of RS232 yourself or look for a ready-made special solution.

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

People who are not dedicated to the topic immediately happily buy a bunch of Chinese USBtoRS232 adapters, and then everything goes very sadly. It’s better not to do this. The second option is to purchase a custom expansion card with a bunch of RS232. This option is already better and has the right to life, but also has its drawbacks. For example, the cost and problems with firewood if you are using the wrong Windows or the wrong version. Also, accessibility in the future is not an unimportant factor, as something goes wrong and the park can expand in the future. Then it turns out that a specific model is no longer produced or not settled in a specific locality, etc. In general, it is always dangerous to tie yourself to a specific device, especially if you can not do this.

We are trying to do something

It may seem strange that primitive RS232 devices are so difficult and expensive to connect normally if in fact there are usually simple protocols and a primitive physical layer. And all because usually such hardware systems are used in commercial profitable 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 prevent you from trying to assemble your budget bike. As a bonus, we will be able to change the behavior of such a peculiar RS232 multiplexer and completely bypass the problem of writing USB drivers. HID profile is supported almost everywhere.

I once read an excellent article by RaJaBefore that, I was interested in USB, but I didn’t get to practice. I had several cheap debug 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 works at a frequency of 72Mhz and will break any Arduino of a similar form factor in terms of characteristics. But the main advantage is the ubiquity of this board. I do not know simpler and cheaper ways to feel the "iron" USB.

By adding three cheap RS232toUART MAX2323 converters and a bit of “loose streaks”, you can assemble 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 the usual RS232 <=> USB adapters, you will end up with the fact that the system has a bunch of identical devices to which it is not clear what is connected and all this is wildly buggy, but it still looks more sad.

If you drop a little aside which USB to UART chips are available everywhere, you will find that there are a lot of fakes. I think it’s not worth explaining how all this then behaves with the original drivers. And no one can guarantee you that even branded adapters suddenly do not buy the left batch of chips. Cheaper production, it occurs everywhere.

As a result, we came up with about such a simple switching scheme, it can even be depicted in the Arduino style:

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

The pinout of RS232 connectors in the program ( Fritzing ) where I sketched a strange scheme, but I think it would be easy for anyone to find it on the Internet, the contacts are used as usual 2,3 and 5. And it will be even more convenient to use a UART to RS232 converter board which already has an RS232 connector .

Low level

I wrote and debugged the code for the microcontroller using 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.). I took the project from the article above as a basis. This is the first example that I earned right after pouring.

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

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

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

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

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

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

The project lies here .

High level

The second part of the software is examples and tests collected in a confused Java project written by IDE IDEA. It is assumed that work with the device is integrated into high-level software using various wrappers for working with the USB stack, depending on the language, this software is written on the court. Now it’s difficult to find such a PL so that there are no such wrappers for it. Separately, for old believers, I note that java and usb are compatible if cooked correctly, this is proven by practice and is used in a commercial project.

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

The difference is that on Windows you can access the USB device directly 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 that's it, work only with it please. Work in this way is a little slower than directly, but directly also possible if you convince the system not to install drivers. This is real.

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


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

Do not forget to punch author's RaJa article in which fingers explain how to feel the iron USB and keep the desire to understand further.

Also popular now: