简体   繁体   English

STM32F3如何使用MODBUS TCP?

[英]How to use the MODBUS TCP with STM32F3?

As a result of large research, I found this source code for STM32f1 this l ink and I changed it for STM32f3.作为大型研究的结果,我发现这个STM32f1 L此源代码油墨和我改变它STM32f3。 And the build and install to my STM32.以及构建并安装到我的 STM32。 My ethernet cable connect between my computer and enc28j60 module.我的以太网电缆连接在我的电脑和 enc28j60 模块之间。 If I debugging this code my code stack in main.c and while loop:如果我在main.c和 while 循环中调试此代码我的代码堆栈:

  while (1)
    {


        eMBPoll();
        led_poll();

        /* 从网络设备读取一个IP包,返回数据长度 */
        uip_len = tapdev_read();
        /* 收到数据 */
        **if (uip_len > 0)**
        {
            /* 处理IP数据包 */
            if (BUF->type == htons(UIP_ETHTYPE_IP))
            {
                uip_arp_ipin();
                uip_input();

                if (uip_len > 0)
                {
                    uip_arp_out();
                    tapdev_send();
                }
            }
            /* 处理ARP报文 */
            else if (BUF->type == htons(UIP_ETHTYPE_ARP))
            {
                uip_arp_arpin();
                if (uip_len > 0)
                {
                    tapdev_send();
                }
            }
        }

I stuck if (uip_len > 0) line because uip_len return 0 for this line:我卡住if (uip_len > 0)行,因为 uip_len 为该行返回 0:

(My code same as bellow github link so i dont share all of code ) (我的代码与下面的 github 链接相同,所以我不共享所有代码)

enc28j_60.c in the unsigned int enc28j60_packet_receive(unsigned char *packet, unsigned int maxlen) function: enc28j_60.c 中的 unsigned int enc28j60_packet_receive(unsigned char *packet, unsigned int maxlen) 函数:

unsigned int enc28j60_packet_receive(unsigned char *packet, unsigned int maxlen)
{

    unsigned int rxstat;
    unsigned int len;

    if (enc28_read(EPKTCNT) == 0)
    {
        return (0);
    }

    enc28_write(ERDPTL, (next_pack_ptr));
    enc28_write(ERDPTH, (next_pack_ptr) >> 8);

    next_pack_ptr = enc28_readOp(ENC28J60_READ_BUF_MEM, 0);
    next_pack_ptr |= enc28_readOp(ENC28J60_READ_BUF_MEM, 0) << 8;

    len = enc28_readOp(ENC28J60_READ_BUF_MEM, 0);
    len |= enc28_readOp(ENC28J60_READ_BUF_MEM, 0) << 8;

    len -= 4;

    rxstat = enc28_readOp(ENC28J60_READ_BUF_MEM, 0);
    rxstat |= enc28_readOp(ENC28J60_READ_BUF_MEM, 0) << 8;

    if (len > maxlen - 1)
    {
        len = maxlen - 1;
    }

    **if ((rxstat & 0x80) == 0)
    {
        GPIO_SetBits(GPIOE, GPIO_Pin_9);

        len = 0;
    }**
    else
    {

        des_enc28_readBuffer(packet, len);
    }

    enc28_write(ERXRDPTL, (next_pack_ptr));
    enc28_write(ERXRDPTH, (next_pack_ptr) >> 8);

    enc28_writeOp(ENC28J60_BIT_FIELD_SET, ECON2, ECON2_PKTDEC);

    return (len);
}

Why is the rxstat & 0x80) == 0 ?为什么rxstat & 0x80) == 0 I do not understand.我不明白。

According to the ENC28J60 datasheet , it seems like RXSTAT flag should be at bit 12:根据ENC28J60 数据表,似乎RXSTAT标志应该在位 12:

ENC28J60 物理寄存器

I am not exactly sure if des_enc28_readOp(ENC28J60_READ_BUF_MEM, 0) is reading the right thing, but I believe you should have something like:我不确定des_enc28_readOp(ENC28J60_READ_BUF_MEM, 0)是否正在阅读正确的东西,但我相信你应该有类似的东西:

unsigned PHSTAT2 = des_enc28_readOp(ENC28J60_READ_BUF_MEM, 0);
PHSTAT2 |= des_enc28_readOp(ENC28J60_READ_BUF_MEM, 0) << 8;

unsigned RXSTAT = (PHSTAT2 & 0x1000) != 0;
if (RXSTAT)
{
    // RXSTAT flag is set
    des_enc28_readBuffer(packet, len);
}
else
{
    ...
}

I would also dump the values of this register to a log or serial port, to make sure you understand what its contents actually are:我还会将此寄存器的值转储到日志或串行端口,以确保您了解其实际内容:

// I noticed serialprint in your other question, so I am presuming this is your log func
serialprint("PHSTAT2 = 0x%04x\n", PHSTAT2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM