# Project#2 - Sending and Receiving Data! # Information - In this project, we're going to send data to the RPInoRPI and wait for a response. # The RPInoRPI is waiting specifically for a value of 53, which is the ASCII representation of the decimal of 5 # When the RPInoRPI receives 53ASCII/5DEC, it will say 'Waving back" on the LCD. import time # The 'time' module is included with pycharm and allows for us to create delays. import serial # The 'serial' or 'pyserial' module needs to be imported. See the project video for more info. # The first thing that we need to do is to talk to our RPInoRPI. try: # Try to establish communication with the RPInoRPI board. if successful, only the 'try:' will be executed MCU = serial.Serial(port='COM10', baudrate=115200, timeout=1) # Notice the timeout being 1 second. # More on this timeout later. time.sleep(5) # Wait five seconds. Why five seconds? The first communication gets screwy without this delay except: # If communication with the RPInoRPI could not be established, then perform the following instead: print('System Locked - Cannot communicate with MCU. Close this program, then reconnect to MCU and restart program') while True: # Forever loop. Close the program and start again. Check connection to the RPInoRPI pass # Do nothing def slicer(value): # This function takes the returned value from the RPInoRPI and formats it. # What do I mean? The RPInoRPI returns values that look like this: b'5'. # So what? So we need to change that string value into a number for comparison. value = value.replace('b','') # We've received value, which looks like b'5'. Replace 'b' with nothing. # value does from looking like b'5' to '5' value = value.replace("'",'') # Remove all ' from value. Now we're left with 5 value = int(value) # At this point value is still a string. int changes it into a comparable number =D return value # This is the end of this function. The original value was an unusable string. Now a usable number! # This function serves to send a request to the MCU and wait for returned data def write_read(x): MCU.write(bytes(x, 'utf-8')) # Unicode Transformation Format - 8 bits. Write an 8-bit value to the MCU global data # Set as a global varible data = MCU.readline() # Wait for a response from the MCU, and store the returned value in 'data' return data # Return the data to the main code # Main Code x = 0 # Create a variable named x and set it to 0 counter = 0 # Create a variable named 'counter' and set it to 0 while True: # Run the following code until you close the program (loop forever) x = str(counter) # x now equals the string value in counter. Counter is an integer. We can't send integers. print('Sending: ' + x) # print: Sending: (whatever value x equals) value = write_read(x) # call the write_read function and send 'x' along with it. We can only send string data print('Returned Value = ' + str(value)) # Concatenate the string + the string version of 'value' together and print value = slicer(str(value)) # Less complicated than it looks. send 'value' to the slicer function in string form print('New number after slicer function = ' + str(value)) # Print the sliced string>number if value == 5: # The returned 'value' after being sent to the slicer() function is a number. print('The RPInoRPI just waved back!') # If said number is 5, print this. counter = counter + 1 # Increment counter. Or rather, add 1 to counter. Counter is an integer. A while number. if counter == 10: # If counter is 10, reset to 0. We will send values 0 through 9. counter = 0 # Only set counter to 0 when counter equals 10 time.sleep(0.2) # Wait for 0.2 seconds then start over # This is the end of the second project. I think we did a good job ob building on project#1 # The timeout when connecting to the MCU... This is needed to save the returned value of the first transmission. # Try changing the timeout to 0.1 and run the program. You'll see that the first returned value is: # Returned Value = b'', and not Returned Value = b'0', which is garbage. We'll work around this in an upcoming project # You've also been introduced to the slicer function, which removes some string data from the received RPInoRPI data. # It also changes the sliced data into a number 'int' so that we can compare it to other numbers. # Changing strings into numbers, and numbers into strings is important. Slicing up and formatting strings is important. # Stay tuned for project#3!