STM32F的232口简易MODBUS-RTU通讯程序(2)
- UID
- 1061697
|
STM32F的232口简易MODBUS-RTU通讯程序(2)
void Modbus_Function_6(void)
{
u16 tempdress = 0;
u8 tx_flat = 0;
u16 crcresult;
tempdress = (Uart0_rev_buff[2]<<8) + Uart0_rev_buff[3];
switch(tempdress)
{
case 0x0126:
{
Mkzt_bz = (Uart0_rev_buff[4]<<8) + Uart0_rev_buff[5];
if(user_day > 0)
{
tx_flat = 1;
}
}break;
case 0x012A:
{
OutX_current = (Uart0_rev_buff[4]<<8) + Uart0_rev_buff[5];
if(user_day > 0)
{
tx_flat = 1;
}
}break;
case 0x012C:
{
Jc_voltage = (Uart0_rev_buff[4]<<8) + Uart0_rev_buff[5];
if(user_day > 0)
{
tx_flat = 1;
}
}break;
case 0x012E:
{
Fc_voltage = (Uart0_rev_buff[4]<<8) + Uart0_rev_buff[5];
if(user_day > 0)
{
tx_flat = 1;
}
}break;
case 0x01EE:
{
user_day = (Uart0_rev_buff[4]<<8) + Uart0_rev_buff[5];
tx_flat = 1;
//eeprom_write_word (&user_day_eep,user_day);
}break;
default: //命令码无效不应答
{
tx_flat = 0;
}
}
if(tx_flat == 1)
{
Uart0_send_buff[0] = Com0_id;
Uart0_send_buff[1] = 0x06;
Uart0_send_buff[2] = Uart0_rev_buff[2];
Uart0_send_buff[3] = Uart0_rev_buff[3];
Uart0_send_buff[4] = Uart0_rev_buff[4];
Uart0_send_buff[5] = Uart0_rev_buff[5];
Uart0_send_counter = 6;
//UCSRB |= (1<<TXCIE)|(1<<TXEN);//发送、发送中断允许
crcresult = getCRC16(Uart0_send_buff,Uart0_send_counter);
Uart0_send_buff[Uart0_send_counter] = crcresult & 0xff;
Uart0_send_buff[Uart0_send_counter+1] = (crcresult >> 8) & 0xff;
Uart0_send_counter = Uart0_send_counter+2;
Uart0_send_pointer = Uart0_send_buff;
USART_SendData(USART1, *Uart0_send_pointer++);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
}
}
/////////////////////////////////////////////////////////////
void Com0_Communication(void)
{
s8 i =0;
if(Uart0_rev_comflag == 1)//接收完成标志=1处理,否则退出
{
if(Uart0_rev_buff[0] == Com0_id)//地址错误不应答
{
unsigned short crcresult;
unsigned char temp[2];
crcresult = getCRC16(Uart0_rev_buff,Crc_counter-2);
temp[1] = crcresult & 0xff;
temp[0] = (crcresult >> 8) & 0xff;
if((Uart0_rev_buff[Crc_counter-1] == temp[0])&&(Uart0_rev_buff[Crc_counter-2] == temp[1]))//crc校验错误不应答
{
//SETBIT(PORTC,PC6);
Delay(1);
switch(Uart0_rev_buff[1])
{
case 0x03:
{
if(user_day > 0)
{
Modbus_Function_3();
}
}
break;
case 0x06:
{
Modbus_Function_6();
}
break;
}
}
}
Uart0_rev_comflag = 0;
for(i = 100;i > -1;i--)
{
Uart0_rev_buff = 0;
}
}
}
/*******************************************************************************
* Function Name : Delay
* Description : Inserts a delay time.
* Input : nCount: specifies the delay time length.
* Output : None
* Return : None
*******************************************************************************/
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
////////////////////////////////////////////////////////////////////////// |
|
|
|
|
|