├── serial_input.py ├── .gitignore ├── LICENSE ├── README.md └── serial_input_old.py /serial_input.py: -------------------------------------------------------------------------------- 1 | import serial 2 | import time 3 | from pynput import keyboard,mouse 4 | 5 | 6 | ser=serial.Serial("/dev/serial0",115200) 7 | ser.timeout=0.1 8 | 9 | if ser.isOpen(): 10 | print("Serial is Open") 11 | 12 | def on_press(key): 13 | ser.write('{}'.format(key).encode()) 14 | ser.flush() 15 | print('key {} pressed'.format(key)) 16 | 17 | def on_release(key): 18 | if key==keyboard.Key.esc: 19 | return False 20 | 21 | with keyboard.Listener( 22 | on_press=on_press, 23 | on_release=on_release) as listener: 24 | listener.join() 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled source # 2 | ################### 3 | *.com 4 | *.class 5 | *.dll 6 | *.exe 7 | *.o 8 | *.so 9 | *.pyc 10 | 11 | # Packages # 12 | ############ 13 | # it's better to unpack these files and commit the raw source 14 | # git has its own built in compression methods 15 | *.7z 16 | *.dmg 17 | *.gz 18 | *.iso 19 | *.rar 20 | #*.tar 21 | *.zip 22 | 23 | # Logs and databases # 24 | ###################### 25 | *.log 26 | *.sqlite 27 | *.xml 28 | _windows/ 29 | 30 | # OS generated files # 31 | ###################### 32 | .DS_Store 33 | ehthumbs.db 34 | Icon 35 | Thumbs.db 36 | .tmtags 37 | .idea/ 38 | mydjangosite/.idea/ 39 | tags 40 | vendor.tags 41 | tmtagsHistory 42 | *.sublime-project 43 | *.sublime-workspace 44 | .bundle -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2019, Intelligent-distributed Cloud and Security Laboratory (ICNS Lab.) 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Set up Raspberry Pi Serial Communication 2 | ## 1. Introduction 3 | EC_serial_communication allows Raspberry pi 3 to establish serial communication with stm32f4discovery board. 4 | (It can also communicate with other boards.) 5 | For serial communication, Raspberry pi requires several settings. 6 | We use a pypnut module to display the status of the communication on the screen. 7 | More details are described below. 8 | ## 2. Configuration 9 | ### 1) Enable GPIO serial port 10 | The GPIO serial port is disabled by default. In order to enable it, edit config.txt. 11 | 12 | $ sudo nano /boot/config.txt 13 | 14 | and add the line at the bottom: 15 | 16 | enable_uart=1 17 | ### 2) Disable serial console 18 | You need to disable serial console to use serial port. 19 | 20 | $ sudo systemctl stop serial-getty@ttyS0.service 21 | $ sudo systemctl disable serial-getty@ttyS0.service 22 | 23 | You also need to remove the console from the cmdline.txt. 24 | 25 | $ sudo vi /boot/cmdline.txt 26 | 27 | and you will see the following comment. 28 | 29 | dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes root wait 30 | 31 | Then, remove the line: 32 | 33 | console=serial0,115200 34 | 35 | (You can also do the same thing on the interface.) 36 | 37 | ### 3) Swap the serial ports 38 | To use the high performance serial port /dev/ttyAMA0, you can assign miniuart(/dev/ttys0) for bluetooth. 39 | 40 | $ sudo nano /boot/config.txt 41 | 42 | and add: 43 | 44 | dtoverlay=pi3-miniuart-bt 45 | 46 | You can check that it has worked by: 47 | 48 | $ ls -l /dev 49 | 50 | (before) 51 | serial0 -> ttyS0 52 | serial1 -> ttyAMA0 53 | 54 | (after) 55 | serial0 -> ttyAMA0 56 | serial1 -> ttyS0 57 | 58 | reference : https://spellfoundry.com/2016/05/29/configuring-gpio-serial-port-raspbian-jessie-including-pi-3-4/ -------------------------------------------------------------------------------- /serial_input_old.py: -------------------------------------------------------------------------------- 1 | # This file is a part of EC_serial_communication 2 | # BSD 3-Clause License 3 | # 4 | # Copyright (c) 2019, Intelligent-distributed Cloud and Security Laboratory (ICNS Lab.) 5 | # All rights reserved. 6 | # 7 | # Redistribution and use in source and binary forms, with or without 8 | # modification, are permitted provided that the following conditions are met: 9 | # 10 | # 1. Redistributions of source code must retain the above copyright notice, this 11 | # list of conditions and the following disclaimer. 12 | # 13 | # 2. Redistributions in binary form must reproduce the above copyright notice, 14 | # this list of conditions and the following disclaimer in the documentation 15 | # and/or other materials provided with the distribution. 16 | # 17 | # 3. Neither the name of the copyright holder nor the names of its 18 | # contributors may be used to endorse or promote products derived from 19 | # this software without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 25 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | # title : serial_input.py 33 | # description : Python, edge cloud serial communication 34 | # author : Yunhwan Kim. 35 | # date : 20190822 36 | # version : 0.3 37 | # notes : This EC_serial_communication is an implementation of edge cloud for communicating 38 | # self-driving cart and edge cloud. 39 | 40 | import threading 41 | import time 42 | import numpy as np 43 | import serial 44 | #import zbar 45 | import sys, getopt 46 | import pygame 47 | import Adafruit_GPIO as GPIO 48 | import Adafruit_CharLCD as LCD 49 | 50 | 51 | from pynput import keyboard, mouse 52 | 53 | 54 | lcd_rs = 25 55 | lcd_en = 24 56 | lcd_d4 = 23 57 | lcd_d5 = 17 58 | lcd_d6 = 18 59 | lcd_d7 = 22 60 | lcd_backlight = 2 61 | # Define LCD column and row size for 16x2 LCD. 62 | lcd_columns = 16 63 | lcd_rows = 2 64 | lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight) 65 | ser = serial.Serial( 66 | "/dev/ttyAMA0", 67 | baudrate=115200, 68 | parity=serial.PARITY_NONE, 69 | stopbits=serial.STOPBITS_ONE, 70 | bytesize=serial.EIGHTBITS, 71 | writeTimeout=1, 72 | timeout=10, 73 | rtscts=False, 74 | dsrdtr=False, 75 | xonxoff=False) 76 | 77 | 78 | def on_press(key): 79 | try: 80 | ser.write('{0}'.format(key).encode()) 81 | lcd.message('{0}'.format(key)) 82 | print('{0}'.format(key)) 83 | print('alphanumeric key {0} pressed'.format( 84 | key.char)) 85 | except AttributeError: 86 | print('special key {0} pressed'.format( 87 | key)) 88 | 89 | def on_release(key): 90 | print('{0} released'.format( 91 | key)) 92 | if key == keyboard.Key.esc: 93 | # Stop listener 94 | return False 95 | 96 | # Collect events until released 97 | with keyboard.Listener( 98 | on_press=on_press, 99 | on_release=on_release) as listener: 100 | listener.join() 101 | ''' 102 | # Initialize the game engine 103 | pygame.init() 104 | 105 | # Define the colors we will use in RGB format 106 | BLACK = ( 0, 0, 0) 107 | WHITE = (255, 255, 255) 108 | BLUE = ( 0, 0, 255) 109 | GREEN = ( 0, 255, 0) 110 | RED = (255, 0, 0) 111 | 112 | # Set the height and width of the screen 113 | size = [400, 300] 114 | screen = pygame.display.set_mode(size) 115 | #font = pygame.font.SysFont("consolas", 20) 116 | 117 | #pygame.display.set_caption("Game Title") 118 | 119 | #Loop until the user clicks the close button. 120 | done = False 121 | flag = None 122 | clock = pygame.time.Clock() 123 | 124 | 125 | # print text function 126 | def printText(msg, color='BLACK', pos=(50, 50)): 127 | textSurface = font.render(msg, True, pygame.Color(color), None) 128 | textRect = textSurface.get_rect() 129 | textRect.topleft = pos 130 | 131 | screen.blit(textSurface, textRect) 132 | 133 | while not done: 134 | 135 | # This limits the while loop to a max of 10 times per second. 136 | # Leave this out and we willimport Adafruit_GPIO as GPIO 137 | 138 | 139 | clock.tick(10) 140 | 141 | # Main Event Loop 142 | for event in pygame.event.get(): # User did something 143 | if event.type == pygame.KEYDOWN: # If user release what he pressed. 144 | pressed = pygame.key.get_pressed() 145 | buttons = [pygame.key.name(k) for k,v in enumerate(pressed) if v] 146 | flag = True 147 | elif event.type == pygame.KEYUP: # If user press any key. 148 | flag = False 149 | elif event.type == pygame.QUIT: # If user clicked close. 150 | done = True 151 | 152 | 153 | # All drawing code happens after the for loop and but 154 | # inside the main while done==False loop. 155 | 156 | # Clear the screen and set the screen background 157 | # screen.fill(WHITE) 158 | 159 | # Print red text if user pressed any key. 160 | if flag == True: 161 | # printText('you just key down!!', 'RED') 162 | # printText('--> you pressed any key.', 'RED', (50, 70)) 163 | # printText('Pressed Key : ' + buttons[0], 'RED', (50, 90)) 164 | ser.write(buttons[0].encode()) 165 | lcd.message(buttons[0]) 166 | print(buttons[0]) 167 | 168 | # Print blue text if user released any key. 169 | elif flag == False: 170 | # printText('you just key up!!', 'BLUE') 171 | # printText('--> released what you pressed.', 'BLUE', (50, 70)) 172 | print(buttons[0]) 173 | # Print default text if user do nothing. 174 | # else: 175 | # printText('Please press any key.') 176 | # Go ahead and update the screen with what we've drawn. 177 | # This MUST happen after all the other drawing commands. 178 | # pygame.display.flip() 179 | 180 | # Be IDLE friendly 181 | pygame.quit() 182 | ''' --------------------------------------------------------------------------------