文件名称:C#上位机源代码
文件大小:1.39MB
文件格式:RAR
更新时间:2021-04-03 11:08:32
C# STM32 UART通信
void Send_toMy(short a,short b,short c,short d,short e,short f) { u8 tbuf[14]; tbuf[0]=0XAA; tbuf[1]=0XAA; tbuf[2]=(a>>8)&0XFF; tbuf[3]=a&0XFF; tbuf[4]=(b>>8)&0XFF; tbuf[5]=b&0XFF; tbuf[6]=(c>>8)&0XFF; tbuf[7]=c&0XFF; tbuf[8]=(d>>8)&0XFF; tbuf[9]=d&0XFF; tbuf[10]=(e>>8)&0XFF; tbuf[11]=e&0XFF; tbuf[12]=(f>>8)&0XFF; tbuf[13]=f&0XFF; UART1PutBuff(tbuf,14); } extern short Rec1,Rec2; u8 USART_RX_BUF[20] = {0}; u8 seri_count=0; u8 state = 0; void USART1_IRQHandler(void) //串口1接收中断 { u8 dat = 0; if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断 { dat =USART_ReceiveData(USART1); //接收到的数据 if(dat == 0xFA && state == 0) { state = 1; seri_count = 0; } else if(dat == 0xFA && state == 1) { state = 2; seri_count = 0; } else if(state == 2) { USART_RX_BUF[seri_count] = dat; seri_count++; Rec1 = (short)((USART_RX_BUF[0] <<8)|(USART_RX_BUF[1])); Rec2 = (short)((USART_RX_BUF[2] <<8)|(USART_RX_BUF[3])); if(seri_count>3) { //UART1PutBuff(USART_RX_BUF,4); seri_count = 0; state = 0; } } } }