PIC16F877A串口通信C#温度实时曲线

更新时间:2023-08-05 20:02:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

PIC16F877A串口通信C#温度实时曲线、RS232串口通信

上位机界面

下位机电路图,可以仿真

利用虚拟端口仿真效果

PIC16F877A串口通信C#温度实时曲线、RS232串口通信

上位机C#源程序//包含画图程序

using System;

using System.Collections.Generic;

using http://www.77cn.com.cnponentModel;

using System.Data;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Text;

using System.Windows.Forms;

using System.IO.Ports;

namespace CommPort

{

public partial class Form2 : Form

{

private int txd;

private int rxd;

public float pon;//存ä?放¤?数ºy据Y的Ì?变À?量¢?

private int n;

private float x1, x2, y1, y2,y11, y21 ;//画-线?的Ì?坐Á?标À¨º

private Pen Mypen1;//画-笔À¨º

private Pen Mypen2;

//private Graphics grfx;

public Point ptlist;//存ä?放¤?坐Á?标À¨º圆2点Ì?

PIC16F877A串口通信C#温度实时曲线、RS232串口通信

// Random rm = new Random();//随?机¨²数ºy产¨²生¦¨²器¡Â

//Timer mytimer = new Timer();//定¡§时º¡À器¡Â

public Form2()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

txd = 0;//接¨®收º?字Á?符¤?个?数ºy计?数ºy变À?量¢?

rxd = 0;//发¤¡é送¨ª字Á?符¤?个?数ºy计?数ºy变À?量¢?

//count = 0;

ptlist = new Point(0, this.pictureBox1.Height);

x1 = ptlist.X;

x2 = ptlist.X;

y1 =ptlist.Y;

Mypen1 = new Pen(Color.Blue,1);

Mypen2 = new Pen(Color.Red, 2);

//y2 =ptlist.Y;

string[] ports = System.IO.Ports.SerialPort.GetPortNames();//获?取¨?当Ì¡À前¡ã计?算?机¨²的Ì?串ä?行D端?口¨²名?称?数ºy组Á¨¦,ê?存ä?在¨²数ºy组Á¨¦里¤?

mycomm.ReadTimeout = 32;//设¦¨¨置?读¨¢取¨?操¨´作Á¡Â未¡ä完ª¨º成¨¦时º¡À发¤¡é生¦¨²超?时º¡À之?前¡ã的Ì?毫¨¢秒?数ºy。¡ê

int i = 0;

foreach (string port in ports)// foreach 语®?句?用®?于®¨²循-环¡¤访¤?问¨º集¡¥合? {

combo1.Items.Add(port);//添¬¨ª加¨®端?口¨²到Ì?选?择?控?件t项?目?中D

i++;//计?算?端?口¨²个?数ºy

}

if (i > 0)

{

combo1.SelectedIndex = 0;//获?取¨?当Ì¡À前¡ã选?择?项?,ê?并¡é将?索¡Â引°y设¦¨¨置?为a0

if(mycomm.IsOpen)

{

mycomm.Close();

}

mycomm.PortName = combo1.SelectedItem.ToString();

comminfo.Text = combo1.SelectedItem.ToString() + " 9600,8,1";

try

{

//mycomm.Open();

PIC16F877A串口通信C#温度实时曲线、RS232串口通信

//openbtn.Text = "关?闭À?串ä?口¨²";

}

catch

{

//MessageBox.Show("没?发¤¡é现?次ä?串ä?口¨²或¨°串ä?口¨²已°?经-在¨²使º1用®?");

}

}

}

/*

private void Form1_Leave(object sender, EventArgs e)

{

if(mycomm.IsOpen)

{

mycomm.Close();

}

}

*/

private void openbtn_Click(object sender, EventArgs e)

{

if(openbtn.Text =="关?闭À?串ä?口¨²")

{

this.timer1.Enabled = false ;//可¨¦以°?使º1用®?

openbtn.Text = "打䨰开a串ä?口¨²";

if(mycomm.IsOpen)

{

mycomm.Close();

}

}

else

{

try

{

//动¡¥态¬?添¬¨ª加¨®一°?个?定¡§时º¡À器¡Â

this.timer1.Enabled = true;//可¨¦以°?使º1用®?

this.timer1.Interval = 100;//定¡§时º¡À时º¡À间?为a100毫¨¢秒?

this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

this.timer1.Start();//启?动¡¥定¡§时º¡À器¡Â

if(mycomm.IsOpen)

{

mycomm.Close();

}

PIC16F877A串口通信C#温度实时曲线、RS232串口通信

mycomm.PortName =combo1.SelectedItem.ToString();

mycomm.Open();

openbtn.Text = "关?闭À?串ä?口¨²";

comminfo.Text = combo1.SelectedItem.ToString() + " 9600,8,1";

}

catch

{

MessageBox.Show("没?发¤¡é现?次ä?串ä?口¨²或¨°串ä?口¨²已°?经-在¨²使º1用®?");

openbtn.Text = "打䨰开a串ä?口¨²";

}

}

}

private void send_Click(object sender, EventArgs e)//发¤¡é送¨ª数ºy据Y处ä|理¤¨ª程¨¬序¨° {

string str = sendtext.Text;//将?发¤¡é送¨ª框¨°中D的Ì?字Á?符¤?给?变À?量¢?

if (mycomm.IsOpen)//半ã?段?端?口¨²是º?否¤?打䨰开a

{

mycomm.WriteLine(str);//将?字Á?符¤?写¡ä入¨?新?的Ì?一°?行D

txd = txd + str.Length;//记?录?字Á?符¤?的Ì?长¡è度¨¨

tds.Text = txd.ToString();//将?长¡è度¨¨值¦Ì再¨´文?本À?框¨°中D显?示º?出?来¤¡ä }

}

private void mycomm_DataReceived(object sender,

System.IO.Ports.SerialDataReceivedEventArgs e)//接¨®收º?数ºy据Y处ä|理¤¨ª事º?件t

{

try

{

mycomm.ReadTimeout = 2000;

//string str = mycomm.ReadExisting();//读¨¢取¨? SerialPort 对?象¨®的Ì?流¢¡Â和¨ª输º?入¨?缓o冲?区?中D所¨´有®D立¢¡é即¡ä可¨¦用®?的Ì?字Á?节¨²。¡ê

string str = mycomm.ReadTo("A");//读¨¢取¨?到Ì?A

pon=float.Parse(str);//获?得Ì?一°?个?值¦Ì(ꡧ0-50)ê?

SetText(str+" ");

//SetText(this.revtext.Text + " " + str);

x1 = x2;

y1 = y2;

if (button_tu.Text=="停ª¡ê止1绘?图ª?")

{

if (x2 > this.pictureBox1.Width)//超?出?绘?图ª?区?

PIC16F877A串口通信C#温度实时曲线、RS232串口通信

{

this.pictureBox1.Image = null;//清?空?图ª?片?

// x1 = ptlist.X;

x2 = ptlist.X;

}

else

{

x2=x2 + 5;

}

y2 = pon*4;

// if (y1 == lineShape1.Y2)//

// {

// y1 = y2;

// }

Graphics grfx = this.pictureBox1.CreateGraphics();

y11 = ptlist.Y- y1;

y21 = ptlist.Y-y2;

grfx.DrawLine(Mypen1, x1,y11, x2, y21);

// grfx.DrawLine(Mypen1, 0,0, 50, 50);

//grfx.DrawLine(Mypen1, 450, y11, x2, y21);

if (n == 0)

{

grfx.DrawEllipse(Mypen2,x2-1,y21+1,2,2);

//grfx.pr str;//把ã?点Ì?的Ì?纵ÁY坐Á?标À¨º显?示º?出?来¤¡ä

}

}

// this.revtext.Text = this.revtext.Text + " " + str;//将?读¨¢取¨?到Ì?的Ì?字Á?符¤?接¨®这a上¦?次ä?的Ì?显?示º?出?来¤¡ä

rxd = rxd + str.Length;//记?录?接¨®收º?数ºy据Y的Ì?总Á¨¹长¡è度¨¨

rds.Text = rxd.ToString();//将?长¡è度¨¨值¦Ì显?示º?在¨²文?本À?框¨°

}

catch ( TimeoutException)

{

Console.WriteLine(e);

}

}

private void combo1_SelectedIndexChanged(object sender, EventArgs e)//选?择?控?件t选?择?项?目?发¤¡é生¦¨²改?变À?时º¡À处ä|理¤¨ª事º?件t

{

try

{

PIC16F877A串口通信C#温度实时曲线、RS232串口通信

if (mycomm.IsOpen)// 判D断?串ä?口¨²是º?否¤?打䨰开a,打䨰开a的Ì?话¡ã先¨¨关?闭À?了¢?才?能¨¹选?择?

{

mycomm.Close();

}

mycomm.PortName = combo1.SelectedItem.ToString();//串ä?口¨²号?位?控?件t选?择?的Ì?项?目?

comminfo.Text = combo1.SelectedItem.ToString() + " 9600,8,1"; //将?选?择?的Ì?信?息¡é显?示º?在¨²ToolStripStatusLabel 在¨² StatusStrip 中D提¬¨¢供?一°?个?显?示º?区?域®¨°,ê?用®?于®¨²显?示º?文?本À?或¨°图ª?像?,ê?或¨°者?同ª?时º¡À显?示º?二t者?

//openbtn.Text = "关?闭À?串ä?口¨²";

}

catch

{

MessageBox.Show("没?发¤¡é现?次ä?串ä?口¨²或¨°串ä?口¨²已°?经-在¨²使º1用®?"); openbtn.Text = "打䨰开a串ä?口¨²";

}

}

private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)

{

rxd = 0;

txd = 0;

tds.Text = "0";

rds.Text = "0";

}

private void clear_Click(object sender, EventArgs e)

{

revtext.Text = "";

}

//自Á?己o写¡ä的Ì?解a决?跨?线?程¨¬调Ì¡Â用®?方¤?法¤¡§

public delegate void SetTextHandler(string text);

private void SetText(string text)

{

if (revtext.InvokeRequired == true)

{

SetTextHandler set = new SetTextHandler(SetText);//委¡¥托ªD的Ì?方¤?法¤¡§参?数ºy应®|和¨ªSetText一°?致?

revtext.Invoke(set, new object[] { text }); //此ä?方¤?法¤¡§第̨²二t参?数ºy用®?于®¨²传ä?入¨?方¤?法¤¡§,代䨲替¬?形?参?text

}

else

PIC16F877A串口通信C#温度实时曲线、RS232串口通信

{

revtext.Text = text;

}

}

private void button1_Click(object sender, EventArgs e)

{

if (mycomm.IsOpen)// 判D断?串ä?口¨²是º?否¤?打䨰开a,打䨰开a的Ì?话¡ã先¨¨关?闭À?了¢?才?能¨¹选?择?

{

mycomm.Close();

}

Application.Exit();

}

private void button2_Click(object sender, EventArgs e)

{

if (button_tu.Text =="停ª¡ê止1绘?图ª?")

{

timer1.Enabled = false;

button_tu.Text = "开a始º?绘?图ª?";

}

else//button_tu.Text =="开a始º?绘?图ª?"

{

timer1.Enabled = true ;

pictureBox1.Image = null;

button_tu.Text = "停ª¡ê止1绘?图ª?";

}

}

private void timer1_Tick(object sender, EventArgs e)

{

if (n == 20)//'20*100/1000=2秒?

n = 0;

else

n = n + 1;

}

}

PIC16F877A串口通信C#温度实时曲线、RS232串口通信

}

单片机源程序,采用PIC16F877A,希望能帮到大家,这里有个16进制转化十进制过程

#include "pic.h"

__CONFIG(XT&WDTDIS&LVPDIS);

unsigned char toasc(unsigned char s);

unsigned char toascA(unsigned char s);

void zhuan(unsigned int dat);

//unsigned char t;

//char j=0;

//char p=0x41;

// char flg=0;

unsigned int shu;

unsigned long int dat_temp;

unsigned char a,b,c,d,e;

char i,j,k;

// unsigned int n=0;

// unsigned char a[4],b[5];

//ASII码转换函数-数字

unsigned char toasc(unsigned char s)

{

return s+48;

}

//ASII码转换函数-字母

unsigned char toascA(unsigned char s)

{

return s+55;

}

void ini_AD()

{

TRISA0=1;

ADFM=0;

PCFG3=0;

PCFG2=0;

PCFG1=0;

PCFG0=0;

ADCON0=0B11000001;

for(i=0;i<25;i++){}

ADGO=1;

PIC16F877A串口通信C#温度实时曲线、RS232串口通信

}

main()

{

ini_AD();

TRISB0=1;

TRISD=0;

//SPEN=1;//使能异步串行端口

SPBRG=25;

SYNC=0;

TXEN=1;//使能发送,置位TXIF

BRGH=1;//botulv

TX9=0;

RCSTA=0B10010000;//SPEN=1

a=b=c=d=e=0;

k=0;

while(RB0)

{

for(i=0;i<120;i++)

{

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

{

RD0=1;

}

}

for(i=0;i<120;i++)

{

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

{

RD0=0;

}

}

}

while(1)

{

if(ADGO==0)

{

k++;

dat_temp = ADRESH;

dat_temp = dat_temp << 2;

PIC16F877A串口通信C#温度实时曲线、RS232串口通信

dat_temp = dat_temp | ADRESL>>6;

dat_temp=dat_temp*5000;

dat_temp=dat_temp/1023;

shu=dat_temp;

zhuan(shu);

if(k==50)

{

a=a+48;

TXREG=a;

while(!TXIF)

{;}

b=b+48;

TXREG=b;

while(!TXIF)

{;}

TXREG=c;

while(!TXIF)

{;}

d=d+48;

TXREG=d;

while(!TXIF)

{;}

e=e+48;

TXREG=e;

while(!TXIF)

{;}

TXREG=65;

while(!TXIF)

{;}

k=0;

}

ADGO=1;

ADRESH=0;

ADRESL=0;

}

}

}

//十六进制转十进制4位

void zhuan(unsigned int dat)

PIC16F877A串口通信C#温度实时曲线、RS232串口通信

{

a=dat/1000;

b=dat/100%10;

c=46;

d=dat/10%10;

e=dat%10;

}

/**********************

/*******

if(RCIF==1)

{

TXREG=RCREG;

}

**********/

//测试代码

/************

if(RCIF==1&&flg==0&&RCREG==49)//如果收到数据 {

flg=1;

}

if(flg==1&&j<10)

{

if(TXIF==1)

{

t=toasc(0x01);

TXREG=t+j;

while(!TXIF)//TXIF=0表示发送进行中,不可以写数据 { //TXIF=1表示可以进行写数据 ;

}

j++;

}

}

**********/

本文来源:https://www.bwwdw.com/article/5r3m.html

Top