1.前言:
利用VSPE可以模擬RS232串口的特性,用來創建虛擬端口,以程式來測試各端口之間通信的狀態。
2.說明:
不論是PC或是NoteBook,實體的RS232接口幾乎要消失,為了要做RS232的測試,模擬的軟體可創造出虛擬的串口,再用程式測試各接口之間的信號傳遞。
VSPE串口模擬器最新版本可在下列網址下載:
http://www.eterlogic.com/Products.VSPE.html
開啟VSPE,選擇Device-->Create-->Device Type(選Pair),下一步選擇Virtual serial port分別為COM10和COM11
點選Emulation-->Start
開啟C#專案,工具箱裡拖拉出兩個SerialPort控制項,分別命名PortName為COM10與COM11
程式碼:
private void Form1_Load(object sender, EventArgs e) { serialPort10.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived1); serialPort11.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived2); serialPort10.Open(); serialPort11.Open(); serialPort10.Write("COM10-->COM11: Hello, what's your name?"); serialPort11.Write("COM11-->COM10: I am Zoper!"); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { serialPort10.Close(); serialPort11.Close(); } private void port_DataReceived1(object sender, SerialDataReceivedEventArgs e) { string response = serialPort10.ReadExisting(); richTextBox1.Invoke(new EventHandler(delegate { richTextBox1.AppendText(response + "\n"); })); } private void port_DataReceived2(object sender, SerialDataReceivedEventArgs e) { string response = serialPort11.ReadExisting(); richTextBox1.Invoke(new EventHandler(delegate { richTextBox1.AppendText(response + "\n"); })); }
圖例:
文章標籤
全站熱搜
留言列表