Wired Network Example Explanation#
Environment Preparation#
First, make sure your CanMV development board is properly connected to a router or switch so that the development board can access the network. At the same time, please confirm that the router or switch is working properly so that the development board can communicate normally.
Example Content Analysis#
The Python example below will help you understand how to configure and use the network LAN interface on the CanMV development board.
Import Module#
import network
This line of code imports the network module, which is a library used to manage network interfaces.
Main Function Definition#
def main():
# Subsequent network operations will be placed within this function
Get LAN Interface#
a = network.LAN()
This line of code creates an instance of a LAN interface and assigns it to the variable a. Through this instance, we can perform various operations on the LAN interface.
Check Network Port Status#
# Get whether the network port is in use
print(a.active())
Here, we first check whether the network port is active (i.e., whether it is in use).
View and Set Network Configuration#
# View network port IP, mask, gateway, DNS configuration
print(a.ifconfig())
# Set network port IP, mask, gateway, DNS configuration
print(a.ifconfig(('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8')))
# View again to confirm the setting is successful
print(a.ifconfig())
This code first prints out the current network configuration (IP address, subnet mask, gateway, and DNS server), then sets a new network configuration, and prints it again to confirm the setting is successful.
Use DHCP for Automatic Configuration#
# Set the network port to DHCP mode
print(a.ifconfig("dhcp"))
# View the automatically obtained network configuration
print(a.ifconfig())
In some cases, we may want the network configuration to be obtained automatically, at which point we can use DHCP mode. By setting the parameter of the ifconfig() method to "dhcp", we can let the development board automatically obtain configuration information such as IP address from the DHCP server.
View MAC Address#
# View network port MAC address
print(a.config("mac"))
The MAC address is the unique identifier of a network interface. The MAC address can be obtained through config(“mac”).
Complete Execution#
Finally, by calling the main() function, all the above operations will be executed.
main()
This example demonstrates how to perform basic network configuration and operations on the CanMV development board, including checking network port status, viewing and setting network configuration, using DHCP for automatic configuration, and viewing and setting the MAC address. Through these operations, you can better understand and control the behavior of your development board on the network.
Complete Example#
import network
def main():
# Get lan interface
a=network.LAN()
# Get whether the network port is in use
print(a.active())
# View network port ip, mask, gateway, dns configuration
print(a.ifconfig())
# Set network port ip, mask, gateway, dns configuration
print(a.ifconfig(('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8')))
# View network port ip, mask, gateway, dns configuration
print(a.ifconfig())
# Set network port to dhcp mode
print(a.ifconfig("dhcp"))
# View network port ip, mask, gateway, dns configuration
print(a.ifconfig())
# View network port mac address
print(a.config("mac"))
# Set network port to dhcp mode
print(a.ifconfig("dhcp"))
# View network port ip, mask, gateway, dns configuration
print(a.ifconfig())
main()
For specific interface definitions, please refer to network
Using USB 4G Ethernet Network Card#
CanMV also supports accessing the Internet through a 4G network card via USB, similar to USB Ethernet
Currently the following 4G network card modules are supported. Users need to compile the firmware themselves to enable the corresponding module drivers.
