C#与51单片机串口通信

来源:互联网 发布:淘宝网秋季斑马道长裙 编辑:程序博客网 时间:2024/04/29 22:32
C#与51单片机串口通信

51接受数据,PC发送数据。

通过单片机的数码管将PC发送的16进制数据显示出来。

51接受数据代码:

  1. #include <reg51.h>  
  2. #include <string.h>    
  3. #include <intrins.h>  
  4.   
  5. sbit LS138A = P2^2;     //定义138译码器的输入A脚由P2.2控制   
  6. sbit LS138B = P2^3;     //定义138译码器的输入脚B由P2.3控制  
  7. sbit LS138C = P2^4;     //定义138译码器的输入脚C由P2.4控制  
  8.   
  9. unsigned char ch;  
  10. bit read_flag= 0 ;  
  11. //此表为 LED 的字模, 共阴数码管 0-9  -   
  12. unsigned char code Disp_Tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};   
  13. void delay(unsigned int i);  
  14. void init_serialcom( void )  
  15.   
  16.    {  
  17.   
  18.        SCON = 0x50 ;  //SCON: serail mode 1, 8-bit UART, enable ucvr    
  19.   
  20.                          //UART为模式1,8位数据,允许接收  
  21.   
  22.           TMOD |= 0x20 ; //TMOD: timer 1, mode 2, 8-bit reload              
  23.   
  24.                          //定时器1为模式2,8位自动重装  
  25.   
  26.           PCON |= 0x80 ; //SMOD=1;  
  27.   
  28.           TH1 = 0xF3;                   // //baud*2  /*  波特率4800、数据位8、停止位1。效验位无 (12M)  
  29.   
  30.           IE |= 0x90 ;     //Enable Serial Interrupt  
  31.   
  32.           TR1 = 1 ;       // timer 1 run  
  33.   
  34.           TI=1;  
  35.   
  36.            
  37.   
  38.        }  
  39.   
  40.            
  41.   
  42. //向串口发送一个字符  
  43.   
  44. void send_char_com( unsigned char ch)  
  45.   
  46.          {  
  47.             SBUF=ch;  
  48.             while (TI== 0);  
  49.                TI= 0 ;  
  50.   
  51.           }  
  52.   
  53.    
  54.   
  55. //串口接收中断函数  
  56.   
  57. void serial () interrupt 4 using 3  
  58.   
  59. {  
  60.     if (RI)  
  61.           {   
  62.   
  63.                  RI = 0 ;  
  64.   
  65.                  ch=SBUF;   
  66.                    
  67.                  read_flag= 1 ; //就置位取数标志  
  68.   
  69.               }  
  70.   
  71. }  
  72.   
  73.    
  74.   
  75.  main()  
  76.   
  77.     {  
  78.     int LedNumVal = 0;  
  79.     unsigned char LedOut[3];  
  80.     int i = 0;  
  81.   
  82.            init_serialcom(); //初始化串口  
  83.   
  84.                   while ( 1 )  
  85.                   {  
  86.   
  87.                     if (read_flag) //如果取数标志已置位,就将读到的数从串口发出  
  88.   
  89.                     {  
  90.   
  91.                      read_flag= 0 ; //取数标志清0  
  92.                      send_char_com(ch);  
  93.                      LedNumVal = ch;  
  94.                      }  
  95.   
  96.      LedOut[0]=Disp_Tab[LedNumVal / 100];  
  97.      LedOut[1]=Disp_Tab[(LedNumVal / 10) % 10];  
  98.      LedOut[2]=Disp_Tab[LedNumVal % 10]|0x80;  
  99.        
  100.   
  101.      for( i=0; i<3; i++)  //实现8位动态扫描循环  
  102.      {     
  103.       P0 = LedOut[i];  //将字模送到P0口显示  
  104.                                                
  105.       switch(i)   //使用switch 语句控制位选   
  106.          {        
  107.             case 0:LS138A=0; LS138B=0; LS138C=0;  break;           
  108.             case 1:LS138A=1; LS138B=0; LS138C=0;  break;  
  109.             case 2:LS138A=0; LS138B=1; LS138C=0;  break;                  
  110.          }  
  111.     delay(100);    
  112.       }  
  113.                    }  
  114. }  
  115.   
  116. void delay(unsigned int i)  
  117. {  
  118.     char j;  
  119.     for(i; i > 0; i--)  
  120.         for(j = 200; j > 0; j--);  
  121. }  

C#发送数据代码:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.IO;  
  10. using System.IO.Ports;  
  11.   
  12. namespace 交通灯串口通信  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.   
  21.         private SerialPort com;  
  22.   
  23.         private void button1_Click(object sender, EventArgs e)  
  24.         {  
  25.             
  26.                 com = new SerialPort();  
  27.                 com.BaudRate = 4800;  
  28.                 com.PortName = "COM4";  
  29.                 com.DataBits = 8;  
  30.                 com.Open();  
  31.                 Byte[] data = new Byte[4];  
  32.                 data[0] = 0x10;  
  33.                 com.Write(data, 0, 1);  
  34.                 com.Close();  
  35.         }  
  36.     }  
  37. OR
0 0
原创粉丝点击