




已阅读5页,还剩37页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
附件1:外文资料翻译译文串行通信串行通信的设备是最古老的沟通机制之一。从IBM个人电脑和兼容式电脑的时代开始,几乎所有的计算机都配有一个或多个串行端口和一个并行端口。顾名思义,一个串行端口发送和接收串行数据,一次一位数据。相反,一个并行端口一次发送和接收8位数据,使用8个单独的电线。提示:要使串行通信工作,你只需要一根三根线的电缆1根发送,1根用来接收,1根接地。对于并行通信,你需要采用8条导线。尽管相对较慢的传输速度远低于并行端口,串行端口通信依然因为它简单的设备和高的成本效益而是一个受欢迎的连接选项。图2-1显示了设备的使用串口连接部分到计算机。使用串行端口,你可以连接到调制解调器,鼠标或设备等作为一个桥梁/路由器配置的目的图1:一些常见的串行设备,调制解调器,鼠标和路由器虽然今天的消费产品中在串行连接的地方使用USB连接,但还有很多的设备使用串行端口作为与外部世界的唯一连接。在这一章中,你将学习如何使用基于NET Framework2.0和NET Compact Framework2.0的串行通信方式来与其他的串行设备进行通信。并且,你将建立3个阐述了如何使用串行通信的项目。第一个项目是一个请求允许两台电脑(连接使用一个串行电缆或蓝牙连接)进行通信的通信申请。在此应用程序的基础上,你可以进行扩展从而与手机等其他外部串行设备进行通信。你将学习如何使用AT命令,以编程的方式控制你的手机通过串行蓝牙进行连接。第二个项目是一个掌上电脑聊天应用程序,这个类似第一个项目。第三个应用程序显示了如何接通全球定位系统接收器,然后提取对于显示你在地图上的当前位置有用的数据。一些串行通信的基础知识如前所述,一个串行设备一次发送和接收一个位数据。有些设备因为在同一时间发送和接收数据,被称为全双工设备。其他可以在任何时间发送或接收被称为单双工。开始传输时,设备先发送一个起始位,其次是数据位。该数据位可以是五,六,七,或8位,基于商定而定。两个发送方和接收器必须设置为相同的数据通信比特或正确的比特率。数据位被发送完后,就会发送一个停止位。一个停止位可以是一位,一个半位,或两位。波特率是数据从一个设备到另一个的传输速度。波特率通常以每秒的位数(bps)来计量。注意:大多数串行设备传输七,八位数据。为了检测数据已被正确发送,一个可选的校验位可以同数据位在一起。一个校验位可以是以下内容:奇数,偶数,mark,space或无(空的奇偶位标志几乎总是被使用)。使用校验位提供了一个基本的机制,以检测已发送数据损坏,但不保证检查数据本身的错误。然而,校验位可用于改善完整性数据传送。大多数串行端口使用RS232C标准,它指定了一个连接器25针或9针(见图2)。大多数系列设备使用9针连接器。图2:25针和9针串行接口利用串口通信你在这一章建立的第一个应用程序将是一个通信应用程序。此通信应用程序将允许两个用户的连接使用串行连接进行通信的计算机。请注意,我提到的是串行连接,而不是串行电缆。这是因为两个连接的用户通过蓝牙也可以使用这个应用程序进行通信。你可以在使用蓝牙配对的两台计算机之间建立串行连接。最常见的方案是使用零调制解调器电缆连接两台计算机(见图3)。图3:零调制解调器电缆图4显示了完成的项目。要开始通讯,选择相应的COM串行端口来连接到远程计算机。单击连接,然后就准备好通讯了。图4:选择一个串口开始通讯需要的硬件为了测试串行通信,你有以下几个选择:计算机到计算机:如上所述,你可以使用零调制解调器电缆连接两台计算机。单台计算机:你可以使用一个两个串口的调制解调器电缆连接到同一计算机上。如果你的计算机只有一个串行端口,你可以使用USB到串口转换器转换到USB端口串行端口(详细信息见下一节关于这一点)。蓝牙连接:你可以在使用蓝牙配对的两台计算机之间建立串行连接。USB到串行端口变换器除非你有两台计算机,否则你将无法进行串行通讯的测试。但是,你可以使用零调制解调器电缆连接到同一计算机上的两个串行端口模拟两台计算机通过串行端口进行通信。但是今天大多数的电脑都带有最多一个串口(笔记本电脑和一些甚至没有一个)。一个好的解决方法是使用USB到串行端口变换器将USB端口转换为串行端口。因此,如果你的电脑没有任何串行端口,你将需要一对USB到串行端口适配器和一对空调制解调器电缆(见图2-5)。然后,每个USB到串行端口适配器连接到USB端口。图5:USB到串行端口变换器每个USB到串行端口适配器都有自己的驱动程序。安装驱动程序后,右键单击桌面上的我的电脑,选择属性。系统属性对话框框中,单击硬件选项卡,单击设备管理器按钮。展开端口(COM和并口)项目,并找到两个新增加的COM端口(见图6)。图6:定位新创建的串行端口在这个例子中,两个串行口分别是和蓝牙适配器除了使用零调制解调器电缆连接两个串行端口,还可以使用蓝牙对两台计算机进行配对。我们可以给每台计算机都配备一个蓝牙适配器(见图7)。然后,就可以给两台计算机配对并建立它们之间的串行连接了。注意:请参阅蓝牙适配器文件的附带文件,从而掌握如何为两个蓝牙电脑建立串行连接。图7:蓝牙适配器构建聊天应用程序使用Visual Studio 2005,创建一个新的Windows应用程序,并将它命名为SerialCommChat。按照图8所示填充默认的Form1。图8:利用各种控件构建默认的Form1按照表1所示设置控件属性表1:控件属性值Form1 Form1 lblMessage txtDataReceived txtDataReceived txtDataToSendText AcceptButtoBorderStyleScrollBars MultiLine MultiLineSerial ChatbtnSendFixedSingleVerticalTrueTrue在NET 2.0中,在工具箱中的Compo-nents标签的位置(见图9)有一个新的Windows窗体串口控件。这个串口控件封装了所有必要的功能,你需要访问它来使用串行通讯进行通信。你可以拖放一个串口控件到你的项目上或从代码建立一个(在这个项目里你将使用这个方法)。图9:工具箱中的串口控件切换到Form1的代码视图 来编辑代码。实例化的串口类首先,声明串口成员变量来代表你要使用的串口。Visual Basic 2005Public Class Form1Private WithEvents serialPort As New IO.Ports.SerialPortC# 2005public partial class Form1 : Formprivate System.IO.Ports.SerialPort serialPort =new System.IO.Ports.SerialPort();注意:你可以使用如前所述的串口控件,或者使用IO.Ports.SerialPort类;两者是相同的。请注意,在Visual Basic 2005中,你需要用WithEvents关键字来声明它。这是因为串口类的DataReceived事件是在数据到达时触发串行端口的,因此你需要使用这个事件来接收数据。对于C2005, DataReceived事件处理程序将被添加在Form1_Load事件中。列出所有可用的串行端口名称在窗体第一次加载时,将检索你的计算机上所有可用的串行端口名称,然后将这些端口名称送到到ComboBox控件。双击表格切换到代码编辑视图,Form1_Load事件处理程序将自动在这里显示出来。按照如下代码编辑事件。注意: Visual Studio 2005会为控件自动创建事件的处理程序,如窗体的Load事件或一个按钮的Click事件(),双击控件编辑事件处理程序。Visual Basic 2005Private Sub Form1_Load( _ByVal sender As System.Object, _ByVal e As System.EventArgs) _Handles MyBase.Load-display all the serial port names on the local computer-For i As Integer = 0 To _My.Computer.Ports.SerialPortNames.Count - 1cbbCOMPorts.Items.Add( _My.Computer.Ports.SerialPortNames(i)NextbtnDisconnect.Enabled = FalseEnd SubC# 2005private void Form1_Load(object sender, EventArgs e)/-set the event handler for the DataReceived event-serialPort.DataReceived += newSystem.IO.Ports.SerialDataReceivedEventHandler(DataReceived);/-display all the serial port names on the local/ computer-string portNames =System.IO.Ports.SerialPort.GetPortNames();for (int i = 0; i & txtDataToSend.Text & vbCrLf).ScrollToCaret()End With-clears the TextBox control-txtDataToSend.Text = String.EmptyCatch ex As ExceptionMsgBox(ex.ToString)End TryEnd SubC# 2005/-Event handler for the Send button-private void btnSend_Click(object sender, EventArgs e)try/-write the string to the serial port-serialPort.Write(txtDataToSend.Text +Environment.NewLine);/-append the sent string to the TextBox control-txtDataReceived.AppendText( + txtDataToSend.Text +Environment.NewLine);txtDataReceived.ScrollToCaret();/-clears the TextBox control-txtDataToSend.Text = string.Empty;catch (Exception ex)MessageBox.Show(ex.ToString(); 串口接收数据串口类的一个优点是,你不必不间断地查询传入的数据。相反,你只需要使用提供的DataReceived事件,它会自动检测到传入的数据并提醒你。但是,由于此事件运行占用一个单独的线程,任何更新主窗口的企图都将导致错误。因此,你需要在主线程(Form1中)使用委托来更新控件。Visual Basic 2005-Event handler for the DataReceived event-Private Sub DataReceived( _ByVal sender As Object, _ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _Handles serialPort.DataReceived-invoke the delegate to retrieve the received data-txtDataReceived.BeginInvoke(New _myDelegate(AddressOf updateTextBox), _New Object() )End SubC# 2005/-Event handler for the DataReceived event-private void DataReceived(object sender,System.IO.Ports.SerialDataReceivedEventArgs e)/-invoke the delegate to retrieve the received data-txtDataReceived.BeginInvoke(newmyDelegate(updateTextBox);Define the delegate and the updateTextBox() subroutine as follows.Visual Basic 2005-Delegate and subroutine to update the TextBox control-Public Delegate Sub myDelegate()Public Sub updateTextBox()-append the received data into the TextBox control-With txtDataReceived.AppendText(serialPort.ReadExisting).ScrollToCaret()End WithEnd SubC# 2005/-Delegate and subroutine to update the TextBox control-public delegate void myDelegate();public void updateTextBox()/-append the received data into the TextBox control-txtDataReceived.AppendText(serialPort.ReadExisting();txtDataReceived.ScrollToCaret();测试应用程序现在已经准备好了测试应用程序。在Visual Studio 2005中按F5调试该程序。你还需要运行应用程序的另一个实例,以测试通讯功能。为此,在你存储解决方案的Debug文件夹目录中找到SerialCommChat.exe应用程序。在应用程序的第一个实例中,选择端口3(根据我们计算机的端口号进行选择,然后检查计算机上的端口号),然后点击连接。在其他实例中,选择端口4,然后点击连接。你现在可以开始通讯了(见图11)!图11:通过两个串行口通讯传输Unicode字符在默认情况下,串口类发送ASCII字符。这是在编码串口类时设置的。如果你想使用其他语言(如中文交谈或日语),你需要设置串口属性类的编码为Unicode,以便可以正确地将数据发送和接收。提示:NET Framework 2.0和最新版本的NET Compact Framework的2.0在你使用ReadExisting()方法来读取Unicode字符时不会正确工作。因此,我们不使用ReadExisting()方法来读取传入的Unicode字符,而是使用Read()方法。首先,你需要设置串口类的编码为Unicode。Visual Basic 2005With serialPort.PortName = cbbCOMPorts.Text.BaudRate = 9600.Parity = IO.Ports.Parity.None.DataBits = 8.StopBits = IO.Ports.StopBits.One-set the encoding the Unicode-.Encoding = System.Text.Encoding.UnicodeEnd WithC# 2005serialPort.PortName = cbbCOMPorts.Text;serialPort.BaudRate = 9600;serialPort.Parity = System.IO.Ports.Parity.None;serialPort.DataBits = 8;serialPort.StopBits = System.IO.Ports.StopBits.One;/-set the encoding the Unicode-serialPort.Encoding = System.Text.Encoding.Unicode;然后,修改updateTextBox()子例程从而正确读取传入的Unicode字符。Visual Basic 2005Public Sub updateTextBox()-UNICODE work-around-With txtDataReceived-find out the number of bytes to read-Dim bytesToRead As Integer = serialPort.BytesToRead-declare a char array-Dim ch(bytesToRead) As Char-read the bytes into the ch array-Dim bytesRead As Integer = 0bytesRead = serialPort.Read(ch, 0, bytesToRead)-convert the ch array into a string-Dim str As String = New String(ch, 0, bytesRead)-append the received string into the TextBox control-.AppendText(str).ScrollToCaret()End WithEnd SubC# 2005public void updateTextBox()/-UNICODE work-around-/-find out the number of bytes to read-int bytesToRead = serialPort.BytesToRead;/-declare a char array-char ch = new charbytesToRead;int bytesRead = 0;/-read the bytes into the ch array-bytesRead = serialPort.Read(ch, 0, bytesToRead);/-convert the ch array into a string-string str = new string(ch, 0, bytesRead);/-append the received string into the TextBox/ control-txtDataReceived.AppendText(str);txtDataReceived.ScrollToCaret();图12显示了发送和接收的中文字符。图12:发送和接收的中文字符附件2:外文原文Serial CommunicationsSerial communicationis one of the oldest mechanisms for devices to communicate witheach other. Starting with the IBM PC and compatible computers, almost all computers are equipped with one or more serial ports and one parallel port. As the name implies, a serialport sends and receives data serially, one bit at a time. In contrast, a parallelport sends and receives data eight bits at a time, using eight separate wires. Tip For serial communication to work,you just need a minimum of three wiresone to send,one to receive,and one signal ground.For parallel communication,you need eight wires.Despite thecomparatively slower transfer speed of serial ports over parallel ports, serial communication remains a popular connectivity option for devices because of its simplicity and cost-effectiveness. Figure 2-1 shows some of the devices that use a serial port to connect to the computer. Using a serial port, you can connect to a modem, a mouse, or a device such as a bridge/router for configuration purposes.Although consumer products today are using USB connections in place of serial connec-tions, still a lot of devices use serial ports as their sole connections to the outside world.In thischapter, you will learn how to communicate with other serial devices using the new SerialPortclass available in the .NET Framework 2.0 and the .NET Compact Framework 2.0. In particular, you will build three projects that illustrate how to use serial communications. The first project is a chat application that allows two computers (connected using either a serial cable or a Bluetooth connection) to communicate. And using the foundation of this application, you can extend it to communicate with other external serial devices such as cellular phones. You will learn how to use the ATcommands to programmatically control your mobile phones through a serial Bluetooth connection. The second project is a Pocket PC chat application, which is similar to the first project. The third application shows how to communicate with a GPS receiver and then extract the useful data for displaying the current locationon a map.Some Serial Communication BasicsAs mentioned, aserial device sends and receives data one bit at a time. Some devices can send and receive data at the same time and are known as full-duplexdevices. Others that can either send or receive at any one time are known as single-duplex.To initiate transmission, a device first transmits a start bit, followed by the data bits. The data bits can be five, six, seven, or eight bits, depending on what has been agreed upon. Both the sender and the receiver must be set to the same data bits for communication to take place correctly. Once the data bits are sent, a stop bitis sent. A stop bit can be one, one and a half, and two bits. The baudrateis the speed of transmission of data from one device to another. Baud rate is usually measured in bits per second(bps). Note Most serial devices transmit in seven or eight bits.To detectthat the data has been sent correctly, an optional parity bitcan be included together with the data bits. A parity bit can be one of the following: odd, even, mark, space, or none (mark and space parity are almost always used). Using a parity bit provides a basic mechanism to detect corruption of data that was sent and does not guarantee that the data received is free from error. Nevertheless, a parity bit is useful for improving the integrity of the data sent.Most serial ports adhere to the RS232C standard, which specifies a connector either with 25 pins or with 9 pins (see Figure 2-2). Most serial devices use thenine-pin connector.The mostcommon scenario is to connect two computers using a null modem cable (see Figure 2-3).Figure 2-4 showsthe finished project. To start the chat, select the COM number corre- sponding to the serial port that is connected to the remote computer. Click Connect, and you are readyto talk!Hardware NeededTo test serialcommunications, you have a couple of options:Computer to computer: As mentioned, you can connect two computers using a null modem cable.Single computer: You can connect two serial ports on the same computer using a null modem cable. If your computer has only one serial port, you can convert a USB port to a serial port using a USB-to-serial-port converter (see the next section for more informa- tion about this).Bluetooth connection: You can create a serialconnection between two Bluetooth-paired computers.USB-to-Serial-Port ConvertersUnless youhave two computers, you wont be able to test serial communications. However, you can use a null modem cable to connect two serial ports on the same computer to simulate two computers communicating over serial ports. But most computers today come with at most one serial port (and some notebooks do not even have one). One good solution is to use a USB-to- serial-port adapter to convert a USB port to a serial port. Hence, if your computer does not have any serial ports, you will need a pair of USB-to-serial-port adapters and a null modem cable (see Figure 2-5). Then, connect each USB-to-serial-port adapter to a USB connection.The USB-to-serial-portadapter comes with its own drivers. After installing the drivers, right-click My Computer on the desktop, and select Properties. In the System Properties dialog box, click the Hardware tab, and click the Device Manager button. Expand the Ports (COM & LPT) item, and locate the two newly added COM ports (see Figure 2-6).In this example, the two USB serial portsare COM3 and COM4.Bluetooth AdaptersBesides using a null modem cable to connect two serial ports, you can also use Bluetooth to pair two computers. You can equip each computer with a Bluetooth adapter (see Figure 2-7).You can then pair the two computers and establish a serial connection between them. Note Refer to the documentation that comes with your Bluetooth adapter to learn how to establish a serial connection between two Bluetooth-paired computers.Building the Chat ApplicationUsing Visual Studio 20
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 拼音线描美术课件
- 产后盆底功能康复治疗
- 联想集团员工激励管理实践分析
- (统编版)语文三年级上册口语交际:名字里的故事 课件
- 补肺汤解析与应用
- 护理心理案例分析与实践应用
- 大学生秋季传染病预防指南
- 饮食护理的种类
- 肺癌的护理查房
- 初中班主任年度个人工作总结模版
- 智联招聘笔试考试题库
- 2025年中考英语作文热点押题及范文
- 特种设备操作人员培训管理制度范文(2篇)
- 【MOOC】大学生创新创业教育-云南大学 中国大学慕课MOOC答案
- 《SQL优化策略》课件
- 剪映专业版教学课件
- 2023-2024学年《软件设计与体系结构》模拟试卷及答案解析
- 智能咖啡机行业营销策略方案
- 2024年部编版九年级语文上册电子课本(高清版)
- 《鱼纹话吉祥》 课件 2024-2025学年岭南美版(2024) 初中美术七年级上册
- 小米智能家居合同模板
评论
0/150
提交评论