小编Swe*_*gin的帖子

指针有问题吗?

我在理解 *binsearch 函数中某些代码行的机制时遇到了问题。特别是,low 和 high 是两个指针,分别初始化为 &tab[0] 和 &tab[n]。在下一行我看到low<high我认为它无效,因为不可能比较两个指针的两个地址。下一行也有同样的问题。我不知道我是否正确,我需要大家的一些想法。

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAXWORD 100
int getword(char *, int);
struct key *binsearch(char *, struct key *, int);
/* count C keywords; pointer version */
main()
{
    char word[MAXWORD];
    struct key *p;
    while (getword(word, MAXWORD) != EOF)
        if (isalpha(word[0]))
            if ((p=binsearch(word, keytab, NKEYS)) != NULL)
                p->count++;
    for (p = keytab; p < keytab + NKEYS; p++)
        if (p->count > 0)
            printf("%4d %s\n", p->count, p->word);
    return 0; …
Run Code Online (Sandbox Code Playgroud)

c struct pointers

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

在 CANbus 上接收消息总是返回与 python-can 相同的值

我有一个带有多个输入的 CAN 总线 (PCAN)。我尝试读取 python 中的输入并将其打印在控制台上。我从总线收到的第一条消息是正确的,但是如果我更改输入的状态,消息中的数据不会更改并继续吐出它收到的第一条数据。在PCAN视图中我可以看到数据变化,所以这不是硬件故障。

我的代码:

import can
from time import sleep


def main():
bus = can.interface.Bus(bustype='pcan', channel='PCAN_USBBUS1', bitrate=500000)

try:
    while True:

        # msg = can.Message(arbitration_id=0x232, data=[0x00], is_extended_id=False)
        # try:
        #     bus.send(msg)
        #     print("message sent on {}".format(bus.channel_info))
        # except can.CanError:
        #     print("message not sent!")

        msg = bus.recv(None)
        try:

            if msg.arbitration_id == 0x1B2:
                print(msg)

            if msg.arbitration_id == 0x1B3:
                print(msg)

            if msg.arbitration_id == 0x1B4:
                print(msg)

            if msg.arbitration_id == 0x1B5:
                print(msg)

        except AttributeError:
            print("Nothing received this time")

        sleep(0.2)

except KeyboardInterrupt:
    print("Program Exited") …
Run Code Online (Sandbox Code Playgroud)

python-3.x python-can

4
推荐指数
1
解决办法
2万
查看次数

如何在matplotlib中部分填充之间,如不同值的不同颜色

我正在尝试为图形线和 x 轴之间的空间着色。颜色应基于线上对应点的值。有点像第一张图: 在此输入图像描述(https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/fill_ Between_demo.html)

如果高于 100,则应为红色;如果低于 100,则应为绿色。我正在寻找的图表应该是红绿交替的。

这就是我现在所拥有的:

import matplotlib.pyplot as plt

lenght = 120

y = [107, 108, 105, 109, 107, 106, 107, 109, 106, 106, 94, 93, 94, 93, 93, 94, 95, 106, 108, 109, 107, 107, 106, 108, 105, 108, 107, 106, 107, 97, 93, 96, 94, 96, 95, 94, 104, 107, 106, 108, 107, 107, 106, 107, 105, 107, 108, 105, 107, 100, 93, 94, 93, 95, 104, 107, 107, 108, 108, 107, …
Run Code Online (Sandbox Code Playgroud)

graph colors matplotlib python-3.x

3
推荐指数
1
解决办法
3524
查看次数

标签 统计

python-3.x ×2

c ×1

colors ×1

graph ×1

matplotlib ×1

pointers ×1

python-can ×1

struct ×1