Search code examples
pythonpyserialchess

How would i write a program in python that connects to a DGT chess board and prints the moves being made in real time?


I am trying to write a program in python that connects to a DGT chess board and recieves the data and prints the moves in real time as they are being made (example: ng1f3). Ive tried asking chat gpt but as usual it has been no help. Ive also looked online for similar projects but found nothing like what i am trying to do.

ive tried getting the data using the serial library. the port and the baudrate are both correct. the board sends 8 bytes of data for each move. this code runs but does not print anything when i move the pieces on the board. ive even tried contacting the manufacturer to see if the boards have any sort of protection against this but they said they couldnt help me with this and instead just sent me the document with the description of the comunication protocol (one drive link to file: https://1drv.ms/b/s!AqF5qQ2nPQ81nkBZqQAMfY93mfdJ?e=eia1mO). i am also fairly new to python and this website.

import serial

ser = serial.Serial("COM7", 9600)

while True:
    data = ser.read(8)
    print(data)

edit: I have since tried:

import serial 
ser = serial.Serial("COM7", 9600, timeout=2) 
ser.write(bytes([0x45])) # message to request serial number 
test = ser.read(20) 
output = test.decode('ascii', errors='replace') # convert bytes to string print(output)

A response was received, but it wasn't quite as expected.


Solution

  • If you feel confident that everything is connected properly, try doing this:

    import serial
    
    ser = serial.Serial("COM7", 9600, timeout=2)
    ser.write(bytes([0x45])) # I think this is the message to request serial number.
    test = ser.read(20)  # Not sure how many bytes are returned
    
    print(test) # this prints a byte string
    print(test.hex()) # this prints the byte string as a string of hex w/out the "0x/"  characters
    print("The Message ID is " +  str(test[0:1]))  # You sent a'0x45' command you get back a'0x91' reply
    print("The length of the message is " + str(test[1:3])) 
    print("The Serial number is " + str(test[3:8]))
    
    
    

    Does that print anything at all?

    edit: Changed the serial write to a byte type.

    edit 2: It looks like the board might write something upon start up. Another thing to try is:

    import serial
    
    ser = serial.Serial("COM7", 9600, timeout=5)  # timeout long enough to turn on board
    test = ser.read(1000) # excess amount to read, but we don't want to miss anything
    print(test)
    

    edit 3: Now that we have verified communications with the board, we can start experimenting with trying to write a script that will read the movement of the chess pieces. Let's try this:

    import serial
    
    ser = serial.Serial("COM7", 9600, timeout=2)
    DTG_REQ_UPDATE_BOARD = bytes([0x44])
    
    while 1:
        timeout = 0
        ser.write(DTG_REQ_UPDATE_BOARD)
        while timeout < 10:
            movement = ser.read(20)  
            
            if movement != 0:
                print(movement)
                timeout = 10
            else:
                timeout += 1
                continue
    
    
    1. Turn on your board.
    2. Start the above script.
    3. Start moving pieces like you would if you were playing a game.
    4. Look for things to start printing out in your console/terminal.

    It's a quick and dirty way of looping (it's an infinite loop and you will have to force stop the process when you are done running your script), but it should do the job for testing purposes.