I am working with canbus in python (Pcan basic api) and would like to make it easier to use. 

Via the bus a lot of devices/modules are connected. They are all allowed to send data, if a collison would happen the lowest ID will win. 

The data Is organized in frames with ID, SubID, hexvalues

To illustrate the problem I am trying to adress, imagine the amplitude of a signal. 

To read the value a frame is send to 

* QuestionID QuestionSUBID QuestionData

If there is no message with higher priority(=lowerID) the answer is written to the bus:

* AnswerID AnswerSubID AnswerData

Since any module/device is allowed to write to the bus, you don't know in advance which answer you will get next. Setting a value morks the same way, just with different IDs. So for the above example the amplitude would have:

1. 4 IDs and SubIds Associated with read/write question/answer
2. Additionally the lenght of the data has (0-8) has to be specified /stored.
3. Since the data is all hex values a parser has to be specified to obtain the human readable value (e.g Voltage in decimal representation)

To store this information I use nested dicts:

```
parameters = {'Parameter_1': {'Read': {'question_ID': ID, 
                                       'question_SUBID': SubID,
                                       'question_Data': hex_value_list,
                                       'answer_ID': ...,
                                       'answer_subID': ..., 
                                       'answer_parser': function},
                               'Write': {'ID': ...,
                                         'SubID': ...,
                                         'parser' ..., 
                                         'answer_ID': ...,
                                         'answer_subID': ...}},
               'Parameter_2': ... }} 

```

There are a lot of tools to show which value was set when, but for hardware control, the order in which paremeters are read are not relevant as long as they are up to date. Thus one part of a possible solution would be storing the whole traffic in a dict of dicts:

```
busdata = {'firstID' : {'first_subID': {'data': data, 
                                        'timestamp': timestamp},
                      'second_subID': {'data': data, 
                                        'timestamp': timestamp},

                      }, 
            secondID': ...}

```

Due to the nature of the bus, I get a lot of answers other devices asked - the bus is quite full - these should not be dismissed since they might be the values I need next and there is no need to create additional traffic - I might use the timestamp with an expiry date, but I didn't think a lot about that so far. 

This works, but is horrible to work with. In general I guess I will have about 300 parameters. The final goal is to controll the devices via a (pyqt) Gui, read some values like serial numbers but as well run measurement tasks.

So the big question is how to define a better datastructure that is easily accesible and understandable? I am looking forward to any suggestion on a clean design.

The main goal would be something like rid of the whole message based aproach.

**EDIT:** My goal is to get rid of the whole CAN specific message based aproach:

I assume I will need one thread for the communication, it should:

1. Read the buffer and update my variables
2. Send requests (messages) to obtain other values/variables
3. Send some values periodically

So from the gui I would like to be abled to: 

1. get parameter by name --> send a string with parameter name
2. set parameter signal --> str(name), value(as displayedin the gui)
3. get values periodically --> name, interval, duration(10s or infinite)

The thread would have to:

1. Log all data on the bus for internal storage
2. Process requests by generating messages from name, value and read until result is obtained
3. Send periodical signals

I would like to have this design idependant of the actual hardware:

* The solution I thought of, is the above *parameters\_dict*

For internal storage I thought about the *bus\_data\_dict*

**Still I am not sure how to**:

1. Pass data from the bus thread to the gui (all values vs. new/requested value)
2. How to implement it with signals and slots in pyqt
3. Store data internally (dict of dicts or some new better idea)
4. If this design is a good choice