小编ktm*_*tmf的帖子

如何提高PySerial读取速度

我目前正在构建一台使用Arduino Mega2560作为其主控制器的机器.Arduino连接到串行,获取命令,执行它并每隔1ms吐出一堆测量数据.我有一个运行Python的Raspberry Pi,为用户提供了一个很好的GUI来发送命令,并以可读的形式呈现数据.

我面临的问题是:Arduino能够每毫秒吐出15个字节的数据(因此只有15kbyte/s),但我正在运行的代码每10毫秒只能处理大约15个字节,所以1.5kB/s .

当我跑步时cat /dev/ttyACM0 > somefile,我很好地看到了所有的数据点.

我有以下精简的Python代码

# Reset Arduino by starting serial
microprocBusy = True
serialPort = serial.Serial("/dev/ttyACM0", baudrate=460800, timeout=0)
time.sleep(0.22);
serialPort.setDTR(False);
time.sleep(0.22);
serialPort.setDTR(True);
time.sleep(0.10);

logfile = open(logfilenamePrefix + "_" + datetime.datetime.now().isoformat() + '.txt', 'a')

# Bootloader has some timeout, we need to wait for that
serialPort.flushInput()
while(serialPort.inWaiting() == 0):
    time.sleep(0.05)

# Wait for welcome message
time.sleep(0.1)
logfile.write(serialPort.readline().decode('ascii'))
logfile.flush()

# Send command
serialPort.write((command + '\n').encode('ascii'))

# Now, receive data
while(True):
    incomingData = serialPort.readline().decode('ascii')
    logfile.write(incomingData) …
Run Code Online (Sandbox Code Playgroud)

python arduino pyserial raspberry-pi

8
推荐指数
3
解决办法
8948
查看次数

如何多次使用ConsoleCancelEventHandler

我一直在忙着编写一个充当前端的应用程序:它有一个GUI,带有按钮等命令行选项,并将它们传递给命令行.exe.它使用应用程序的控制台来显示命令行应用程序的输出.这很好用,但是当使用Ctrl + C或尝试关闭控制台窗口时,GUI也会关闭,这不是我想要的.但是,让程序输出自己的控制台是不可能的,因为它批处理文件,每个文件都会弹出它自己的控制台.

该程序使用MSVC 2012以C++编写,并使用.NET.我试过Console :: CancelKeyPress至少得到Ctrl + C的行为,因为我想要它(停止命令行应用程序,但不是GUI),但有一些麻烦.

我的代码

private: System::Void OnCancelKeyPressed(System::Object^  sender, System::ConsoleCancelEventArgs^  e) {
         e->Cancel = true;
     }
private: System::Void GetConsoleReady() {
         COORD c;
         FreeConsole();
         AllocConsole();
         c.X = 80; c.Y = 8000;
         SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),c);
         Console::Clear();
         Console::TreatControlCAsInput = false;
         Console::CancelKeyPress += 
             gcnew ConsoleCancelEventHandler(this, &Form1::OnCancelKeyPressed);  
     }
Run Code Online (Sandbox Code Playgroud)

每次用户尝试运行要处理的批处理文件时,都会调用此方法.运行批处理后,控制台随FreeConsole()一起发布.第一次它运行良好并使用Ctrl + C杀死命令行应用程序,但GUI中的处理继续,运行其他命令,最后使用FreeConsole().但是,当第二次尝试这样做时,它也会杀死GUI.我尝试在添加新事件之前添加此项以删除上一个事件

             Console::CancelKeyPress -= 
             gcnew ConsoleCancelEventHandler(this, &Form1::OnCancelKeyPressed);  
Run Code Online (Sandbox Code Playgroud)

但不知何故,这会在添加处理程序时引发错误,但只是第二次:mscorlib.dll中出现未处理的类型'System.IO.IOException'异常,附加信息:De参数是onjuist.

最后一部分是荷兰语的"错误论证",而调试人员说它在读取ConsoleCancelEventHandler时会窒息.

如果我尝试通过在加载表单时添加它只添加一次事件处理程序,它什么都不做.

这里发生了什么?

.net c++-cli visual-c++

5
推荐指数
1
解决办法
383
查看次数

标签 统计

.net ×1

arduino ×1

c++-cli ×1

pyserial ×1

python ×1

raspberry-pi ×1

visual-c++ ×1