单片机串口向PC传送一个unsigned int 的数据 急求程序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单片机串口向PC传送一个unsigned int 的数据 急求程序相关的知识,希望对你有一定的参考价值。

#include <AT89X51.h>
#define uchar unsigned char
#define uint unsigned int
uchar idata trdata[]='8','9','S','5','1',0x0d,0x0a,0x00;
uchar idata trdata1[]='R','I','C','H','M','C','U',0x0d,0x0a,0x00;
main()

uchar i;
uint j;
SCON= 0x40; //串口方式1
PCON=0; //SMOD=0
REN=1; //允许接收
TMOD= 0x20; //定时器1定时方式2
TH1= 0xe6; //12MHz 1200波特率
TL1= 0xe6;
TR1= 1; //启动定时器
while(1)

i=0;
while(trdata[i]!=0x00)

SBUF=trdata[i];
while(TI==0);
TI=0;
i++;

for (j=0;j<50000;j++);

i=0;
while(trdata1[i]!=0x00)

SBUF=trdata1[i];
while(TI==0);
TI=0;
i++;

for (j=0;j<50000;j++);

参考技术A 传送部分,假定 my_data是你要传的数据:
SBUF=mydata/256; // 先发送高8位
while(!TI);TI=0; //等待发送完毕
SBUF=mydata%256; //再发送低8位
while(!TI);TI=0; //等待发送完毕
......
PC端也要按照先收高字节再收低字节来设计接收程序
参考技术B 我有的,刚解决的

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类处理,写起来比较累,你还是在百度上搜吧,多得很

以上是关于单片机串口向PC传送一个unsigned int 的数据 急求程序的主要内容,如果未能解决你的问题,请参考以下文章

单片机怎么通过串口发送unsigned int类型的数据?

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

stm32-浅谈串口

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

pc用串口助手给单片机发一字符串,收到的数据在LCD1602上显示,遇到问题,求大神指导

51单片机C语言,电子密码的验证输入程序,矩阵键盘用作输入,密码正确时,向串口送1,否则送0