概述
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
最基本串口操作:
private void sendCmd(string i_cmd, bool i_isSendText)
{
byte[] send, receive;
char[] cSend, cReceive;
int i;
if (i_isSendText)
{
i_cmd += "/x01a";
}
cSend = new char[i_cmd.Trim().Length];
cSend = i_cmd.Trim().ToCharArray();
if (i_cmd.Trim().Length == 0)
{
return;
}
send = new byte[i_cmd.Trim().Length + 2];
for (i = 0; i < i_cmd.Trim().Length; i++)
{
send[i] = (byte)cSend[i];
}
send[i++] = (byte)'/r';
send[i++] = (byte)'/n';
m_receive_text.Text += new string(cReceive);
// serial.Close();
}
public SerialPort port;
public byte[] receive;
public char[] cReceive;
port = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
port.ReadTimeout = 10000;
port.Open();
// 最基本串口操作
int readLen = 0;
port.Write(send, 0, send.Length);
System.Threading.Thread.Sleep(500);
receive = new byte[1024];
cReceive = new char[1024];
readLen = port.Read(receive, 0, receive.Length);
最后
以上就是着急蜻蜓为你收集整理的C# 串口操作的全部内容,希望文章能够帮你解决C# 串口操作所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复