C#中怎么向串口发送数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#中怎么向串口发送数据相关的知识,希望对你有一定的参考价值。

有一串命令需要向串口发送,不知道c#中怎么写,请高手帮忙写点代码参考一下,谢谢了!

给你个串口代码哈,希望能帮到你
需要的空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

public class SerialPortAndShutdownDemoClass

public void button1Click(SerialPort serialPort1, ToolStripButton button1, ToolStripButton button2)

serialPort1.PortName = "COM1";
serialPort1.Open();
button1.Enabled = false;
button2.Enabled = true;


public void button2Click(SerialPort serialPort1, ToolStripButton button1, ToolStripButton button2)

if (button2.Text == "关闭计算机")

byte[] data = Encoding.Unicode.GetBytes("关机");
string str = Convert.ToBase64String(data);
serialPort1.WriteLine(str);
button2.Text = "取消关机";

else

button2.Text = "";
button1.Enabled = true;
button2.Enabled = false;

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine("shutdown /s");
p.StandardInput.WriteLine("exit");



public void serialPort1DataReceived(SerialPort serialPort1)

byte[] data = Convert.FromBase64String(serialPort1.ReadLine());
string str = Encoding.Unicode.GetString(data);
serialPort1.Close();
if (str == "")

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine("shutdown /s");
p.StandardInput.WriteLine("exit");


参考技术A 楼上的 代码 要 人家 关机?

my god!

真的 调试的话,可能有意想不到的事情!
参考技术B 服务器端:
namespace SocketTest

class Program

public static void SendMessage()

Socket socket = serverSocket.Accept();
Console.WriteLine("Connected a client:0",socket.RemoteEndPoint);
socket.Send(Encoding.ASCII.GetBytes("welcome to server"));
//Thread thread = new Thread(ReceiveMessage);
// thread.Start();

public static void ReceiveMessage(object obj)

Socket socket = (Socket)obj;
byte[] data = new byte[1024];
int len = socket.Receive(data);
string dataString = Encoding.ASCII.GetString(data, 0, len);
Console.WriteLine("Receive Data:0 from 1", dataString,socket.RemoteEndPoint);
//Thread thread = new Thread(SendMessage);
//thread.Start(socket);

static Socket serverSocket;
static void Main(string[] args)

//定义接收数据长度变量
int recv;
//定义接收数据的缓存
byte[] data = new byte[1024];
//定义侦听端口
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 5566);
//定义套接字类型
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//连接
serverSocket.Bind(ipEnd);
//开始侦听
serverSocket.Listen(10);
//控制台输出侦听状态
Console.Write("Waiting for a client");
//Socket client;
while (true)

//一旦接受连接,创建一个客户端
Socket client = serverSocket.Accept();
//获取客户端的IP和端口
IPEndPoint ipEndClient = (IPEndPoint)client.RemoteEndPoint;
//输出客户端的IP和端口
Console.WriteLine("Connect with 0 at port 1", ipEndClient.Address, ipEndClient.Port);
//定义待发送字符
string welcome = "Welcome to my server";
//数据类型转换
data = Encoding.ASCII.GetBytes(welcome);
while (true)


try

//发送
client.Send(data, data.Length, SocketFlags.None);
//接收数据可以用线程也可以不用
//ReceiveMessage(client);
Thread thread = new Thread(ReceiveMessage);
thread.Start(client);
////对data清零
//data = new byte[1024];
////获取收到的数据的长度
//recv = client.Receive(data);
////如果收到的数据长度为0,则退出
//if (recv == 0)
// break;
////输出接收到的数据
//Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
//将接收到的数据再发送出去
// client.Send(data, recv, SocketFlags.None);

catch (Exception)

client.Close();
serverSocket.Close();


client.Close();

//Console.WriteLine("Disconnect form0", ipEndClient.Address);

serverSocket.Close();





客户端:
namespace Client

class Program

public static void sendMessage(object obj)

Socket socket = (Socket)obj;
string input = Console.ReadLine();
byte[] data = Encoding.ASCII.GetBytes(input);
socket.Send(data, data.Length, SocketFlags.None);
//Thread thread = new Thread(new ParameterizedThreadStart(ReceiveMessage));
//thread.Start(socket);

public static void ReceiveMessage(object obj)

Socket socket = (Socket)obj;
byte[] data = new byte[1024];
int len = socket.Receive(data);
string dataString = Encoding.ASCII.GetString(data, 0, len);
Console.WriteLine("Receive Data:0 from 1", dataString, socket.RemoteEndPoint);
//Thread thread = new Thread(new ParameterizedThreadStart(SendMessage));
//thread.Start(socket);

static void Main(string[] args)

//定义发送数据缓存
byte[] data = new byte[1024];
//定义字符串,用于控制台输出或输入
string input, stringData;
//定义主机的IP及端口
IPAddress ip = IPAddress.Parse("127.0.0.1");
IPEndPoint ipEnd = new IPEndPoint(ip, 5566);
//定义套接字类型
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//尝试连接
try

socket.Connect(ipEnd);

//异常处理
catch (SocketException e)

Console.WriteLine("Fail to connect server");
Console.WriteLine(e.ToString());
return;

//定义接收数据的长度
int recv = socket.Receive(data);
//将接收的数据转换成字符串
stringData = Encoding.ASCII.GetString(data, 0, recv);
//控制台输出接收到的数据
Console.WriteLine(stringData);
while(true)

//Thread thread = new Thread(sendMessage);
//thread.Start(socket);
//定义从键盘接收到的字符串
input = Console.ReadLine();
if (input == "exit")

break;

//将从键盘获取的字符串转换成整型数据并存储在数组中
data = Encoding.ASCII.GetBytes(input);
//发送该数组
socket.Send(data, data.Length, SocketFlags.None);
////如果字符串是"exit",退出while循环
//if (input == "exit")
//
// break;
//
////对data清零
//data = new byte[1024];
////定义接收到的数据的长度
//recv = socket.Receive(data);
////将接收到的数据转换为字符串
//stringData = Encoding.ASCII.GetString(data, 0, recv);
////控制台输出字符串
//Console.Write(stringData);
////发送收到的数据
//socket.Send(data, recv, 0);

Console.WriteLine("disconnect from server");
socket.Shutdown(SocketShutdown.Both);
socket.Close();


参考技术C 用serialport类处理,写起来比较累,你还是在百度上搜吧,多得很

以上是关于C#中怎么向串口发送数据的主要内容,如果未能解决你的问题,请参考以下文章

请问linux下串口向外发送数据要用啥函数,代码要怎么写?谢谢

c#编程,通过向串口发数据的方式发送中文短信时,但中文显示乱码,如何软件解码?

arduino怎样向串口发送中文字符

单片机怎么通过串口发送一串数据?

ESP8089串口怎么接,AT指令发送无反应

52单片机如何对PC串口发送来的数据进行存储,存储在哪?RAM还是ROM中?