|
|
@@ -146,7 +146,7 @@ namespace HBLConsole.Communication |
|
|
|
/// <summary> |
|
|
|
/// 判断是否有这个串口 |
|
|
|
/// </summary> |
|
|
|
public bool IsHavePort => SerialPort.GetPortNames().GetIndex(PortName) > 0; |
|
|
|
public bool IsHavePort => SerialPort.GetPortNames().Contains(PortName); |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// 打开端口 |
|
|
@@ -154,14 +154,17 @@ namespace HBLConsole.Communication |
|
|
|
/// <returns></returns> |
|
|
|
public void Open() |
|
|
|
{ |
|
|
|
if (!IsHavePort) return; |
|
|
|
if (comPort.IsOpen) comPort.Close(); |
|
|
|
comPort.PortName = portName; |
|
|
|
comPort.BaudRate = (int)baudRate; |
|
|
|
comPort.Parity = parity; |
|
|
|
comPort.DataBits = (int)dataBits; |
|
|
|
comPort.StopBits = stopBits; |
|
|
|
comPort.Open(); |
|
|
|
lock (lck4Serial) |
|
|
|
{ |
|
|
|
if (!IsHavePort) return; |
|
|
|
if (comPort.IsOpen) comPort.Close(); |
|
|
|
comPort.PortName = portName; |
|
|
|
comPort.BaudRate = (int)baudRate; |
|
|
|
comPort.Parity = parity; |
|
|
|
comPort.DataBits = (int)dataBits; |
|
|
|
comPort.StopBits = stopBits; |
|
|
|
comPort.Open(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary> |
|
|
@@ -183,6 +186,7 @@ namespace HBLConsole.Communication |
|
|
|
} |
|
|
|
#endregion |
|
|
|
|
|
|
|
private object lck4Serial = new object(); |
|
|
|
#region 写入数据 |
|
|
|
/// <summary> |
|
|
|
/// 写入数据 |
|
|
@@ -190,9 +194,12 @@ namespace HBLConsole.Communication |
|
|
|
/// <param name="buffer"></param> |
|
|
|
public void Write(byte[] buffer, int offset, int count) |
|
|
|
{ |
|
|
|
if (!IsHavePort) return; |
|
|
|
if (!(comPort.IsOpen)) comPort.Open(); |
|
|
|
comPort.Write(buffer, offset, count); |
|
|
|
lock (lck4Serial) |
|
|
|
{ |
|
|
|
if (!IsHavePort) return; |
|
|
|
if (!(comPort.IsOpen)) comPort.Open(); |
|
|
|
comPort.Write(buffer, offset, count); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary> |
|
|
@@ -201,9 +208,13 @@ namespace HBLConsole.Communication |
|
|
|
/// <param name="buffer">写入端口的字节数组</param> |
|
|
|
public void Write(byte[] buffer) |
|
|
|
{ |
|
|
|
if (!IsHavePort) return; |
|
|
|
if (!(comPort.IsOpen)) comPort.Open(); |
|
|
|
comPort.Write(buffer, 0, buffer.Length); |
|
|
|
lock (lck4Serial) |
|
|
|
{ |
|
|
|
if (!IsHavePort) return; |
|
|
|
if (!(comPort.IsOpen)) comPort.Open(); |
|
|
|
comPort.Write(buffer, 0, buffer.Length); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public void Start() |
|
|
|