本文整理汇总了C#中System.IO.Ports.SerialPort.Write方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.Write方法的具体用法?C# SerialPort.Write怎么用?C# SerialPort.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Ports.SerialPort
的用法示例。
在下文中一共展示了SerialPort.Write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsPVC
bool IsPVC(SerialPort port)
{
string returnMessage = "";
try {
if (port.IsOpen)
port.Close ();
port.Open ();
Thread.Sleep (1500); // Fails if this delay is any shorter
port.Write ("#");
port.Write (port.NewLine);
Thread.Sleep (500); // Fails if this delay is any shorter
int count = port.BytesToRead;
int intReturnASCII = 0;
while (count > 0) {
intReturnASCII = port.ReadByte ();
returnMessage = returnMessage + Convert.ToChar (intReturnASCII);
count--;
}
} catch {
} finally {
port.Close ();
}
if (returnMessage.Contains (Identifier)) {
return true;
} else {
return false;
}
}
示例2: WritePage
public static void WritePage(byte[] pageData, int pageNumber, int pageSize, SerialPort port)
{
port.Write(new byte[1]{Convert.ToByte(pageNumber)},0,1);
port.Write(pageData,pageNumber*pageSize,pageSize);
if(port.ReadChar()!='A')
throw new Exception("No acknowledge from target!");
}
示例3: buttonSend_Click
private void buttonSend_Click(object sender, EventArgs e)
{
string number = textBoxNumber.Text;
string message = textBoxMessage.Text;
_serialPort = new SerialPort("COM7", 115200); //Replace "COM7" with corresponding port name
Thread.Sleep(1000);
_serialPort.Open();
Thread.Sleep(1000);
_serialPort.Write("AT+CMGF=1\r");
Thread.Sleep(1000);
_serialPort.Write("AT+CMGS=\"" + number + "\"\r\n");
Thread.Sleep(1000);
_serialPort.Write(message + "\x1A");
Thread.Sleep(1000);
labelStatus.Text = "Status: Message sent";
_serialPort.Close();
}
示例4: Identify
public string Identify(SerialPort port)
{
string returnMessage = "";
try {
if (port.IsOpen)
port.Close ();
port.Open ();
Thread.Sleep (1500); // Fails sometimes if this delay is any shorter
port.Write (IdentifyRequest);
port.Write (port.NewLine);
Thread.Sleep (500); // Fails sometimes if this delay is any shorter
int count = port.BytesToRead;
int intReturnASCII = 0;
while (count > 0) {
intReturnASCII = port.ReadByte ();
returnMessage = returnMessage + Convert.ToChar (intReturnASCII);
count--;
}
} catch {
} finally {
port.Close ();
}
return returnMessage.Trim();
}
示例5: Send
public void Send(SerialPort SP)
{
SP.Write(new byte[] { Command }, 0, 1);
SP.Write(new byte[] { (byte)(PageNumber >> 8), (byte)(PageNumber & 0xFF) }, 0, 2);
SP.Write(new byte[] { Part }, 0, 1);
SP.Write(new byte[] { (byte)(CRC16 >> 8), (byte)(CRC16 & 0xFF) }, 0, 2);
SP.Write(Payload, 0, Payload.Length); // should be 32 bytes in length
}
示例6: GetBridgeSignature
// Gets the uart->spi bridge's signature (MSP430 etc)
public static string GetBridgeSignature(SerialPort sp)
{
//sp.Write("{g00}");
sp.Write("{");
sp.Write("g");
sp.Write("0");
sp.Write("0");
sp.Write("}");
return sp.ReadLine();
}
示例7: SetColor
public void SetColor(int r, int g, int b)
{
SerialPort sp = new SerialPort("COM7", 9600);
sp.Open();
sp.Write(new Byte[] {0xFF },0,1);
sp.Write(new Byte[] { (Byte)(r) }, 0, 1);
sp.Write(new Byte[] { (Byte)(g) }, 0, 1);
sp.Write(new Byte[] { (Byte)(b) }, 0, 1);
sp.Close();
}
示例8: Write
static void Write(SerialPort port, string data, bool sleep)
{
foreach (char c in data)
{
port.Write(new string(c, 1));
if (sleep)
Thread.Sleep(50);
}
port.Write("\r");
}
示例9: Main
static void Main(string[] args)
{
SerialPort comm;
string portName;
long portNum;
long block;
string input;
string filename;
long pageIndex;
byte inData;
byte checksum;
byte[] buf = new byte[1];
if (args.Length < 1)
return;
Console.Write("COM Port ? ");
input = Console.ReadLine();
portNum = long.Parse(input);
portName = "COM" + portNum.ToString();
//filename = args[0];
filename = "C:\\badge\\out.eeprom";
comm = new SerialPort(portName, 115200, Parity.None, 8, StopBits.One);
comm.Open();
BinaryReader br = new BinaryReader(File.Open(filename, FileMode.Open));
byte data;
// Send the 100 code to start things off
buf[0] = (byte)100;
comm.Write(buf, 0, 1);
for (pageIndex = 0; pageIndex < 256; pageIndex++)
{
inData = (byte)comm.ReadByte();
Console.WriteLine("Writing page " + inData.ToString());
for (int i = 0; i < 128; i++)
{
data = br.ReadByte();
buf[0] = data;
comm.Write(buf, 0, 1);
}
}
comm.Close();
Console.WriteLine("Complete");
}
示例10: openSerial
public Boolean openSerial(String port, String arduinoVer)
{
if (SP != null && SP.IsOpen)
SP.Close();
if (port == "")
return false;
console("Opening Serial Port on: " + port);
SP = new SerialPort(port, 9600, Parity.None, 8);
SP.Open();
open = true;
//handshake
SP.ReadTimeout = 5000;
SP.Write("1");
String s = "";
int t = 0;
while (s.Length < 4 && t < 500)
{
System.Threading.Thread.Sleep(10);
s = SP.ReadExisting();
if (!s.Contains("."))
s = "";
else
s = s.Split('.')[1];
SP.Write("1");
t += 1;
}
String av = arduinoVer.Replace(@".", string.Empty);
if (av.Length < 4)
{
av = av[0] + "" + av[1] + "0" + av[2];
}
if (s == "")
console("No Responce from Arduino, going ahead with connection but errors may occour");
else
{
s = s.Substring(s.Length - 4, 4);
if (Convert.ToInt16(s) < Convert.ToInt16(av))
{
console("Arduino Code Outdated(v" + s[0] + "." + s[1] + "." + s[2] + s[3] + "). Please Update Arduino to at least v" + arduinoVer + " and then Retry");
return false;
}
console("Handshake Sucsessful, Connected to Arduino Running: v" + s[0] + "." + s[1] + "." + s[2] + s[3]);
}
return true;
}
示例11: Actuate
public void Actuate()
{
using (var serial = new SerialPort(Settings.ComPort))
{
if (!serial.IsOpen) serial.Open();
if (!serial.IsOpen) return;
serial.Write(new byte[] {0xFF, 0x01, 0x01}, 0, 3);
Thread.Sleep(Settings.ActuateTime);
serial.Write(new byte[] {0xFF, 0x01, 0x00}, 0, 3);
serial.Close();
}
}
示例12: Main
public static void Main()
{
// A program to set the baud rate on a new HC-06 to
// 115200
//
SerialPort blueComms = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One);
blueComms.Open();
//
// Rename the device
//
byte[] outBuffer = System.Text.Encoding.UTF8.GetBytes("AT+NAMEHC_A");
blueComms.Write(outBuffer, 0, outBuffer.Length);
Thread.Sleep(2000);
while (blueComms.BytesToRead > 0)
{
byte[] a = new byte[1] { 0 };
blueComms.Read(a, 0, 1);
char[] cc = System.Text.Encoding.UTF8.GetChars(a, 0, a.Length);
Debug.Print(cc.ToString());
Thread.Sleep(10);
}
//
// Set the baud rate.
//
outBuffer = System.Text.Encoding.UTF8.GetBytes("AT+BAUD8");
blueComms.Write(outBuffer, 0, outBuffer.Length);
Thread.Sleep(2000);
blueComms.Close();
SerialPort blueFast = new SerialPort(SerialPorts.COM1, 115200, Parity.None, 8, StopBits.One);
blueFast.Open();
outBuffer = System.Text.Encoding.UTF8.GetBytes("AT");
blueComms.Write(outBuffer, 0, outBuffer.Length);
Debug.Print("After Baud rate set");
while (blueComms.BytesToRead > 0)
{
byte[] a = new byte[1] { 0 };
blueComms.Read(a, 0, 1);
char[] cc = System.Text.Encoding.UTF8.GetChars(a, 0, a.Length);
Debug.Print(cc.ToString());
Thread.Sleep(10);
}
}
示例13: Main
static void Main(string[] args)
{
SerialPort mySerialPort = new SerialPort("COM3");
mySerialPort.BaudRate = 9600;
mySerialPort.Open();
while (running)
{
//string myData = mySerialPort.ReadLine();
//Console.WriteLine(myData);
mySerialPort.Write("p");
System.Threading.Thread.Sleep(1000);
mySerialPort.Write("n");
System.Threading.Thread.Sleep(1000);
}
}
示例14: getComPort
public static string getComPort()
{
try {
string[] str = SerialPort.GetPortNames();
string recieved = "";
string com = "";
serialPort = new SerialPort();
foreach (string value in str) {
Console.WriteLine(value);
serialPort.PortName = value;
serialPort.BaudRate = 9600;
serialPort.Parity = 0;
serialPort.DataBits = 8;
serialPort.ReadTimeout = 5000;
serialPort.WriteTimeout = 500;
serialPort.Open();
serialPort.Write(comTest, 0, comTest.Length);
Thread.Sleep(100);
recieved = serialPort.ReadExisting();
if (recieved.Equals("\u001by")) {
Console.WriteLine(value);
com = value;
}
serialPort.Close();
}
return com;
} catch(Exception e) {
Console.WriteLine(e);
return "";
}
}
示例15: QueryDevice
internal static void QueryDevice(string port)
{
try
{
tP = new SerialPort(port, 115200, Parity.None, 8, StopBits.One);
tP.DataReceived += new SerialDataReceivedEventHandler(tP_DataReceived);
tP.ErrorReceived += new SerialErrorReceivedEventHandler(tP_ErrorReceived);
tP.Handshake = Handshake.RequestToSend;
tP.DtrEnable = true;
tP.RtsEnable = true;
tP.NewLine = Environment.NewLine;
tP.ReadTimeout = 2000;
tP.WriteTimeout = 2000;
tP.Open();
Thread.Sleep(200);
tP.Write("AT+GMM" + (char)13);
Thread.Sleep(500);
tP.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (tP.IsOpen)
tP.Close();
}
}