must operate carries on the read-write authority, may use chmod to order to carry on the document jurisdiction revision.
It initialization function is as follows:
The initialization will later start to transmit and to receive the data, first for a string of character transmission the monolithic integrated circuit, after the monolithic integrated circuit receives the data, will give the data returns the position machine. But needs to pay attention, because on the position machine speed is much quicker than the monolithic integrated circuit, a time cannot transmit excessively many data, otherwise will have the possibility to cause extremely to transmit the buffer overflow to lose the data, the transmission from now on, but also will have to wait for period of time, will cause the monolithic integrated circuit to transmit completely the data after on position machine, then will carry on the read. 2.2 direct read-write serial port hardware port method
Is using this method time, must have some understandings to the serial port correspondence hardware principle. The PC machine serial port is by general asynchronous transceiver 8250UART (or 16550) is the core constitution, the register base address respectively is 0x3f8(COM1) and 0x2f8(COM2), but also has the register which other some use in controlling. Has very many registers is with the Modem correlation, but carries on the correspondence when the use three-wire system with, only uses the care and the correspondence related register, compares the use to come for the general function compilation serial port equipment driver, the direct read-write correlation register efficiency is higher.
After the initialization, might carry on the data receiving and dispatching, accepted in front of the data had to guarantee the receive data ready, this might judge through 0x2fd D0, transmitted in front of the data must guarantee transmission register for spatial, this might judge through 0x2fd D5, the code was as follows: 3 conclusions
The experiment indicated, this system uses 2 methods all completely have realized between LinuxPC machine and the monolithic integrated circuit the point-to-point correspondence, the method simple reliable, basically in need to use PC machine and the monolithic integrated circuit serial communication situation may use this method. Along with recent years Linux in the domestic application scope daily strength, in domains and so on industry control, data acquisition also will certainly more and more many use Linux, this article to be possible to calculate did is a beneficial attempt. Certainly in the practical application also needs to consider some questions, for instance carry on make a mistake the processing question, may again transmit a verification after the string of character which must transmit and, when receives the returns the verification and with the transmission verification and inconsistent again carries on the reproduction, again for instance uses the RS232C transmitting range is very short, and antijamming ability is very bad, by now needed to transform the main line the difference transmission RS485/RS422。 Moreover, improves slightly, may realize PC machine with the multi-piece 51 monolithic integrated circuit serial communications,
第26页,共37页
because by now used in common a main line, had to give each monolithic integrated circuit to assign an address, then carried on the arbitration by PC machine to the main line, only then obtained the main line right of use the monolithic integrated circuit to be able to carry on the correspondence with on position machine, here no longer gave unnecessary detail.
In brief, this article only was Linux PC and the monolithic integrated circuit serial communication has provided a typical model, had to apply in the actual project, but also needed according to the actual situation concrete consideration, the nimble application, finally to be able to form one reliably based on the Linux platform system.
第27页,共37页
附录B 中文翻译
Linux PC和51系列单片机串行通信的设计
摘 要:介绍了一种采用Linux操作系统的PC机通过串口和MCS-51系列单片机进行通信的设计方法,给出了基本的硬件原理和通信程序设计的2种方法。实验证明,这种方案简单可靠、价格低廉,为Linux在工业控制等领域中的应用提供了一种解决方法。 关键词:Linux;单片机;串行通信;工业控制
单片机性能稳定、价格低廉、功能强大,在智能仪器、工业装备以及日用电子消费品中得到了越来越广泛的应用。在单片机的输入输出控制中,除直接接上小键盘和LCD显示屏等方法外,一般都通过串口和上位机PC进行通信,而后一种方法由于能实现远程控制,并且能够利用PC机强大的数据处理功能以及友好的控制界面,显得尤为重要。在一般的利用PC机对单片机进行控制的场合,都是采用Windows作为上位机的平台,其优点是界面友好,编程和操作都比较容易,缺点是稳定性太差,这对于需要连续数天或数月运行的装置来说,尤其不合适。在要求比较苛刻的场合,一般都采用UNIX工作站作为主控平台,如合肥同步辐射加速器的主控平台采用的是SUN的Solaris工作站系统,然而UNIX工作站昂贵的价格又大大限制了其使用的范围。近年来,随着Linux的迅猛发展,使其逐渐从少数人的玩具变成了主流的操作系统。Linux是遵循GPL协议的免费源代码开放软件,任何人都可以自由的从Internet上取得其源程序,也可在GPL的协议下修改其源代码以适应特定的应用,其运行在普通的PC上,性能稳定,特别适于做工业控制,因此实现Linux和单片机的串行通信非常有意义,他可以是昂贵的UNIX工作站的一种可选的替代方法。 1 硬件原理
目前国内使用较多的为MCS-51系列的单片机,因此选用的单片机实验对象为一片AT89C51,图1是硬件原理图,由于要实现符合RS232C的串行通信,还应该用一片ICL232CPE(MAX232)作为串行通信的电平转换电路。在实验过程中,为了查看通信是否成功,除了让单片机对上位机回送数据外,还在单片机外围扩展了几片锁存器,几个LED发光二极管和几个小键盘。串行通信是采用最简单的TxD,RxD,GND三线制连接,注意TxD和RxD两边应该交叉连线。
上位机是一台普通的PC机,共有2个串行口COM1,COM2,其运行RedHat8.0,实际上,如果不要求运行Gnome或KDE等图形界面,Linux对系统硬件的要求相当低。
实验证明,此电路简单可靠,非常适用于测试串行通信。 2 串行通信程序设计
串行通信程序包括下位机单片机和上位机PC机的程序。单片机接收上位机传来的数据,放到片内RAM里面,再将RAM内数据同时发送到外部扩展锁存器和上位机,由此可以判断通信是否成功。此程序由汇编语言写成,初始化时波特率设置为4 800 b/s,通信方式为8-N-1。
上位机程序的编写是关键部分,因为要对串口硬件设备进行操作,有2种方法,一是利用Linux内核自带的串口驱动程序,另外一种方法就是直接读写串口硬件端口,下面分别介绍。 2.1 利用串口驱动程序的编程方法
第28页,共37页
利用Linux自身的串口驱动程序进行编程,实际上就是调用驱动程序的一系列函数,完成串口通信参数的设置,数据的发送和接收。在这种方法中,Linux给每个串口都分配一个文件索引号,有相应的文件名称,实际上是将硬件设备看成一种特殊的文件,如COM1,COM2对应的文件分别为/dev/ttyS0,/dev/ttyS1,操作这2个串口实际上就是操作这2个文件,而对硬件设备文件的操作与对普通文件的操作并没有什么不同,都可以使用相同的文件I/O调用函数(open,write,read,close),不同之处在于用系统调用open()打开串口得到相应设备的文件描述符以后,先要对其进行初始化,设置一些特定的参数,如波特率、数据位、输入输出方式等,这些参数存放在structtermios中,函数tcsetattr()可以设置串口的structtermios,tcgetattr()可以得到串口的struct termios。设置完通信参数后就可用read和write对串口文件进行读写了。运行程序时要注意用户是否有对要操作的串口文件进行读写的权力,可以用chmod命令进行文件权限修改。
初始化以后就开始发送和接收数据,先将一个字符串发送给单片机,单片机接收到数据后,将数据返回给上位机。但需要注意的是,由于上位机速度比单片机快得多,一次不能发送过多的数据,否则极有可能使发送缓冲区溢出而丢失数据,发送过后,还需等待一段时间,使单片机将数据完全发送到上位机后,再进行读取。 2.2 直接读写串口硬件端口的方法
在使用这个方法的时候,必须对串口通信的硬件原理有一些了解。PC机的串口是由通用异步收发器8250UART为核心构成的,寄存器基地址分别是0x3f8(COM1)和0x2f8(COM2),还有其他的一些用于控制的寄存器。有很多寄存器是与Modem相关的,而在使用三线制进行通信时用不到,只用关心与通信相关的寄存器,比起使用为通用功能编写的串口设备驱动程序来,直接读写相关寄存器效率更高。初始化后,就可以进行数据的收发了,接受数据之前必须保证接收数据就绪,这可以通过0x2fd的D0来判断,发送数据之前必须保证发送寄存器为空,这可以通过0x2fd的D5来判断。 3 结语
实验表明,此系统采用的2种方法都完全实现了LinuxPC机与单片机之间点对点的通信,方 法简单可靠,基本上在需要用到PC机与单片机串行通信的场合均可采用此种方法。随着近年来Linux在国内应用范围的日益壮大,在工业控制、数据采集等领域也必将越来越多的采用Linux,本文可以算作是一个有益的尝试。当然在实际应用中还需要考虑一些问题,比如进行出错处理的问题,可以在一个要发送的字符串后再发送一个校验和,当收到返回的校验和与发送的校验和不一致时再进行重发,再比如所采用的RS232C传输距离很短,并且抗干扰能力很差,这时需要将总线转换成差分传输的RS485/RS422。另外,稍加改进,就可实现PC机与多片51单片机的串行通信,这时由于共用一条总线,必须给每个单片机分配一个地址,然后由PC机对总线进行仲裁,只有获得总线使用权的单片机才能与上位机进行通信,这里不再赘述。
总之,本文只是为Linux PC和单片机串行通信提供了一个典型的范例,要应用到实际的项目中去,还需要根据实际的情况具体考虑,灵活应用,最终才能形成一个可靠的基于Linux平台的系统。
第29页,共37页
第30页,共37页