使用 python 读取处理上的序列值

问题描述 投票:0回答:1

我正在为学校项目制作雷达,这是我第一次使用Processing。我对 python 有一些了解,所以我使用了该语言。

网上查了也没找到解决办法。

这是我写的代码

add_library('serial')
import math
def setup():
    size(801, 801)#window size
    global f
    f = createFont("Arial",20)
    myPort = Serial(this, "COM5", 9600)
def draw():
    rectMode(CENTER)
    stroke(255)#white border
    fill(0)#black infill
    rect(400,400,800,800) #(x1,y1,base,height)
    ellipseMode(CENTER)
    ellipse(400,400,800,800)
    ellipse(400,400,600,600)
    ellipse(400,400,400,400)
    ellipse(400,400,200,200)
    line(400,0,400,800) #(x1,y1,x2,y2)vertical line
    line(0,400,800,400)#orizzontal line
    line(400-400*math.cos(0.785),400-400*math.sin(0.785),400+400*math.cos(0.785),400+400*math.sin(0.785)) #diagonal1
    line(400-400*math.cos(0.785),400+400*math.sin(0.785),400+400*math.cos(0.785),400-400*math.sin(0.785)) #diagonal2
    textFont(f) 
    fill(255)                               
    text("25cm",440,390)
    text("50cm",540,390)
    text("75cm",640,390)
    text("100cm",730,390)
    value = float(myPort.readline())

我收到此错误:

ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap')
    at jycessing.LibraryImporter.addJarToClassLoader(LibraryImporter.java:315)
    at jycessing.LibraryImporter.recursivelyAddJarsToClasspath(LibraryImporter.java:164)
    at jycessing.LibraryImporter.addLibrary(LibraryImporter.java:140)
    at jycessing.LibraryImporter$1.__call__(LibraryImporter.java:82)
    at org.python.core.PyObject.__call__(PyObject.java:480)
    at org.python.core.PyObject.__call__(PyObject.java:484)
    at org.python.pycode._pyx707.f$0(Radar.pyde:1)
    at org.python.pycode._pyx707.call_function(Radar.pyde)
    at org.python.core.PyTableCode.call(PyTableCode.java:171)
    at org.python.core.PyCode.call(PyCode.java:18)
    at org.python.core.Py.runCode(Py.java:1614)
    at org.python.core.Py.exec(Py.java:1658)
    at org.python.pycode._pyx706.f$0(C:/Users/Enrico/AppData/Local/Temp/Radar15742756830072344729/Radar.pyde:96)
    at org.python.pycode._pyx706.call_function(C:/Users/Enrico/AppData/Local/Temp/Radar15742756830072344729/Radar.pyde)
    at org.python.core.PyTableCode.call(PyTableCode.java:171)
    at org.python.core.PyCode.call(PyCode.java:18)
    at org.python.core.Py.runCode(Py.java:1614)
    at org.python.core.Py.exec(Py.java:1658)
    at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:276)
    at jycessing.PAppletJythonDriver.processSketch(PAppletJythonDriver.java:233)
    at jycessing.PAppletJythonDriver.findSketchMethods(PAppletJythonDriver.java:613)
    at jycessing.Runner.runSketchBlocking(Runner.java:399)
    at jycessing.mode.run.SketchRunner.lambda$2(SketchRunner.java:112)
    at java.base/java.lang.Thread.run(Thread.java:833)

如何解决这个错误?

python arduino processing
1个回答
0
投票

试试这个

# Simple Radar Simulation in Processing (Python Mode)

def setup():
    size(400, 400)  # Set canvas size
    background(255)  # White background

def draw():
    translate(width / 2, height / 2)  # Move origin to center
    noFill()
    stroke(0)  # Black stroke color
    ellipse(0, 0, 200, 200)  # Outer circle (represents radar range)

# Simulate radar sweep
for angle in range(0, 360, 10):
    x = cos(radians(angle)) * 100  # Convert angle to x-coordinate
    y = sin(radians(angle)) * 100  # Convert angle to y-coordinate
    line(0, 0, x, y)  # Draw radar sweep line

noLoop()  # Stop continuous animation

# Run the sketch
run()
© www.soinside.com 2019 - 2024. All rights reserved.