[代码段1:串口中断函数]
//
UNUSED(huart);
//收到先累加
Uart1ReciveBuffer[Uart1ReciveBufferIndex] = Uart1IntBuffer[0];
Uart1ReciveBufferIndex++;
//判断收到的内容中是否有回车换行
char Cmd[2] = { "\r\n" };
if (strstr((char *)Uart1ReciveBuffer, Cmd))
{
//判断第13位是回车,第14位是换行
if(Uart1ReciveBuffer[12] == 0x0d || Uart1ReciveBuffer[13] == 0x0a)
{
//判断格式是否非法
if(Uart1ReciveBuffer[0] != 0x2d)
{
int _j = 2;
for (int _i=0; _i < 8; _i++)
{
if (Uart1ReciveBuffer[_i + 1] != 0x20)
{
//1-9
if (Uart1ReciveBuffer[_i + 1] >= 0x31 && Uart1ReciveBuffer[_i + 1] <= 0x39)
{
HidKeyboardSend[_j] = Uart1ReciveBuffer[_i + 1] - 19;
}
//0
if (Uart1ReciveBuffer[_i + 1] == 0x30)
{
HidKeyboardSend[_j] = 0x27;
}
//.
if (Uart1ReciveBuffer[_i + 1] == 0x2e)
{
HidKeyboardSend[_j] = 0x37;
}
//
_j++;
}
}
//收到量具返回后将状态设置成2
CurrentNotify = 2;
}
else
{
//清空缓存区
memset(Uart1ReciveBuffer, 0, sizeof(Uart1ReciveBuffer));
//计数清零
Uart1ReciveBufferIndex = 0;
}
}
else
{
//清空缓存区
memset(Uart1ReciveBuffer, 0, sizeof(Uart1ReciveBuffer));
//计数清零
Uart1ReciveBufferIndex = 0;
}
[代码段2:主循环函数]
if (CurrentNotify == 2)
{
//打印收到的内容
uint8_t HidKeyboardSendTemp[8] = { 0 };
for (int _i = 0; _i < 6; _i++)
{
//请空HID缓冲区
memset(HidKeyboardSendTemp, 0, sizeof(HidKeyboardSendTemp));
//发送一个字节
if(HidKeyboardSend[_i + 2] != 0x00)
{
//
HidKeyboardSendTemp[2] = HidKeyboardSend[_i + 2];
//
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t*)HidKeyboardSendTemp, 8);
HAL_Delay(10);
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t*)HidKeyboardUp, 8);
HAL_Delay(10);
}
}
}
if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == GPIO_PIN_RESET)
{
//发送控制符
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t*)HidKeyboardEnter, 8);
HAL_Delay(10);
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t*)HidKeyboardUp, 8);
HAL_Delay(10);
}
else
{
//发送控制符
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t*)HidKeyboardTab, 8);
HAL_Delay(10);
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t*)HidKeyboardUp, 8);
HAL_Delay(10);
}
//
CurrentNotify = 0; |