hardbyte / hardbyte/python-can

SocketCan eventually ran out of memory

Ouverte
#1,567 3 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
bug
Langage dominant
Python
Étoiles
1.6k
Forks
697
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

### Describe the bug

When running socketcan to send message for more than 48 hours, it eventually would crash with "Cannot allocate memory" error message

### To Reproduce

```
from signal import signal, SIGINT
from sys import exit
import can
import serial
import time
import os
import subprocess
os.system('/usr/local/bin/startSMAGsUsbCan.sh can5')
#
# when board configured as socketcan, bring up link first:
# sudo ip link set can0 up type can bitrate 500000
#
#

def getSignedNumber(number, bitLength):
mask = (2 ** bitLength) - 1
if number & (1 << (bitLength - 1)):
return number | ~mask
else:
return number & mask

def getbytes(integer):
return divmod(integer, 0x100)

def handler(signal_received, frame):
# Handle any cleanup here
print('SIGINT or CTRL-C detected. Exiting gracefully')
exit(0)

if __name__ == "__main__":
# Tell Python to run the handler() function when SIGINT is recieved
signal(SIGINT, handler)

print('Running. Press CTRL-C to exit.')

print ("Can bus init")
canBusTTY = "/dev/ttyACM0"
canBus = can.interface.Bus(bustype='socketcan', channel="can5", bitrate=500000, txqueuelen=10000)
canBus.shutdown()
print ("Can bus init done")

while True:
os.system('/usr/local/bin/startSMAGsUsbCan.sh can5')
canBus = can.interface.Bus(bustype='socketcan', channel="can5", bitrate=500000, txqueuelen=10000)
time.sleep(2)
# Do nothing and hog CPU forever until SIGINT received.

SoC_HD = 99.5
Soc = int(SoC_HD)
Req_Charge_HD = 100
Req_Charge_A = 100.0
Req_Discharge_HD = 30
Req_Discharge_A = 200.0
Max_V_HD = 56
Max_V = 60.0
Min_V = 46.0

#breakup some of the values for CAN packing
SoC_HD = int(SoC_HD*100)
SoC_HD_H, SoC_HD_L = getbytes(SoC_HD)
Req_Charge_HD = int(Req_Charge_A*10)
Req_Charge_H, Req_Charge_L = getbytes(Req_Charge_HD)
Req_Discharge_HD = int(Req_Discharge_A*10)
Req_Discharge_H, Req_Discharge_L = getbytes(Req_Discharge_HD)
Max_V_HD = int(Max_V*10)
Max_V_H, Max_V_L = getbytes(Max_V_HD)
Min_V_HD = int(Min_V*10)
Min_V_H, Min_V_L = getbytes(Min_V_HD)

msg = can.Message(arbitration_id=0x351,
data=[Max_V_L, Max_V_H, Req_Charge_L, Req_Charge_H, Req_Discharge_L, Req_Discharge_H, Min_V_L, Min_V_H],
is_extended_id=False)
msg2 = can.Message(arbitration_id=0x355,
data=[Soc, 0x00, 0x64, 0x0, SoC_HD_L, SoC_HD_H],
is_extended_id=False)
msg3 = can.Message(arbitration_id=0x356,
data=[0x00, 0x00, 0x00, 0x0, 0xf0, 0x00],
is_extended_id=False)
msg4 = can.Message(arbitration_id=0x35a,
data=[0x00, 0x00, 0x00, 0x0, 0x00, 0x00, 0x00, 0x00],
is_extended_id=False)
msg5 = can.Message(arbitration_id=0x35e,
data=[0x42, 0x41, 0x54, 0x52, 0x49, 0x55, 0x4d, 0x20],
is_extended_id=False)
msg6 = can.Message(arbitration_id=0x35f,
data=[0x03, 0x04, 0x0a, 0x04, 0x76, 0x02, 0x00, 0x00],
is_extended_id=False)

try:
canBus.send(msg)
time.sleep(.100)

canBus.send(msg2)
time.sleep(.100)

canBus.send(msg3)
time.sleep(.100)

canBus.send(msg4)
time.sleep(.100)

canBus.send(msg5)
time.sleep(.100)

canBus.send(msg6)
time.sleep(.100)

print("Sent 6 frames on {}".format(canBus.channel_info))
canBus.shutdown()

except (can.CanError) as e:
print("CAN BUS Transmit error (is controller missing?): %s" % e.message)

pass
```
the shell script: /usr/local/bin/startSMAGsUsbCan.sh
```
#!/bin/sh

# called from udev ADD action to bring up the related link
#
#
# $1 is the device (e.g., can10)
#
# these actions are only required prior to v2.90~18

if [ ! -f "/etc/venus/newUdevRules" ] ; then
# bring the link up
/sbin/ip link set $1 down
/sbin/ip link set $1 up type can bitrate 500000
/sbin/ip link set $1 txqueuelen 1000

# restart services so they see this new interface
svc -t /service/can-bus-bms.$1
svc -t /service/dbus-systemcalc-py
svc -t /service/dbus-adc
svc -t /service/dbus-modbus-client
svc -t /service/netmon
fi
```
### Expected behavior

I expect the loop to keep running and sending messages

### Additional context

OS and version: Venus OS v2.93
Python version: python3.8.13
python-can version: 4.1.0
python-can interface/s (if applicable):

Traceback and logs

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.