def get_joystick(ser): cmd = [0, 2] cmd.extend([0, 0, 0, 0, 0, 0, 0, 0]) comms.send_cmd(ser, cmd) returnvalue = ser.read(2) if returnvalue != b'': return list(returnvalue)
def echo(ser, number_list): cmd = [0, 1] cmd.extend(number_list) comms.send_cmd(ser, cmd) returnvalue = ser.read(8) if returnvalue != b'': return list(returnvalue)
def mcp2515_read(ser, address, data_length): cmd = [2, 3, address, data_length, 0, 0, 0, 0, 0, 0] comms.send_cmd(ser, cmd) returncode = ser.read(data_length) if returncode != b'': return int.from_bytes(returncode, byteorder='big') return -1
def mcp2515_read_status(ser): cmd = [2, 0, 0, 0, 0, 0, 0, 0, 0, 0] comms.send_cmd(ser, cmd) read_status = ser.read(1) if read_status != b'': return int.from_bytes(read_status, byteorder='big') return None
def can_pending_rx_buffer(ser): cmd = [3, 3] cmd.extend([0, 0, 0, 0, 0, 0, 0, 0]) comms.send_cmd(ser, cmd) returncode = ser.read(2) print(returncode, list(returncode)) if returncode != b'': return list(returncode) return None
def motor_read_encoder(ser): cmd = [4, 1] cmd.extend([0,0,0,0,0,0,0,0]) comms.send_cmd(ser, cmd) returncode = ser.read(2) if returncode != b'': return int.from_bytes(returncode, byteorder='big', signed=True)
def write_can_rx_flag(ser, value): """ This is a helper function to set the receive_can_on_interrupt_flag """ cmd = [2, 8, value, 0, 0, 0, 0, 0, 0, 0] comms.send_cmd(ser, cmd) status = ser.read(1) if status != b'': status = int.from_bytes(status, byteorder='big') if status == 0: return 0 raise Exception
def mcp2515_init(ser, mode): if not isinstance(mode, MCP_MODE): raise TypeError('mode is not a valid MPC_MODE') cmd = [2, 1, mode, 0, 0, 0, 0, 0, 0, 0] comms.send_cmd(ser, cmd) returncode = ser.read(1) if returncode != b'': return int.from_bytes(returncode, byteorder='big') return -1
def sram_read(ser, address): cmd = [1, 1] cmd.extend(list(address.to_bytes(2, byteorder='big'))) cmd.extend([0,0,0,0,0,0]) comms.send_cmd(ser, cmd) returncode = ser.read(1) if returncode != b'': return int.from_bytes(returncode, byteorder='big') return -1
def can_valid_tx_buffer(ser): cmd = [3, 2] cmd.extend([0, 0, 0, 0, 0, 0, 0, 0]) comms.send_cmd(ser, cmd) returncode = ser.read(2) print(returncode) if returncode != b'': return list(returncode) return None
def can_transmit(ser, msg_id, msg_len, msg_data): cmd = [3, 0] cmd.extend(list(msg_id.to_bytes(2, byteorder='big'))) cmd.append(msg_len) cmd.extend(msg_data) cmd_empty_len = 10 - (msg_len + 5) empty_vector = [0] * (cmd_empty_len) cmd.extend(empty_vector) comms.send_cmd(ser, cmd) returncode = ser.read(1) if returncode != b'': return int.from_bytes(returncode, byteorder='big') return -1
def synchronize(ser): """ Tries to sync sender and receiver. Returns 0 on success Returns 1 on failiure """ _timeout = ser.timeout ser.timeout = 0.1 cmd = [0] for i in range(0, 100): comms.send_cmd(ser, cmd) returnvalue = ser.read(1) if returnvalue != b'': if int.from_bytes(returnvalue, byteorder='big') == 1: ser.timeout = _timeout return 0 ser.timeout = _timeout return 1
def can_receive(ser): cmd = [3, 1] cmd.extend([0, 0, 0, 0, 0, 0, 0, 0]) comms.send_cmd(ser, cmd) # Check the can_status. # The first byte returned will be the can_status. A 0 indicated that # we received a message, a 1 indicates that we did not returncode = ser.read(1) #print("Returncode:", returncode) if returncode != b'': # Not a zero indicates no message received. Return 1 to indicate failiure if int.from_bytes(returncode, byteorder='big') != 0: # print("No message received") return 1 raw_data = ser.read(11) #print("Raw_data:", raw_data, list(raw_data)) if raw_data != b'': # Convert the data into a nicer format # The main thing is to combine two bytes into the ID, # and throw away data bytes that are not actually valid. (We will always # get 11 bytes, but not all are valid data because the data.len matters) ID = int.from_bytes(raw_data[0:2], byteorder='big') Length = int.from_bytes(raw_data[2:3], byteorder='big') data = list(raw_data[3:4 + Length - 1]) print("Received a message width ID={}, len={}, data={}".format( ID, Length, data)) returnlist = [ID, Length] returnlist.extend(data) return returnlist return 1