Bluetooth SPP to a Raspberry returns looping weird values - bluetooth

I'm running out of ideas, I finally got a connection to a Bluetooth Barcode Scanner via SPP, but once I'm listening to the connection with
sudo cat /dev/rfcomm0
It's repeating the scanned code, everytime prepending some ^JJJ values:
^J^JJ^JJJJ^J^4000539103766
^J^JJ^JJJJ^J^4000539103766
^^J^JJ^JJJJ^J^4000539103766
^J^J^JJ^JJJJ^J^4000539103766
^^J^J^JJ^JJJJ^J^4000539103766
J^^J^J^JJ^JJJJ^J^4000539103766
and this continues, sometimes till it stop by itself or till I kill it.
Anyone has ever seen this?

Related

Python3.10.4 serial readline() not displaying prompts

Right now, I am connecting to a device using Tera Term and manually running over 50 tests. My goal is to automate these tests. Currently I am trying to do this through python using the pyserial library. The issue I am running into is readline() will display everything until the login prompt appears. It stops on the line before.
What it should look like
Booting: 2022.08
CPU: cpu_id
In: serial
OUT: serial
Normal Boot
Hit any key to stop autoboot: 0
Starting Kernel ...
Enter your credentials
cpu_id login:
what readline() is displaying:
Booting: 2022.08
CPU: cpu_id
In: serial
OUT: serial
Normal Boot
Hit any key to stop autoboot: 0
Starting Kernel ...
Enter your credentials
Here is the code I am currently playing with:
Any insight would help. Or, if you know of another way to run tests through a serial port, I'm all ears.Thanks!
Thanks to the comments of #ShadowRanger I was able find a work around. I ended the readline() loop the line before the login prompt. I then logged in and ran a test blind then started reading again afterwards. Here is the code I used:
This is not the cleanest code but it explains the process well enough.

Change text file when Raspberry Pi loses power

I'm writing a small application for my Raspberry Pi to surveil my surroundings and I want to know when the Raspberry Pi loses power using NodeJs.
I've found that you can use a signal that the Pi send, SIGPWR, when it loses power.
I made this little test script:
// test.js
var fs = require('fs');
var path = '/home/pi/Documents/Code/surveillanceCam/log/logfile.txt';
fs.writeFileSync(path, new Date().toString() + ': Start\n');
process.on('exit', function () {
fs.appendFileSync(path, new Date().toString() + ': exit\n');
});
process.on('SIGPWR', function () {
fs.appendFileSync(path, new Date().toString() + ': SIGPWR\n');
});
process.stdin.resume();
If I run the script with node test.js I get a line in logfile.txt that end with Start so that works, and if I remove the last line (so the scripts don't run until I stop it), I also get a new line with exit in the end.
But if I keep the last line so the script keeps on running, and then I pull the cable of the Pi, then insert it again. When I go to watch the file after it boots I only get the line with the start in the end..
I want to have 2 lines in my logfile.txt one with the time of start, and one with the time of power loss..
And from what I've read the SIGPWR signal is sent when power is lost. Does the script not have enough time to write to the file or is there something else I can do?
EDIT: What I want to do is to simulate a power loss and when the power is lost write to the file.
EDIT2: I think how I will solve this problem is to add a process.on('SIGINT',...) signal. So when the user ends the program with Ctrl+C I will then and only then write to the file. And then when the node server start I will check if there is something in the file. If there isn't then the node server didn't shut of gracefully and I should display an error.
Unplugging the power would not be a graceful shutdown, and I doubt that you could depend on the reliability of any event for figuring out if it shut down. This is why so many things have troubles recovering when the power goes out, because things are not able to be shut down gracefully. Everything dies before the OS can probably even realize it, so nothing gets gracefully shutdown.
The standard way you detect this sort of situation is by creating flag file when the Pi boots up, and removing it on a normal shutdown.
If the flag file exists next time the Pi starts up (you check before creating it), you know that that Pi crashed for someone reason (you don't know if it was a power loss, kernel panic, or something else).
There is no way to respond directly to a power loss in your code unless you add some sort of battery backup unit that sends the appropriate signal.

How to select a particular USB port on raspberry pi 3B+ to program two arduino boards with arduino-mk?

I have a python program on my Raspberry pi 3B+ witch updates an Arduino mega2560 and an Arduino Uno by using Arduino-mk through bash commands. Each Arduino has its own code folder with its code (.ino) and a Makefile which looks like:
ARDUINO_DIR = /usr/share/arduino
ARDUINO_PORT = /dev/ttyUSB0 (here is the problem)
USER_LIB_PATH = /home/pi/sketchbook/libraries
BOARD_TAG = mega2560 (Uno for the uno one)
include /usr/share/arduino/Arduino.mk
When I call the following command:
os.system("(cd ~/sketchbook/mega; make upload clean)")
with only the mega connected on the USB port everything works and the mega gets programed (same with Uno only). but when I try to get both done:
os.system("(cd ~/sketchbook/mega; make upload clean)")
os.system("(cd ~/sketchbook/uno; make upload clean)")
arduino-mk can't program the Uno (multiple timeouts).
At first the second line in Makefile was:
ARDUINO_PORT = /dev/ttyAMC0
but it was not working so I checked on the web and found that some Arduino copies worked better with:
ARDUINO_PORT = /dev/ttyUSB0
Then it worked but I couldn't get any result with ttyUSB1 up to ttyUSB3 (I hoped it would work like COM ports on windows).
I also noticed that if I keep testing with ttyUSB0 and try with each physical USB port and worked so ttyUSB0 refers to the four physical ports right?
Please how can I get one particular port name?
Or is there an other way to make Arduino-mk work?
Plug in one device at a time and look at the output of ls -l /dev/serial/by-id/, you should see a symlink to the actual device node, but uniquely named for your specific device.
You can then set ARDUINO_PORT=/dev/serial/by-id/unique_name in the respective makefiles.

PyUSB Barcode Scanner without Button by Honeywell

I have a Honeywell N5600 that I'm trying to use, but I don't get it to scan anything.
The device is recognized as /dev/hidraw1, but as it does not have any button to press, I cannot trigger the scan and thus, when I try to read from it, it just waits forever.
I started creating a python program, that seems to be able to communicate with it (different return values):
VENDOR_LEGO = 0x0c2e
PRODUCT_EV3 = 0x0967
device = usb.core.find(idVendor=VENDOR_LEGO, idProduct=PRODUCT_EV3)
device.detach_kernel_driver(0)
#"TRGMOD8." would - when scanned (which I cannot do, at this point) - activate a permanent scanning mode
device.ctrl_transfer(0x21, 0x9, wValue=0x200, wIndex=0x00, data_or_wLength='TRGMOD8.')
#8 is returned
device.ctrl_transfer(0x21, 0x9, wValue=0x200, wIndex=0x00, data_or_wLength='PAPSPC.')
#7 is returned
Unluckily the link given in this post is not reachable anymore. It seems like it would have solved my problem.
How can I activate the scanning?
The document on Honeywell N5600 is as follows.
N5600 OEM 2D Imager Data Sheet
N56XX Decoded Engine User's Guide
You need a Windows PC that can connect to the Internet, but there is a tool called EZConfig-Scanning that configures and maintains the scanner.
EZConfig Device Management Data Sheet
Perhaps the recognition device /dev/hidraw1 is probably because the scanner is set to USB HID Bar Code Scanner mode.
If you change the setting to USB Keyboard(PC/Mac), it is always readable and the barcode you read will be notified as keyboard input.
By changing the setting to USB Serial, reading start/stop can be controlled by SerialPort command transmission. The barcode read is notified by receiving data from SerialPort.

LibUsbDotNet Barcode scanner

I have a barcode scanner. and i successfully generated the .inf for my scanner, and it install successfully. and i tried sample code, but i keep getting IoTimedOut:IoTimedOut:No more bytes! . Is it because of my configuration, interface or endpoint problem?
Thanks you
this is my scanner configuration from the LibUSBDotNet, scanner.jpg
That sample code will timeout after 5 seconds if no input was read. In other words, you did not scan a barcode within 5 seconds of starting the application.

Resources