zoukankan
html css js c++ java
C#与51单片机串口通信
C#与51单片机串口通信
51接受数据,PC发送数据。
通过单片机的数码管将PC发送的16进制数据显示出来。
51接受数据代码:
#include <reg51.h> #include <string.h> #include <intrins.h> sbit LS138A = P2^2; //定义138译码器的输入A脚由P2.2控制 sbit LS138B = P2^3; //定义138译码器的输入脚B由P2.3控制 sbit LS138C = P2^4; //定义138译码器的输入脚C由P2.4控制 unsigned char ch; bit read_flag= 0 ; //此表为 LED 的字模, 共阴数码管 0-9 - unsigned char code Disp_Tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; void delay(unsigned int i); void init_serialcom( void ) { SCON = 0x50 ; //SCON: serail mode 1, 8-bit UART, enable ucvr //UART为模式1,8位数据,允许接收 TMOD |= 0x20 ; //TMOD: timer 1, mode 2, 8-bit reload //定时器1为模式2,8位自动重装 PCON |= 0x80 ; //SMOD=1; TH1 = 0xF3; // //baud*2 /* 波特率4800、数据位8、停止位1。效验位无 (12M) IE |= 0x90 ; //Enable Serial Interrupt TR1 = 1 ; // timer 1 run TI=1; } //向串口发送一个字符 void send_char_com( unsigned char ch) { SBUF=ch; while (TI== 0); TI= 0 ; } //串口接收中断函数 void serial () interrupt 4 using 3 { if (RI) { RI = 0 ; ch=SBUF; read_flag= 1 ; //就置位取数标志 } } main() { int LedNumVal = 0; unsigned char LedOut[3]; int i = 0; init_serialcom(); //初始化串口 while ( 1 ) { if (read_flag) //如果取数标志已置位,就将读到的数从串口发出 { read_flag= 0 ; //取数标志清0 send_char_com(ch); LedNumVal = ch; } LedOut[0]=Disp_Tab[LedNumVal / 100]; LedOut[1]=Disp_Tab[(LedNumVal / 10) % 10]; LedOut[2]=Disp_Tab[LedNumVal % 10]|0x80; for( i=0; i<3; i++) //实现8位动态扫描循环 { P0 = LedOut[i]; //将字模送到P0口显示 switch(i) //使用switch 语句控制位选 { case 0:LS138A=0; LS138B=0; LS138C=0; break; case 1:LS138A=1; LS138B=0; LS138C=0; break; case 2:LS138A=0; LS138B=1; LS138C=0; break; } delay(100); } } } void delay(unsigned int i) { char j; for(i; i > 0; i--) for(j = 200; j > 0; j--); }
C#发送数据代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.IO.Ports; namespace 交通灯串口通信 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private SerialPort com; private void button1_Click(object sender, EventArgs e) { com = new SerialPort(); com.BaudRate = 4800; com.PortName = "COM4"; com.DataBits = 8; com.Open(); Byte[] data = new Byte[4]; data[0] = 0x10; com.Write(data, 0, 1); com.Close(); } } }
查看全文
相关阅读:
搭建VirtoCommerce2.6开发环境,同官方dev分支保持同步(上)
VirtoCommerce中文博客站,2015年11月18日上线了!
C#编程连接数据库,通过更改配置文件切换数据库功能。
浅谈dataGridView使用,以及画面布局使用属性,对datagridview进行增删改查操作,以及委托使用技巧
将listBox中信息显示在dataGridview中,操作datagridview后删除listBox信息和SQL数据库信息 续(浅谈listBox..)
for语句嵌套使用 实现9*9乘法表
浅谈ListBox控件,将对象封装在listBox中,在ListBox中显示对象中某个属性,在ListBox中移除和移动信息
使用SignalR为FineUI/Webform打造消息总线
封装一个错误重试类
封装一个锁处理类
原文地址:https://www.cnblogs.com/johnpher/p/2570601.html
最新文章
TCO 11 muddy round
TCO
7.19 解题报告
线段树&N
H
G
F
E
D
A
热门文章
C
B
c++ 模版 大数
Visual Studio2008环境下查找C#中方法的“查看所有引用”
IIS发布WCF服务项目之本地
Winform打砖块游戏制作step by step第6节---双缓冲应用
WinForm开发之取送货管理2
Winform打砖块游戏制作step by step第5节---重构代码,利用继承多态
WinForm开发之取送货管理1
搭建VirtoCommerce2.6开发环境,同官方dev分支保持同步(下)
Copyright © 2011-2022 走看看