本文整理汇总了C#中ComPort.Write方法的典型用法代码示例。如果您正苦于以下问题:C# ComPort.Write方法的具体用法?C# ComPort.Write怎么用?C# ComPort.Write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ComPort
的用法示例。
在下文中一共展示了ComPort.Write方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TransPrint
private void TransPrint(ComPort port, string line, bool close)
{
lastFuncName = "TransPrint";
NOM = 40;
//Creating data
DataForSend = new byte[1 + line.Length];
DataForSend[0] = (byte)line.Length;
byte[] BinLine = Encoding.GetEncoding(866).GetBytes(line.Replace('�', 'i').Replace('�', 'I'));
for (byte i = 0; i < BinLine.Length; i++)
DataForSend[i + 1] = BinLine[i];
//Making data
InputData = CreateInputData(NOM, DataForSend);
//sending and getting data
SendGetData(port, 20, true);
//Next code for command
GetNextCmdCode();
//System.Threading.Thread.Sleep(200);
if (close)
{
//Creating data
DataForSend = new byte[1];
DataForSend[0] = 0xFF;
//Making data
InputData = CreateInputData(NOM, DataForSend);
//sending and getting data
if (port.IsOpen)
if (port.Write(InputData))
if (port.Read(ref OutputData, out ReadedBytes))
DecodeAnswer();
//Next code for command
GetNextCmdCode();
}
}
示例2: 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);
}
示例3: 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;
//.........这里部分代码省略.........