首页 分享 C#未完成

C#未完成

来源:花匠小妙招 时间:2025-11-25 18:35

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO.Ports;

namespace WindowsApplication1

{

public partial class Form1 : Form

{

SerialPort sp = new SerialPort(SerialPort.GetPortNames()[0].ToString(), 115200, Parity.None, 8, StopBits.One);

bool isOpen = false;

byte[] sendData = new byte[8];

byte[] receiveData = new byte[8];

char[] ch = new char[8];

public Form1()

{

InitializeComponent();

sendData[0] = 0x20;

sendData[1] = 0x24;

sendData[2] = 0x05;

sendData[3] = 0x09;

sendData[4] = 0x15;

sendData[5] = 0x00;

sendData[6] = 0x01;

sendData[7] = 0x0A;

ch[0] = (char)0xff;

}

private void Form1_Load(object sender, EventArgs e)

{

this.MaximumSize = this.Size;

this.MinimumSize = this.Size;

this.MaximizeBox = false;

PC_Set_PortProperty();

}

private void PC_Set_PortProperty()

{

serialPort1.PortName = "COM1";

serialPort1.BaudRate = 115200;

serialPort1.StopBits = StopBits.One;

serialPort1.DataBits = 8;

serialPort1.Parity = Parity.None;

serialPort1.ReadTimeout = -1;

Open_Com();

}

private void Open_Com()

{

try

{

serialPort1.Open();

isOpen=true;

lblStatus.Text = "打开串口成功!";

}

catch (Exception)

{

lblStatus.Text = "打开串口时发生错误!";

}

}

private void Close_Com()

{

try

{

serialPort1.Close();

isOpen = false;

lblStatus.Text = "关闭串口成功!";

}

catch (Exception)

{

lblStatus.Text = "关闭串口时发生错误!";

}

}

public int Send_Data(byte[] sendDatas)

{

sendDatas[6] =Convert.ToByte(PC_Calculate_Parity(sendDatas));

MessageBox.Show(sendDatas[6].ToString());

if (isOpen)

{

try

{

for (int i = 0; i < 8; i++)

{

serialPort1.WriteLine(sendDatas[i].ToString());

}

MessageBox.Show("发送成功!");

return 1;

}

catch (Exception)

{

lblStatus.Text = "发送数据时发生错误!";

return 0;

}

}

return 0;

}

public int Receive_Data()

{

if (isOpen)

{

try

{

for (int i = 0; i < 8; i++)

{

receiveData[i] = Convert.ToByte(serialPort1.ReadLine());

tbxRecvData.Text += receiveData[i] + "rn" + Environment.NewLine;

}

PC_Check_Data(receiveData);

return 1;

}

catch (Exception)

{

lblStatus.Text = "读取串口1时发生错误!";

return 0;

}

}

else

{

MessageBox.Show("串口未打开!", "错误提示");

try

{

for (int i = 0; i < 8; i++)

{

tbxRecvData.Text += serialPort1.ReadLine() + "n";

}

return 1;

}

catch (Exception)

{

lblStatus.Text = "读取串口2时发生错误!";

return 0;

}

}

return 0;

}

private int PC_Calculate_Parity(byte[] Datas)

{

int[] check = new int[6];

int sum=0;

int num = 0xAA,count=0;

char a = (char)0xAf;

for (int i = 0; i < 6; i++)

{

check[i] = Convert.ToInt32(Convert.ToString(Datas[i], 16)) + 0x00;

while (check[i] > 0)

{

check[i] = (check[i] - 1) & check[i];

sum++;

}

MessageBox.Show(sum.ToString());

}

if (sum % 2 == 1)

return 1;

else

return 0;

}

private int PC_Check_Data(byte[] Datas)

{

int[] check = new int[6];

int sum = 0;

for (int i = 0; i < 6; i++)

{

for (int j = 0; j < 8; j++)

{

if ((Datas[i] & (1 << j)) == 1)

{

check[i] += 1;

}

}

sum += check[i];

}

sum +=Convert.ToInt32(Datas[6]);

if (sum % 2 == 0)

{

lblStatus.Text = "数据正确!";

return 1;

}

else

{

lblStatus.Text = "数据失败!";

return 0;

}

}

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)

{

Receive_Data();

}

private void btnSend_Click(object sender, EventArgs e)

{

Send_Data(sendData);

}

private void btnExit_Click(object sender, EventArgs e)

{

Close_Com();

this.Close();

}

}

}

#include   "config.h"

#include "lcd12864.h"

#define  LED1CON    0x00000800          //P0.11引脚控制LED1,低电平点亮    

uint8  sCount=0;      

struct receiveData

{

    unsigned  char cmd;            

    unsigned  char cmd_info;        

    unsigned  char option_first;        

    unsigned  char option_second;    

    unsigned  char option_third;        

    unsigned  char option_four;    

    unsigned  char check;            

    unsigned  char end;            

}receData={'h','e','l','l','o','g','o','d'};

struct receiveData *stu=&receData;

void UART_SendByte(uint8 data,uint8 num);

void LPC_Send_Str(struct receiveData *str,uint8 num);

char LPC_Check_Data(struct receiveData *str);

void LPC_Open_Com()

{

    PINSEL0|=0xFFF5FFFF;                        

}

void LPC_Close_Com()

{

    PINSEL0|=0xFFF0FFFF;                        

}

__irq void LPC_Receive_Str(void)

{

    uint32 i;

    i=IOSET;

if((i & LED1CON)==0)  

        IOSET=LED1CON;

else

        IOCLR=LED1CON;

switch(sCount)

    {

case 0: (*stu).cmd=U1RBR;sCount++;UART_SendByte((*stu).cmd,1);while((U1LSR&0x20)==0);break;

case 1:    (*stu).cmd_info=U1LSR;sCount++;UART_SendByte((*stu).cmd_info,1);while((U1LSR&0x20)==0);break;

case 2:    (*stu).option_first=U1LSR;sCount++;UART_SendByte((*stu).option_first,1);while((U1LSR&0x20)==0);break;

case 3:    (*stu).option_second=U1LSR;sCount++;UART_SendByte((*stu).option_second,1);while((U1LSR&0x20)==0);break;

case 4:    (*stu).option_third=U1LSR;sCount++;UART_SendByte((*stu).option_third,1);while((U1LSR&0x20)==0);break;

case 5:    (*stu).option_four=U1LSR;sCount++;UART_SendByte((*stu).option_four,1);while((U1LSR&0x20)==0);break;

case 6:    (*stu).check=U1LSR;sCount++;UART_SendByte((*stu).check,1);while((U1LSR&0x20)==0);break;

case 7:    (*stu).end=U1LSR;sCount=0;UART_SendByte((*stu).end,1);while((U1LSR&0x20)==0);break;         

default  :break;

    }

    LPC_Check_Data(stu);

    VICVectAddr=0x00;

}

void LPC_UART_Init(void)

{  

    U1LCR=0x83;                      

    U1DLL=6;                         

    U1DLM=0x00;                      

    U1LCR=0x03;                      

    U0LCR=0x83;                       

    U0DLL=6;

    U0DLM=0x00;

    U0LCR=0x03;

}

void UART_SendByte(uint8 data,uint8 num)

{   

if(num==0)

    {

        U0THR=data;                     

while((U0LSR&0x20)==0);         

    }

if(num==1)

    {

        U1THR=data;                     

while((U1LSR&0x20)==0);         

    }    

}

char LPC_Calculate_Parity(struct receiveData *str)

{

char i=0,j=0,sum=0,check[6];

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

    {

switch(i)

        {

case 0:for(j=0;j<8;j++){if(((*str).cmd<<j)==1)check[i]++;};break;

case 1:for(j=0;j<8;j++){if(((*str).cmd_info<<j)==1)check[i]++;};break;

case 2:for(j=0;j<8;j++){if(((*str).option_first<<j)==1)check[i]++;};break;

case 3:for(j=0;j<8;j++){if(((*str).option_second<<j)==1)check[i]++;};break;

case 4:for(j=0;j<8;j++){if(((*str).option_third<<j)==1)check[i]++;};break;

case 5:for(j=0;j<8;j++){if(((*str).option_four<<j)==1)check[i]++;};break;

        }

        sum+=check[i];

    }

if(sum%2==1)

return 1;

else

return 0;

}

char LPC_Check_Data(struct receiveData *str)

{

char i=0,j=0,sum=0,check[6];

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

    {

switch(i)

        {

case 0:for(j=0;j<8;j++){if(((*str).cmd<<j)==1)check[i]++;};break;

case 1:for(j=0;j<8;j++){if(((*str).cmd_info<<j)==1)check[i]++;};break;

case 2:for(j=0;j<8;j++){if(((*str).option_first<<j)==1)check[i]++;};break;

case 3:for(j=0;j<8;j++){if(((*str).option_second<<j)==1)check[i]++;};break;

case 4:for(j=0;j<8;j++){if(((*str).option_third<<j)==1)check[i]++;};break;

case 5:for(j=0;j<8;j++){if(((*str).option_four<<j)==1)check[i]++;};break;

        }

        sum+=check[i];

    }

    sum+=(*str).check;

if(sum%2==0)

return 1;

else

return 0;

}

void LPC_Send_Str(struct receiveData *str,uint8 num)

{  

char i=0;          

            (*str).check=LPC_Calculate_Parity(str);            

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

            {

switch(i)

                {

case 0:UART_SendByte((*str).cmd,1);    break;

case 1:UART_SendByte((*str).cmd_info,1);break;

case 2:UART_SendByte((*str).option_first,1);break;

case 3:UART_SendByte((*str).option_second,1);break;

case 4:UART_SendByte((*str).option_third,1);break;

case 5:UART_SendByte((*str).option_four,1);    break;

case 6:UART_SendByte((*str).check,1);break;

case 7:UART_SendByte((*str).end,1);    break;    

default  :break;

                }

            }

}

int main(void)

{     

    PINSEL0=0x00050005;                        

    PINSEL1=0x00000000;

    IODIR=LED1CON;                             

    IOSET=LED1CON;                                

    LPC_UART_Init();

    U0FCR=0x81;                                

    U1FCR=0x81;                                

    U1IER=0x01;                                

    IRQEnable();                                

    VICIntSelect=0x00000000;                   

    VICVectCntl0=0x27;                         

    VICVectAddr0=(int)LPC_Receive_Str;

    VICIntEnable=0x00000080;                   

    LPC_Send_Str(stu,1);

while(1);      

}                                 

csharp

运行

相关知识

C# .NET鲜花配送系统
字符串(C# 编程指南)
字符串基础(C# 编程指南)
C#实现的表白软件,玫瑰花资源
超市库存管理系统C#源代码资源
C#编写的鲜花预订管理系统资源
C# '
C#桌面玫瑰花程序情人节玫瑰花礼物资源
给对象的小惊喜c#玫瑰花
C#玫瑰花源码实现,传递爱意的最佳方式

网址: C#未完成 https://www.huajiangbk.com/newsview2483570.html

所属分类:花卉
上一篇: 花儿的成长感受体验手抄报 感受自
下一篇: 選年花

推荐分享