Transmit buffer full - python-can

I am executing a python and can code to make a data log but when I run it I get the error Transmit buffer full
This is the code
#!/usr/bin/python3
#
## obdii_logger.py
#
# This python3 program sends out OBDII request then logs the reply to the sd card.
# For use with PiCAN boards on the Raspberry Pi
# http://skpang.co.uk/catalog/pican2-canbus-board-for-raspberry-pi-2-p-1475.html
#
# Make sure Python-CAN is installed first http://skpang.co.uk/blog/archives/1220
#
# 24-08-16 SK Pang
#
import RPi.GPIO as GPIO
import can
import time
import os
import queue
from threading import Thread
led = 22
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(led,GPIO.OUT)
GPIO.output(led,True)
# For a list of PIDs visit https://en.wikipedia.org/wiki/OBD-II_PIDs
ENGINE_COOLANT_TEMP = 0x05
ENGINE_RPM = 0x0C
VEHICLE_SPEED = 0x0D
MAF_SENSOR = 0x10
O2_VOLTAGE = 0x14
THROTTLE = 0x11
PID_REQUEST = 0x7DF
PID_REPLY = 0x7E8
outfile = open('log.txt','w')
print('\n\rCAN Rx test')
print('Bring up CAN0....')
# Bring up can0 interface at 500kbps
os.system("sudo /sbin/ip link set can0 up type can bitrate 500000")
time.sleep(0.1)
print('Ready')
try:
bus = can.interface.Bus(channel='can0', bustype='socketcan')
except OSError:
print('Cannot find PiCAN board.')
GPIO.output(led,False)
exit()
def can_rx_task(): # Receive thread
while True:
message = bus.recv()
if message.arbitration_id == PID_REPLY:
q.put(message) # Put message into queue
def can_tx_task(): # Transmit thread
while True:
GPIO.output(led,True)
# Sent a Engine coolant temperature request
msg = can.Message(arbitration_id=PID_REQUEST,data=[0x02,0x01,ENGINE_COOLANT_TEMP,0x00,0x00,0x00,0x00,0x00], is_extended_id=False)
bus.send(msg)
time.sleep(0.05)
# Sent a Engine RPM request
msg = can.Message(arbitration_id=PID_REQUEST,data=[0x02,0x01,ENGINE_RPM,0x00,0x00,0x00,0x00,0x00], is_extended_id=False)
bus.send(msg)
time.sleep(0.05)
# Sent a Vehicle speed request
msg = can.Message(arbitration_id=PID_REQUEST,data=[0x02,0x01,VEHICLE_SPEED,0x00,0x00,0x00,0x00,0x00],is_extended_id=False)
bus.send(msg)
time.sleep(0.05)
# Sent a Throttle position request
msg = can.Message(arbitration_id=PID_REQUEST,data=[0x02,0x01,THROTTLE,0x00,0x00,0x00,0x00,0x00],is_extended_id=False)
bus.send(msg)
time.sleep(0.05)
GPIO.output(led,False)
time.sleep(0.1)
q = queue.Queue()
rx = Thread(target = can_rx_task)
rx.start()
tx = Thread(target = can_tx_task)
tx.start()
temperature = 0
rpm = 0
speed = 0
throttle = 0
c = ''
count = 0
# Main loop
try:
while True:
for i in range(4):
while(q.empty() == True): # Wait until there is a message
pass
message = q.get()
c = '{0:f},{1:d},'.format(message.timestamp,count)
if message.arbitration_id == PID_REPLY and message.data[2] == ENGINE_COOLANT_TEMP:
temperature = message.data[3] - 40 #Convert data into temperature in degree C
if message.arbitration_id == PID_REPLY and message.data[2] == ENGINE_RPM:
rpm = round(((message.data[3]*256) + message.data[4])/4) # Convert data to RPM
if message.arbitration_id == PID_REPLY and message.data[2] == VEHICLE_SPEED:
speed = message.data[3] # Convert data to km
if message.arbitration_id == PID_REPLY and message.data[2] == THROTTLE:
throttle = round((message.data[3]*100)/255) # Conver data to %
c += '{0:d},{1:d},{2:d},{3:d}'.format(temperature,rpm,speed,throttle)
print('\r {} '.format(c))
print(c,file = outfile) # Save data to file
count += 1
except KeyboardInterrupt:
#Catch keyboard interrupt
GPIO.output(led,False)
outfile.close() # Close logger file
os.system("sudo /sbin/ip link set can0 down")
print('\n\rKeyboard interrtupt')
I tried the command sudo ifconfig can0 txqueuelen 1000 but I still keep getting the same error Transmit buffer full

Related

PJSUA2 linux segmentation fault

I using PJSUA2 to make a python3 script for calling, my script is making the registration and calling but i'm getting "zsh: segmentation fault" I wonder if anyone can help me out, here is my code.
python
import time
import pjsua2 as pj
distAddrs = "192.168.1.202"
port = 5060
ext = "101"
passwd = "123456"
# Create my endpoint
endPoint = pj.Endpoint()
endPointConfig = pj.EpConfig()
endPoint.libCreate()
endPoint.libInit(endPointConfig)
# Create SIP transport
sipTransportConfig = pj.TransportConfig()
sipTransportConfig.port = port
endPoint.transportCreate(pj.PJSIP_TRANSPORT_UDP, sipTransportConfig)
# Setting my audio device
endPoint.audDevManager().setNullDev()
for codec in endPoint.codecEnum2():
priority = 0
if "PCMA/8000" in codec.codecId:
priority = 255
endPoint.codecSetPriority(codec.codecId, priority)
# Start the library
endPoint.libStart()
# Subclass to extend the Call and get notifications etc.
class Call(pj.Call):
def onRegState(self, prm):
print("***onRegState == " + prm.reason)
# pjsua2 registration and calling function
def pbxReg(distAddrs, ext, passwd):
accountConfig = pj.AccountConfig()
accountConfig.idUri = "sip:%s#%s" % (ext,distAddrs,)
accountConfig.regConfig.registrarUri = "sip:%s" % (distAddrs,)
credentials = pj.AuthCredInfo("digest","*",ext,0,passwd,)
accountConfig.sipConfig.authCreds.append(credentials)
# Creating the account
account = pj.Account()
account.create(accountConfig)
time.sleep(1)
if account.getInfo().regIsActive == True:
call = Call(account)
prm = pj.CallOpParam(True)
prm.opt.audioCount = 1
prm.opt.videoCount = 0
call.makeCall("sip:103#192.168.1.202", prm)
time.sleep(1)
prm = pj.CallOpParam()
call.hangup(prm)
endPoint.hangupAllCalls()
# Destroy the library
def libDestroy():
endPoint.libDestroy()
def main():
pbxReg(distAddrs, ext, passwd)
libDestroy()
main()
The first obvious thing I can see is that you are using time.sleep(1) instead of endPoint.libHandleEvents(1000) which was an issue for me.
# your code
time.sleep(1)
# possible fix
endPoint.libHandleEvents(1000)

pyaudio callback called only once

I try use pyaudio with the callback option, and I want to yield the data instead of reading from a file. When I use the callback option, it gets called only once.
There is another question with the same problem, but it doesn't have an answer. I have made a minimal reproducable example. The code works when blocking is used.
import time
import numpy as np
import scipy.signal
import sounddevice as sd
import pyaudio
sample_rate=44100
max_amp = 2**(15)-1
f0 = 500
duration = 1
f1 = 3000
x = np.arange(0, duration, 1/sample_rate)
y_float = max_amp*scipy.signal.chirp(x, f0, duration, f1)
y = y_float.astype(np.int16)
data = y.tostring()
def create_data_generator(data):
periodsize = 1000
for i in range(int(len(data)/(periodsize))):
chunk = data[periodsize*i:periodsize*(i+1)]
yield chunk
data_generator = create_data_generator(data)
def callback(in_data, frame_count, time_info, status):
data = next(data_generator)
return (data, pyaudio.paContinue)
# -------- blocking------------------
# periodsize = 1000
# p = pyaudio.PyAudio()
# stream = p.open(format=pyaudio.paInt16,
# channels=1,
# rate=sample_rate,
# output=True)
# start = time.time()
# for i in range(int(len(data)/(periodsize))):
# chunk, status = callback(0, 0, 0, 0)
# stream.write(chunk)
# time.sleep(duration-(time.time()-start))
# stream.stop_stream()
# stream.close()
# p.terminate()
# -------- callback ------------------
periodsize = 1000
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=sample_rate,
output=True,
stream_callback=callback)
# start the stream (4)
stream.start_stream()
# wait for stream to finish (5)
while stream.is_active():
time.sleep(0.1)
# stop stream (6)
stream.stop_stream()
stream.close()
# close PyAudio (7)
p.terminate()

Receive message on CANbus always returns same value with python-can

I have a CAN bus (PCAN) with several inputs. I try to read the inputs in python and print them on the console.
The first message I get from the bus is correct, however if I change the state on the input, the data in the message doesn't change and keeps spitting the first data it got.
In PCAN-view I can see the data change, so it isn't a hardware failure.
My code:
import can
from time import sleep
def main():
bus = can.interface.Bus(bustype='pcan', channel='PCAN_USBBUS1', bitrate=500000)
try:
while True:
# msg = can.Message(arbitration_id=0x232, data=[0x00], is_extended_id=False)
# try:
# bus.send(msg)
# print("message sent on {}".format(bus.channel_info))
# except can.CanError:
# print("message not sent!")
msg = bus.recv(None)
try:
if msg.arbitration_id == 0x1B2:
print(msg)
if msg.arbitration_id == 0x1B3:
print(msg)
if msg.arbitration_id == 0x1B4:
print(msg)
if msg.arbitration_id == 0x1B5:
print(msg)
except AttributeError:
print("Nothing received this time")
sleep(0.2)
except KeyboardInterrupt:
print("Program Exited")
except can.CanError:
print("Message NOT sent")
bus.shutdown()
if __name__ == '__main__':
main()
I tried sending a message before each receive but it didn't help.
Same with calling the can.Listener().stop() function on a listener after printing the received message.
PCAN-view shows the hardware is working as I can see the changes happening in the screen.
Eg.
at the beginning, input 0 and 1 are high on unit 0x1B2
bus.recv returns [00 03], which is correct
I clear input 0 and 1
PCAN-view shows data [00 00]
bus.recv returns [00 03], which is incorrect. It didn't change.
I've read the readthedocs several times but I'm afraid I missed something. Do I need to flush the inputbuffer? I only saw info on a function to flush the output buffer.
So, I decided to implement it differently with a buffer and notifier. I also disabled the auto-update feature of the units so they don't spam the bus.
I came up with this class to use the bus.
import can
import logging
def _get_message(msg):
return msg
class PCANBus(object):
RX_SDO = 0x600
TX_SDO = 0x580
RX_PDO = 0x200
TX_PDO = 0x180
id_unit_a = [120, 121, 122, 123]
id_unit_b = [124, 125, 126, 127]
def __init__(self):
logging.info("Initializing CANbus")
self.bus = can.Bus(channel="PCAN_USBBUS1", bustype="pcan")
self.buffer = can.BufferedReader()
self.notifier = can.Notifier(self.bus, [_get_message, self.buffer])
def send_message(self, message):
try:
self.bus.send(message)
return True
except can.CanError:
logging.error("message not sent!")
return False
def read_input(self, id):
msg = can.Message(arbitration_id=self.RX_PDO + id,
data=[0x00],
is_extended_id=False)
self.send_message(msg)
return self.buffer.get_message()
def flush_buffer(self):
msg = self.buffer.get_message()
while (msg is not None):
msg = self.buffer.get_message()
def cleanup(self):
self.notifier.stop()
self.bus.shutdown()
def disable_update(self):
for i in [50, 51, 52, 53]:
msg = can.Message(arbitration_id=0x600 + i,
data=[0x23, 0xEA, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00],
is_extended_id=False)
self.send_message(msg)
And can be used as:
pcan = PCANBus()
msg = Message(arbitration_id=pcan.RX_PDO + 50, is_extended_id=False, data=[0x4F, 0x00])
pcan.send_message(msg)
ret = pcan.read_input(0x78)
pcan.cleanup()
'ret' holds the return message

read MFRD522 data using raspberry pi and send this data by http(python3)

I'm working on a project and I want to use RFID as a position reference(when raspberry pi and MFRC522 read the data, send it to server and make sure where is the position or adjust position while RFID reader is fixed)
My code is below:
import RPi.GPIO as GPIO
import MFRC522
import signal
import socket
HOST = '192.168.11.48'
PORT = 9009
continue_reading = True
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
global continue_reading
print("Ctrl+C captured, ending read.")
continue_reading = False
GPIO.cleanup()
# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)
# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()
# Welcome message
print("Welcome to the MFRC522 data read example")
# This loop keeps checking for chips. If one is near it will get the UID and authenticate
with socket.socket(socket.AF_INET,socket.SOCK_STREAM) as sock:
sock.connect((HOST,PORT))
#print("closing client")
while True:
while continue_reading:
# Scan for cards
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
# If a card is found
if status == MIFAREReader.MI_OK:
print("Card detected")
# Get the UID of the card
(status,uid) = MIFAREReader.MFRC522_Anticoll()
# If we have the UID, continue
if status == MIFAREReader.MI_OK:
# Print UID
print("Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
# This is the default key for authentication
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
# Select the scanned tag
MIFAREReader.MFRC522_SelectTag(uid)
# Authenticate
status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
data = "str(uid[0])"
msg = data
sock.sendall(msg.encode())
data = sock.recv(128)
print('date from Echo server is [%s]'%data.decode())
# Check if authenticated
if status == MIFAREReader.MI_OK:
MIFAREReader.MFRC522_Read(8)
MIFAREReader.MFRC522_StopCrypto1()
else:
print("Authentication error")
I am using echo server to make sure that it's working
code for echo server is:
import socketserver
HOST = ''
PORT = 9009
class MyTcpHandler(socketserver.BaseRequestHandler):
def handle(self):
print("[%s]conneted"%self.client_address[0])
try:
while True:
self.data = self.request.recv(1024)
if self.data.decode() == "/quit":
print('ended by user [%s]'%self.client_address[0])
return
print('[%s]'%self.data.decode())
self.request.sendall(self.data)
except Exceptions as e:
print(e)
def runServer():
print("starting echo server")
print("if you want to close echo server, click Ctrl+C")
try:
server = socketserver.TCPServer((HOST,PORT), MyTcpHandler)
server.serve_forever()
except KeyboardInterrupt:
print("--- closing server")
runServer()
I ran fist piece of code while the second one was running, but only result I got was -
Welcome to the MFRC522 data read example
Card detected
Card read UID:178,29,209,48
size:8.
Anyone had idea what I should change to make it work?
thanks

can't open device: Too many open files Error

Hi I Have Terrible codes in Raspberry Pi 3. I am trying to read 4 RC522 Module and i can do it. But after few minutes I got "can't open device: Too many open files" Error, and my while loop terminated.
I didnt share all the functions. I guess important part is while loop. I create instance in every loop. I guess this is big mistake. I cant add delay. I tried assign null to my object but i got still same error. My Codes are Below
Note: I have 4 SPI address, and i create instance with them.
#!/usr/bin/env python
# -*- coding: utf8 -*-
import time
import sys
import os
import RPi.GPIO as GPIO
import MFRC522
import signal
from time import gmtime, strftime
from time import sleep
import requests
#import xml.etree.ElementTree as ET
#import xmltodict, json
from lxml import objectify
from bs4 import BeautifulSoup
continue_reading = True
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
GPIO.setup(37, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
global continue_reading
print "Ctrl+C captured, ending read."
continue_reading = False
GPIO.cleanup()
kullanici = "xxxx"
sifre = "xxxx"
birim = "xxxx"
ogrKEY = " "
def end_read(signal,frame):
global continue_reading
print "Ctrl+C captured, ending read."
continue_reading = False
GPIO.cleanup()
# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)
# Create an object of the class MFRC522
#
adresler = ["/dev/spidev0.0", "/dev/spidev0.1", "/dev/spidev1.0","/dev/spidev1.1"]
# Welcome message
print "Welcome to the MFRC522 data read example"
print "Press Ctrl-C to stop."
j = 0
# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
MIFAREReader = None
pin = 0
if j == 100:
j = 0
i = j % 4
if i == 0:
MIFAREReader = MFRC522.MFRC522(adresler[0], 16)
pin = 15
elif i == 1:
MIFAREReader = MFRC522.MFRC522(adresler[1], 18)
pin = 13
elif i == 2:
MIFAREReader = MFRC522.MFRC522(adresler[2], 33)
pin = 7
else:
MIFAREReader = MFRC522.MFRC522(adresler[3], 31)
pin = 37
# Scan for cards
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
# If a card is found
if status == MIFAREReader.MI_OK:
print "Card detected"
# Get the UID of the card
(status,uid) = MIFAREReader.MFRC522_Anticoll()
# If we have the UID, continue
if status == MIFAREReader.MI_OK:
# print "su okuyucudan okundu" % i
# Print UID
print "Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3])
# This is the default key for authentication
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
# Select the scanned tag
MIFAREReader.MFRC522_SelectTag(uid)
# Authenticate
status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 10, key, uid)
# Check if authenticated
if status == MIFAREReader.MI_OK:
MIFAREReader.MFRC522_Read(10)
ogrKEY = MIFAREReader.returnOGR()
sonuc = parser(ogrKEY)
if sonuc == True:
openTurnsTile(pin)
else:
sonuc = personelKontrol(ogrKey)
if sonuc == True:
openTurnsTile(pin)
MIFAREReader.MFRC522_StopCrypto1()
else:
print "Authentication error"
j = j + 1
When you create the MFRC522 object it actually calls spi.openSPI() but never closes it. This python library is good for single instance but now that you are dealing with multiple instance where FDs are opened, needs to be closed as well using spi.closeSPI(), else you will get the error "Too many open FDs".
To check how many open FDs are there for your PID.

Resources