Emulate a UART like piece of hardware in linux - linux

In order to test a piece of software, I need to write an emulator. This emulator has to represent the hardware very closely, it has to be able to start off at 300 baud, then negotiate with the software and eventually end up at another baudrate, for example. Also, things like parity and additional stop bits are very important.
How can I emulate such a device under Linux? I tried pty's, but it seems immume for baudrate settings and parity settings. I also tried socat, but it suffers the same fate: no matter how you configure the receiver, the data always arrives fine. I need this emulator to be able to send out the data on 300 baud in such a way that if you listen at 9600 baud you get rubbish at best. How can I do such a thing?

Related

Constant Delay in Bluetooth Low Energy (BLE) data transmission

I am trying to evaluate the suitability of some different wireless interfaces for our project on 2xRaspberry Pi 4 and currently I’m evaluating Bluetooth Low Energy. Therefore I have written an Central and Peripheral device application with the Qt framework (5.15). In my case the latency time between messages is important, because of some security aspects. The message size of each command is around 80-100 Bytes. In one of my tests I have sent 80 Bytes commands every 80ms. Ideally the messages should be received on the other device in 80ms interval as well. For the LAN (TCP) interface this test works well.
For Bluetooth Low Energy I observed that messages, which are sent from Peripheral to Central work quite good and I measured no big delay. Different results I got for the Central to Peripheral direction. Here, I have received the messages in the interval of 100ms to 150ms really exactly. It seems that there couldn’t be a very big magic behind it, so is there any plausible explanation for this? I tested it with a Python script as well and I observed the same results. So it seems that the Qt implementation shouldn’t be the problem.
During research I found out, that the connection interval may influence this, but in Qt the QLowEnergyConnectionParameterRequest (QLowEnergyConnectionParameters Class | Qt Bluetooth 5.15.4) doesn’t work for me. Is there any command, where I can set the connection interval for test purposes at the command line on Linux?
Kind regards,
BenFR
It is possible that your code is slower from central to peripheral because WRITE is used instead of WRITE WITHOUT RESPONSE. The difference is that WRITE waits for an acknowledgement, therefore slowing the communication down, while WRITE WITHOUT RESPONSE is very much like how notifications/indications work in that there's no ACK at the ATT layer. You can change this by changing the write mode of your application and ensuring that the peripheral's characteristic supports WriteNoResponse.
Regarding changing the connection interval, the change needs to be accepted from the remote side in order for it to take effect. In other words, if you are requesting the connection parameter change from the peripheral, then the central needs to have code to receive this connection parameter change request and accept it.
Have a look at the links below for more information:-
How does BLE parameter negotiation work
Understand BLE connection intervals and events
The different types of BLE write

How to upload to Arduino STM32 with HC-05 Serial Bluetooth

I got myself a little STM32F103C8T6 based board that is similar in size to the Arduino Nano, but with a little bit more memory and speed. But for me more important, with 3.3V I/O. It can also be used with the Arduino IDE after you install the correct libraries.
I wanted to connect this to a HC-05 serial bluetooth, especially for remote uploading. But this didn't work. Basic transfer worked, but upload for reprogramming failed.
I tried to adjust the baudrate with AT-commands of the HC-05: e.g.:
AT+UART=57600,0,0
But it still didn't work. What is wrong?
After several tests I noticed that stm32flash printed "8e1" as upload setting. This means even parity. So the correct AT-command should be:
AT+UART=57600,0,2
Baudrates 115200 and 230400 also worked as long as the parity is correct.
But since there are even more things that can go wrong, here is some more information:
Connecting via bluetooth to the HC-05 required using the default password "1234". (This can later be changed with command "AT+PSWD=....".)
Then on my Windows 10 machine two new COM-ports appeared, of which only one was functional.
The most confusing thing was that on Windows you still can adjust the baud rate and serial settings, but it has no effect!
To use the AT-commands I had to connect to another serial port (from an USB to serial adapter). Holding the "Key"-button during power-on enables the AT-mode with 38400 baud.
There is a video that shows the details for a regular Arduino:
https://www.youtube.com/watch?v=xXUKfTNHkKE
"HC 05 Bluetooth Wireless Upload to Arduino" (Wayne Holder)
Even with the correct baud settings, remember to put the STM32 board into correct boot mode and press RESET. And use the correct COM-port.
If you use the serial port in your uploaded program you also must remember to use these settings. E.g.:
Serial.begin(57600,SERIAL_8E1); // for serial init

Bluetooth Low Energy - Connecting to same device

I'm writing a Bluetooth Low Energy library. For now, it will only run on Linux (and with Bluez 5.41+).
I'd like to have tests that can be run on any computer with BLE support and test the entire stack (application, host and controller), without requiring extra hardware (another BLE-capable device). Requiring extra hardware is a great way of making sure the tests won't be run more than once in a blue moon.
For that end, I'd like to use my computer to connect to itself (as both a peripheral and central) during the tests. It seems like this should be possible, since I can be connected at the same time as peripheral and central to other devices. But advertisements from my own computer never show up on (my own computer's) BLE scans.
Is it all possible to have the computer connect to itself in this way?
No, not if you only have one Bluetooth controller.
The reason is that radio peripherals can only transmit OR receive in a given time moment.

weird characters displayed during serial communication OSX

I have tried communicating via serial (OSX w/ prolific drivers -> USB RS232 adapter -> Tx,Rx and GND pins on device serial ttl port) to a device and done so successfully using
screen /dev/tty.usbserial 115200 8N1
I get to log in and use it as if I was SSH or TelNetted in...
However whenever I try to go into system recovery mode (holding CTRL+1) while the device is powering on, it starts displaying weird characters and until I close the screen session it will continue showing weird characters:
Of course when we tried doing the same thing on my boss' macbook running windows and PuTTY and everything worked fine, even in system recovery mode; characters were displayed properly.
What gives? Id like to learn the intuition to use because up till now I concluded that since I can bot into the system and see characters normally everything about the connection should be fine and its must have been the recovery partition that was broken. This was wrong of course...
Niko
Dunno if it is a OSX problem,
I had similar problems before. I fixed it either by changing the Baud rate of the com ports or by making sure the Ground on the circuit is the same as the ground of the supply.

Sniffing IOCTL and serial port communication

I'm trying to reverse the protocol used by an early nineties logic analyzer an its PC software.
The device is connected via RS-232 (propietary wiring) and communicates with a DOS program, successfully running on DOSBOX.
I'm able to control the device with the original software but it would be useful to autimathe the downloading of data from the analyzer using a custom program but to do so I need to know what is going on the serial port.
Ineed to know what mode is the serial port set, while I know for sure the datarate is either 1200 or 9600 bps (configurable on the device) I don't know the flow control (I guess it is RTS/CTS).
I also need to tap into the conversation between the prgram and the device without disturbing their communication.
Reading the serial port with another program (cutecom/minicom) prevents the emulator from receiving the data from the hardware.
So, summing up, what I need to know is:
What configuration is set on /dev/ttyS0 (via IOCTL calls, i think)
What goes on between program and device.
I was thinking in programming a library which acts as a proxy for the standard c library (via LD_PRELOAD) but there must be an easier way to do this.
You can use slsnif (Serial Line SNIFfer).
http://linux.die.net/man/1/slsnif
Here's a link to the sourceforge project so you can download it. I don't believe it comes with any modern distributions but I could be wrong so check your distro's software repository first.
http://sourceforge.net/projects/slsnif/
I use ttyrpld for tty sniffing. I ported it to PPC and run it on 2.6.32. It logs all of the tty traffic on the board to files, one per tty. Works well.

Resources