Problems with serial communicaiton on Raspberry Pi 3 - python-3.x

I am trying to establish communication between a Raspberry Pi3 and a digital Sensor that has a RS-232 connection. I have bought a hat that also has a RS-232 port on it and can be connected to the rx and tx pins on the Pi. The Pi has raspbian installed and I am using Python 3 to write the code.
In the tutorials I read it says that i have to configure the Pi first to allow serial communication. I have done following steps:
Step 1:
sudo raspi-config -> Interfacing Options -> Serial -> [login shell accesible over serial? -> no] -> [serial port hardware enabled? -> yes]
Step 2:
sudo nano /boot/cmdline.txt
change line to:
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
Step 3:
sudo nano /boot/config.txt
add lines:
dtoverlay=pi3-miniuart-bt
enable_uart=1
force_turbo=1
Step 4:
sudo apt-get install python-serial
After this setup i wrote a very short program in Python 3 to test it:
import serial
ser = serial.Serial('/dev/ttyAMA0', 9600)
ser.write(bytes(5, 'UTF8'))
The number 5 tells the sensor to change the displayed unit. The code runs without an error message but I dont see the unit of the sensor change. If I connect the RS232 to my laptop with a serial to usb converter and use Putty to monitor the serial port, I also do not see anyting being send.
It would be great if someone can tell me where my problem is. But I would also be happy about any input on how I can find a solution myself.
Many thanks in advance,
Florian

I think Raspberry Pi 3 uses a different device for the serial port. Try
ser = serial.Serial('/dev/ttyS0', 9600)

Related

Serial Ports Linux vs Windows

I am having issues with my pyserial program on Ubuntu 16.
I am trying to send a break command to a hardware device using pyserial. I wrote a python script that:
Sets the port/baud/parity/bytesize/...
Opens the port
Sends a break command
Reads the return message from the device.
I run the script on my Ubuntu 16.04 machine and I get zero response, it just hangs or eventually timeouts. I copy the same script to my Windows machine, change the port (from /dev/ttyUSB0 to COM#) and my script works perfectly, gets a response from the device immediately.
When I run the script on Ubunutu I have to give permissions to the port (sudo chmod 666 /dev/ttyUSB), or I get permission denied errors when opening the port. Not sure if this matters.
Does anyone have any insight on what might be going on? I know Windows and Linux handle serial/com ports differently but I am a newbie to both Linux and serial so not sure if I am missing something.
I am using this USB to Serial cable (http://www.ugreen.com.cn/product-681-en.html) and I had to install some drivers. I connected the serial read/write pins and tested to make sure data is going through (which it is) so I know that works.
import serial
ser = serial.Serial()
ser.port = '/dev/ttyUSB0' # or COM12 on windows
ser.baudrate = 9600
ser.parity = serial.PARITY_NONE
ser.bytesize = serial.EIGHTBITS
ser.stopbits = serial.STOPBITS_ONE
ser.open()
ser.send_break(duration=0.9)
print(ser.read(10))
First off, you need to have proper permissions for accessing serial ports. It can be done by including your user into the group dialout:
sudo usermod -aG dialout <user>
You need to restart the system to complete it.
To avoid of some information to remain in buffers, you may need to clear read and write buffers before serial port operations:
pyserial 3.0:
ser.reset_input_buffer()
ser.reset_output_buffer()
pyserial 2.7 or earlier:
ser.flushInput()
ser.flushOutput()
Don't forget to close a port after all operations being done. Hope this helps.
First forget about your app and focus on the port troubleshooting, use putty in serial mode for sending a few terminal chars using this port. Use a jumper for conecting DB9 pin 3 to pin 2(rx and tx) an validate that you receive an echo of each character you type.

How to check serial connection between 2 Linux PCs

I need to connect 2 linux PCs via serial port. I used serial connector cable (null modem cable) to connect 2 linux PCs. But now how can I establish the connection I mean how can I test whether they are connected or not. Can anybody Please let me know the procedure.. I have tried screen command and when i run
screen /dev/ttyS0
This command is just opening new screen and i cant do anything on that new screen.
Am using Fedora-20 with kernel version 3.* in one PC and another one with Fedora-27 kernel version is 4.13
Hi finally i found solution to my problem Using this_link.
at both ends tried below steps:
1) yum install minicom
2) Then I configured minicom using minicom -s then using down arrow selected serial port setup
then pressed A from keyborad to change that serial device from
/dev/modem
to
/dev/ttyS0
press enter and chose save as dfl option then exit from minicom
After these configuration steps executed the commands suggested by #Niall Cosgrove
at one end executed cat /dev/ttyS0
at other end executed echo hello > /dev/ttyS0
finally i got hello message from one pc to other end using serial port:)

BBB - WLAN USB Adapter configuration problems

yesterday i baught a new WLAN Nano USB Adapter (LogiLink/WL0084E/Should be supported by Linux). Now i have tried to get it running with my BBB which is running on Debian Wheezy.
First i pluged the WLAN USB Adapter in and i got following Result using lsusb:
Bus 001 Device 003: ID 0bda:8179 Realtek Semiconductor Corp.
I have googled for the ID 0bda:8179 and found following Website: https://wiki.debian.org/rtl819x wich told me to install firmware-realtek by using sudo apt-get install firmware-realtek. After that i should restart by System shutdown -h now and power on again (I know a reboot will do the same).
But the WLAN Adapter was not recoginzed as wlan0 - if i type in ip a i got following result:
1: lo: ...
2: eth0: ...
3: usb0: ...
The Website mentioned above told me that i need the module r8188eu - But on lsmod i got following output:
Module Size Used by
g_multi 50407 2
libcomposite 15028 1 g_multi
omap_rng 4062 0
mt7601Usta 458758 0
So i tought mt7601Usta can may be the right one and i typed in modinfo mt7601Usta
filename: /lib/modules/3.8.13-bone79/kernel/drivers/net/wireless/mt7601Usta.ko
version: 3.0.0.3
description: RT2870 Wireless Lan Linux Driver
But may this driver doesnt Support the WL0084E (Supported by Linux!).
What should i do?
Why do i not have the wlan0 interface?
How can i fix this Problem?
If you need more information, told me commands to execute ;)
Thank you!
Download the latest 8188 driver from realteks website, extract and run install.sh
http://www.realtek.com/downloads/downloadsView.aspx?Langid=1&PFid=48&Level=5&Conn=4&ProdID=274&DownTypeID=3&GetDown=false&Downloads=true
If that doesn't work either just give up with trying to get Wheezy to support it and just hope it works automatically in a newer distro/kernel

How to setup serial communication in Processing to /dev/rfcomm0

I am trying to perform serial communication on between Ubuntu 12.04 and a JY-MCU bluetooth serial module connected to an Arduino.
I have created this configuration in /etc/bluetooth/rfcomm.conf
rfcomm0 {
# # Automatically bind the device at startup
bind yes;
#
# # Bluetooth address of the device
device 00:12:11:19:08:54
# # RFCOMM channel for the connection
channel 1;
# # Description of the connection
comment "Linvor Bluetooth Module";
}
I can use putty to communicate with the /dev/rfcomm0 serial port and this works perfectly.
However, despite many attempts I simply cannot see how to create a serial port in Processing that works in any way.
For example :
println(Serial.list());
prints nothing at all.
If I execute:
String portName = "/dev/rfcomm0";
myPort = new Serial(this, portName, 9600);
println(myPort);
I see this in the monitor:
processing.serial.Serial#1712651
But if I then call:
myPort.write('9');
I get an exception:
java.lang.NullPointerException
at processing.serial.Serial.write(Serial.java:572)
...
I can't understand why this fails. I have been following all the instructions from Tom Igoe's "Making Things Talk", but this just does not work the way he says...
Any help would b great!
Thanks,
Bob
Aftert searching high and low, I have made this work.
The key issue is that processing uses the rxtx java library (RXTX-2.1-7) for serial communications.
The RXTX wiki says:
"rxtx tries to detect ports on by scanning /dev for files matching any
of a set of known-good prefixes, such as 'ttyS', 'ttym', and since 2.2
'ttyUSB' and so on. "
And since the bluetooth device is named rfcomm* it cannot be detected.
The trick is to create a sym link to fool rxtx (use a ttyS device that is not yet assigned):
$ sudo ln -s /dev/rfcomm0 /dev/ttyS99
Then, connect:
$ sudo rfcomm connect 0
Connected /dev/rfcomm0 to 00:12:11:19:08:54 on channel 1
Press CTRL-C for hangup
At this point the red led on the JY-MCU becomes solid and processing can detect it:
println(Serial.list());
output is:
[0] "/dev/ttyACM0"
[1] "/dev/ttyS99"
So, serial communication can work.
To summarize, the following process will allow a processing script to communicate via a serial port with a JY-MCU device in a BlueZ linux framework
One time setup:
power up the JY-MCU,
use the following command to get its hardware address, mine is 00:12:11:19:08:54
$ hcitool scan
use that to create the /etc/bluetooth/rfcomm.conf file; you'll note that I chose 0 for the rfcomm device , we need that for connection later:
$ cat /etc/bluetooth/rfcomm.conf
rfcomm0 {
bind yes;
device 00:12:11:19:08:54;
channel 1;
comment "Linvor Bluetooth Module";
}
use BlueMan to pair the JY-MCU.
Every time you want to use the JY-MCU
create the sym link:
$ sudo ln -s /dev/rfcomm0 /dev/ttyS99
connect to the JY-MCU:
$ sudo rfcomm connect 0
Connected /dev/rfcomm0 to 00:12:11:19:08:54 on channel 1
Press CTRL-C for hangup
you can now run a processing script and connect to the JY-MCU with
the code:
String portName = "/dev/ttyS99";
myPort = new Serial(this, portName, 9600);
after running the processing script, be sure to CTRL-C at the
command line to disconnect the JY-MCU.
That should do it!
Ciao,
Bob
Just something popped up in my mind.
I had similar problems that were caused due to channel 1 is already used. If you bind to a channel which already is in use, bad things may happen.
sdptool browse local
Use that command to see which channels are available on your Ubuntu device.

A2DP sink without pulseaudio

So I'm trying to make my linux server play music sent from my Android phone using bluetooth (the linux machine is the A2DP sink and the phone is the source).
What I have done so far is to:
install bluez and enable audiosource/audiosink
pair phone and server
connect to server from phone (phone says it's streaming audio over bluetooth)
But I can't hear anything. Also, most guides on the internet assumes Pulseaudio and I would prefer to use ALSA.
I currently have the following in /etc/asound.conf:
pcm.!default{
type bluetooth
profile "auto"
}
I'm running Bluez v4.99 and Alsa v1.0.25.
Any ideas?
I know this is an old post, but hopefully the answer is useful to people currently working on this.
You can use /etc/bluetooth/audio.conf, which is the system-wide file, or ~/.asoundrc, which is your local file. Both are read by BlueZ/ALSA. However, I think you need to include the MAC address in your config file, z.B.:
pcm.btheadset {
type bluetooth
device "XX:XX:XX:XX:XX:XX" #MAC address
profile "auto"
}
The best resources I've found for this are:
1) James B's blog post: Bluez must be one of the best kept secrets in Linux
He explains the structure and interface between BlueZ and ALSA, which I found nowhere else on the internet.
2) His second post with code: Bluez A2DP AudioSink for ALSA
3) The ALSA site, which introduces the structure of pcm plugins, but doesn't really explain them very well.
4) Some ALSA plugin tutorials: The ALSA wiki
Some useful commands:
$sudo service bluetooth restart
$sudo alsa force-reload
Run these after you change the asoundrc or audio.conf files.
Here you find a manual to configure bluetooth with ALSA or Pulseaudio:
-tested on Linux Mint 17.3 Mate, 64bit / Ubuntu 14.04 Mate, 64bit-
HOW TO MAKE ALSA AND BLUETOOTH WORK TOGETHER WITHOUT PA
This part is for pure ALSA-based systems without Pulseaudio like KXStudio, QStudio64,Tango studio..!
Be sure that Pulseaudio is deinstalled and your soundcards
configured right in ALSA!
delete pulse-audio cookies and files in
/home/USERNAME/.config/pulse
Modify your bluetooth-audio.conf:
type:
gksudo pluma /etc/bluetooth/audio.conf
Set
# This section contains options which are not specific to any
# particular interface
[General]
Enable=Socket
Save the file.
setup ~/.asoundrc file
type:
sudo hcitool scan
Scanning ...
XX:XX:XX:XX:XX:XX
Stereo Headset
Create a hidden-file named .asoundrc in your home-folder!and write:
pcm.!default {
type plug
slave.pcm {
type bluetooth
device "XX:XX:XX:XX:XX:XX"
profile "auto"
}
}
Replace “XX:XX..” with the adress of your device.
Save the file!
Run these after you change the asoundrc or audio.conf files:
sudo service bluetooth restart
sudo alsa force-reload
or reboot your computer!
———————————————————————————————————–—-
Go to your blueman-applet at your taskbar, search & setup your new device!
Note: Some bluetooth-devices need a passphrase (eg. 0000) by default
while some others takes shuffle-pairing.
Keep your found and paired device to “trust”!
Connect your device as AUDIO via A2DP!
————————————————————————————————————
If your device is connected with your system you can play sound with totem (gui), vlc (gui+terminal), mplayer (terminal), qmmp (gui), banshee and browsers over bluetooth while setting the output in players to “default”!
—————————————————————————————————–———
RENAME THE .ASOUNDRC IN HOME-FOLDER TO “.ASOUNDRC_OFF”
TO GET YOUR OLD SYSTEM-SETTINGS WITHOUT BLUETOOTH BACK!
type: mv /home/USERNAME/.asoundrc /home/USERNAME/.asoundrcOFF
->to disable the bluetooth-specific asoundrc
type: mv /home/USERNAME/.asoundrcOFF /home/USERNAME/.asoundrc
-> to enable it!
Alternative:
Make two “scripts” to activate/deactivate bluetooth with .asoundrc in your home-folder:
Create one empty file and write/paste:
#!/bin/bash
mv /home/USERNAME/.asoundrc /home/USERNAME/.asoundrcOFF
or:
#!/bin/bash
cd /home/USERNAME/
mv .asoundrc .asoundrcOFF
To activate your .asoundrc for bluetooth again, create another file with following entry:
#!/bin/bash
mv /home/USERNAME/.asoundrcOFF /home/USERNAME/.asoundrc
or:
#!/bin/bash
cd /home/USERNAME/
mv .asoundrcOFF .asoundrc
Make the files executable and run them with “open with terminal”!
Now you have two “buttons” to switch bluetooth-sound On or Off.
Give them individual-icons:
;-)
————————————————————————————————————-
PLAYERS
————–
It’s recommend to use totem, Qmmp or Banshee-player - because there is nothing more to do than
play and listen! Also totem-player shows videos too.
VLC-player needs the “default” sounddriver for bluetooth to working right!
Check in: /home/USERNAME/.config/vlc/vlcrc
that alsa-audio-device
in >>line1666<<
is:
# ALSA Device Name (string)
alsa-audio-device=default
Use this commands to use players with terminal:
To play music, type:
MPlayer
———––
mplayer /home/USERNAME/Musik/1.mp3
If you hear no sound or get errors try:
mplayer -ao alsa:device=default /home/USERNAME/Musik/1.mp3
VLC-Player
—————-
cvlc /home/USERNAME/Musik/1.mp3
vlc /home/USERNAME/Musik/1.mp3
rvlc /home/USERNAME/Musik/1.mp3
To add whole directories type:
rvlc add /home/USERNAME/Musik/
type: start/stop/next... to navigate!
or use the vlc-gui.
—————————————————————————————————–——-
To use mplayer with graphical-interface you have to install the gui-package:
type:
sudo apt-get install mplayer-gui
Open a terminal via Ctrl-Alt-T and use the commands below.Use the lines and type/copy one by one to avoid confusion(!):
cd /usr/share/mplayer/skins/default
for FILE in *.png; do sudo convert “$FILE” -define png:format=png24 “$FILE”; done
cd /usr/share/mplayer/skins; sudo rm default; sudo ln -s Blue default
———————————————————————————————————–
Now you can start the mplayer-gui without errors!
If you prefer the terminal type:
gmplayer /home/USERNAME/Musik/1.mp3
or
gmplayer -ao alsa:device=default /home/USERNAME/Musik/1.mp3
if you have problems!
terminal command without gui:
mplayer /DIRECTION/TO/FILE
Available Output-Modules and Drivers:
mplayer -vo help
———————————————————————-
BLUETOOTH WITH PULSEAUDIO
————————————————–--------
For Linux systems that comes with pulseaudio:
(1.) Edit /etc/bluetooth/audio.conf and uncomment the following line:
AutoConnect=true
Run the following command to install latest packages for blueman and related modules:
sudo add-apt-repository ppa:blueman/ppa
sudo apt-get update
sudo apt-get upgrade blueman
enter code here
(3.) Check if the following lines are added in /etc/pulse/default.pa:
.ifexists module-bluetooth-discover.so
load-module module-bluetooth-discover
.endif
If not, add the lines.
REBOOT!
Run the new blueman-applet.
Right click on the panel applet and turn bluetooth on.
The old profie of you bluetooth device would still be there, remove it.
Right click on the blueman-panel applet and select Setup New Device.
Complete the wizard for your device.(If you have problems with “pairing” restart your system and skip this step!)
Now you get a connected bluetooth device!
To send/hear audio over it just follow these 2 steps:
Open Mate-volume control-settings (or pavucontrol) with rightclick in taskbar,
browse to “HARDWARE” -
and use the dropdown to turn the Built-in audio profiles of all other soundcards “off”
so that the bluetooth-device gets active!
use the other dropdown to set the bluetooth device to use A2DP
Now start the totem-player or vlc (with pulseaudio-output) to send audio to bluetooth!
Enjoy your sound!
;-)
WLAN-ISSUES:
——————–—
In case you see a WiFi connection drop after connecting to the bluetooth device run the following
command:
$ echo “options iwlwifi 11n_disable=1 bt_coex_active=0 power_save=0 auto_agg=0 swcrypto=1” |
sudo tee -a /etc/modprobe.d/iwlwifi.conf > /dev/null
tested 2017 for LM 17.3 (Rosa) MATE and Ubuntu 14.04 by chalee:
http://mayastudio.tumblr.com/bluetooth
My guess is that your audio path isn't routing the bluetooth to your speakers. I have seen similar issue, which was fixed by manually doing step 5 "Connect PulseAudio bluetooth source to PulseAudio ALSA sink" from this link: http://jprvita.wordpress.com/2009/12/15/1-2-3-4-a2dp-stream/
I just tried to do the same thing, I'm using Fedora 19, so your mileage my vary. These are the steps I took.
Made computer discoverable
Pair phone to computer (it had a headphone logo next to it)
On my HTC phone I clicked "connect" on the pairing
Go into gnome3's sound settings
Go to the input tab
Select the bluetooth item for your phone
Maybe adjust volume...

Resources