本文整理汇总了C#中ICommsSerial.Write方法的典型用法代码示例。如果您正苦于以下问题:C# ICommsSerial.Write方法的具体用法?C# ICommsSerial.Write怎么用?C# ICommsSerial.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICommsSerial
的用法示例。
在下文中一共展示了ICommsSerial.Write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: doCommand
public string doCommand(ICommsSerial comPort, string cmd, int level = 0)
{
if (!comPort.IsOpen)
return "";
comPort.ReadTimeout = 1000;
// setup to known state
comPort.Write("\r\n");
// alow some time to gather thoughts
Sleep(100);
// ignore all existing data
comPort.DiscardInBuffer();
lbl_status.Text = "Doing Command " + cmd;
log.Info("Doing Command " + cmd);
// write command
comPort.Write(cmd + "\r\n");
// read echoed line or existing data
string temp;
try
{
temp = Serial_ReadLine(comPort);
}
catch { temp = comPort.ReadExisting(); }
log.Info("cmd " + cmd + " echo " + temp);
// delay for command
Sleep(500);
// get responce
string ans = "";
while (comPort.BytesToRead > 0)
{
try
{
ans = ans + Serial_ReadLine(comPort) + "\n";
}
catch { ans = ans + comPort.ReadExisting() + "\n"; }
Sleep(50);
if (ans.Length > 500)
{
break;
}
}
log.Info("responce " + level + " " + ans.Replace('\0', ' '));
Regex pattern = new Regex(@"^\[([0-9+])\]\s+",RegexOptions.Multiline);
if (pattern.IsMatch(ans))
{
Match mat = pattern.Match(ans);
ans = pattern.Replace(ans,"");
}
// try again
if (ans == "" && level == 0)
return doCommand(comPort, cmd, 1);
return ans;
}
示例2: SendBlock
public static void SendBlock(FileStream fs, ICommsSerial Serial, int bNumber)
{
byte[] packet = new byte[133];
byte[] bits = new byte[128];
UInt16 CRC = 0;
for (int i = 0; i < bits.Length; i++) { bits[i] = 0x26; }
packet[0] = SOH;
packet[1] = (byte)(bNumber % 256);
packet[2] = (byte)(255 - (bNumber % 256));
var bytesRead = fs.Read(bits, 0, bits.Length);
if (bytesRead == bits.Length)
{
CRC = CRC_calc(bits, 128);
System.Buffer.BlockCopy(bits, 0, packet, 3, 128);
packet[131] = (byte)(CRC >> 8);
packet[132] = (byte)(CRC);
Serial.Write(packet, 0, packet.Length);
}
else if (bytesRead > 0)
{
CRC = CRC_calc(bits, 128);
System.Buffer.BlockCopy(bits, 0, packet, 3, 128);
packet[131] = (byte)(CRC >> 8);
packet[132] = (byte)(CRC);
Serial.Write(packet, 0, packet.Length);
Serial.Write("" + EOT);
CustomMessageBox.Show("Firmware upgraded successfully.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (bytesRead == 0)
{
Serial.Write("" + EOT);
CustomMessageBox.Show("Firmware upgraded successfully.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
示例3: turnon_off
public void turnon_off(ICommsSerial port, byte clas, byte subclass, byte every_xsamples)
{
byte[] datastruct1 = { clas, subclass, 0, every_xsamples, 0, every_xsamples, 0, 0 };
var packet = generate(0x6, 0x1, datastruct1);
port.Write(packet, 0, packet.Length);
}
示例4: SetupM8P
public void SetupM8P(ICommsSerial port)
{
// port config - 115200 - uart1
var packet = generate(0x6, 0x00, new byte[] { 0x01, 0x00, 0x00, 0x00, 0xD0, 0x08, 0x00, 0x00, 0x00, 0xC2,
0x01, 0x00, 0x23, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00 });
port.Write(packet, 0, packet.Length);
System.Threading.Thread.Sleep(100);
// set rate to 1hz
packet = generate(0x6, 0x8, new byte[] { 0xE8, 0x03, 0x01, 0x00, 0x01, 0x00 });
port.Write(packet, 0, packet.Length);
System.Threading.Thread.Sleep(100);
// surveyin msg - for feedback
packet = generate(0x6, 0x1, new byte[] { 0x01, 0x3b, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00 });
port.Write(packet, 0, packet.Length);
// pvt msg - for feedback
packet = generate(0x6, 0x1, new byte[] { 0x01, 0x7, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00 });
port.Write(packet, 0, packet.Length);
// 1005 - 5s
packet = generate(0x6, 0x1, new byte[] { 0xF5, 0x05, 0x00, 0x05, 0x00, 0x05, 0x00, 0x00 });
port.Write(packet, 0, packet.Length);
// 1077 - 1s
packet = generate(0x6, 0x1, new byte[] {0xF5, 0x4D, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00 });
port.Write(packet, 0, packet.Length);
// 1087 - 1s
packet = generate(0x6, 0x1, new byte[] { 0xF5, 0x57, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00 });
port.Write(packet, 0, packet.Length);
System.Threading.Thread.Sleep(100);
System.Threading.Thread.Sleep(100);
// survey in config - 60s and < 2m
packet = generate(0x6, 0x71, new byte[] {0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3C, 0x00,
0x00, 0x00, 0x20, 0x4E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
port.Write(packet, 0, packet.Length);
System.Threading.Thread.Sleep(100);
}
示例5: Terminal_Load
private void Terminal_Load(object sender, EventArgs e)
{
try
{
MainV2.comPort.giveComport = true;
comPort = MainV2.comPort.BaseStream;
if (comPort.IsOpen)
comPort.Close();
comPort.ReadBufferSize = 1024 * 1024;
comPort.PortName = MainV2.comPortName;
comPort.BaudRate = int.Parse(MainV2._connectionControl.CMB_baudrate.Text);
comPort.Open();
comPort.toggleDTR();
comPort.DiscardInBuffer();
Console.WriteLine("Terminal_Load");
System.Threading.Thread t11 = new System.Threading.Thread(delegate()
{
threadrun = true;
Console.WriteLine("Terminal thread start");
// 10 sec
waitandsleep(10000);
Console.WriteLine("Terminal thread 1");
// 100 ms
readandsleep(100);
Console.WriteLine("Terminal thread 2");
try
{
if (!inlogview)
comPort.Write("\n\n\n");
// 1 secs
if (!inlogview)
readandsleep(1000);
if (!inlogview)
comPort.Write("\r\r\r?\r");
}
catch (Exception ex) { Console.WriteLine("Terminal thread 3 " + ex.ToString()); threadrun = false; return; }
Console.WriteLine("Terminal thread 3");
while (threadrun)
{
try
{
System.Threading.Thread.Sleep(10);
if (inlogview)
continue;
if (!comPort.IsOpen)
break;
if (comPort.BytesToRead > 0)
{
comPort_DataReceived((object)null, (SerialDataReceivedEventArgs)null);
}
}
catch (Exception ex) { Console.WriteLine("Terminal thread 4 " + ex.ToString()); }
}
threadrun = false;
try
{
comPort.DtrEnable = false;
}
catch { }
try
{
Console.WriteLine("term thread close");
comPort.Close();
}
catch { }
Console.WriteLine("Comport thread close");
});
t11.IsBackground = true;
t11.Name = "Terminal serial thread";
t11.Start();
// doesnt seem to work on mac
//comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
TXT_terminal.AppendText("Opened com port\r\n");
inputStartPos = TXT_terminal.SelectionStart;
}
catch (Exception) { TXT_terminal.AppendText("Cant open serial port\r\n"); return; }
//.........这里部分代码省略.........
示例6: Log_Load
private void Log_Load(object sender, EventArgs e)
{
status = serialstatus.Connecting;
comPort = GCSViews.Terminal.comPort;
try
{
Console.WriteLine("Log_load " + comPort.IsOpen);
if (!comPort.IsOpen)
comPort.Open();
//Console.WriteLine("Log dtr");
//comPort.toggleDTR();
Console.WriteLine("Log discard");
comPort.DiscardInBuffer();
Console.WriteLine("Log w&sleep");
try
{
// try provoke a response
comPort.Write("\n\n?\r\n\n");
}
catch
{
}
// 10 sec
waitandsleep(10000);
}
catch (Exception ex)
{
log.Error("Error opening comport", ex);
CustomMessageBox.Show("Error opening comport");
return;
}
var t11 = new System.Threading.Thread(delegate()
{
var start = DateTime.Now;
threadrun = true;
if (comPort.IsOpen)
readandsleep(100);
try
{
if (comPort.IsOpen)
comPort.Write("\n\n\n\nexit\r\nlogs\r\n"); // more in "connecting"
}
catch
{
}
while (threadrun)
{
try
{
updateDisplay();
System.Threading.Thread.Sleep(1);
if (!comPort.IsOpen)
break;
while (comPort.BytesToRead >= 4)
{
comPort_DataReceived((object) null, (SerialDataReceivedEventArgs) null);
}
}
catch (Exception ex)
{
log.Error("crash in comport reader " + ex);
} // cant exit unless told to
}
log.Info("Comport thread close");
}) {Name = "comport reader", IsBackground = true};
t11.Start();
// doesnt seem to work on mac
//comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
}
示例7: Log_Load
private void Log_Load(object sender, EventArgs e)
{
status = serialstatus.Connecting;
comPort = MainV2.comPort.BaseStream;
//comPort.ReceivedBytesThreshold = 50;
//comPort.ReadBufferSize = 1024 * 1024;
try
{
comPort.DtrEnable = false;
System.Threading.Thread.Sleep(100);
comPort.DtrEnable = true;
//comPort.Open();
}
catch (Exception)
{
MessageBox.Show("Error opening comport");
}
System.Threading.Thread t11 = new System.Threading.Thread(delegate()
{
DateTime start = DateTime.Now;
threadrun = true;
System.Threading.Thread.Sleep(2000);
try
{
comPort.Write("\n\n\n\n");
}
catch { }
while (threadrun)
{
try
{
updateDisplay();
System.Threading.Thread.Sleep(10);
if (!comPort.IsOpen)
break;
while (comPort.BytesToRead >= 4)
{
comPort_DataReceived((object)null, (SerialDataReceivedEventArgs)null);
}
}
catch (Exception ex) { Console.WriteLine("crash in comport reader " + ex.ToString()); } // cant exit unless told to
}
Console.WriteLine("Comport thread close");
});
t11.Name = "comport reader";
t11.Start();
MainV2.threads.Add(t11);
// doesnt seem to work on mac
//comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
}
示例8: Log_Load
private void Log_Load(object sender, EventArgs e)
{
if (MainV2.config["log_isarducopter"] != null)
{
CHK_arducopter.Checked = bool.Parse(MainV2.config["log_isarducopter"].ToString());
CHK_arduplane.Checked = bool.Parse(MainV2.config["log_isarduplane"].ToString());
CHK_ardurover.Checked = bool.Parse(MainV2.config["log_isardurover"].ToString());
}
status = serialstatus.Connecting;
MainV2.comPort.giveComport = true;
comPort = MainV2.comPort.BaseStream;
comPort.DtrEnable = false;
comPort.RtsEnable = false;
try
{
Console.WriteLine("Log_load " + comPort.IsOpen);
if (!comPort.IsOpen)
comPort.Open();
Console.WriteLine("Log dtr");
comPort.toggleDTR();
Console.WriteLine("Log discard");
comPort.DiscardInBuffer();
Console.WriteLine("Log w&sleep");
try
{
// try provoke a responce
comPort.Write("\n\n?\r\n\n");
}
catch
{
}
// 10 sec
waitandsleep(10000);
}
catch (Exception ex)
{
log.Error("Error opening comport", ex);
CustomMessageBox.Show("Error opening comport");
}
var t11 = new System.Threading.Thread(delegate()
{
var start = DateTime.Now;
threadrun = true;
readandsleep(100);
try
{
comPort.Write("\n\n\n\nexit\r\nlogs\r\n"); // more in "connecting"
}
catch
{
}
while (threadrun)
{
try
{
updateDisplay();
System.Threading.Thread.Sleep(10);
if (!comPort.IsOpen)
break;
while (comPort.BytesToRead >= 4)
{
comPort_DataReceived((object)null, (SerialDataReceivedEventArgs)null);
}
}
catch (Exception ex)
{
log.Error("crash in comport reader " + ex);
} // cant exit unless told to
}
log.Info("Comport thread close");
}) {Name = "comport reader"};
t11.Start();
// doesnt seem to work on mac
//comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
}
示例9: doConnect
public bool doConnect(ICommsSerial comPort)
{
try
{
Console.WriteLine("doConnect");
// setup a known enviroment
comPort.Write("ATO\r\n");
// wait
Sleep(1100, comPort);
comPort.DiscardInBuffer();
// send config string
comPort.Write("+++");
Sleep(1100,comPort);
// check for config response "OK"
log.Info("Connect btr " + comPort.BytesToRead + " baud " + comPort.BaudRate);
// allow time for data/response
byte[] buffer = new byte[20];
int len = comPort.Read(buffer, 0, buffer.Length);
string conn = ASCIIEncoding.ASCII.GetString(buffer, 0, len);
log.Info("Connect first response " + conn.Replace('\0', ' ') + " " + conn.Length);
if (conn.Contains("OK"))
{
//return true;
}
else
{
// cleanup incase we are already in cmd mode
comPort.Write("\r\n");
}
doCommand(comPort, "AT&T");
string version = doCommand(comPort, "ATI");
log.Info("Connect Version: " + version.Trim() + "\n");
Regex regex = new Regex(@"SiK\s+(.*)\s+on\s+(.*)");
if (regex.IsMatch(version))
{
return true;
}
return false;
}
catch { return false; }
}
示例10: upload_xmodem
bool upload_xmodem(ICommsSerial comPort)
{
// try xmodem mode
// xmodem - short cts to ground
try
{
uploader_LogEvent("Trying XModem Mode");
//comPort.BaudRate = 57600;
comPort.BaudRate = MainV2.comPort.BaseStream.BaudRate;
comPort.ReadTimeout = 1000;
Thread.Sleep(2000);
var tempd = comPort.ReadExisting();
Console.WriteLine(tempd);
comPort.Write("U");
Thread.Sleep(1000);
var resp1 = Serial_ReadLine(comPort); // echo
var resp2 = Serial_ReadLine(comPort); // echo 2
var tempd2 = comPort.ReadExisting(); // posibly bootloader info / use to sync
// identify
comPort.Write("i");
// responce is rfd900....
var resp3 = Serial_ReadLine(comPort); //echo
var resp4 = Serial_ReadLine(comPort); // newline
var resp5 = Serial_ReadLine(comPort); // bootloader info
uploader_LogEvent(resp5);
if (resp5.Contains("RFD900"))
{
// start upload
comPort.Write("u");
var resp6 = Serial_ReadLine(comPort); // echo
var resp7 = Serial_ReadLine(comPort); // Ready
if (resp7.Contains("Ready"))
{
comPort.ReadTimeout = 3500;
// responce is C
var isC = comPort.ReadByte();
var temp = comPort.ReadExisting();
if (isC == 'C')
{
XModem.LogEvent += uploader_LogEvent;
XModem.ProgressEvent += uploader_ProgressEvent;
// start file send
XModem.Upload(@"SiK900x.bin",
comPort);
XModem.LogEvent -= uploader_LogEvent;
XModem.ProgressEvent -= uploader_ProgressEvent;
return true;
}
}
}
}
catch (Exception ex2)
{
log.Error(ex2);
}
return false;
}
示例11: poll_msg
public void poll_msg(ICommsSerial port, byte clas, byte subclass)
{
byte[] datastruct1 = { };
var packet = generate(clas, subclass, datastruct1);
port.Write(packet, 0, packet.Length);
System.Threading.Thread.Sleep(10);
}
示例12: SetupM8P
public void SetupM8P(ICommsSerial port, PointLatLngAlt basepos, int surveyindur = 0, double surveyinacc = 0)
{
port.BaseStream.Flush();
// port config - 115200 - uart1
var packet = generate(0x6, 0x00, new byte[] { 0x01, 0x00, 0x00, 0x00, 0xD0, 0x08, 0x00, 0x00, 0x00, 0xC2,
0x01, 0x00, 0x23, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00 });
port.Write(packet, 0, packet.Length);
System.Threading.Thread.Sleep(300);
// port config - usb
packet = generate(0x6, 0x00, new byte[] { 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x23, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00 });
port.Write(packet, 0, packet.Length);
System.Threading.Thread.Sleep(300);
// set rate to 1hz
packet = generate(0x6, 0x8, new byte[] { 0xE8, 0x03, 0x01, 0x00, 0x01, 0x00 });
port.Write(packet, 0, packet.Length);
System.Threading.Thread.Sleep(200);
// set navmode to stationary
packet = generate(0x6, 0x24, new byte[] { 0xFF ,0xFF ,0x02 ,0x03 ,0x00 ,0x00 ,0x00 ,0x00 ,0x10 ,0x27 ,0x00 ,0x00 ,0x05 ,0x00
,0xFA ,0x00 ,0xFA ,0x00 ,0x64 ,0x00 ,0x2C ,0x01 ,0x00 ,0x00 ,0x00 ,0x00 ,0x10 ,0x27 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00
,0x00 ,0x00 });
port.Write(packet, 0, packet.Length);
System.Threading.Thread.Sleep(200);
// turn off all nmea
for (int a = 0; a <= 0xf; a++)
{
if (a == 0xb || a == 0xc || a == 0xe)
continue;
turnon_off(port, 0xf0, (byte)a, 0);
}
// mon-ver
poll_msg(port, 0xa, 0x4);
// surveyin msg - for feedback
turnon_off(port, 0x01, 0x3b, 1);
// pvt msg - for feedback
turnon_off(port, 0x01, 0x07, 1);
// 1005 - 5s
turnon_off(port, 0xf5, 0x05, 5);
// 1077 - 1s
turnon_off(port, 0xf5, 0x4d, 1);
// 1087 - 1s
turnon_off(port, 0xf5, 0x57, 1);
// rxm-raw/rawx - 1s
turnon_off(port, 0x02, 0x15, 1);
turnon_off(port, 0x02, 0x10, 1);
// rxm-sfrb/sfrb - 5s
turnon_off(port, 0x02, 0x13, 5);
turnon_off(port, 0x02, 0x11, 5);
System.Threading.Thread.Sleep(100);
System.Threading.Thread.Sleep(100);
if (basepos == PointLatLngAlt.Zero)
{
// survey in config - 60s and < 2m
packet = generate(0x6, 0x71, new ubx_cfg_tmode3((uint)surveyindur, surveyinacc));
port.Write(packet, 0, packet.Length);
}
else
{
byte[] data = new ubx_cfg_tmode3(basepos.Lat, basepos.Lng, basepos.Alt);
packet = generate(0x6, 0x71, data);
port.Write(packet, 0, packet.Length);
}
System.Threading.Thread.Sleep(100);
}
示例13: Upload
public static void Upload(string firmwarebin, ICommsSerial comPort)
{
var fs = new FileStream(firmwarebin, FileMode.Open);
var len = (int)fs.Length;
len = (len % 128) == 0 ? len / 128 : (len / 128) + 1;
var startlen = len;
int a = 1;
while (len > 0)
{
if (LogEvent != null)
LogEvent("Uploading block " + a + "/" + startlen);
XModem.SendBlock(fs, comPort, a);
// responce ACK
var ack = comPort.ReadByte();
while (ack == 'C')
ack = comPort.ReadByte();
if (ack==ACK)
{
len--;
a++;
if (ProgressEvent != null)
ProgressEvent(len / startlen);
}
else if (ack==NAK)
{
CustomMessageBox.Show("Corrupted packet. Please power cycle and try again.\r\n", "Warning",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
len = 0;
}
}
// boot
comPort.Write("b");
}
示例14: Log_Load
private void Log_Load(object sender, EventArgs e)
{
log.Info("state ->Connecting\r");
status = serialstatus.Connecting;
connect_substate = 0;
comPort = GCSViews.Terminal.comPort;
t11 = new System.Threading.Thread(delegate()
{
var start = DateTime.Now;
bool threaderror = false;
threadrun = true;
try
{
comPort.Write("exit\rlogs\r"); // more in "connecting"
}
catch (Exception ex)
{
log.Info("state ->Error\r");
status = serialstatus.Error;
log.Error("Error in comport thread " + ex);
threaderror = true;
}
while (threadrun)
{
try
{
updateDisplay();
System.Threading.Thread.Sleep(1);
while (comPort.BytesToRead > 0)
{
comPort_DataReceived((object)null, (SerialDataReceivedEventArgs)null);
}
}
catch (Exception ex)
{
if (!threaderror)
{
log.Info("state ->Error\r");
status = serialstatus.Error;
log.Error("Error in comport thread " + ex);
threaderror = true;
}
} // cant exit unless told to
}
log.Info("Comport thread close");
});
t11.IsBackground = true;
t11.Name = "Log serial thread";
t11.Start();
// doesnt seem to work on mac
//comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
}
示例15: Log_Load
private void Log_Load(object sender, EventArgs e)
{
status = serialstatus.Connecting;
comPort = MainV2.comPort.BaseStream;
//comPort.ReceivedBytesThreshold = 50;
//comPort.ReadBufferSize = 1024 * 1024;
try
{
comPort.toggleDTR();
//comPort.Open();
}
catch (Exception ex)
{
log.Error("Error opening comport", ex);
CustomMessageBox.Show("Error opening comport");
}
var t11 = new System.Threading.Thread(delegate()
{
var start = DateTime.Now;
threadrun = true;
System.Threading.Thread.Sleep(2000);
try
{
comPort.Write("\n\n\n\n"); // more in "connecting"
}
catch
{
}
while (threadrun)
{
try
{
updateDisplay();
System.Threading.Thread.Sleep(10);
if (!comPort.IsOpen)
break;
while (comPort.BytesToRead >= 4)
{
comPort_DataReceived((object)null, (SerialDataReceivedEventArgs)null);
}
}
catch (Exception ex)
{
log.Error("crash in comport reader " + ex);
} // cant exit unless told to
}
log.Info("Comport thread close");
}) {Name = "comport reader"};
t11.Start();
// doesnt seem to work on mac
//comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
}