pySerialTransfer 双向通信问题

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

我编写了一个最小的双向Python和串行通信脚本,将整数发送到arduino。然后,arduino 递增整数并通过串行将其发送回。

comm.py

from time import sleep
from pySerialTransfer import pySerialTransfer as txfer


class struct:
    control = 0

class receivestruct:
    status = 0


if __name__ == '__main__':
    try:
        testStruct = struct
        rstruct =receivestruct 
        link = txfer.SerialTransfer('COM7')
        
        link.open()
        sleep(2)
        
        counter = 0
    
        while True:
            sendSize = 0
            
            testStruct.control = counter
            
            sendSize = link.tx_obj(testStruct.control, start_pos=sendSize)
            
            link.send(sendSize)

            print('sent')
            #sleep(0.5)

            read_correctly = False
            while (read_correctly == False):
                try:
                    if not (link.available()):
                        pass

                    recSize = 0
                    # this section returns errors with can't read str as int 
                    rstruct.status = link.rx_obj(obj_type='i', start_pos=recSize)
                    counter = rstruct.status
                    recSize += txfer.STRUCT_FORMAT_LENGTHS['i']

                    print(rstruct.status)
                    read_correctly = True
                except:
                    pass

        
    except KeyboardInterrupt:
        link.close()

arduino.ino

#include "SerialTransfer.h"


SerialTransfer myTransfer;

uint32_t rcontrol;

uint32_t control;

void setup()
{
  Serial.begin(115200);
  myTransfer.begin(Serial);
}

void loop()
{
  if(myTransfer.available())
  {
    // use this variable to keep track of how many
    // bytes we've processed from the receive buffer
    uint16_t recSize = 0;

    recSize = myTransfer.rxObj(rcontrol, recSize);


    control = rcontrol + 1;
    uint16_t sendSize = 0;


    sendSize = myTransfer.txObj(control, sendSize);

    myTransfer.sendData(sendSize);
    
  }
}

现在,我认为这可以正常工作。然而,这里是控制台日志,看起来 python 没有正确读取增量值,并且每 n 次尝试才读取正确的增量值:

115
sent
115
sent
116
sent
116
sent
116
sent
116
sent
116
sent
116
sent
116
sent
116
sent
116
sent
117
sent
117
sent
117
sent
117
sent
117
sent
117
sent
117
sent
117
sent
117
sent
118
sent
118
sent
118
sent
118
sent
118
sent
118
sent
118
sent
118
sent
118
sent
119
sent
119
sent
119
sent
119
sent
119
sent
119
sent
119
sent
119
sent

我在这里尝试了 pySerialTransfer 的两种方式示例,但它似乎使用了不再受支持的过时版本的库。知道为什么我在双向串行通信中遇到这些问题吗?

python arduino serial-port serial-communication
1个回答
0
投票

我在我的 Python 设置上安装了

pySerialTransfer
版本
2.6.9
,并为我的 Arduino Uno 安装了
SerialTransfer
版本
3.1.3

您说您想发送单个整数值,所以我遵循了

datum
库中的 pySerialTransfer
examples

我最终得到了 Arduino 上的代码,用于接收整数、递增它并返回新值:

#include "SerialTransfer.h"


SerialTransfer myTransfer;

int y;


void setup()
{
  Serial.begin(115200);
  myTransfer.begin(Serial);
}


void loop()
{
  if(myTransfer.available())
  {
    myTransfer.rxObj(y);
    y++;
    myTransfer.sendDatum(y);
  }
  delay(500);
}

我使用的是 Arduino Uno,因此

int
存储为 16 位(2 字节)值。得出的范围为 -32,768 到 32,767。这意味着对于 Python 代码,我必须使用源代码中的信息将 Python 代码中的对象类型重写为 2 字节有符号整数:
rx_obj
:

https://github.com/PowerBroker2/pySerialTransfer/blob/74067630dcac6bc0db8925a715a7f9e31ea55bd5/pySerialTransfer/pySerialTransfer.py#L319-L321

tx_obj

https://github.com/PowerBroker2/pySerialTransfer/blob/74067630dcac6bc0db8925a715a7f9e31ea55bd5/pySerialTransfer/pySerialTransfer.py#L242-L243

这是我用于测试的Python代码:

from time import sleep
from pySerialTransfer import pySerialTransfer as txfer

y = 0
send_data = True
port_id = 'xxxx'  # Insert actual value e.g: 'COM7'

if __name__ == '__main__':
    try:
        link = txfer.SerialTransfer(port_id)
        link.open()
        sleep(5)

        while True:
            if send_data:
                sendSize = link.tx_obj(y, val_type_override='h')
                link.send(sendSize)
                send_data = False
                print("Data sent:", y, end=' -+- ')
            if not send_data and link.available():
                y = link.rx_obj(obj_type='h')
                print("Data received:", y)
                send_data = True
            if link.status <= 0:
                if link.status == txfer.CRC_ERROR:
                    print('ERROR: CRC_ERROR')
                elif link.status == txfer.PAYLOAD_ERROR:
                    print('ERROR: PAYLOAD_ERROR')
                elif link.status == txfer.STOP_BYTE_ERROR:
                    print('ERROR: STOP_BYTE_ERROR')

    except KeyboardInterrupt:
        pass

    finally:
        link.close()

这给了我以下输出:

Data sent: 0 -+- Data received: 1
Data sent: 1 -+- Data received: 2
Data sent: 2 -+- Data received: 3
Data sent: 3 -+- Data received: 4
Data sent: 4 -+- Data received: 5
Data sent: 5 -+- Data received: 6
Data sent: 6 -+- Data received: 7
Data sent: 7 -+- Data received: 8
Data sent: 8 -+- Data received: 9
Data sent: 9 -+- Data received: 10
Data sent: 10 -+- Data received: 11
Data sent: 11 -+- Data received: 12
Data sent: 12 -+- Data received: 13
Data sent: 13 -+- Data received: 14
Data sent: 14 -+- Data received: 15
Data sent: 15 -+- Data received: 16

pySerialTransfer
库的 GitHub 版本比从 PyPI 安装的版本更新,因此当文件与最新版本不同时,我尝试链接到 GitHub 上文件的正确版本。例如。示例目录。

© www.soinside.com 2019 - 2024. All rights reserved.