?? serialport sample.txt
字號:
//serialport sample
using System;
using System.IO.Ports;
using System.Windows.Forms;
namespace SPC
{
/// <summary>
/// 串口控制
/// </summary>
public class PortControl
{
/// <summary>
/// 定義一個串口類
/// </summary>
private SerialPort MyPort;
/// <summary>
/// 初始化類
/// </summary>
public PortControl()
{
MyPort = new SerialPort();
setup();
}
/// <summary>
/// 直接使用給某個串口
/// </summary>
/// <param name="port">COM1,COM2。。。。。。</param>
public PortControl(string port)
{
_portname = port;
MyPort = new SerialPort(_portname);
setup();
}
private void setup()
{
MyPort.DataReceived += new SerialDataReceivedEventHandler(DataReceived);
}
public bool Open()
{
try
{
if (MyPort.IsOpen != true) { MyPort.Open(); };
return true ;
}
catch
{
return false;
}
}
public void Open(string port)
{
MyPort.PortName = _portname;
MyPort.Open();
}
public void Close()
{
MyPort.Close();
}
private string _portname;
/// <summary>
/// 端口名稱
/// </summary>
/// <example>COM1 COM2</example>
public string PortName
{
get { return _portname; }
set { _portname = value; }
}
public Exception LastException;
/// <summary>
/// 最后收到的信息
/// </summary>
public string LastReceived
{
get { return hex_return; }
}
public bool Received = false;//是否收到了信息。 發送完數據,如果需要車檢器返回數據,該屬性設置為false;
//當收到消息,并解析完成后。這個設置為true;
string hex_return = "";//收到數據后把十六進制存到這個字符串中。
byte[] bin_return = new byte[] { };
double _timeout = 0.8;
public double TimeOut
{
get { return _timeout; }
set { _timeout = value; }
}
/// <summary>
/// 向端口中發送命令。
/// </summary>
/// <param name="cmdstring">"0A 46 0B 31 30 30 32 35"</param>
/// <example> Send("0A 46 0B 31 30 30 32 35")</example>
/// <remarks>超時設置為5秒,一般來說,端口速度不會延時超過1秒。</remarks>
/// <summary>
/// 向端口中發送命令。
/// </summary>
/// <param name="cmdstring">"0A 46 0B 31 30 30 32 35"</param>
/// <param name="timeout">指定超時,按秒計算。端口速度一般不會遲延超過1秒。</param>
/// <example> Send("0A 46 0B 31 30 30 32 35")</example>
public string Send(string cmdstring)
{
byte[] buff = Funs.HexStringToBinary(cmdstring.Trim());//轉換命令字符串為字節數組
hex_return = "";//十六進制返回設置為空。
bin_return = new byte[] { };//重新初始化返回字節數組。
Received = false;//設置標識為沒有接受到信息
MyPort.Write(buff, 0, buff.Length);//寫入串口命令。
double tmpx = DateTime.Now.TimeOfDay.TotalSeconds;//記錄下當前總計秒數
do
{
Application.DoEvents();//釋放CPU
} while ((Received != true) && (DateTime.Now.TimeOfDay.TotalSeconds - tmpx < _timeout));
if (Received == false) { return null; }
//如果接受到了數據或者已超時就不再循環。
string rt;//初始化返回內容。
int sum = 0;//初始化命令總數。
for (int i = 3; i < bin_return.Length - 2; i++)
{
sum += bin_return[i];
}
int sum1 = bin_return[bin_return.Length - 2] + bin_return[bin_return.Length - 1]*256;
if (
sum != sum1
&&
bin_return[bin_return.Length - 3] == 3
)
{ rt = null; }
else
{
rt = Funs.BinaryToHexString(bin_return);
}
return rt;
}
/// <summary>
/// 如果接受到了數據。
/// </summary>
/// <param name="sender">發送者</param>
/// <param name="e">時間參數</param>
private void DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//MyPort.DiscardInBuffer(); //丟棄事件發生前的數據。
int n = MyPort.BytesToRead;//讀取當前有多少數據可讀。
byte[] binary = new byte[n];//新建內容。
MyPort.Read(binary, 0, n);//讀取
Array.Resize(ref bin_return, bin_return.Length + n);//改寫數組大小
if (bin_return.Length < 1) { return; }
binary.CopyTo(bin_return, bin_return.Length - binary.Length );//復制數據。
int infleng = 0;
//16 16 02 10 01 02 00 07 00 03 1D 00
if (bin_return.Length > 7)//基本信息頭應該是7個字節。
{
if (bin_return[0] == bin_return[1] && bin_return[0]== 22 && bin_return[2] == 2)
//如果第零字節和第一字節相等,并且第二自己為0,那么表示信息的開頭 。
{
//計算第五字節和第六字節。這兩個字節表示長度。
infleng = bin_return[5] + bin_return[6] * 256;
}
}
else if (bin_return.Length == 3)
{
Received = true;
return;
}
Received = ((10 + infleng) == bin_return.Length);//命令基本格式包含10個字節。
//加上信息長度如果等于bin接收到的長度的話。說明接受完了。接受完了就設置receive為真。
Console.WriteLine(String.Format("讀取字節數:{0}:內容:{1}----{2}", n, hex_return,e.EventType));
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -