简体   繁体   English

从串口读取数据

[英]Read data from serial port

How do i read data from serial port using C ? 如何使用C从串口读取数据? and then again transfer the data to modem ? 然后再将数据传输到调制解调器? I am using RS 232 cable for serial communication ... 我使用RS 232电缆进行串行通信......

A lot of useful information about dealing with serial ports both on PCs and on the device end of the wire can be found at Jan Axelson's site for the book Serial Port Complete . 有关在PC上和设备端设备端口处理串口的许多有用信息都可以在Jan Axelson的网站上找到串行端口完成一书。

Actual interaction with the port will be highly platform-specific. 与端口的实际交互将是高度特定于平台的。 On *nix of most flavors you open a device named something like /dev/ttya and use read() , write() , and ioctl() to read, write, and configure the port. 在大多数版本的* nix上,您打开一个名为/dev/ttya并使用read()write()ioctl()来读取,写入和配置端口。

On Windows, you open a file named COM1 (or \\\\.\\COM1 on some flavors) with the CreateFile() function, and then get it configured and happy with functions like SetCommState() and the DCB structure. 在Windows上,使用CreateFile()函数打开一个名为COM1 (或某些版本的\\\\.\\COM1 )的CreateFile() ,然后对其进行配置, SetCommState()DCB结构等函数感到满意。 The details are similar in broad outline to the things you need to do on *nix, but organized completely differently. 详细信息与您在* nix上需要做的事情大致相似,但组织完全不同。 You can find the whole discussion of configuring and using COM ports at MSDN . 您可以在MSDN上找到有关配置和使用COM端口的完整讨论。

On the embedded device, you will either be directly accessing the hardware registers of the UART or interacting with an RTOS. 在嵌入式设备上,您可以直接访问UART的硬件寄存器或与RTOS进行交互。 Either way, there is essentially no way to make the code on the device end broadly portable. 无论哪种方式,基本上都没有办法使设备端的代码可以广泛移植。

First of all this is very dependant on OS you use. 首先,这非常依赖于您使用的操作系统。

For *nix type you usually open serial port device like normal file (/dev/ttyS0, for example), and start reading to or writing from it. 对于* nix类型,您通常打开串行端口设备,例如普通文件(例如/ dev / ttyS0),并开始读取或写入它。 As for windows, i can give you advise to search for a reliable library, because windows serial communication can be a real nightmare. 至于Windows,我可以建议您搜索一个可靠的库,因为Windows串行通信可能是一个真正的噩梦。 Even more, there may be some differences between old windows versions and never ones. 更重要的是,旧的Windows版本和从未版本的版本之间可能存在一些差异。

Personally, because i'm wxWidgets user i use wxCTB. 就个人而言,因为我是wxWidgets用户我使用wxCTB。 You should look for library that will suit IDE you are using. 您应该寻找适合您正在使用的IDE的库。

some free of charge links ;) www.robbayer.com/files/serial-win.pdf www.lookrs232.com/com_port_programming/ 一些免费链接;)www.robbayer.com/files/serial-win.pdf www.lookrs232.com/com_port_programming/

In the embedded world your compiler will have some mechanism (usually compiler specific) that allows it to access the hardware registers that are used to implement the serial port function either within the device or in an external memory or IO mapped device. 在嵌入式领域,您的编译器将具有某种机制(通常是编译器特定的),允许它访问用于在设备内或外部存储器或IO映射设备中实现串行端口功能的硬件寄存器。 These register definitions are often provided by the tool or processor manufacturer and it is then just a case of writing the configuration control values to the serial port registers to set its operating mode and then reading the data out of tje receive buffer register. 这些寄存器定义通常由工具或处理器制造商提供,然后只是将配置控制值写入串行端口寄存器以设置其操作模式然后从接收缓冲寄存器中读取数据。

Sounds simple but, on the MSP-430 mailing list USART programming questions are the most common single topic. 听起来很简单但是,在MSP-430邮件列表中,USART编程问题是最常见的单一主题。

To expand on Ian's answer, a typical embedded c compiler will have a compiler directive or a statement whose parameters control settings such as baud rate, and which associates the UART (or other I/O hardware) with a stream. 为了扩展Ian的答案,典型的嵌入式c编译器将具有编译器指令或语句,其参数控制波特率等设置,并将UART(或其他I / O硬件)与流相关联。 You can specify a stream name (if you have more than one serial port) or use the defaults. 您可以指定流名称(如果您有多个串行端口)或使用默认值。 Then you can access the serial port with the usual c character routines, and there will be an interrupt when it receives each character. 然后,您可以使用通常的c字符例程访问串行端口,并在收到每个字符时出现中断。 So your interrupt routine reads each character and puts it in a buffer. 因此,您的中断例程会读取每个字符并将其放入缓冲区。

In the Windows land, you better find a library that makes all the magic happen. 在Windows领域,你最好找到一个让所有魔法发生的库。

Personally I'm using Borland C++ to put together small utility to talk to my hardware (bootloaders, configuration and testing) and I'm very happy with the TurboPower Async Professional Library that works both in Delphi and BCC and is open source (Mozilla Public License 1.1). 就个人而言,我正在使用Borland C ++将小实用程序与我的硬件(引导加载程序,配置和测试)组合在一起,我对TurboPower Async Professional Library非常满意,该在Delphi和BCC中均可使用,并且是开源的(Mozilla Public)许可证1.1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM