gmenounos / kw1281test

VW KW1281 Protocol Test Tool
MIT License
111 stars 14 forks source link

New LinuxInterface using libc calls to drive UART on Linux #73

Open ofauchon opened 2 months ago

ofauchon commented 2 months ago

KW1281 protocol requires a special 5 bauds byte transmission to wake up device (eg:ECU) on the K-Line. Such very low baud rates are generally not supported by OSes and uart chipsets.

The trick is to bit bang this first 'init' byte at 5 bauds: Break signals are used to drive the UART TX Line low during transmission.

Although this trick works properly on Windows, .Net Linux implementation don't seem to handle properly line breaks

Here comes the new LinuxInterface...

This new interface implements UART access in a more direct/low level way (without using .Net framework). As the code uses "libc" calls, it could break the build on Windows .

The workaround is to compile the driver only when building on Linux OS. This is done by detecting the OS through .csproj directives, and use macros (#if Linux) in the code.

This PR had been tested on Arch Linux, FT232 KLL Cable:

$ lsusb | grep FT232
Bus 003 Device 004: ID 0403:6001 Future Technology Devices International, Ltd FT232 Serial (UART) IC

$ lsmod | grep ftdi
ftdi_sio               77824  0

$ ./bin/Debug/net8.0/kw1281test /dev/ttyUSB0 9600 1 ReadFaultCodes
Version 0.97.0-beta (https://github.com/gmenounos/kw1281test/releases)
Args: /dev/ttyUSB0 9600 1 ReadFaultCodes
OSVersion: Unix 6.7.8.1
.NET Version: 8.0.3
Culture: en-US
Opening Linux serial port /dev/ttyUSB0
Sending wakeup message
Reading sync byte
Reading sync byte done
Keyword Lsb $01
Keyword Msb $8A
Protocol is KW 1281 (8N1)
ECU: 037906025R  SIMOS 4S           2104
Software Coding 00000, Workshop Code: 00000
Sending ReadFaultCodes block
Fault codes:
    00518 - 29-00
    00530 - 31-00
    00523 - 30-00
    00522 - 30-00

All comments/feedback welcome

Sveshais commented 4 weeks ago

Got curious and tried this out, given example of reading fault codes from ECU works, but limited support of baud rates is troublesome, using suggested 10400 for cluster:

Version 0.97.0-beta (https://github.com/gmenounos/kw1281test/releases) Args: /dev/ttyUSB0 10400 1 GetSKC OSVersion: Unix 6.8.11.300 .NET Version: 8.0.4 Culture: en-US Opening Linux serial port /dev/ttyUSB0 Caught: System.IO.IOException Unknown/invalid baudrate Unhandled exception: System.IO.IOException: Unknown/invalid baudrate at BitFab.KW1281Test.Interface.LinuxInterface.SetBaudRate(Int32 baudRate) in /home/sveshais/Downloads/kw1281test-linux-native-interface/Interface/LinuxInterface.cs:line 211 at BitFab.KW1281Test.Interface.LinuxInterface..ctor(String portName, Int32 baudRate) in /home/sveshais/Downloads/kw1281test-linux-native-interface/Interface/LinuxInterface.cs:line 127 at BitFab.KW1281Test.Program.OpenPort(String portName, Int32 baudRate) in /home/sveshais/Downloads/kw1281test-linux-native-interface/Program.cs:line 501 at BitFab.KW1281Test.Program.Run(String[] args) in /home/sveshais/Downloads/kw1281test-linux-native-interface/Program.cs:line 259 at BitFab.KW1281Test.Program.Main(String[] args) in /home/sveshais/Downloads/kw1281test-linux-native-interface/Program.cs:line 31

ofauchon commented 3 weeks ago

Thanks for testing this feature. I'll try to improve the code so it can handle custom baudrates.

Some informations here: https://www.downtowndougbrown.com/2013/11/linux-custom-serial-baud-rates/

mnaberez commented 1 week ago

Support for 10400 baud in particular should be retained. The cluster that I have on my bench (1J0919991BX) uses 10400 baud. The VW Premium 5 radio from Delco also uses 10400 baud.

Many radios have a feature where they will connect to the cluster using KWP1281 in order to automatically unlock from SAFE mode. I disassembled the code for this in several radios and found they were hardcoded to expect 10400 baud from the cluster, which suggests that 10400 baud is widely used in the clusters.