我用MSP430F5529做一个数据格式信息的判断,想得到的结果是;
07 05 FF 00 FF
06 05 01 6B FF 06 05 31 1E FF
06 05 01 68 FF 06 05 31 1C FF
‘
‘
‘
07 05 FF 01 FF
其中 07表示控制信息标志位,05 表示数据包的长度;06表示数据信息标志位;ff 00 表示开始接收数据标志位;ff 01 表示数据接收完成后发送的标志位;5d 0d 和64 36 为接收到的数据;
实际的到的结果是:
07 05 FF 00 FF 06 05 01 6B FF 06 05 31 1E FF
07 05 FF 00 FF 06 05 01 68 FF 06 05 31 1C FF
07 05 FF 00 FF 06 05 01 6A FF 06 05 31 1B FF
07 05 FF 00 FF 06 05 01 69 FF 06 05 31 1D FF
07 05 FF 00 FF 06 05 01 68 FF 06 05 31 1C FF
07 05 FF 00 FF 06 05 01 6A FF 06 05 31 1B FF
07 05 FF 00 FF 06 05 01 68 FF 06 05 31 1C FF
07 05 FF 00 FF 06 05 01 65 FF 06 05 31 1D FF
07 05 FF 00 FF 06 05 01 6B FF 06 05 31 26 FF
07 05 FF 00 FF 06 05 01 66 FF 06 05 31 1C FF
07 05 FF 00 FF 06 05 01 69 FF 06 05 31 19 FF
07 05 FF 00 FF 06 05 01 6A FF 06 05 31 19 FF
07 05 FF 00 FF 06 05 01 69 FF 06 05 31 1B FF
07 05 FF 00 FF 06 05 01 68 FF 06 05 31 1F FF
07 05 FF 00 FF 06 05 01 69 FF 06 05 31 1A FF
07 05 FF 00 FF 06 05 01 69 FF 06 05 31 19 FF
以下是我实现的函数:
unsigned int Rs232_Start_Msg_Cortrol(void)
{
return RS_232_SEND_MSG_START_COLLECT; //开始信息判断 0xff00
}
void Rs232_Start_Msg_Process(void)
{
unsigned char i,j;
RS232_SEND_MSG_ARRAY[0] = RS_232_SEND_MSG_CONTROL; //控制标志位 0x07
RS232_SEND_MSG_ARRAY[1] = 5; //数据长度 0x05
RS232_SEND_MSG_ARRAY[4] = End_Flag_Value; //结束标志位0xff
if(un_rs232_msg.st_rs232_msg.type == 0x01) //从上位机进行数据开始的判断;接收PC开始标志
{
RS232_SEND_MSG_ARRAY[2] = Rs232_Start_Msg_Cortrol()>>8; //如果位为开始,则发送0xff00,作为开始标志
RS232_SEND_MSG_ARRAY[3] = Rs232_Start_Msg_Cortrol();
for(i=0;i<5;i++)
{
if((RS232_SEND_MSG_ARRAY[i]&0xff00)== 0)
{
j=RS232_SEND_MSG_ARRAY[i];
RS232_send_one_byte(j); //发送一个字节数据
}
else
{
j=RS232_SEND_MSG_ARRAY[i];
RS232_send_one_byte(j);
j=RS232_SEND_MSG_ARRAY[i]>>8;
RS232_send_one_byte(j);
}
}
}
}
unsigned int Rs232_End_Msg_Cortrol(void)
{
return RS_232_SEND_MSG_END_COLLECT;
}
void Rs232_End_Msg_Process(void)
{
unsigned char i,j;
RS232_SEND_MSG_ARRAY[0] = RS_232_SEND_MSG_CONTROL; //控制标志位 0x07
RS232_SEND_MSG_ARRAY[1] = 5; //控制信息长度
RS232_SEND_MSG_ARRAY[4] = End_Flag_Value; //结束标志 0xff
for(g_frq_end_flag = g_frq_start ; g_frq_end_flag <= g_frq_end ; g_frq_end_flag += g_frq_interval);
if(g_frq_end_flag == g_frq_end)//扫描频率的判断标志,与结束频率是否相等
{
RS232_SEND_MSG_ARRAY[2] = Rs232_End_Msg_Cortrol()>>8; //将结束控制标志位0xff01发送给PC
RS232_SEND_MSG_ARRAY[3] = Rs232_End_Msg_Cortrol();
for(i=0;i<5;i++)
{
if((RS232_SEND_MSG_ARRAY[i]&0xff00)== 0)
{
j=RS232_SEND_MSG_ARRAY[i];
RS232_send_one_byte(j);
}
else
{
j=RS232_SEND_MSG_ARRAY[i];
RS232_send_one_byte(j);
j=RS232_SEND_MSG_ARRAY[i]>>8;
RS232_send_one_byte(j);
}
}
}
}
char Judge_Msg_Or_Control(void)
{
if((g_ad1_value != 0) && (g_ad2_value == 0) )
{
return RS_232_SEND_MSG_AMP ; //返回幅值信息 0x04
}
else if((g_ad1_value != 0) && (g_ad2_value != 0))
{
return RS_232_SEND_MSG_PHASE; //返回相位信息0x06
}
else
{
return RS_232_SEND_MSG_REEOR; //如果都没有则返回0x00错误信息
}
}
void Rs232_Data_Msg_Process(void)
{
unsigned char i,j;
RS232_SEND_MSG_ARRAY[0] = Judge_Msg_Or_Control();
RS232_SEND_MSG_ARRAY[1] = 5;
RS232_SEND_MSG_ARRAY[4] = End_Flag_Value;
switch(RS232_SEND_MSG_ARRAY[0])
{
case 0x05:
RS232_SEND_MSG_ARRAY[2] = g_ad1_value>>8;
RS232_SEND_MSG_ARRAY[3] = g_ad1_value;
for(i=0;i<5;i++)
{
if((RS232_SEND_MSG_ARRAY[i]&0xff00)== 0)
{
j=RS232_SEND_MSG_ARRAY[i];
RS232_send_one_byte(j);
}
else
{
j=RS232_SEND_MSG_ARRAY[i];
RS232_send_one_byte(j);
j=RS232_SEND_MSG_ARRAY[i]>>8;
RS232_send_one_byte(j);
}
}
break;
case 0x06:
RS232_SEND_MSG_ARRAY[2] = g_ad1_value>>8;
RS232_SEND_MSG_ARRAY[3] = g_ad1_value;
for(i=0;i<5;i++)
{
if((RS232_SEND_MSG_ARRAY[i]&0xff00)== 0)
{
j=RS232_SEND_MSG_ARRAY[i];
RS232_send_one_byte(j);
}
else
{
j=RS232_SEND_MSG_ARRAY[i];
RS232_send_one_byte(j);
j=RS232_SEND_MSG_ARRAY[i]>>8;
RS232_send_one_byte(j);
}
}
RS232_SEND_MSG_ARRAY[2] = g_ad2_value>>8;
RS232_SEND_MSG_ARRAY[3] = g_ad2_value;
for(i=0;i<5;i++)
{
if((RS232_SEND_MSG_ARRAY[i]&0xff00)== 0)
{
j=RS232_SEND_MSG_ARRAY[i];
RS232_send_one_byte(j);
}
else
{
j=RS232_SEND_MSG_ARRAY[i];
RS232_send_one_byte(j);
j=RS232_SEND_MSG_ARRAY[i]>>8;
RS232_send_one_byte(j);
}
}
break;
case 0x00:
RS232_SEND_MSG_ARRAY[2] = (0x00>>8);
RS232_SEND_MSG_ARRAY[3] = 0x00;
for(i=0;i<5;i++)
{
if((RS232_SEND_MSG_ARRAY[i]&0xff00)== 0)
{
j=RS232_SEND_MSG_ARRAY[i];
RS232_send_one_byte(j);
}
else
{
j=RS232_SEND_MSG_ARRAY[i];
RS232_send_one_byte(j);
j=RS232_SEND_MSG_ARRAY[i]>>8;
RS232_send_one_byte(j);
}
}
break;
default:
break;
}
}
void main()
{
... //初始化
while (1)
{
WDTCTL = WDTPW + WDTHOLD;
if(g_frq_time_flag != 0) //检查频率时间标志是否为0 //定时扫描
{
g_frq_time_flag = 0;
g_ad1_value = ADC161S626_1_Read(); //ad1返回数据
g_ad2_value = ADC161S626_2_Read(); //ad2 返回数据
Rs232_Start_Msg_Process(); //控制开始信息
Rs232_Data_Msg_Process(); //数据信息
Rs232_End_Msg_Process(); //控制结束信息
ad_9832_process(); //接收PC机传输的数据信息
}
}
} |