Skip to content

CAN Frame Stream IO Example + Relevant OOP layer changes#42

Merged
jashnani merged 5 commits intoni:masterfrom
jashnani:frameStreamExample
Jun 28, 2017
Merged

CAN Frame Stream IO Example + Relevant OOP layer changes#42
jashnani merged 5 commits intoni:masterfrom
jashnani:frameStreamExample

Conversation

@jashnani
Copy link

  • This contribution adheres to CONTRIBUTING.md.
  • New tests have been created for any new features or regression tests for bugfixes.
  • tox successfully runs, including unit tests and style checks (see CONTRIBUTING.md).

What does this Pull Request accomplish?

Adds Output to the CAN Frame Stream Input example and enhances the OOP API layer as needed.

Why should this Pull Request be merged?

This is a stepping stone to fleshing out the API.

What testing has been done?

None

@jashnani jashnani added this to the 0.1 milestone Jun 27, 2017
@jashnani jashnani requested review from epage and lilesj June 27, 2017 20:29
@jashnani jashnani self-assigned this Jun 27, 2017
nixnet/types.py Outdated


class CanFrame(object):
class CANFrame(object):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this back to standard PascalCase

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

with nx.Session(database_name, cluster_name, list, interface1, input_mode) as input_session:
print('Input session created successfully.')
with nx.Session(database_name, cluster_name, list, interface2, output_mode) as output_session:
print('Output session created successfully.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need these "successful" prints

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following what XNET has for the C examples but maybe these are not necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might make a little more sense in C which is compiled and doesn't have that great of error reporting mechanisms.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind leaving the console output verbose. Having the extra output can't hurt a new user.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

# Start the input session manually to make sure that the first
# frame value sent before the initial read will be received.
input_session.start(constants.StartStopScope.NORMAL)
print('Input session started manually.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure we need this print?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

try:
id = int(six.input('Enter identifier: '))
except ValueError:
print('Not a number')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will still crash when ValueError is thrown but instead because id is assigned never assigned in the except clause

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe qualify the expected data format in the request. Something like 'Enter decimal identifier: 0d'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've formatted the request to incorporate the type we expect.
I'm now setting id in except as well.

try:
payload_list = [int(x) for x in six.input('Enter payload: ').split()]
except ValueError:
print('Not a number')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto about crashing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. Elaborate on the expected format of the user input in the string.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed same as above

while True:
for byte in payload:
byte = byte + i
with types.CANFrame(id, extended, constants.FRAME_TYPE_CAN_DATA, payload) as frame:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this using a with-statement?

for byte in payload:
byte = byte + i
with types.CANFrame(id, extended, constants.FRAME_TYPE_CAN_DATA, payload) as frame:
output_session.write_frame(list(frame), number_of_bytes_for_frames, 10)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. why list(frame)? Do you mean [frame]?
  2. Why are you passing the number of bytes?
  3. What is 10?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Yes I meant [frame]
  2. I thought I needed to based on incorrect assumption.
  3. timeout - I've added a variable for it now


TIMEOUT_NONE = _cconsts.NX_TIMEOUT_NONE
TIMEOUT_INFINITE = _cconsts.NX_TIMEOUT_INFINITE
FRAME_TYPE_CAN_DATA = _cconsts.NX_FRAME_TYPE_CAN_DATA
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#43 is adding a constants.FrameType.CAN_DATA that you should instead be using

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

i = 0
while True:
for byte in payload:
byte = byte + i
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats this loop trying to do?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed the loop to do what I was intending. Increment the payload values before each write.

nixnet/nx.py Outdated

@intf_can_term.setter
def intf_can_term(self, value):
_props.set_session_intf_can_term(self._handle, value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've gone ahead and merged #45. Can you rebase this onto it and use my version of these properties?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

else:
input_session.intf_can_term = constants.CanTerm.ON
output_session.intf_can_term = constants.CanTerm.ON

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should explicitly set termination for both interfaces for clarity. I.e
if terminated_cable.lower() == "y":
output_session.intf_can_term = constants.CanTerm.OFF
input_session.intf_can_term = constants.CanTerm.ON

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

for frame in frames:
print('Received frame: ')
pp.pprint(frame)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to slow the loop down a bit so we don't have a wall of text in the output. 1 Second should be sufficient.
We can also format the output to be on a single line. Maybe have a initial line that is a header with the following lines formatted with data like so:
Data Transmitted | Data Received
FF FF FF 0A 1B CF FF 00 FF FF FF 0A 1B CF FF 00
FF FF FF 0A 1B CF FF 01 FF FF FF 0A 1B CF FF 01...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've slowed down the loop to 1 second.

I spoke to Jeff about changing the printing format and we decided that it's not critical at the moment and we'll handle example polishing in a different PR.

Copy link
Contributor

@lilesj lilesj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overall example structure looks good to use as a baseline for further examples.

@jashnani jashnani force-pushed the frameStreamExample branch from b686b31 to 517d1c4 Compare June 28, 2017 20:29
@jashnani jashnani merged commit 50bac2d into ni:master Jun 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants