初级会员
- 积分
- 108
- 金钱
- 108
- 注册时间
- 2022-1-1
- 在线时间
- 31 小时
|
楼主 |
发表于 2024-8-14 19:59:18
|
显示全部楼层
附上主函数代码:
int main(void)
{
u8 i;
u8 order[9]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
u8 sock_int,rxlen=0;
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
LED_Init(); //LED初始化
IO_Init(); //IO端口初始化
usart_init(115200); //串口初始化为460800
ch395_hardware_init(); //ch395初始化
do
{
ch395q_handler();
}
while (ch395q_Structure.dhcp_status == DHCP_STA); /* 获取DHCP */
ch395_sockct_config[0].socket_enable = CH395Q_ENABLE; /* 使能socket对 */
ch395_sockct_config[0].socket_index = CH395Q_SOCKET_0; /* 设置socket对 */
ch395_sockct_config[0].des_port = 8080; /* 目标端口 */
ch395_sockct_config[0].sour_port = 8080; /* 源端口 */
memcpy(ch395_sockct_config[0].des_ip, ch395_des_ipaddr, sizeof(ch395_sockct_config[0].des_ip)); /* 设置目标IP地址 */
memcpy(ch395_sockct_config[0].net_config.ipaddr, ch395_ipaddr, sizeof(ch395_sockct_config[0].net_config.ipaddr)); /* 设置静态本地IP地址 */
memcpy(ch395_sockct_config[0].net_config.gwipaddr, ch395_gw_ipaddr, sizeof(ch395_sockct_config[0].net_config.gwipaddr)); /* 设置静态网关IP地址 */
memcpy(ch395_sockct_config[0].net_config.maskaddr, ch395_ipmask, sizeof(ch395_sockct_config[0].net_config.maskaddr)); /* 设置静态子网掩码地址 */
memcpy(ch395_sockct_config[0].net_config.macaddr, ch395_macaddr, sizeof(ch395_sockct_config[0].net_config.macaddr)); /* 设置静态MAC地址 */
ch395_sockct_config[0].proto = CH395Q_SOCKET_UDP; /* 设置协议 */
ch395_sockct_config[0].send.buf = socket0_send_buf; /* 发送数据 */
ch395_sockct_config[0].send.size = sizeof(socket0_send_buf); /* 发送数据大小 */
ch395_sockct_config[0].recv.buf = socket0_recv_buf; /* 接收数据缓冲区 */
ch395_sockct_config[0].recv.size = sizeof(socket0_recv_buf); /* 接收数据大小 */
ch395q_socket_config(&ch395_sockct_config[0]); /* 配置socket参数 */
while(1)
{
sock_int = ch395_get_socket_int(CH395Q_SOCKET_0);
if (sock_int & SINT_STAT_RECV) /* 接收中断 */
{
ch395q_Structure.socket[CH395Q_SOCKET_0].config.recv.size = ch395_get_recv_length(CH395Q_SOCKET_0); /* 获取当前缓冲区内数据长度 */
rxlen = ch395q_Structure.socket[CH395Q_SOCKET_0].config.recv.size;
ch395_get_recv_data(CH395Q_SOCKET_0, rxlen, ch395q_Structure.socket[CH395Q_SOCKET_0].config.recv.buf); /* 读取数据 */
ch395q_Structure.socket[CH395Q_SOCKET_0].config.recv.buf[rxlen] = '\0';
for(i=0;i<rxlen;i++)
{
order=socket0_recv_buf;
}
ch395q_Structure.socket[CH395Q_SOCKET_0].config.recv.recv_flag |= 0x04;
//网口发送指令判断
if(order[0]==0xAA && order[1]==0x1A && order[2]==0x01)
{
LED1=1;
ch395_send_data(0, order, 3);
ch395_send_data(0,"\r\n",2);
LED1=0;
}
if(order[0]==0xAA && order[1]==0x1A && order[2]==0x02)
{
LED2=1;
ch395_send_data(0, order, 3);
ch395_send_data(0,"\r\n",2);
LED2=0;
}
if(order[0]==0xAA && order[1]==0x1A && order[2]==0x03)
{
LED3=1;
ch395_send_data(0, order, 3);
ch395_send_data(0,"\r\n",2);
LED3=0;
}
if(order[0]==0xAA && order[1]==0x1A && order[2]==0x04)//自定义IP地址
{
ch395_ipaddr[0]=order[3];//0xC0、0xA8、0x01、0x83——>192.168.1.131
ch395_ipaddr[1]=order[4];
ch395_ipaddr[2]=order[5];
ch395_ipaddr[3]=order[6];
ch395_hardware_init();
memcpy(ch395_sockct_config[0].net_config.ipaddr, ch395_ipaddr, sizeof(ch395_sockct_config[0].net_config.ipaddr)); /* 设置静态本地IP地址 */
printf("%d\r\n",ch395_sockct_config[0].net_config.ipaddr[0]);
printf("%d\r\n",ch395_sockct_config[0].net_config.ipaddr[1]);
printf("%d\r\n",ch395_sockct_config[0].net_config.ipaddr[2]);
printf("%d\r\n",ch395_sockct_config[0].net_config.ipaddr[3]);
do
{
ch395q_handler();
}
while (ch395q_Structure.dhcp_status == DHCP_STA); /* 获取DHCP */
ch395_sockct_config[0].socket_enable = CH395Q_ENABLE;
ch395q_socket_config(&ch395_sockct_config[0]);
}
}
ch395q_handler();
}
}
|
|