1-1 串口发送端(stm32)
1字符串发送
motor_position = Read_Encoder_Angle(Encoder);
sensor_position = Get_Adc_Average_Angle(Adc);
motor_velocity = Read_Encoder_Speed(Encoder);
sensor_velocity = Get_Adc_Average_Speed();
pc_fil = LPF(motor_position, pc_fil,0.2f);
vc_fil = LPF(motor_velocity, vc_fil,0.2f);
ec_fil = LPF(sensor_position,ec_fil,0.2f);
wc_fil = LPF(sensor_velocity,wc_fil,0.2f);
sprintf(data_str, "%-8.4f, %-8.4f, %-8.4f, %-8.4f\n", pc_fil, ec_fil, vc_fil, wc_fil);
Usart_SendString(USART1, data_str);
motor_velocity = 0.123;
2 16进制传输(整型)
data_array[0] = 0x12;
data_array[1] = 0x34;
data_array[2] = (int)Encoder & 0xFF;
data_array[3] = ((int)Encoder >> 8) & 0xFF;
data_array[4] = (int)Adc & 0xFF;
data_array[5] = ((int)Adc >> 8) & 0xFF;
data_array[6] = 0x56;
data_array[7] = 0x78;
for(uint8_t i = 0 ; i < 8; i++)
{
USART_SendData(USART1, *(data_array + i));
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
}
3 16进制传输(整型和浮点型)
void FloatToByte(float floatNum, unsigned char* byteArry) {
char* pchar = (char*)&floatNum;
for (int i = 0; i < sizeof(float); i++) {
*byteArry = *pchar;
pchar++;
byteArry++;
}
}
FloatToByte(motor_velocity, byteArry);
data_array[0] = 0x12;
data_array[1] = 0x34;
data_array[2] = (int)motor_position & 0xFF;
data_array[3] = ((int)motor_position >> 8) & 0xFF;
data_array[4] = byteArry[0];
data_array[5] = byteArry[1];
data_array[6] = byteArry[2];
data_array[7] = byteArry[3];
data_array[8] = 0x56;
data_array[9] = 0x78;
for(uint8_t i = 0 ; i < sizeof(data_array); i++)
{
USART_SendData(USART1, *(data_array + i));
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
}
4 仅发送浮点型小数
void FloatToByte(float floatNum, unsigned char* byteArry) {
char* pchar = (char*)&floatNum;
for (int i = 0; i < sizeof(float); i++) {
*byteArry = *pchar;
pchar++;
byteArry++;
}
}
FloatToByte(motor_velocity, byteArry);
for(uint8_t i = 0 ; i < sizeof(float); i++)
{
USART_SendData(USART1, *(byteArry + i));
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
}
1-2 串口接收端-python
接收数据:编码器(整型)、角位移传感器(整型)
def read_serial_one_data_encoder_adc(ser):
global receive_result
BUF_SIZE = 8
buf = bytearray([0x12, 0x34, 0x00, 0x00, 0x00, 0x00, 0x56, 0x78])
c1 = ib = flag = 0
while True:
R = ser.read(1)
if R == b'':
print("Read Fail")
ser.close()
break
c = int.from_bytes(R, byteorder='big')
if flag > 0:
if ib < BUF_SIZE:
buf[ib] = c
ib += 1
if ib == 8:
if buf[6] == 0x56 and buf[7] == 0x78:
Encoder = (buf[3] << 8) + buf[2]
Adc= (buf[5] << 8) + buf[4]
receive_result = [Encoder, Adc]
break
else:
print("CRC Fail")
flag = 0
if flag == 0:
if c1 == 0x12 and c == 0x34:
flag = 1
ib = 2
c1 = c
return receive_result
接收数据:编码器(整型)、角位移传感器(浮点型)
def read_serial_one_data_motor_position_velocity(ser):
global receive_result
BUF_SIZE = 10
buf = bytearray([0x12, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x56, 0x78])
c1 = c2 = ib = flag = 0
while True:
R = ser.read(1)
if R == b'':
print("Read Fail")
ser.close()
break
c = int.from_bytes(R, byteorder='big')
if flag > 0:
if ib < BUF_SIZE:
buf[ib] = c
ib += 1
if ib == 8:
if buf[8] == 0x56 and buf[9] == 0x78:
motor_position = (buf[3] << 8) + buf[2]
motor_veclocity = Byte2Float(buf[4:8])
receive_result = [motor_position, motor_veclocity]
break
else:
print("CRC Fail")
flag = 0
if flag == 0:
if c1 == 0x12 and c == 0x34:
flag = 1
ib = 2
c1 = c
return receive_result
2-1 串口发送端python
action = bytearray([0x12, 0x34, 0x00, 0x00, 0x00, 0x56, 0x78])
action_ = int(control_motor(result[0], result[1]))
print("action_", action_)
action[0] = 0x2D
action[1] = 0x01
if action_ < 0:
action[2] = 0x45
action[3] = action_ & 0xFF
action[4] = (action_ >>8) & 0xFF
action[5] = 0x0d
action[6] = 0x0a
for byte in action:
ser.write(byte.to_bytes(1, 'big'))
2-2 串口接收端stm32
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
Res = USART_ReceiveData(USART1);
printf("%02X", Res);
if((USART_RX_STA&0x8000)==0)
{
if(USART_RX_STA&0x4000)
{
if(Res!=0x0A){
USART_RX_STA=0;
memset(USART_RX_BUF,0,USART_REC_LEN);
}
else{
USART_RX_STA|=0x8000;
if(USART_RX_BUF[0] == 0x2D && USART_RX_BUF[1] == 0x01 )
{
float value = 0;
int16_t sign = 1;
if(USART_RX_BUF[2] == 0x45)
{
sign = -1;
}
value = (USART_RX_BUF[4] << 8) + USART_RX_BUF[3];
action = sign * value;
USART_RX_STA = 0;
memset(USART_RX_BUF,0,USART_REC_LEN);
}
else{
USART_RX_STA = 0;
memset(USART_RX_BUF,0,USART_REC_LEN);
}
}
}
else
{
if(Res==0x0D)
USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))
{
USART_RX_STA=0;
memset(USART_RX_BUF,0,USART_REC_LEN);
}
}
}
}
}
}