本文整理汇总了C#中MissionPlanner.Comms.SerialPort.Write方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.Write方法的具体用法?C# SerialPort.Write怎么用?C# SerialPort.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MissionPlanner.Comms.SerialPort
的用法示例。
在下文中一共展示了SerialPort.Write方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: genstkv2packet
/// <summary>
/// STK v2 generate packet
/// </summary>
/// <param name="serialPort"></param>
/// <param name="message"></param>
/// <returns></returns>
static byte[] genstkv2packet(SerialPort serialPort, byte[] message)
{
byte[] data = new byte[300];
byte ck = 0;
data[0] = 0x1b;
ck ^= data[0];
data[1] = 0x1;
ck ^= data[1];
data[2] = (byte)((message.Length >> 8) & 0xff);
ck ^= data[2];
data[3] = (byte)(message.Length & 0xff);
ck ^= data[3];
data[4] = 0xe;
ck ^= data[4];
int a = 5;
foreach (byte let in message)
{
data[a] = let;
ck ^= let;
a++;
}
data[a] = ck;
a++;
serialPort.Write(data, 0, a);
//Console.WriteLine("about to read packet");
byte[] ret = BoardDetect.readpacket(serialPort);
//if (ret[1] == 0x0)
{
//Console.WriteLine("received OK");
}
return ret;
}
示例2: UploadFW
//.........这里部分代码省略.........
{
// default baud... guess
comPort.BaudRate = 57600;
}
else
{
comPort.BaudRate = MainV2.comPort.BaseStream.BaudRate;
}
try
{
comPort.Open();
}
catch
{
CustomMessageBox.Show("Error opening port", "Error");
return;
}
uploader.ProgressEvent += uploader_ProgressEvent;
uploader.LogEvent += uploader_LogEvent;
uploader_LogEvent("Trying Firmware Mode");
bootloadermode = false;
}
// check for either already bootloadermode, or if we can do a ATI to ID the firmware
if (bootloadermode || doConnect(comPort))
{
// put into bootloader mode/update mode
if (!bootloadermode)
{
try
{
comPort.Write("AT&UPDATE\r\n");
var left = comPort.ReadExisting();
log.Info(left);
Sleep(700);
comPort.BaudRate = 115200;
}
catch
{
}
if (upload_xmodem(comPort))
{
comPort.Close();
return;
}
comPort.BaudRate = 115200;
}
try
{
// force sync after changing baudrate
uploader.connect_and_sync();
}
catch
{
CustomMessageBox.Show("Failed to sync with Radio");
goto exit;
}
var device = Uploader.Board.FAILED;
var freq = Uploader.Frequency.FAILED;
示例3: DetectBoard
/// <summary>
/// Detects APM board version
/// </summary>
/// <param name="port"></param>
/// <returns> (1280/2560/2560-2/px4/px4v2)</returns>
public static boards DetectBoard(string port)
{
SerialPort serialPort = new SerialPort();
serialPort.PortName = port;
if (!MainV2.MONO)
{
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_SerialPort"); // Win32_USBControllerDevice
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject obj2 in searcher.Get())
{
Console.WriteLine("PNPID: " + obj2.Properties["PNPDeviceID"].Value.ToString());
// check vid and pid
if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_2341&PID_0010"))
{
// check port name as well
if (obj2.Properties["Name"].Value.ToString().ToUpper().Contains(serialPort.PortName.ToUpper()))
{
log.Info("is a 2560-2");
return boards.b2560v2;
}
}
if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0010"))
{
// check port name as well
//if (obj2.Properties["Name"].Value.ToString().ToUpper().Contains(serialPort.PortName.ToUpper()))
{
log.Info("is a px4");
return boards.px4;
}
}
if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0011"))
{
log.Info("is a px4v2");
return boards.px4v2;
}
if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0001"))
{
log.Info("is a px4v2 bootloader");
return boards.px4v2;
}
if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0016"))
{
log.Info("is a px4v2 bootloader");
CustomMessageBox.Show("You appear to have a bootloader with a bad PID value, please update your bootloader.");
return boards.px4v2;
}
//|| obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0012") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0013") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0014") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0015") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0016")
}
}
else
{
// if its mono
if (DialogResult.Yes == CustomMessageBox.Show("Is this a APM 2+?", "APM 2+", MessageBoxButtons.YesNo))
{
return boards.b2560v2;
}
else
{
if (DialogResult.Yes == CustomMessageBox.Show("Is this a PX4/PIXHAWK?", "PX4/PIXHAWK", MessageBoxButtons.YesNo))
{
if (DialogResult.Yes == CustomMessageBox.Show("Is this a PIXHAWK?", "PIXHAWK", MessageBoxButtons.YesNo))
{
return boards.px4v2;
}
return boards.px4;
}
else
{
return boards.b2560;
}
}
}
if (serialPort.IsOpen)
serialPort.Close();
serialPort.DtrEnable = true;
serialPort.BaudRate = 57600;
serialPort.Open();
Thread.Sleep(100);
int a = 0;
while (a < 20) // 20 * 50 = 1 sec
{
//Console.WriteLine("write " + DateTime.Now.Millisecond);
//.........这里部分代码省略.........
示例4: DetectVersion
/// <summary>
/// detects STK version 1 or 2
/// </summary>
/// <param name="port">comportname</param>
/// <returns>string either (1280/2560) or "" for none</returns>
public static string DetectVersion(string port)
{
SerialPort serialPort = new SerialPort();
serialPort.PortName = port;
if (serialPort.IsOpen)
serialPort.Close();
serialPort.DtrEnable = true;
serialPort.BaudRate = 57600;
serialPort.Open();
serialPort.toggleDTR();
Thread.Sleep(100);
int a = 0;
while (a < 20) // 20 * 50 = 1 sec
{
//Console.WriteLine("write " + DateTime.Now.Millisecond);
serialPort.DiscardInBuffer();
serialPort.Write(new byte[] { (byte)'0', (byte)' ' }, 0, 2);
a++;
Thread.Sleep(50);
//Console.WriteLine("btr {0}", serialPort.BytesToRead);
if (serialPort.BytesToRead >= 2)
{
byte b1 = (byte)serialPort.ReadByte();
byte b2 = (byte)serialPort.ReadByte();
if (b1 == 0x14 && b2 == 0x10)
{
serialPort.Close();
log.Info("is a 1280");
return "1280";
}
}
}
serialPort.Close();
log.Warn("Not a 1280");
Thread.Sleep(500);
serialPort.DtrEnable = true;
serialPort.BaudRate = 115200;
serialPort.Open();
serialPort.toggleDTR();
Thread.Sleep(100);
a = 0;
while (a < 4)
{
byte[] temp = new byte[] { 0x6, 0, 0, 0, 0 };
temp = ArduinoDetect.genstkv2packet(serialPort, temp);
a++;
Thread.Sleep(50);
try
{
if (temp[0] == 6 && temp[1] == 0 && temp.Length == 2)
{
serialPort.Close();
log.Info("is a 2560");
return "2560";
}
}
catch
{
}
}
serialPort.Close();
log.Warn("Not a 2560");
return "";
}