Jack1455866134 2017-03-20 07:50 采纳率: 0%
浏览 2269

关于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;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string data;
SolidBrush bush1 = new SolidBrush(Color.Red);
SolidBrush bush2 = new SolidBrush(Color.Green);
public Form1()
{
InitializeComponent();
}

    private void button1_Click(object sender, EventArgs e)
    {

        axMSComm1.PortOpen = false;
        Close();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        axMSComm1.CommPort = 1; //'设置通信端口号为COM1
        axMSComm1.Settings = "9600,e,7,1"; //'设置串口参数
        axMSComm1.InputMode = 0;//'接收文本型数据
        axMSComm1.PortOpen = true;//'打开通信端口1
        axMSComm1.RThreshold = 1;
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        axMSComm1.PortOpen = false;
    }


    private void timer1_Tick(object sender, EventArgs e)
    {
        //向PLC发送指令:02 30 30 30 38 30 30 34 03 35 46,功能是从PLC的X0~X37读取4个字节数据,反映X0~X37的状态信息。
        //向PLC发送指令:02 30 30 30 38 30 30 3 03 35 45,功能是从PLC的X0~X27读取3个字节数据,反映X0~X27的状态信息。
        //向PLC发送指令:02 30 30 30 38 30 30 32 03 35 44,功能是从PLC的X0~X17读取2个字节数据,反映X0~X17的状态信息。
        //向PLC发送指令:02 30 30 30 38 30 30 31 03 35 43,功能是从PLC的X0~X7读取1个字节数据,反映X0~X7的状态信息。

        // byte[] bs = { 2, 48, 48, 48, 56, 48, 48, 52, 3, 53, 70 };
        //byte[] bs = { 2, 48, 48, 48, 56, 48, 48, 51, 3, 53, 69 };
        byte[] bs = { 2, 48, 48, 48, 56, 48, 48, 50, 3, 53, 68 };
        //byte[] bs = { 2, 48, 48, 48, 56, 48,48,49,3,53,67 };
        string ss = System.Text.Encoding.ASCII.GetString(bs);
        axMSComm1.Output = (ss);

    }

    private void axMSComm1_OnComm(object sender, EventArgs e)
    {




        data = axMSComm1.Input.ToString();
        textBox1.Text = data.Substring(1, 1);
        textBox2.Text = data.Substring(2, 1);
        textBox3.Text = data.Substring(3, 1);
        textBox4.Text = data.Substring(4, 1);
        //textBox5.Text = data.Substring(5, 1);
        //textBox6.Text = data.Substring(6, 1);
        //    textBox7.Text = data.Substring(7, 1);
        //     textBox8.Text = data.Substring(8, 1);

        Graphics g1 = this.pictureBox1.CreateGraphics();
        Graphics g2 = this.pictureBox2.CreateGraphics();
        Graphics g3 = this.pictureBox3.CreateGraphics();
        Graphics g4 = this.pictureBox4.CreateGraphics();
        Graphics g5 = this.pictureBox5.CreateGraphics();
        Graphics g6 = this.pictureBox6.CreateGraphics();
        Graphics g7 = this.pictureBox7.CreateGraphics();
        Graphics g8 = this.pictureBox8.CreateGraphics();
        Graphics g9 = this.pictureBox9.CreateGraphics();
        Graphics g10 = this.pictureBox10.CreateGraphics();
        Graphics g11 = this.pictureBox11.CreateGraphics();
        Graphics g12 = this.pictureBox12.CreateGraphics();
        Graphics g13 = this.pictureBox13.CreateGraphics();
        Graphics g14 = this.pictureBox14.CreateGraphics();
        Graphics g15 = this.pictureBox15.CreateGraphics();
        Graphics g16 = this.pictureBox16.CreateGraphics();
        //Graphics g17 = this.pictureBox17.CreateGraphics();
        //Graphics g18 = this.pictureBox18.CreateGraphics();
        //Graphics g19 = this.pictureBox19.CreateGraphics();
        //Graphics g20 = this.pictureBox20.CreateGraphics();
        //Graphics g21 = this.pictureBox21.CreateGraphics();
        //Graphics g22 = this.pictureBox22.CreateGraphics();
        //Graphics g23 = this.pictureBox23.CreateGraphics();
        //Graphics g24 = this.pictureBox24.CreateGraphics();





        int b = int.Parse(data.Substring(1, 1), System.Globalization.NumberStyles.HexNumber);//十六进制转十进制
        string aa = Convert.ToString(b, 2);//十进制转二进制

        while (aa.Length < 4)//补零
        {
            aa = "0" + aa;
        }
       int b2 = int.Parse(data.Substring(2, 1), System.Globalization.NumberStyles.HexNumber);
       string aa2 = Convert.ToString(b2, 2);

       while (aa2.Length < 4)
       {

            aa2 = "0" + aa2;
      }

        if (aa.Substring(0, 1) == "1")
            g8.FillEllipse(bush1, 0, 0, 20, 20);
        else
            g8.FillEllipse(bush2, 0, 0, 20, 20);
        if (aa.Substring(1, 1) == "1")
            g7.FillEllipse(bush1, 0, 0, 20, 20);
        else
            g7.FillEllipse(bush2, 0, 0, 20, 20);
        if (aa.Substring(2, 1) == "1")
            g6.FillEllipse(bush1, 0, 0, 20, 20);
        else
            g6.FillEllipse(bush2, 0, 0, 20, 20);
        if (aa.Substring(3, 1) == "1")
            g5.FillEllipse(bush1, 0, 0, 20, 20);
        else
            g5.FillEllipse(bush2, 0, 0, 20, 20);
        if (aa2.Substring(0, 1) == "1")
            g4.FillEllipse(bush1, 0, 0, 20, 20);
        else
            g4.FillEllipse(bush2, 0, 0, 20, 20);
        if (aa2.Substring(1, 1) == "1")
            g3.FillEllipse(bush1, 0, 0, 20, 20);
        else
            g3.FillEllipse(bush2, 0, 0, 20, 20);
        if (aa2.Substring(2, 1) == "1")
            g2.FillEllipse(bush1, 0, 0, 20, 20);
        else
            g2.FillEllipse(bush2, 0, 0, 20, 20);
        if (aa2.Substring(3, 1) == "1")
            g1.FillEllipse(bush1, 0, 0, 20, 20);
        else
            g1.FillEllipse(bush2, 0, 0, 20, 20);


        int b3 = int.Parse(data.Substring(3, 1), System.Globalization.NumberStyles.HexNumber);//十六进制转十进制
        string aa3 = Convert.ToString(b3, 2);//十进制转二进制

        while (aa3.Length < 4)//补零
        {
            aa3 = "0" + aa3;
        }
        int b4 = int.Parse(data.Substring(4, 1), System.Globalization.NumberStyles.HexNumber);
        string aa4 = Convert.ToString(b4, 2);

        while (aa4.Length < 4)
        {

            aa4 = "0" + aa4;
        }

        if (aa3.Substring(0, 1) == "1")
            g16.FillEllipse(bush1, 0, 0, 20, 20);
        else
            g16.FillEllipse(bush2, 0, 0, 20, 20);
        if (aa3.Substring(1, 1) == "1")
            g15.FillEllipse(bush1, 0, 0, 20, 20);
        else
            g15.FillEllipse(bush2, 0, 0, 20, 20);
        if (aa3.Substring(2, 1) == "1")
            g14.FillEllipse(bush1, 0, 0, 20, 20);
        else
            g14.FillEllipse(bush2, 0, 0, 20, 20);
        if (aa3.Substring(3, 1) == "1")
            g13.FillEllipse(bush1, 0, 0, 20, 20);
        else
            g13.FillEllipse(bush2, 0, 0, 20, 20);
        if (aa4.Substring(0, 1) == "1")
            g12.FillEllipse(bush1, 0, 0, 20, 20);
        else
            g12.FillEllipse(bush2, 0, 0, 20, 20);
        if (aa4.Substring(1, 1) == "1")
            g11.FillEllipse(bush1, 0, 0, 20, 20);
        else
            g11.FillEllipse(bush2, 0, 0, 20, 20);
        if (aa4.Substring(2, 1) == "1")
            g10.FillEllipse(bush1, 0, 0, 20, 20);
        else
            g10.FillEllipse(bush2, 0, 0, 20, 20);
        if (aa4.Substring(3, 1) == "1")
            g9.FillEllipse(bush1, 0, 0, 20, 20);
        else
            g9.FillEllipse(bush2, 0, 0, 20, 20);


        //int b5 = int.Parse(data.Substring(5, 1), System.Globalization.NumberStyles.HexNumber);//十六进制转十进制
        //string aa5 = Convert.ToString(b5, 2);//十进制转二进制

        //while (aa5.Length < 4)//补零
        //{
        //    aa5 = "0" + aa5;
        //}
        //int b6 = int.Parse(data.Substring(6, 1), System.Globalization.NumberStyles.HexNumber);
        //string aa6 = Convert.ToString(b6, 2);

        //while (aa6.Length < 4)
        //{

        //    aa6 = "0" + aa;
        //}

        //if (aa5.Substring(0, 1) == "1")
        //    g24.FillEllipse(bush1, 0, 0, 20, 20);
        //else
        //    g24.FillEllipse(bush2, 0, 0, 20, 20);
        //if (aa5.Substring(1, 1) == "1")
        //    g23.FillEllipse(bush1, 0, 0, 20, 20);
        //else
        //    g23.FillEllipse(bush2, 0, 0, 20, 20);
        //if (aa5.Substring(2, 1) == "1")
        //    g22.FillEllipse(bush1, 0, 0, 20, 20);
        //else
        //    g22.FillEllipse(bush2, 0, 0, 20, 20);
        //if (aa5.Substring(3, 1) == "1")
        //    g21.FillEllipse(bush1, 0, 0, 20, 20);
        //else
        //    g21.FillEllipse(bush2, 0, 0, 20, 20);
        //if (aa6.Substring(0, 1) == "1")
        //    g20.FillEllipse(bush1, 0, 0, 20, 20);
        //else
        //    g20.FillEllipse(bush2, 0, 0, 20, 20);
        //if (aa6.Substring(1, 1) == "1")
        //    g19.FillEllipse(bush1, 0, 0, 20, 20);
        //else
        //    g19.FillEllipse(bush2, 0, 0, 20, 20);
        //if (aa6.Substring(2, 1) == "1")
        //    g18.FillEllipse(bush1, 0, 0, 20, 20);
        //else
        //    g18.FillEllipse(bush2, 0, 0, 20, 20);
        //if (aa6.Substring(3, 1) == "1")
        //    g17.FillEllipse(bush1, 0, 0, 20, 20);
        //else
        //    g17.FillEllipse(bush2, 0, 0, 20, 20);
    }




}

}图片说明图片说明

  • 写回答

2条回答 默认 最新

  • threenewbee 2017-03-20 15:51
    关注

    FillEllipse 这是画界面上的小圆圈
    别的代码都有注释,涉及com通讯和plc的指令,和编程没关系,你得看plc的手册

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大