Windows下串口操作
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Restoring a configuration(获取并保存 获取并保存 串口当前状态) 串口当前状态
保存当前设备状态—多任 务程序设计规则: DCB oldDcbConfig =config_;//??
DCB structure
Modifying a configuration
When you already have serial port configuration in the DBC format, you have to modify parameters a bit. Following code shows the parameters modified:
The serial port can be implemented by several techniques such as ActiveX, access I/O and file operation. This class explains the use of serial port on Win32 platform by file operation technique. The programmer can use kernel32.lib library that is provided with the Microsoft Visual C++ Version 6.0. In Microsoft Windows (2000, Me, XP and 95/98), serial port can be treated as a file. Therefore it‘s possible to open a serial port by using Windows file-creating function. (串口通讯可以 使用很多技术实现,如使用ActiveX,直接I/O控制和文件 操作。这里讲述Win32下基于文件操作的方式。程序员可 以使用kernel32.lib 对串口进行控制。在Windows里, 串口可以视作一个文件,从而可以象操作文件一样操作串 口。)
Receive/Send data(数据收发) (数据收发)
Sending data Receiving data
Closing a serial port (关闭串口)
Initial/Open serial port
The first step in opening a serial port is initiation or setting a serial port‘s configuration. The purpose of this is to create the serial port agent. All throughout the article we are going to use a file handle as serial port agent.(打开串口首先要创建 与串口关联的文件句柄,并进行配置。以 后的操作将都使用文件句柄作为串口操作 的中介。)
键盘交互 和辅助线程交互 决策 Task0 Task1 创建辅助线程处理串口操作 no Task2 Task_Port quit Show data yes 串口初始化 磁盘 结束 port 读写后处理 写 w 读 磁盘 r port
退出前预处理
结束
Creating a Serial communication on Win32 (使用WIN32建立串口通讯)
BYTE ByteSize: 字节的大小
Number of bits/byte, 4-8 (default = 8)
Note: Recommend that programmers use default value for typical communication. As shown in figure, Watch Dialog Box shows the default values that are used for typical communication.
Objects:对象,核心对象(kernel object)
HANDLE:句柄
附加说明
Communications Resources
The CreateFile function can create a handle to a communications resource, such as the serial port COM1. For communications resources, the dwCreationDisposition parameter must be OPEN_EXISTING, and the hTemplate parameter must be NULL. Read, write, or read/write access can be specified, and the handle can be opened for overlapped I/O. For more information about communications, see Communications.
Setting a Time-Out communication
ReadIntervalTimeout(读字符间时间间 读字符间时间间 隔)
Specifies the maximum time, in milliseconds, allowed to elapse between the arrival of two characters on the communications line. During a ReadFile operation, the time period begins when the first character is received. If the interval between the arrival of any two characters exceeds this amount, the ReadFile operation is completed and any buffered data is returned. A value of zero indicates that interval time-outs are not used.
A male DE9connector used for a serial port on an PC style computer.
Windows下串口操作
RS232自Electronic Industries Alliance (EIA,电子工业 联盟) 发布RS-232-C 以来,得到了广泛的应用。其在低速 有线短距离通讯一直发挥着巨大的作用。随着PC工业、科研 应用的日见普及,RS232的应用也在不停的发展,它的编程 模式也在发展。
Config DCB
DWORD BaudRate: 波特率
Current baud rate (default = 9600)
BYTE StopBits: 停止位
0,1,2 = 1, 1.5, 2 (default = 0)
来自百度文库
BYTE Parity: 校验
0-4= no, odd, even, mark, space (default = 0)
Creating a port handle(创建端口(文件)句柄) 创建端口(文件)句柄 创建端口
The serial port's handle is a handle that can be used to access the object of serial port. The function that is used to create the serial port handle is the CreateFile function. The following code shows the function that is used to create a handle:
handlePort_ = CreateFile( portName, // Specify port device: default "COM1" GENERIC_READ | GENERIC_WRITE, // Specify mode that open device. 0, // the devide isn't shared. NULL, // the object gets a default security. OPEN_EXISTING, // Specify which action to take on file. 0, // default. NULL); // default.
RS-232-C
Electronic Industries Alliance (EIA) standard RS-232-C [3] as of 1969 defines:
Electrical signal characteristics such as voltage levels, signaling rate, timing and slew-rate of signals, voltage withstand level, short-circuit behavior, maximum stray capacitance and cable length Interface mechanical characteristics, pluggable connectors and pin identification Functions of each circuit in the interface connector Standard subsets of interface circuits for selected telecom applications The standard does not define bit rates for transmission, although the standard says it is intended for bit rates lower than 20,000 bits per second.
DOS Vs Win
程序初始化 键盘交互 no 需要读写串口 yes 串口初始化 磁盘 写 port 读写后处理 w 读 磁盘 r port DOS下单用户单任务,(实际 WIN2000及以下的都是单用户), 由用户程序负责时间的协调。
结束
程序初始化
Windows下多任务(多线 程,thread)
CreateFile
Consoles 控制台 Communications resources 通讯资源 Directories (open only) 目录 Disk devices (Windows NT/2000 only) 磁盘设备 Files 文件 Mailslots 邮件槽 Pipes 管道
Set a configuration(设置串口到特定配置 设置串口到特定配置) 设置串口到特定配置
The next step is the storage of new configuration that is modified already into device control. Call SetCommState API function to store the configuration. The SetCommState function configures a communications device according to the specifications in a device-control block (a DBC structure). The function reinitializes all hardware and control settings, but it does not empty output or input queues. Following code shows storage of a new configuration:
Basic process(基本过程)
Initial/Open serial port communication(打开并初始化串口) (打开并初始化串口)
Creating a port handle Restoring a configuration (DCB) Modifying a configuration Storing a configuration Setting a Time-Out communication