Raspberry Pi与Arduino和Python的串行通信

时间:2018-11-06 14:09:31

标签: python arduino raspberry-pi serial-port

我想使用Python在Raspberry Pi和Arduino之间进行通信。 到目前为止,Arduino已成功将串行消息发送到Raspberry Pi,并使用Python中的 ser.readline()函数读取了该消息。 但是,当我想通过IF语句闪烁连接到Raspberry Pi的LED时将不起作用

blink()函数和其他所有功能都可以使用,但是代码不会进入IF语句,该语句使用检查 ser.readline()值>字符串变量

这是我的Arduino的代码:

BodyLayerStack

这是在我的Raspberry Pi上运行的Python代码:

java.lang.RuntimeException: com.intellij.ide.plugins.PluginManager$StartupAbortedException: Fatal error initializing 'com.intellij.openapi.fileTypes.FileTypeManager'
at com.intellij.idea.IdeaApplication.run(IdeaApplication.java:212)
at com.intellij.idea.MainImpl$1.lambda$null$0(MainImpl.java:49)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:762)
at java.awt.EventQueue.access$500(EventQueue.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:715)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:732)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:380)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Caused by: com.intellij.ide.plugins.PluginManager$StartupAbortedException: Fatal error initializing 'com.intellij.openapi.fileTypes.FileTypeManager'
at com.intellij.ide.plugins.PluginManager.handleComponentError(PluginManager.java:257)
at com.intellij.openapi.components.impl.PlatformComponentManagerImpl.handleInitComponentError(PlatformComponentManagerImpl.java:43)
at com.intellij.openapi.components.impl.ComponentManagerImpl$ComponentConfigComponentAdapter.getComponentInstance(ComponentManagerImpl.java:501)
at com.intellij.openapi.components.impl.ComponentManagerImpl.createComponents(ComponentManagerImpl.java:106)
at com.intellij.openapi.application.impl.ApplicationImpl.lambda$createComponents$9(ApplicationImpl.java:437)
at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$1(CoreProgressManager.java:157)
at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:580)
at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:525)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:85)
at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:144)
at com.intellij.openapi.application.impl.ApplicationImpl.createComponents(ApplicationImpl.java:444)
at com.intellij.openapi.components.impl.ComponentManagerImpl.init(ComponentManagerImpl.java:90)
at com.intellij.openapi.application.impl.ApplicationImpl.load(ApplicationImpl.java:396)
at com.intellij.openapi.application.impl.ApplicationImpl.load(ApplicationImpl.java:382)
at com.intellij.idea.IdeaApplication.run(IdeaApplication.java:205)
... 16 more

Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.intellij.util.PlatformIcons
at com.intellij.openapi.fileTypes.UserFileType.<init>(UserFileType.java:28)
at com.intellij.openapi.fileTypes.impl.AbstractFileType.<init>(AbstractFileType.java:58)
at com.intellij.openapi.fileTypes.impl.FileTypeManagerImpl.loadFileType(FileTypeManagerImpl.java:1383)
at com.intellij.openapi.fileTypes.impl.FileTypeManagerImpl.initStandardFileTypes(FileTypeManagerImpl.java:317)
at com.intellij.openapi.fileTypes.impl.FileTypeManagerImpl.<init>(FileTypeManagerImpl.java:255)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.picocontainer.defaults.InstantiatingComponentAdapter.newInstance(InstantiatingComponentAdapter.java:193)
at com.intellij.util.pico.CachingConstructorInjectionComponentAdapter.doGetComponentInstance(CachingConstructorInjectionComponentAdapter.java:103)
at com.intellij.util.pico.CachingConstructorInjectionComponentAdapter.instantiateGuarded(CachingConstructorInjectionComponentAdapter.java:80)
at com.intellij.util.pico.CachingConstructorInjectionComponentAdapter.getComponentInstance(CachingConstructorInjectionComponentAdapter.java:63)
at com.intellij.openapi.components.impl.ComponentManagerImpl$ComponentConfigComponentAdapter.getComponentInstance(ComponentManagerImpl.java:459)
... 28 more

这是我在终端中看到的: terminal_image

我一直在努力寻找解决方案,但是没有运气。 我也刚刚开始用Python编程。 谢谢您的宝贵时间。

1 个答案:

答案 0 :(得分:0)

import serial                                                                                          
import RPi.GPIO as GPIO                                                                                
import time                                                                                            

LedPin = 11    # pin11                                                                                 
ser=serial.Serial("/dev/ttyUSB0",9600)  #change ACM number as found from ls /dev/tty/ACM*              
ser.baudrate=9600                                                                                      
def setup():                                                                                           
   GPIO.setmode(GPIO.BOARD)       # Set the board mode to numbers pins by physical location            
   GPIO.setup(LedPin, GPIO.OUT)   # Set pin mode as output                                             
   GPIO.output(LedPin, GPIO.HIGH) # Set pin to high(+3.3V) to off the led                              

def blink():                                                                                           
           print 'led on'                                                                              
           GPIO.output(LedPin, GPIO.LOW)   # led on                                                    
           time.sleep(1.0)                 # wait 1 sec                                                
           print 'led off'                                                                             
           GPIO.output(LedPin, GPIO.HIGH)  # led off                                                   
           time.sleep(1.0)                 # wait 1 sec                                                


setup()                                                                                                
while True:                                                                                            
   serialmessage = ser.readline()                                                                      
   print("serial message is " + serialmessage)                                                         

   if serialmessage == "hello":                                                                        
       print("message recieved")                                                                       
       blink()

Python使用间距来检测代码块。

您需要在while循环中放置一个if语句,该语句带有对blink方法的调用。

还可以通过在Raspberry pi上运行命令来检查传入的串行数据

sudo cat /dev/ttyUSB0