1 / 26

Windows CE

Windows CE. 串口通讯编程. 艾博唯科技 Ableway Technology Co. Ltd. 串行协议和 OSI 模型. 应用层. 表示层. 会话层. 传输层. 网络层. 物理层. 应用程序. 串行 API. 数据链路层. 串行设备驱动程序. 串行口. IR 端口. 串行通信概要. 用于串行通信的函数和结构在 Winbase.h 头文件中定义 交换数据方式类似文件读写 函数可以用于打开、关闭和操作串行口,传送和接收数据,以及管理连接等。

rivka
Download Presentation

Windows CE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Windows CE 串口通讯编程 艾博唯科技Ableway Technology Co. Ltd.

  2. 串行协议和OSI模型 应用层 表示层 会话层 传输层 网络层 物理层 应用程序 串行API 数据链路层 串行设备驱动程序 串行口 IR端口

  3. 串行通信概要 • 用于串行通信的函数和结构在Winbase.h头文件中定义 • 交换数据方式类似文件读写 • 函数可以用于打开、关闭和操作串行口,传送和接收数据,以及管理连接等。 • 可以使用点对点协议(PPP)和串行线路Internet协议(SLIP)实现串行连接和拨号连接

  4. 串行通信函数(一) • CreatFile • 打开串行端口 • GetCommStat • 用指定通信设备的当前控制设置填充设备控制块(DCB结构) • SetCommStat • 按照DCB结构的说明配置通信设备。这个函数重新初始化所有硬件和控制设备,但不清空I/O队列 • GetCommTimeouts • 获得指定通信设备上所有读/写操作的超时参数

  5. 串行通信函数(二) • SetCommTimeouts • 设置指定通信设备上所有读/写操作的超时参数 • WriteFile • 向串口写数据 • ReadFile • 从串口读数据 • SetCommMask • 指定为通信设备监视的一组事件 • GetCommMask • 获得指定通讯设备的时间掩码值

  6. 串行通信函数(三) • WaitCommEvent • 等待指定通信设备的事件的发生。事件包含在与设备句柄相关联的事件掩码中 • EscapeCommFunction • 指定通信设备执行扩展功能,通常用于将串口设置为IR模式 • ClearCommBreak • 恢复指定通信设备的字符传输,并设置传输线路为不可中断状态 • ClearCommError • 获得通信错误数据,并报告指定通信设备的当前状态

  7. 串行通信编程(一) 打开串行口 • // Open the serial port. • hPort = CreateFile (lpszPortName, // Pointer to the name of the port • GENERIC_READ | GENERIC_WRITE, • // Access (read-write) mode • 0, // Share mode • NULL, // Pointer to the security attribute • OPEN_EXISTING,// How to open the serial port • 0, // Port attributes • NULL); // Handle to port with attribute • // to copy

  8. 串行通信编程(二) 配置串行口 • // Change the DCB structure settings. • PortDCB.BaudRate = 9600; // Current baud • PortDCB.fBinary = TRUE; // Binary mode; no EOF check • PortDCB.fParity = TRUE; // Enable parity checking • PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control • PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control • PortDCB.fDtrControl = DTR_CONTROL_ENABLE; • // DTR flow control type • PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity • PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx • PortDCB.fOutX = FALSE; // No XON/XOFF out flow control • PortDCB.fInX = FALSE; // No XON/XOFF in flow control • PortDCB.fErrorChar = FALSE; // Disable error replacement • PortDCB.fNull = FALSE; // Disable null stripping • PortDCB.fRtsControl = RTS_CONTROL_ENABLE; • // RTS flow control • PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on • // error • PortDCB.ByteSize = 8; // Number of bits/byte, 4-8 • PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space • PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2

  9. 串行通信编程(三) 配置超时值 • 每次打开串口时都必须用COMMTIMEOUTS结构设置通信超时值。如果不配置,则使用驱动程序的缺省超时值,或者上一个程序的值。 • // Change the COMMTIMEOUTS structure settings. • CommTimeouts.ReadIntervalTimeout = MAXDWORD; • CommTimeouts.ReadTotalTimeoutMultiplier = 0; • CommTimeouts.ReadTotalTimeoutConstant = 0; • CommTimeouts.WriteTotalTimeoutMultiplier = 10; • CommTimeouts.WriteTotalTimeoutConstant = 1000;

  10. 串行通信编程(四) 写端口 • 由于Windows CE不支持异步I/O,所以主线程不应该试图向串口写大量数据,这样容易导致线程阻塞。应用程序可以通过创建多个线程处理读/写操作以模拟异步I/O。为了协调多个线程,可以调用WaitCommEvent函数,直至发生特定的通信事件。 • WriteFile (hPort, // Port handle • &Byte, // Pointer to the data to write • 1, // Number of bytes to write • &dwNumBytesWritten, // Pointer to the number of bytes • // written • NULL)) // Must be NULL for Windows CE

  11. 串行通信编程(五) 读端口 • 通常情况下,读操作是一个独立的线程,它总是随时准备处理达到串口的数据。通信事件通知读线程有数据可读。读线程通常一次读一个字节,直至读完所有字节,然后等待下一个通信事件。 • // Read the data from the serial port. • ReadFile (hPort, //Port handle • &Byte, //Pointer to data to read • 1, //Number of bytes to read • &dwBytesTransferred, //Pointer to number of bytes read • NULL //Must be NULL for Windows CE • );

  12. 串行通信编程(六) 使用通信事件 • 通信事件是当发生重要事件时Windows CE向应用程序发送的通知。应用程序可以用WaitCommEvent函数阻塞线程,直至特定事件发生。用SetCommMask函数可以指定继续处理前必须发生的事件。如果指定了多个事件,则任何一个事件的发生将导致WaitCommEvent函数返回。 • 这种机制使应用程序能够知道数据何时到达串行口。通过等待表示数据到达的通信事件,应用程序可以避免因调用ReadFile函数等待数据到达而阻塞串行口。只有当数据可读时才应该调用ReadFile函数。

  13. 串行通信编程(六) 使用通信事件 • // Specify a set of events to be monitored for the port. • SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD | EV_RING); • while (hPort != INVALID_HANDLE_VALUE) • { • // Wait for an event to occur for the port. • WaitCommEvent (hPort, &dwCommModemStatus, 0); • // Re-specify the set of events to be monitored for the port. • SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RING); • if (dwCommModemStatus & EV_RXCHAR) • { • // Loop for waiting for the data. • do • { • // Read the data from the serial port. • ReadFile (hPort, &Byte, 1, &dwBytesTransferred, NULL); • } while (dwBytesTransferred == 1); • }

  14. 串行通信编程(七) 关闭串口 • if (hCommPort != INVALID_HANDLE_VALUE) • { • // Close the communication port. • if (!CloseHandle (hCommPort)) • { • dwError = GetLastError (); • return FALSE; • } • else • { • hCommPort = INVALID_HANDLE_VALUE; • return TRUE; • } • }

  15. 注意事项(一) • Windows CE是一个基于Unicode的操作系统 • Windows CE不支持Windows下常用的串行通信重叠I/O方式(OVERLAPPED)。但是串行口驱动程序可以使多线程同时访问串行口(即一个线程被ReadFile()调用阻塞的同时另一个线程可以调用WriteFile())。 • 不要使用相对路径。与Windows 2000不一样,Windows CE没有当前目录这个概念,因此,任何路径只是相对于根目录而言的。

  16. 注意事项(二) 1 和 6 短接 4 2 3 3 2 4 1 和 6 短接 5 5 7 8 8 7 Null Modem 线针脚定义 (DB-9 to DB-9)

  17. 使用ActiveSync(零) 预备工作 • 1、确定CE支持ActiveSync • 2、确定串口线的针脚定义如前所示 • 3、确认CE注册表串口设置中的sysint和IO base的设置应该和BIOS里保持一致。

  18. 使用ActiveSync(一) 连接操作一 • 1、连上串口线,启动CE后,在网络连接中新建连接,选择直接电缆连接,设置好BAUT RATE(应与开发机的端口速度保持一致)。

  19. 使用ActiveSync(二) 连接操作二 • 2、在CE下的电脑连接中,把当前连接设为“我的连接”。

  20. 使用ActiveSync(三) 连接操作三 • 3、在开发主机上打开超级终端,在CE下,在运行中执行repllog,如果是连通的状态,应该能在超级终端里看到CLIENT几个字母。如果还连接不通,就要确认以上几点是不是都做对了。

  21. 使用ActiveSync(四) 连接操作四 • 4、在开发主机上打开ActiveSync,点出Get Connected对话框,不要点“下一步”。

  22. 使用ActiveSync(五) 连接操作五 • 5、在CE下,在运行中敲入repllog,然后回车。在开发机上,在Get Connected对话框中点“下一步”,ActiveSync即可连通。

  23. 使用ActiveSync(六) 连接操作六 • 6、在主机上,可以选择建立同步关系。

  24. 在EVC中使用ActiveSync • 1、在开发主机上打开EVC++; • 2、运行Tools->”Configure Platform Manager”; • 3、选择正确的平台和设备; • 4、点“Properties…”; • 5、在Transport和Startup中都选择“Microsoft ActiveSync”; • 6、点“Test”测试连接结果。

  25. 在EVC中使用ActiveSync 连接上后,可以使用多种远程工具

  26. Q & A Q U E S T I O N S A N S W E R S

More Related