Arduino与Proteus仿真实例-Arduino网络仿真环境搭建

Arduino网络仿真环境搭建

Proteus支持两种网卡仿真:ENC28J60和RTL8019AS。Proteus的网络仿真需要安装winap

1、网卡安装

Proteus的网卡仿真是基于NICS模型的,因此需要电脑上有物理网卡及其驱动程序。仅在一台机器上工作时,需要在物理网卡的属性中禁用“校验和卸载”或“硬件校验和”(名称取决于网络驱动程序),以便与虚拟网卡进行通信。 如果通过网络上的另一台计算机提供与模拟网卡的通信,则无需禁用此设置。

在没有网络的情况,可以使用VMWare或VirtualBox虚拟网络进行调试。

如果计算机中安装了多个网卡,则必须在网络控制器原理图部分的模型属性中设置网卡的数量。 可以使用iflist.exe程序获取网卡号,如下所示:

  • C:\Program Files (x86)\Labcenter Electronics\Proteus 7 Professional\BIN\iflist.exe
  • C:\Program Files (x86)\Labcenter Electronics\Proteus 8 Professional\BIN\iflist.exe

在CMD命令中,运行,可以得到如下结果:

在这里插入图片描述

红色方框里的数字代理网卡的索引号,在后面将使用到。

2、仿真电路原理图

下面将使用ENC28J60网卡结合Arduino进行仿真,电路原理图如下:

在这里插入图片描述
选择网卡:
在这里插入图片描述

3、仿真代码实现

本次实例使用到如下开源库:

演示代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Demonstrates usage of the new udpServer feature.
// You can register the same function to multiple ports,
// and multiple functions to the same port.
//
// 2013-4-7 Brian Lee <cybexsoft@hotmail.com>
//
// License: GPLv2

#include <EtherCard.h>
#include <IPAddress.h>

#define STATIC 0 // set to 1 to disable DHCP (adjust myip/gwip values below)

#if STATIC
// ethernet interface ip address
static byte myip[] = { 192,168,6,200 };
// gateway ip address
static byte gwip[] = { 192,168,6,1 };
#endif

// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x70,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[500]; // tcp/ip send and receive buffer

//callback that prints received packets to the serial port
void udpSerialPrint(uint16\_t dest_port, uint8\_t src_ip[IP_LEN], uint16\_t src_port, const char \*data, uint16\_t len){
IPAddress src(src_ip[0],src_ip[1],src_ip[2],src_ip[3]);

Serial.print("dest\_port: ");
Serial.println(dest_port);
Serial.print("src\_port: ");
Serial.println(src_port);


Serial.print("src\_port: ");
ether.printIp(src_ip);
Serial.println("data: ");
Serial.println(data);
}

void setup(){
Serial.begin(57600);
Serial.println(F("\n[backSoon]"));

// Change 'SS' to your Slave Select pin, if you arn't using the default pin
if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0)
Serial.println(F("Failed to access Ethernet controller"));
#if STATIC
ether.staticSetup(myip, gwip);
#else
if (!ether.dhcpSetup())
Serial.println(F("DHCP failed"));
#endif

ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);

//register udpSerialPrint() to port 1337
ether.udpServerListenOnPort(&udpSerialPrint, 1337);

//register udpSerialPrint() to port 42.
ether.udpServerListenOnPort(&udpSerialPrint, 42);
}

void loop(){
//this must be called for ethercard functions to work.
ether.packetLoop(ether.packetReceive());
}

4、仿真结果

在这里插入图片描述

文章来源: https://iotsmart.blog.csdn.net/article/details/121767177