AlexKlimaj / LiPow-Firmware

Lipo Battery Charger with USB C Power Delivery Based on the STM32G0
https://arkelectron.com/product/lipow-the-usb-c-lipo-battery-charger/
GNU General Public License v3.0
321 stars 97 forks source link

Trouble issuing commands via UART. #30

Closed blank-supportgis closed 1 year ago

blank-supportgis commented 1 year ago

I'm connecting the LiPow charger via UART to a Raspberry Pi 3. When opening a serial console with cu -l /dev/ttyS0 -s 921600, I can run commands without any problem.

pi@dronebox:~ $ cu -l /dev/ttyS0 -s 921600
Connected.
>help

help:
 Lists all the registered commands

stats:
 Displays a table showing the system stats

cal:
 Calibrates the ADC based on a known input voltage. Expects one argument as a float in milivolts. Connect input voltage to cells 1-4 and the XT60 battery output.

write_otp:
 Writes the calibration scalars to OTP flash. Will fail if scalars not set or out of range. Must run cal first with known accurate voltage. Can run up to ~32 times.

task-stats:
 Displays a table showing the state of each FreeRTOS task

run-time-stats:
 Displays a table showing how much processing time each FreeRTOS task has used

[Press ENTER to execute the previous command again]
>stats
Variable                    Value
************************************************
Battery Voltage MCU(V)       0.000
Battery Voltage Reg (V)      2.880
Charging Current (A)         0.000
Charging Power (W)           0.000
Cell One Voltage (V)         0.000
Cell Two Voltage (V)         0.000
Cell Three Voltage (V)       0.000
Cell Four Voltage (V)        0.000
2 Series Voltage (V)         0.000
3 Series Voltage (V)         0.000
4 Series Voltage (V)         0.000
MCU Temperature (C)          29
VDDa (V)                     3.291
XT60 Connected               0
Balance Connection State     0
Number of Cells              0
Battery Requires Charging    0
Balancing State/Bitmask      0
Regulator Connection State   1
Charging State               0
Max Charge Current           0.000
Vbus Voltage (V)             5.056
Input Current (A)            0.000
Input Power (W)              0.000
Efficiency (OutputW/InputW)  nan
Battery Error State          0

[Press ENTER to execute the previous command again]
>

However, the following Python script, using the pySerial package

import serial
from time import sleep

ser = serial.Serial("/dev/ttyS0", baudrate=921600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=8, timeout=1)
print("sending...")
ser.write('help\n'.encode())
print('receiving...')
received_data = ser.read()
sleep(0.03)
data_left = ser.inWaiting() 
received_data += ser.read(data_left)
print (received_data.decode()) 

, yields the following output:

sending...
receiving...
e
Command not recognized.  Enter 'help' to view a list of available commands.

[Press ENTER to execute the previous command again]
>

Command not recognized.  Enter 'help' to view a list of available commands.

[Press ENTER to execute the previous command again]
>

Command not recognized.  Enter 'help' to view a list of available commands.

[Press ENTER to execute the previous command again]
>

Note the e right after receiving. It seems that the charger echoes part of the command back, as if only an arbitrary substring of the issued command was transmitted.

I've also tried

blank-supportgis commented 1 year ago

It turns out you need to put a little pause in between the sent characters. The following script works for me:

import serial
from time import sleep

ser = serial.Serial(
   port='/dev/serial0',
   baudrate = 921600,
   parity=serial.PARITY_NONE,
   stopbits=serial.STOPBITS_ONE,
   bytesize=serial.EIGHTBITS,
   timeout=0.1,
   xonxoff=1,
   rtscts=0
)
print("sending...")
for c in "help\r":
    ser.write(c.encode())
    sleep(0.05)
print('receiving...')
received_data = ser.read()              #read serial port
sleep(0.03)
data_left = ser.inWaiting()             #check for remaining byte
received_data += ser.read(data_left)
print (received_data.decode())