使用LUFA作为CDC进行字符串传输

发布于 2024-12-25 21:35:56 字数 1862 浏览 3 评论 0原文

我正在尝试使用我的 AT90USB162(Minimus USB 板)作为 CDC,将常量字符串发送到连接到端口的超级终端。 所以我得到了演示代码 Demos/Device/ClassDriver/VirtualSerial 并做了一些更改:

在 makefile 中:

MCU = at90usb162
BOARD = MINIMUS
F_CPU = 16000000

在 VirtualSerial.h 中:

  • 删除了与 Joystick.h 相关的所有条目,因为AT90USB162没有它
  • 创建了函数SendSpecificString()的头(以交换旧的CheckJoystickMovement(),它与Joystick.h相关)

在VirtualSerial.c中:

从SetupHardware():删除了对Joystick_Init()的调用,因此新代码是(没有注释)。

void SetupHardware(void)
{
    MCUSR &= ~(1 << WDRF);
    wdt_disable();
    clock_prescale_set(clock_div_1);
    LEDs_Init();
    USB_Init();
}

删除了 void CheckJoystickMovement(void) 并创建了 void SendSpecificString(void),基于第一个,但没有操纵杆内容:

void SendSpecificString(void)
{
    char*       ReportString  = "data packet";
    static bool ActionSent    = false;

    if ((ReportString != NULL) && (ActionSent == false))
    {
        ActionSent = true;
        fputs(ReportString, &USBSerialStream);
    }
}

最后在 main() 中:将 CheckJoystickMovement() 调用交换为 void SendSpecificString() 调用。

int main(void)
{
    SetupHardware();
    CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);
    LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
    sei();

    for (;;)
    {
        SendSpecificString();
        CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
        CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
        USB_USBTask();
    }
}

因此,代码构建完成后,我将其烧录到 AT90USB162 中并启用它。 comport(在我的例子中为#6)出现,我可以从超级终端连接到它(我大部分时间都使用 HypoTerminal,但使用 Microsoft Hyperterminal 会出现相同的结果)。当我连接到comport时,终端没有按预期卡住,但是我还预计字符串ReportString =“data packet”会连续出现在超级终端中,但实际上什么也没有出现。那么,我会错过什么?

谢谢。

I'm trying to use my AT90USB162 (Minimus USB board) as a CDC for sending a constant string to an hyperterminal connected to a comport.
So I got the demo code Demos/Device/ClassDriver/VirtualSerial and made some changes:

In makefile:

MCU = at90usb162
BOARD = MINIMUS
F_CPU = 16000000

In VirtualSerial.h:

  • Removed all entries related to Joystick.h, since AT90USB162 does not have it
  • Created the header of functon SendSpecificString() (in exchange of the old CheckJoystickMovement(), which was related to the Joystick.h)

In VirtualSerial.c:

From SetupHardware(): removed call to Joystick_Init(), so the new code is (without comments).

void SetupHardware(void)
{
    MCUSR &= ~(1 << WDRF);
    wdt_disable();
    clock_prescale_set(clock_div_1);
    LEDs_Init();
    USB_Init();
}

Removed the void CheckJoystickMovement(void) and created the void SendSpecificString(void), based on the first, but without the joystick stuffs:

void SendSpecificString(void)
{
    char*       ReportString  = "data packet";
    static bool ActionSent    = false;

    if ((ReportString != NULL) && (ActionSent == false))
    {
        ActionSent = true;
        fputs(ReportString, &USBSerialStream);
    }
}

And finally in main(): exchanged the CheckJoystickMovement() call to the void SendSpecificString() call.

int main(void)
{
    SetupHardware();
    CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);
    LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
    sei();

    for (;;)
    {
        SendSpecificString();
        CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
        CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
        USB_USBTask();
    }
}

So, code builds and I burn into AT90USB162 and enable it. The comport (#6 in my case) appears and I can connect to it from hyperterminal (I'm using HypoTerminal most of the times, but same result occurs with Microsoft Hyperterminal). When I connect to the comport, the terminal does not get stuck as expected, however I expected also that the string ReportString = "data packet" would appear continuously in hyperterminal, but actually nothing appears. Then, what would have I missed?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聆听风音 2025-01-01 21:35:56

我刚刚发现问题不在于 fputs 或 CDC_Device_SendString 调用。条件循环不是必需的,在本例中就足够了

void SendSpecificString(void)
{
    char*       ReportString  = "data packet\r\n";
    fputs(ReportString, &USBSerialStream);
}

I just discovered that the problem was not with the fputs or the CDC_Device_SendString calls. The condition loop was not necessary, in this case is enough to make

void SendSpecificString(void)
{
    char*       ReportString  = "data packet\r\n";
    fputs(ReportString, &USBSerialStream);
}

That's it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文