資源簡介
新手入門串口調試助手,簡單實用。C#開發,VS2012開發。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.IO.Ports;
using?System.Linq;
using?System.Text;
using?System.Threading.Tasks;
using?System.Windows.Forms;
namespace?VS串口控件使用
{
????public?partial?class?Form1?:?Form
????{
????????SerialPort?serialPort;?
????????
????????public?Form1()
????????{
????????????InitializeComponent();
????????????Control.CheckForIllegalCrossThreadCalls?=?false;
????????????//獲取串口
????????????String[]?serialPortArray?=?SerialPort.GetPortNames();
????????????if?(serialPortArray?!=?null?&&?serialPortArray.Length?!=?0)
????????????{
????????????????//對串口進行排序
????????????????Array.Sort(serialPortArray);
????????????????foreach?(String?port?in?serialPortArray)
????????????????{
????????????????????//添加到combox的item
????????????????????if?(port?!=?null?&&?port.Length?!=?0)
????????????????????????comboBox_SPort.Items.Add(port);
????????????????}
????????????}
????????????//設置初始顯示的值?
????????????comboBox_SPort.SelectedIndex?=?0;
????????????serialPort?=?new?SerialPort();
????????????serialPort.DataReceived?+=?new?SerialDataReceivedEventHandler(serialPort_DataReceived);??
????????????//serialPort1.Open();
????????}????????
????????private?void?serialPort_DataReceived(object?sender?SerialDataReceivedEventArgs?e)
????????{
????????????byte[]?byteRead?=?new?byte[serialPort.BytesToRead];????//BytesToRead:sp1接收的字符個數??
????????????if?(radioButton_char.Checked)??????????????????????????//‘發送字符串‘單選按鈕??
????????????{
????????????????textBox_uartReceive.Text?+=?serialPort.ReadLine()?+?“\r\n“;?//注意:回車換行必須這樣寫,單獨使用“\r“和“\n“都不會有效果??
????????????????serialPort.DiscardInBuffer();??????????????????????//清空SerialPort控件的Buffer???
????????????}
????????????else????????????????????????????????????????????//‘發送16進制按鈕‘??
????????????{
????????????????try
????????????????{
????????????????????Byte[]?receivedData?=?new?Byte[serialPort.BytesToRead];????????//創建接收字節數組??
????????????????????serialPort.Read(receivedData?0?receivedData.Length);?????????//讀取數據?????????????????????????
????????????????????serialPort.DiscardInBuffer();??????????????????????????????????//清空SerialPort控件的Buffer??
????????????????????string?strRcv?=?null;
????????????????????for?(int?i?=?0;?i?????????????????????{
????????????????????????strRcv?+=?receivedData[i].ToString(“X2“);??//16進制顯示??
????????????????????}
????????????????????textBox_uartReceive.Text?+=?strRcv?+?“\r\n“;
????????????????}
????????????????catch?(System.Exception?ex)
????????????????{
????????????????????MessageBox.Show(ex.Message?“出錯提示“);
????????????????????textBox_uartSend.Text?=?““;
????????????????}???????????????
????????????}?
????????}
????????private?void?button_send_Click(object?sender?EventArgs?e)
????????{
????????????//if?(serialPort?!=?null?&&?serialPort.IsOpen)
???????????//?{
??
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????950??2017-12-29?16:00??VS串口控件使用.sln
????..A..H.?????84480??2017-12-29?18:32??VS串口控件使用.v11.suo
?????文件????????187??2017-12-29?16:00??VS串口控件使用\App.config
?????文件??????13824??2017-12-29?18:31??VS串口控件使用\bin\Debug\VS串口控件使用.exe
?????文件????????187??2017-12-29?16:00??VS串口控件使用\bin\Debug\VS串口控件使用.exe.config
?????文件??????32256??2017-12-29?18:31??VS串口控件使用\bin\Debug\VS串口控件使用.pdb
?????文件??????22984??2017-12-29?18:32??VS串口控件使用\bin\Debug\VS串口控件使用.vshost.exe
?????文件????????187??2017-12-29?16:00??VS串口控件使用\bin\Debug\VS串口控件使用.vshost.exe.config
?????文件????????490??2017-03-19?05:00??VS串口控件使用\bin\Debug\VS串口控件使用.vshost.exe.manifest
?????文件???????7745??2017-12-29?18:32??VS串口控件使用\Form1.cs
?????文件???????8811??2017-12-29?18:24??VS串口控件使用\Form1.Designer.cs
?????文件???????5817??2017-12-29?18:05??VS串口控件使用\Form1.resx
?????文件???????1308??2017-12-29?18:30??VS串口控件使用\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????7471??2017-12-29?18:19??VS串口控件使用\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件??????????0??2017-12-29?16:00??VS串口控件使用\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
?????文件??????????0??2017-12-29?16:00??VS串口控件使用\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
?????文件??????????0??2017-12-29?16:00??VS串口控件使用\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
?????文件???????1923??2017-12-29?18:32??VS串口控件使用\obj\Debug\VS串口控件使用.csproj.FileListAbsolute.txt
?????文件????????975??2017-12-29?18:24??VS串口控件使用\obj\Debug\VS串口控件使用.csproj.GenerateResource.Cache
?????文件???????2209??2017-12-29?16:17??VS串口控件使用\obj\Debug\VS串口控件使用.csprojResolveAssemblyReference.cache
?????文件??????13824??2017-12-29?18:31??VS串口控件使用\obj\Debug\VS串口控件使用.exe
?????文件????????180??2017-12-29?18:24??VS串口控件使用\obj\Debug\VS串口控件使用.Form1.resources
?????文件??????32256??2017-12-29?18:31??VS串口控件使用\obj\Debug\VS串口控件使用.pdb
?????文件????????180??2017-12-29?17:38??VS串口控件使用\obj\Debug\VS串口控件使用.Properties.Resources.resources
?????文件????????532??2017-12-29?16:00??VS串口控件使用\Program.cs
?????文件???????1360??2017-12-29?16:00??VS串口控件使用\Properties\AssemblyInfo.cs
?????文件???????2892??2017-12-29?16:00??VS串口控件使用\Properties\Resources.Designer.cs
?????文件???????5612??2017-12-29?16:00??VS串口控件使用\Properties\Resources.resx
?????文件???????1107??2017-12-29?16:00??VS串口控件使用\Properties\Settings.Designer.cs
?????文件????????249??2017-12-29?16:00??VS串口控件使用\Properties\Settings.settings
............此處省略12個文件信息
評論
共有 條評論