資源簡介
利用串口控件實現的16進制收發通訊程序:收到的數據以16進制顯示,發送數據以16進制形式發送。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
namespace?HexCommPort
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????if?(mycomm.IsOpen)
????????????{
????????????????mycomm.Close();
????????????}
????????????mycomm.ReadTimeout?=?32;
????????????try
????????????{
????????????????mycomm.Open();
????????????????button3.Text?=?“關閉串口1“;
????????????}
????????????catch
????????????{
????????????????button3.Text?=?“打開串口1“;
????????????????MessageBox.Show(“沒發現此串口或串口已經在使用“);
????????????}
????????}
????????private?byte[]?getdata()
????????{
????????????int?len?=?tsend.Text.Length;
????????????int?j?=?0;
????????????byte?[]datat?=?new?byte[len];
????????????for(int?i=0;i ????????????{
????????????????if((tsend.Text[i]<=‘9‘)&&(tsend.Text[i]>=‘0‘)&&(tsend.Text[i+1]>=‘0‘)&&(tsend.Text[i+1]<=‘9‘)&&(tsend.Text[i+2]<=‘?‘))
????????????????{
????????????????????datat[j]?=?(byte)((tsend.Text[i]-‘0‘)*16+(tsend.Text[i+1]-‘0‘));
????????????????????j++;
????????????????}
????????????}
????????????byte[]?datarev?=?new?byte[j];
????????????for(int?k=0;k ????????????{
????????????????datarev[k]?=?datat[k];
????????????}
????????????return?datarev;
????????}
????????private?bool?ishex(char?x)
????????{
????????????bool?re?=?false;
????????????if((x<=‘9‘)&&(x>=‘0‘))
????????????{
????????????????re?=?true;
????????????}
????????????else?if((x<=‘F‘)&&(x>=‘A‘))
????????????{
????????????????re?=??true;
????????????}
????????????else?if?((x?<=?‘f‘)?&&?(x?>=?‘a‘))
????????????{
????????????????re?=?true;
????????????}
????????????return?re;
????????}
????????private?byte[]?GetByteData(string?s)
????????{
????????????byte[]?data?=?new?byte[s.Length?/?2];
????????????for?(int?i?=?0;?i?????????????{
????????????????if?(s[i?*?2]?<=?‘9‘)
????????????????{
????????????????????data[i]?=?(byte)((s[i?*?2]?-?‘0‘)?*?16);
????????????????}
????????????????else?if?(s[i?*?2]?<=?‘f‘?&&?s[i?*?2]?>=?‘a‘)
????????????????{
????????????????????data[i]?=?(byte)((s[i?*?2]?-?‘a‘?+?10)?*?16);
????????????????}
????????????????else?if?(s[i?*?2]?<=?‘F‘?&&?s[i?*?2]?>=?‘A‘)
????????????????{
????????????????????data[i]?=?(byte)((s[i?*?2]?-?‘A‘?+?10)?*?16);
????????????????}
????????????????if?(s[i?*?2?+?1]?<=?‘9‘)
????????????????{
????????????????????data[i]?=?(byte)(data[i]?+?(byte)((s[i?*?2?+?1]?-?‘0‘)));
????????????????}
????????????????else?if?(s[i?*?2?+?1]?<=?‘f‘?&&?s[i?*?2?+?1]?>=?‘a‘)
????????????????{
????????????????????data[i]?=?(byte)(data[i]?+?(byte)((s[i?*?2?+?1]?-?‘a‘?+?10)));
????????????????}
????????????????else?if?(s[i?*?2?+?1]?<=?‘F‘?&&?s[i?*?2?+?1]?>=?‘A‘)
???
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????5612??2008-06-24?11:47??HexCommPort\HexCommPort\Properties\Resources.resx
?????文件????????249??2008-06-24?11:47??HexCommPort\HexCommPort\Properties\Settings.settings
?????文件???????1184??2008-06-24?11:47??HexCommPort\HexCommPort\Properties\AssemblyInfo.cs
?????文件???????2876??2008-06-24?11:47??HexCommPort\HexCommPort\Properties\Resources.Designer.cs
?????文件???????1094??2008-06-24?11:47??HexCommPort\HexCommPort\Properties\Settings.Designer.cs
?????文件???????6820??2008-06-24?16:00??HexCommPort\HexCommPort\Form1.cs
?????文件???????3229??2008-06-24?12:00??HexCommPort\HexCommPort\HexCommPort.csproj
?????文件???????5632??2005-12-08?14:51??HexCommPort\HexCommPort\bin\Debug\HexCommPort.vshost.exe
?????文件??????24576??2008-06-24?16:00??HexCommPort\HexCommPort\bin\Debug\HexCommPort.exe
?????文件??????26112??2008-06-24?16:00??HexCommPort\HexCommPort\bin\Debug\HexCommPort.pdb
?????文件????????180??2008-06-24?14:29??HexCommPort\HexCommPort\obj\Debug\HexCommPort.Form1.resources
?????文件????????180??2008-06-24?12:00??HexCommPort\HexCommPort\obj\Debug\HexCommPort.Properties.Resources.resources
?????文件??????24576??2008-06-24?16:00??HexCommPort\HexCommPort\obj\Debug\HexCommPort.exe
?????文件????????842??2008-06-24?14:29??HexCommPort\HexCommPort\obj\Debug\HexCommPort.csproj.GenerateResource.Cache
?????文件??????26112??2008-06-24?16:00??HexCommPort\HexCommPort\obj\Debug\HexCommPort.pdb
?????文件????????296??2008-06-24?16:00??HexCommPort\HexCommPort\obj\HexCommPort.csproj.FileList.txt
?????文件????????470??2008-06-24?11:47??HexCommPort\HexCommPort\Program.cs
?????文件???????4747??2008-06-24?14:29??HexCommPort\HexCommPort\Form1.Designer.cs
?????文件???????6008??2008-06-24?14:29??HexCommPort\HexCommPort\Form1.resx
?????文件????????922??2008-06-24?11:47??HexCommPort\HexCommPort.sln
????..A..H.?????11776??2008-06-24?16:48??HexCommPort\HexCommPort.suo
?????目錄??????????0??2008-06-24?11:47??HexCommPort\HexCommPort\obj\Debug\TempPE
?????目錄??????????0??2008-06-24?14:01??HexCommPort\HexCommPort\obj\Debug\Refactor
?????目錄??????????0??2008-06-24?11:47??HexCommPort\HexCommPort\bin\Debug
?????目錄??????????0??2008-06-24?11:47??HexCommPort\HexCommPort\obj\Debug
?????目錄??????????0??2008-06-24?11:47??HexCommPort\HexCommPort\Properties
?????目錄??????????0??2008-06-24?11:47??HexCommPort\HexCommPort\bin
?????目錄??????????0??2008-06-24?11:47??HexCommPort\HexCommPort\obj
?????目錄??????????0??2008-06-24?11:47??HexCommPort\HexCommPort
?????目錄??????????0??2008-06-24?11:47??HexCommPort
............此處省略3個文件信息
- 上一篇:C# TCP服務器,支持多個客服端連入
- 下一篇:asp.net實驗報告
評論
共有 條評論