A communication class for serial port

本文介绍了一个用于串口通信的CSerialPort类,适用于驱动或读取硬件设备。该类易于使用,只需创建实例并调用InitPort即可初始化串口设置。文章提供了示例代码展示如何在软件中使用此类进行数据收发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In the newgroups there are many questions about serial communication so I thought: make it public! It's freeware. The only thing I expect from users is that they drop me a mail. All modifications on this class are free, but please let me know if it solves a bug or adds some good features. Also comment your code and don't let me solve your bugs!

Target:

The class is not intended to use as a baseclass for modemcommunication but more for driving hardware or reading hardware via the serial port.

From the classes included there is only one class important: CSerialPort. The other classes are only there to illustrate the use of this class.

Usage:

In your software you only need to create an instance of the CSerialPort class and call InitPort.
 
  1. // the owner (CWnd) of the port (receives message)
  2.  
  3. BOOL CSerialPort::InitPort(CWnd* pPortOwner,
  4. UINT portnr, // portnumber (1..4)
  5. UINT baud, // baudrate
  6. char parity, // parity
  7. UINT databits, // databits
  8. UINT stopbits, // stopbits
  9. DWORD dwCommEvents, // EV_RXCHAR, EV_CTS etc
  10. UINT writebuffersize) // size of the writebuffer

The dwCommEvents flag can be used for communication with the owner of this class.

The flags can be one of the following (or combined with |):

  • WM_COMM_BREAK_DETECTED A break was detected on input.
  • WM_COMM_CTS_DETECTED The CTS (clear-to-send) signal changed state.
  • WM_COMM_DSR_DETECTED The DSR (data-set-ready) signal changed state.
  • WM_COMM_ERR_DETECTED A line-status error occurred. Line-status errors are CE_FRAME, CE_OVERRUN, and CE_RXPARITY.
  • WM_COMM_RING_DETECTED A ring indicator was detected.
  • WM_COMM_RLSD_DETECTED The RLSD (receive-line-signal-detect) signal changed state.
  • WM_COMM_RXCHAR A character was received and placed in the input buffer.
  • WM_COMM_RXFLAG_DETECTED The event character was received and placed in the input buffer.

Accept the first parameter all parameters are optional. The standard values are:

 
  1. portnr = 1
  2. baud = 19200
  3. parity = 'N'
  4. databits = 8,
  5. stopsbits = 1,
  6. dwCommEvents = EV_RXCHAR | EV_CTS,
  7. nBufferSize = 512);

So the follwing code is enough to make communication possible:

in the header of the owner:

 
  1. CSerialPort m_Serial;

in the code:

 
  1. m_Serial.InitPort(this);
  2. m_Serial.StartMonitoring();

Then the tread that watches the port is started and all events on the port are send to the owner. The receive a character the owner needs a messageentry in the messagemap:

 
  1. BEGIN_MESSAGE_MAP(CCommtestDlg, CDialog)
  2. //{{AFX_MSG_MAP(CCommtestDlg)
  3. ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
  4. ON_MESSAGE(WM_COMM_CTS_DETECTED, OnCTSDetected)
  5. //}}AFX_MSG_MAP
  6. END_MESSAGE_MAP()
and they must be handled:
 
  1. LONG CCommtestDlg::OnCommunication(WPARAM ch, LPARAM port)
  2. {
  3. // do something with the received character
  4.  
  5. return 0;
  6. }

This is it for reading. Writing can be done with WriteChar or WriteToPort

Downloads

Download sample project - 66K


转自:http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c2483/A-communication-class-for-serial-port.htm

### 关于USBX虚拟串口实现与配置 USBX 提供了一个高性能的 USB 堆栈,支持主机、设备以及 OTG 功能,并完全集成到实时操作系统(RTOS)中。对于虚拟串口的功能实现,可以通过 USB 通信类设备(CDC - Communication Device Class)来完成。以下是关于如何在 USBX 中实现和配置虚拟串口的相关信息。 #### 虚拟串口的核心概念 虚拟串口通过 CDC 类协议实现,在 USB 设备模式下模拟传统串行接口的行为。这种设计允许应用程序像操作标准串口一样读写数据,而无需关心底层 USB 协议细节[^2]。 #### 实现步骤概述 虽然不使用步骤词汇,但仍需说明几个关键部分: 1. **初始化 USBX 系统** 使用 `ux_system_initialize` 函数初始化 USBX 控制数据结构并分配必要的内存资源。 ```c ux_system_initialize(ux_stack, sizeof(ux_stack), UX_NULL, 0); ``` 2. **创建和启动 USB 设备** 定义一个 USB 设备实例并通过 `ux_device_create` 和 `ux_device_start` 来激活它。 ```c UX_DEVICE *device; device = ux_device_create(); ux_device_start(device); ``` 3. **注册 CDC 类驱动程序** 在 USBX 中,CDC 类驱动程序需要被显式注册以便处理特定的数据包格式和控制请求。 ```c status = ux_host_class_cdc_acm_register(&cdc_driver_instance); ``` 4. **事件循环处理** 主循环中持续调用 `ux_device_process` 或者对应的主机版本函数以响应 USB 数据流和其他事件。 ```c while (1) { ux_device_process(device); } ``` 以上代码片段展示了基本框架,实际开发过程中还需要根据目标硬件平台调整参数设置[^5]。 #### 示例代码展示 下面给出一段简化版的虚拟串口实现代码作为参考: ```c #include "ux_api.h" // 全局变量定义 UX_THREAD thread_virtual_serial; int main() { // 初始化 USBX 系统 ux_system_initialize(UX_NULL, 0, UX_NULL, 0); // 创建线程用于管理虚拟串口逻辑 ux_thread_create( &thread_virtual_serial, "Virtual Serial Port", application_thread_entry, 0, stack_area_for_thread, sizeof(stack_area_for_thread), THREAD_PRIORITY, THREAD_PRIORITY, UX_NO_TIME_SLICE, UX_DONT_START ); // 启动线程 ux_thread_resume(&thread_virtual_serial); // 主循环保持运行状态 while (true) {} } void application_thread_entry(UX_PARAMETER_TYPE parameter) { UX_DEVICE *device; // 创建并启动 USB 设备 device = ux_device_create(); ux_device_start(device); // 循环处理 USB 事件 while (true) { ux_device_process(device); } } ``` 此示例仅作演示用途,真实场景下的应用可能涉及更复杂的错误检测与恢复机制[^4]。 ### 性能优化建议 为了提高基于 USBX 的虚拟串口性能,可以考虑以下几点: - 尽量减少中断延迟时间; - 对缓冲区大小进行合理规划以平衡吞吐率与内存占用; - 利用 DMA 技术加速大批量数据传输过程; 问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值