Serial Communications -外文翻譯.doc_第1頁
Serial Communications -外文翻譯.doc_第2頁
Serial Communications -外文翻譯.doc_第3頁
Serial Communications -外文翻譯.doc_第4頁
Serial Communications -外文翻譯.doc_第5頁
已閱讀5頁,還剩37頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

附件1:外文資料翻譯譯文串行通信串行通信的設備是最古老的溝通機制之一。從IBM個人電腦和兼容式電腦的時代開始,幾乎所有的計算機都配有一個或多個串行端口和一個并行端口。顧名思義,一個串行端口發(fā)送和接收串行數據,一次一位數據。相反,一個并行端口一次發(fā)送和接收8位數據,使用8個單獨的電線。提示:要使串行通信工作,你只需要一根三根線的電纜1根發(fā)送,1根用來接收,1根接地。對于并行通信,你需要采用8條導線。盡管相對較慢的傳輸速度遠低于并行端口,串行端口通信依然因為它簡單的設備和高的成本效益而是一個受歡迎的連接選項。圖2-1顯示了設備的使用串口連接部分到計算機。使用串行端口,你可以連接到調制解調器,鼠標或設備等作為一個橋梁/路由器配置的目的圖1:一些常見的串行設備,調制解調器,鼠標和路由器雖然今天的消費產品中在串行連接的地方使用USB連接,但還有很多的設備使用串行端口作為與外部世界的唯一連接。在這一章中,你將學習如何使用基于NET Framework2.0和NET Compact Framework2.0的串行通信方式來與其他的串行設備進行通信。并且,你將建立3個闡述了如何使用串行通信的項目。第一個項目是一個請求允許兩臺電腦(連接使用一個串行電纜或藍牙連接)進行通信的通信申請。在此應用程序的基礎上,你可以進行擴展從而與手機等其他外部串行設備進行通信。你將學習如何使用AT命令,以編程的方式控制你的手機通過串行藍牙進行連接。第二個項目是一個掌上電腦聊天應用程序,這個類似第一個項目。第三個應用程序顯示了如何接通全球定位系統(tǒng)接收器,然后提取對于顯示你在地圖上的當前位置有用的數據。一些串行通信的基礎知識如前所述,一個串行設備一次發(fā)送和接收一個位數據。有些設備因為在同一時間發(fā)送和接收數據,被稱為全雙工設備。其他可以在任何時間發(fā)送或接收被稱為單雙工。開始傳輸時,設備先發(fā)送一個起始位,其次是數據位。該數據位可以是五,六,七,或8位,基于商定而定。兩個發(fā)送方和接收器必須設置為相同的數據通信比特或正確的比特率。數據位被發(fā)送完后,就會發(fā)送一個停止位。一個停止位可以是一位,一個半位,或兩位。波特率是數據從一個設備到另一個的傳輸速度。波特率通常以每秒的位數(bps)來計量。注意:大多數串行設備傳輸七,八位數據。為了檢測數據已被正確發(fā)送,一個可選的校驗位可以同數據位在一起。一個校驗位可以是以下內容:奇數,偶數,mark,space或無(空的奇偶位標志幾乎總是被使用)。使用校驗位提供了一個基本的機制,以檢測已發(fā)送數據損壞,但不保證檢查數據本身的錯誤。然而,校驗位可用于改善完整性數據傳送。大多數串行端口使用RS232C標準,它指定了一個連接器25針或9針(見圖2)。大多數系列設備使用9針連接器。圖2:25針和9針串行接口利用串口通信你在這一章建立的第一個應用程序將是一個通信應用程序。此通信應用程序將允許兩個用戶的連接使用串行連接進行通信的計算機。請注意,我提到的是串行連接,而不是串行電纜。這是因為兩個連接的用戶通過藍牙也可以使用這個應用程序進行通信。你可以在使用藍牙配對的兩臺計算機之間建立串行連接。最常見的方案是使用零調制解調器電纜連接兩臺計算機(見圖3)。圖3:零調制解調器電纜圖4顯示了完成的項目。要開始通訊,選擇相應的COM串行端口來連接到遠程計算機。單擊連接,然后就準備好通訊了。圖4:選擇一個串口開始通訊需要的硬件為了測試串行通信,你有以下幾個選擇:計算機到計算機:如上所述,你可以使用零調制解調器電纜連接兩臺計算機。單臺計算機:你可以使用一個兩個串口的調制解調器電纜連接到同一計算機上。如果你的計算機只有一個串行端口,你可以使用USB到串口轉換器轉換到USB端口串行端口(詳細信息見下一節(jié)關于這一點)。藍牙連接:你可以在使用藍牙配對的兩臺計算機之間建立串行連接。USB到串行端口變換器除非你有兩臺計算機,否則你將無法進行串行通訊的測試。但是,你可以使用零調制解調器電纜連接到同一計算機上的兩個串行端口模擬兩臺計算機通過串行端口進行通信。但是今天大多數的電腦都帶有最多一個串口(筆記本電腦和一些甚至沒有一個)。一個好的解決方法是使用USB到串行端口變換器將USB端口轉換為串行端口。因此,如果你的電腦沒有任何串行端口,你將需要一對USB到串行端口適配器和一對空調制解調器電纜(見圖2-5)。然后,每個USB到串行端口適配器連接到USB端口。圖5:USB到串行端口變換器每個USB到串行端口適配器都有自己的驅動程序。安裝驅動程序后,右鍵單擊桌面上的我的電腦,選擇屬性。系統(tǒng)屬性對話框框中,單擊硬件選項卡,單擊設備管理器按鈕。展開端口(COM和并口)項目,并找到兩個新增加的COM端口(見圖6)。圖6:定位新創(chuàng)建的串行端口在這個例子中,兩個串行口分別是和藍牙適配器除了使用零調制解調器電纜連接兩個串行端口,還可以使用藍牙對兩臺計算機進行配對。我們可以給每臺計算機都配備一個藍牙適配器(見圖7)。然后,就可以給兩臺計算機配對并建立它們之間的串行連接了。注意:請參閱藍牙適配器文件的附帶文件,從而掌握如何為兩個藍牙電腦建立串行連接。圖7:藍牙適配器構建聊天應用程序使用Visual Studio 2005,創(chuàng)建一個新的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事件是在數據到達時觸發(fā)串行端口的,因此你需要使用這個事件來接收數據。對于C2005, DataReceived事件處理程序將被添加在Form1_Load事件中。列出所有可用的串行端口名稱在窗體第一次加載時,將檢索你的計算機上所有可用的串行端口名稱,然后將這些端口名稱送到到ComboBox控件。雙擊表格切換到代碼編輯視圖,Form1_Load事件處理程序將自動在這里顯示出來。按照如下代碼編輯事件。注意: Visual Studio 2005會為控件自動創(chuàng)建事件的處理程序,如窗體的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(); 串口接收數據串口類的一個優(yōu)點是,你不必不間斷地查詢傳入的數據。相反,你只需要使用提供的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字符在默認情況下,串口類發(fā)送ASCII字符。這是在編碼串口類時設置的。如果你想使用其他語言(如中文交談或日語),你需要設置串口屬性類的編碼為Unicode,以便可以正確地將數據發(fā)送和接收。提示: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顯示了發(fā)送和接收的中文字符。圖12:發(fā)送和接收的中文字符附件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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論