忽略写入串行端口的数据,直到使用pyserial读取

时间:2018-11-27 09:17:15

标签: pyserial

我有与本文中所示相同的问题,但提出的解决方案虽然很有用,但效果不佳。

Reading a Serial Port - Ignore portion of data written to serial port for certain time

但是,我认为Farmer Joe的解决方案存在相同的问题-有时数据会减少一半,例如:

b'2.36\r\n'
b'2.36\r\n'
b'2.36\r\n'
b'\n'
b'\r\n'
b'36\r\n'
b'2.36\r\n'
b'2.36\r\n'

但最终解决方案从未发布!

1 个答案:

答案 0 :(得分:0)

我发现一种简单的方法,涉及在需要时关闭和打开端口,而不是在需要读取时刷新缓冲数据的com端口-间歇读取时可以完成此工作。

void hyper_quick(int *array, int rank, int comm_sz, int s, int e) {
    printf("(arr, %d, %d, %d, %d)\n", rank, comm_sz, s, e);
    // Keeps recursing until there is only one element
    if (s < e) {
            int pivot;
            if (comm_sz > 1) {
                    // One process gets a random pivot within its range and sends that to every process looking at that range
                    if (rank % comm_sz == 0) {
                            pivot = rand() % (e - s) + s;
                            for (int i = rank + 1; i < comm_sz; i++) {
                                    int partner = rank + i;
                                    printf("Rank %d sending pivot %d to %d\n", rank, pivot, partner);
                                    MPI_Send(&pivot, 1, MPI_INT, partner, rank, MPI_COMM_WORLD);
                                    printf("Rank %d successfully sent %d to %d\n", rank, pivot, partner);
                            }
                    }
                    else {
                            int partner = rank - (rank % comm_sz);
                            printf("Rank %d pre-recv from %d\n", rank, partner);
                            MPI_Recv(&pivot, 1, MPI_INT, partner, rank, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
                            printf("Rank %d received pivot %d from %d\n", rank, pivot, partner);
                    }
            }
            else {
                    pivot = rand() % (e - s) + s;
            }
            int tmp = array[pivot];
            array[pivot] = array[e];
            array[e] = tmp;
            // Here is where the actual quick sort happens
            int i = s;
            int j = e - 1;
            while (i < j) {
                    while (array[e] >= array[i] && i < j) {
                            i++;
                    }
                    while (array[e] < array[j] && i < j) {
                            j--;
                    }
                    if (i < j) {
                            tmp = array[i];
                            array[i] = array[j];
                            array[j] = tmp;
                    }
            }
            if (array[e] < array[i]) {
                    tmp = array[i];
                    array[i] = array[e];
                    array[e] = tmp;
                    pivot = i;
            }
            else {
                    pivot = e;
            }
            // Split remaining elements between remaining processes
            if (comm_sz > 1) {
                    // Elements greater than pivot
                    if (rank % comm_sz >= comm_sz/2) {
                            hyper_quick(array, rank, comm_sz/2, pivot + 1, e);
                    }
                    // Elements lesser than pivot
                    else {
                            hyper_quick(array, rank, comm_sz/2, s, pivot - 1);
                    }
            }
            // Recurse remaining elements in current process
            else {
                    hyper_quick(array, rank, 1, s, pivot - 1);
                    hyper_quick(array, rank, 1, pivot + 1, e);
            }
    }

第二个解决方案是丢弃第一个值。我发现以所需的频率阅读时此方法效果很好。例如,当数据以1000Hz的频率发送到com端口,并且以0.1Hz的频率读取端口时,在1s的时间内读取了100个样本。

serial.open()

while (serial.inWaiting()==0): #wait until there is data
    pass

valueRead = serial.readline()
serial.close()