You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python在Processing读取串口值遇类转换异常的解决方法

解决Processing Python模式下Serial库加载的ClassCastException错误

我正在为学校项目制作雷达,这是我第一次使用Processing。我具备一定Python基础,因此选用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)

解决办法

  • 这个错误根源是Java 9及以上版本的类加载器机制改动,Processing的Python模式(Jycessing)暂未适配。最稳妥的解决方式是改用Java 8运行Processing:
    1. 下载安装Java 8的JDK或JRE
    2. 打开Processing的偏好设置,在"Java"选项中指定Java 8的安装路径
  • 如果必须使用高版本Java,可尝试手动导入Serial库:
    1. 找到Processing安装目录下的libraries/serial/library文件夹,复制其中的serial.jar文件
    2. 在你的sketch项目文件夹中新建code目录,将serial.jar放入该目录
    3. 删除代码开头的add_library('serial'),替换为from processing.serial import Serial来导入库
  • 额外修复代码中的变量作用域问题:myPort是setup()里的局部变量,draw()无法访问,需要将其声明为全局变量:
    修改后的setup()函数:
    def setup():
        size(801, 801)#window size
        global f, myPort
        f = createFont("Arial",20)
        myPort = Serial(this, "COM5", 9600)
    

内容的提问来源于stack exchange,提问作者Enrico Negrin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 13:26:03