收藏 分销(赏)

Serial Communications -外文翻译.doc

上传人:xrp****65 文档编号:9006061 上传时间:2025-03-11 格式:DOC 页数:41 大小:1.60MB 下载积分:10 金币
下载 相关 举报
Serial Communications -外文翻译.doc_第1页
第1页 / 共41页
Serial Communications -外文翻译.doc_第2页
第2页 / 共41页


点击查看更多>>
资源描述
附件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:定位新创建的串行端口 在这个例子中,两个USB串行口分别是COM3和COM4 蓝牙适配器 除了使用零调制解调器电缆连接两个串行端口,还可以使用蓝牙对两台计算机进行配对。我们可以给每台计算机都配备一个蓝牙适配器(见图7)。然后,就可以给两台计算机配对并建立它们之间的串行连接了。 注意:请参阅蓝牙适配器文件的附带文件,从而掌握如何为两个蓝牙电脑建立串行连接。 图7:蓝牙适配器 构建聊天应用程序 使用Visual Studio 2005,创建一个新的Windows应用程序,并将它命名为SerialCommChat。按照图8所示填充默认的Form1。 图8:利用各种控件构建默认的Form1 按照表1所示设置控件属性 表1: 控件 属性 值 Form1 Form1 lblMessage txtDataReceived txtDataReceived txtDataToSend Text AcceptButto BorderStyle ScrollBars MultiLine MultiLine "Serial Chat" btnSend FixedSingle Vertical True True 在NET 2.0中,在工具箱中的Compo-nents标签的位置(见图9)有一个新的Windows窗体串口控件。这个串口控件封装了所有必要的功能,你需要访问它来使用串行通讯进行通信。你可以拖放一个串口控件到你的项目上或从代码建立一个(在这个项目里你将使用这个方法)。 图9:工具箱中的串口控件 切换到Form1的代码视图 来编辑代码。 实例化的串口类 首先,声明串口成员变量来代表你要使用的串口。 Visual Basic 2005 Public Class Form1 Private WithEvents serialPort As New IO.Ports.SerialPort C# 2005 public partial class Form1 : Form { private System.IO.Ports.SerialPort serialPort = new System.IO.Ports.SerialPort(); 注意:你可以使用如前所述的串口控件,或者使用IO.Ports.SerialPort类;两者是相同的。 请注意,在Visual Basic 2005中,你需要用WithEvents关键字来声明它。这是因为串口类的DataReceived事件是在数据到达时触发串行端口的,因此你需要使用这个事件来接收数据。对于C#2005, DataReceived事件处理程序将被添加在Form1_Load事件中。 列出所有可用的串行端口名称 在窗体第一次加载时,将检索你的计算机上所有可用的串行端口名称,然后将这些端口名称送到到ComboBox控件。双击表格切换到代码编辑视图,Form1_Load事件处理程序将自动在这里显示出来。按照如下代码编辑事件。 注意: Visual Studio 2005会为控件自动创建事件的处理程序,如窗体的Load事件或一个按钮的Click事件(),双击控件编辑事件处理程序。 Visual Basic 2005 Private 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 - 1 cbbCOMPorts.Items.Add( _ My.Computer.Ports.SerialPortNames(i)) Next btnDisconnect.Enabled = False End Sub C# 2005 private void Form1_Load(object sender, EventArgs e) { //---set the event handler for the DataReceived event--- serialPort.DataReceived += new System.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 <= portNames.Length - 1; i++) { cbbCOMPorts.Items.Add(portNames[i]); } btnDisconnect.Enabled = false; } C#中的事件转移 在C#版本的Form1_Load事件中,我还增加了一个事件处理程序的串口类的DataReceived事件。在串行口有数据输入时,此事件将被注销。你不需要在Visual Basic 2005中处理这个事件的程序,因为你可以使用Handles关键字来连接事件的处理程序。 //---set the event handler for the DataReceived event--- // serialPort.DataReceived += new // System.IO.Ports.SerialDataReceivedEventHandler( // DataReceived); 图10显示了ComboBox控件在第一次加载时的外观。 图10:ComboBox控件在第一次加载时的外观 打开一个串行口 一旦一个端口的名称被选中,用户点击连接按钮时,将会打开选择的端口。你可以用下面的方法实现这一点。 Visual Basic 2005 '---Event handler for the Connect button--- Private Sub btnConnect_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnConnect.Click '---close the serial port if it is open--- If serialPort.IsOpen Then serialPort.Close() End If Try '---configure the various parameters of the serial port--- With serialPort .PortName = cbbCOMPorts.Text .BaudRate = 9600 .Parity = IO.Ports.Parity.None .DataBits = 8 .StopBits = IO.Ports.StopBits.One End With '---open the serial port--- serialPort.Open() '---update the status of the serial port and ' enable/disable the buttons--- lblMessage.Text = cbbCOMPorts.Text & " connected." btnConnect.Enabled = False btnDisconnect.Enabled = True Catch ex As Exception MsgBox(ex.ToString) End Try End Sub C# 2005 //---Event handler for the Connect button--- private void btnConnect_Click(object sender, EventArgs e) { //---close the serial port if it is open--- if (serialPort.IsOpen) { serialPort.Close(); } try { //---configure the various parameters of the serial // port--- serialPort.PortName = cbbCOMPorts.Text; serialPort.BaudRate = 9600; serialPort.Parity = System.IO.Ports.Parity.None; serialPort.DataBits = 8; serialPort.StopBits = System.IO.Ports.StopBits.One; //---open the serial port--- serialPort.Open(); //---update the status of the serial port and // enable/disable the buttons--- lblMessage.Text = cbbCOMPorts.Text + " connected."; btnConnect.Enabled = false; btnDisconnect.Enabled = true; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } 值得注意的是,你需要设置串口类的各种属性,如PortName,各种性能,波特率,奇偶校验等。 注意:通信双方必须具有相同的属性设置。也就是说,他们必须有同样的波特率,奇偶校验,数据位,停止位。 断开串口 断开按钮用来关闭当前打开的串行端口。 Visual Basic 2005 '---Event handler for the Disconnect button--- Private Sub btnDisconnect_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnDisconnect.Click Try '---close the serial port--- serialPort.Close() '---update the status of the serial port and ' enable/disable the buttons--- lblMessage.Text = serialPort.PortName & " disconnected." btnConnect.Enabled = True btnDisconnect.Enabled = False Catch ex As Exception MsgBox(ex.ToString) End Try End Sub C# 2005 //---Event handler for the Disconnect button--- private void btnDisconnect_Click(object sender, EventArgs e) { try { '---close the serial port--- serialPort.Close(); //---update the status of the serial port and // enable/disable the buttons--- lblMessage.Text = serialPort.PortName + " disconnected."; btnConnect.Enabled = true; btnDisconnect.Enabled = false; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } 使用串行端口发送数据 要通过串口发送数据,需要使用串口类的Write()方法。 Visual Basic 2005 '---Event handler for the Send button--- Private Sub btnSend_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnSend.Click Try '---write the string to the serial port--- serialPort.Write(txtDataToSend.Text & vbCrLf) '---append the sent string to the TextBox control--- With txtDataReceived .AppendText(">" & txtDataToSend.Text & vbCrLf) .ScrollToCaret() End With '---clears the TextBox control--- txtDataToSend.Text = String.Empty Catch ex As Exception MsgBox(ex.ToString) End Try End Sub C# 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 Sub C# 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(new myDelegate(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 With End Sub C# 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 2005 With 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.Unicode End With C# 2005 serialPort.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 2005 Public 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 = 0 bytesRead = 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 With End Sub C# 2005 public void updateTextBox() { //---UNICODE work-around--- //---find out the number of bytes to read--- int bytesToRead = serialPort.BytesToRead; //---declare a char array--- char[] ch = new char[bytesToRead]; 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 Communications Serial 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 wires—one 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 Basics As 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 p
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服