本文整理汇总了C#中ComPort.Open方法的典型用法代码示例。如果您正苦于以下问题:C# ComPort.Open方法的具体用法?C# ComPort.Open怎么用?C# ComPort.Open使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ComPort
的用法示例。
在下文中一共展示了ComPort.Open方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendGetData
private bool SendGetData(ComPort port, int totRead, bool close)
{
string exceptionMsg = "";
if (!port.IsOpen)
try
{
port.Open();
}
catch { }
if (port.IsOpen && port.Write(InputData))
{
//WinAPI.OutputDebugString("W");
int t = totRead;
byte[] buffer = new byte[512];
OutputData = new byte[0];
do
{
Array.Clear(buffer, 0, buffer.Length);
//WinAPI.OutputDebugString("R");
if (port.Read(ref buffer, out ReadedBytes))
{
Array.Resize<byte>(ref OutputData, (int)(OutputData.Length + ReadedBytes));
Array.Copy(buffer, 0, OutputData, OutputData.Length - ReadedBytes, ReadedBytes);
try
{
//WinAPI.OutputDebugString("D");
switch (DecodeAnswer())
{
case "busy":
{
//WinAPI.OutputDebugString("S");
Thread.Sleep(200);
break;
}
case "true":
{
//WinAPI.OutputDebugString("T");
ErrorFlags[lastFuncName] = false;
return true;
}
case "false":
{
//WinAPI.OutputDebugString("F");
t--;
Thread.Sleep(50);
break;
}
case "failed":
{
throw new Exception("������� ��������� ������� \"" + lastFuncName + "\"");
}
}
}
catch (Exception ex)
{
exceptionMsg = "\r\n" + ex.Message;
break;
}
}
else
break;
} while (t > 0);
//WinAPI.OutputDebugString("E");
}
if (close)
port.Close();
ErrorFlags[lastFuncName] = true;
throw new Exception("������� ������� � ���������� ��������" + " " + Protocol_Name + exceptionMsg);
}
示例2: LoadBMP
private void LoadBMP(ComPort port, ushort progPass, bool allow, string fpath)
{
lastFuncName = "LoadBMP";
NOM = 45;
byte[] binArr = null;
byte[] pass = Methods.GetByteArray((uint)progPass, 2);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(fpath);
byte[] img = new byte[bmp.Width / 8 * bmp.Height];
byte oneByte = 0x00;
int k = 0;
int wo = 0;
for (int h = 0, w = 0; h < bmp.Height; h++)
for (w = 0; w < (bmp.Width / 8 * 8); w += 8)
{
for (wo = 0; wo < 8; wo++)
{
oneByte <<= 1;
if (bmp.GetPixel(w + wo, h).Name == "ff000000")
oneByte |= 1;
}
img[k] = oneByte;
k++;
}
k = img.Length / 64;
if (img.Length % 64 != 0)
k++;
object[] imgBlocks = new object[k];
long idx = 0;
byte cs = 0;
for (k = 0; k < imgBlocks.Length; k++)
{
if (idx + 64 < img.Length)
binArr = new byte[64];
else
binArr = new byte[img.Length - idx];
Array.Copy(img, idx, binArr, 0, binArr.Length);
cs = (byte)(0 - Methods.SumMas(binArr));
Array.Resize<byte>(ref binArr, binArr.Length + 1);
binArr[binArr.Length - 1] = cs;
imgBlocks[k] = binArr;
idx += 64;
}
//Creating data
DataForSend = new byte[2 + 1 + 2 + 2];
DataForSend[0] = pass[0];
DataForSend[1] = pass[1];
if (allow)
DataForSend[2] = (byte)1;
else
DataForSend[2] = (byte)0;
binArr = Methods.GetByteArray(bmp.Width / 8 * 8, 2);
DataForSend[3] = binArr[0];
DataForSend[4] = binArr[1];
binArr = Methods.GetByteArray(bmp.Height, 2);
DataForSend[5] = binArr[0];
DataForSend[6] = binArr[1];
//Making data
InputData = CreateInputData(NOM, DataForSend);
//sending and getting data
if (!port.IsOpen)
port.Open();
byte[] buffer = new byte[1];
uint rb = 20;
int totRd = 20;
bool enqDetected = false;
port.Write(InputData);
for (k = 0; k < imgBlocks.Length; k++)
{
totRd = 20;
enqDetected = false;
buffer = new byte[1];
while (true)
{
if (totRd < 0)
break;
port.Read(ref buffer, out rb);
if (buffer[0] == ENQ)
{
port.PortClear();
enqDetected = true;
break;
}
if (buffer[0] == SYN)
continue;
totRd--;
}
if (!enqDetected)
break;
//.........这里部分代码省略.........
示例3: StartConnect
public bool StartConnect()
{
PortInitInfo p = new PortInitInfo(9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
Com = new ComPort(SysPara.ComPort, p);
if (Com.Open())
{
connected = true;
Program.WriteEventLog(this.GetType().FullName, string.Format("端口{0}打开成功!", SysPara.ComPort), "");
return true;
}
else
{
Program.WriteEventLog(this.GetType().FullName, string.Format("端口{0}打开失败!", SysPara.ComPort), "");
return false;
}
}
示例4: but_OpenCom_Click
private void but_OpenCom_Click(object sender, EventArgs e)
{
but_OpenCom.Enabled = false;
try
{
PortInitInfo p = new PortInitInfo(9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
cport = new ComPort(cbo_Port.Text, p);
if (cport.Open())
{
MessageBox.Show(string.Format("端口{0}打开成功!", cbo_Port.Text));
btn_Read.Enabled = true;
}
else
{
MessageBox.Show(string.Format("端口{0}打开失败!", cbo_Port.Text));
but_OpenCom.Enabled = true;
}
}
catch(Exception ex)
{
MessageBox.Show(string.Format("端口{0}打开失败!-{1}", cbo_Port.Text,ex.Message));
but_OpenCom.Enabled = true;
}
}