新闻中心

EEPW首页>嵌入式系统>设计应用> AVR单片机与上位机通信协议的制定

AVR单片机与上位机通信协议的制定

作者: 时间:2013-12-07 来源:网络 收藏

在单片机主循环程序的最前部分进行指令译码

if(Uart_RecvFlag)//接收到命令
{

switch(rx_buffer[1])
{
case 0xAA://单片机状态命令控制;
ucWorkStatue=rx_buffer[2];//指令数据

break;
case 0xDD://PWM值修改指令

OCR2=rx_buffer[2];
break;
case 0xFF://初始温度设定
break;
}
Uart_RecvFlag=0;
}

//随后进行执行指令

switch(ucWorkStatue)
{
case 1://空闲模式
break;
case 2://测量模式,但不输出
break;
case 3://测量模式,由串口输出
break;
case 4://PWM输出测试
break;
default:
break;
}
这样就可以利用串口对单片机进行在线命令控制了;

上行协议的制定和下行协议基本一致!

程序中定义了串口通信输出缓冲区,缓冲区的字长正好为协议的长度;

//串口发送缓冲区变量声明
volatile unsigned char tx_buffer[TX_BUFFER_SIZE];//定义串口发送缓冲区
volatile unsigned char tx_wr_index=0,tx_rd_index=0,tx_counter=0;//rx_wr_index写指针,rx_rd_index读指针,rx_counter缓冲区数据个数

//USART发送函数
void USART_Transmit(unsigned char data)//发送数据函数
{
while(tx_counter==TX_BUFFER_SIZE);//输出缓冲区满,等待
asm("cli");
if(tx_counter||((UCSRA DATA_REGISTER_EMPTY)==0))
{
tx_buffer[tx_wr_index]=data;
if(++tx_wr_index==TX_BUFFER_SIZE)
tx_wr_index=0;
++tx_counter;
}
else
UDR = data;
asm("sei");
}

//发送中断服务程序

ISR(USART_TXC_vect)//USART发送数据中断
{
if(tx_counter)
{
--tx_counter;
UDR=tx_buffer[tx_rd_index];
if(++tx_rd_index==TX_BUFFER_SIZE)
{
tx_rd_index=0;
}
}
}
在C#编写的中,利用串口接收事件响应方法定义

serialPort1.ReceivedBytesThreshold = RECEIVE_LENTH;

在时间响应事件中调用协议分析处理函数serialPortCaculate()来分析协议

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{

this.label_dispzedNum.Invoke(new MethodInvoker(delegate
{ //匿名方法
int inNumSData=0;
try
{
inNumSData = this.serialPort1.BytesToRead;


this.lab_serial_bufin_diplay.Text = inNumSData.ToString();

//串行数据处理
//图像显示
byte dataID = 0x00;
double temp = this.serialPortCaculate(ref dataID);
switch(dataID)
{
case TEMVAL:

break;
default:
this.serialPort1.DiscardInBuffer

()
break;
}

}
catch
{ }


}));
}

///////接收转换协议,接收数据时直接调用
private double serialPortCaculate(ref byte dataID)
{
Byte[] BReceiveTemp = new Byte[RECEIVE_LENTH];
for (int i = 0; i RECEIVE_LENTH; i++)//接收定长数据字符串
{
BReceiveTemp[i] = Convert.ToByte(this.serialPort1.ReadByte());
}
dataID=BReceiveTemp[1];
switch (BReceiveTemp[1])
{
case TEMVAL:
default :
}
}


上一页 1 2 下一页

评论


相关推荐

技术专区

关闭