仿三菱

仿三菱_第1张图片
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.Ports;
//using Microsoft.VisualBasic.Devices;
namespace 模拟三菱PLC
{

public partial class Form1 : Form
{
    int M0toM7_value = 0;
    private SerialPort port = new SerialPort("COM1", 9600, Parity.Even, 7, StopBits.One);
    int mygetM0M7_value()
    {
        int value = 0;
        //读取M0状态
        if (lable_M0.BackColor == Color.Red)
        {
            value += 0x01;
        }
        //读取M1状态
        if (lable_M1.BackColor == Color.Red)
        {
            value += 0x02;
        }
        //读取M2状态
        if (lable_M2.BackColor == Color.Red)
        {
            value += 0x04;
        }
        if (lable_M3.BackColor == Color.Red)
        {
            value += 0x08;
        }
        if (lable_M4.BackColor == Color.Red)
        {
            value += 0x10;
        }
        if (lable_M5.BackColor == Color.Red)
        {
            value += 0x20;
        }
        if (lable_M6.BackColor == Color.Red)
        {
            value += 0x40;
        }
        if (lable_M7.BackColor == Color.Red)
        {
            value += 0x80;
        }
        return value;
    
    }
    public Form1()
    {
        InitializeComponent();
    }
    private bool MyBytesisEquals(byte[] b1, byte[] b2)
    {
        if (b1.Length != b2.Length) return false;
        if (b1 == null || b2 == null) return false;
        for (int i = 0; i < b1.Length; i++)
            if (b1[i] != b2[i])
                return false;
        return true;
    }
    void myopenport(String comport)
    {
        try
        {

            if (port.IsOpen)
                port.Close();
            port.PortName = comport;
            port.Open();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
    private void init()
    {
        cmb_COM.Items.Clear();
        String[] ports = SerialPort.GetPortNames();
        foreach (String portname in ports)
        {
            cmb_COM.Items.Add(portname);

        }
         
        Control.CheckForIllegalCrossThreadCalls = false;
        port.ReadTimeout = 1000;
        port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
        cmb_COM.SelectedIndex = 0;
        lable_M0.BackColor = Color.Green;
        lable_M1.BackColor = Color.Green;
        lable_M2.BackColor = Color.Green;
        lable_M3.BackColor = Color.Green;
        lable_M4.BackColor = Color.Green;
        lable_M5.BackColor = Color.Green;
        lable_M6.BackColor = Color.Green;
        lable_M7.BackColor = Color.Green;
        //myopenport("COM1");
        ;
        byte[] bytes1 = { 1, 2, 3 };
        byte[] bytes2 = { 2, 3,4 };
        bool isequal = MyBytesisEquals(bytes1,bytes2);
        //MessageBox.Show(isequal.ToString());
        
        

    }

    void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {

        int len = port.BytesToRead;
        byte[] readDatas = new byte[len];
        port.Read(readDatas, 0, len);
        
        bool isequal = MyBytesisEquals(readDatas, FXPLC.bytes_ReadM348);
        if (isequal)
        {
            M0toM7_value = 0;
            M0toM7_value = mygetM0M7_value();
            System.Threading.Thread.Sleep(50);
            byte[] newbytes = FXPLC.Gender_MBytes(M0toM7_value);
            port.Write(newbytes, 0, newbytes.Length);
        }
        else
            port.Write(FXPLC.response_wrong, 0, 1);


        //MessageBox.Show(isequal.ToString());
        //throw new NotImplementedException();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        init();

    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (port.IsOpen)
            port.Close();
    }

    private void cmb_COM_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }

    private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {

    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        //Global.a=0;
        if(port.IsOpen)
            port.Write(FXPLC.bytes_ReadM348, 0, FXPLC.bytes_ReadM348.Length);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        myopenport(cmb_COM.SelectedItem.ToString());
       // timer1.Enabled = true;
    }

    private void lable_M0_Click(object sender, EventArgs e)
    {
        if (lable_M0.BackColor == Color.Red)
            lable_M0.BackColor = Color.Green;
        else
            lable_M0.BackColor = Color.Red;

    }

    private void lable_M1_Click(object sender, EventArgs e)
    {
        if (lable_M1.BackColor == Color.Red)
            lable_M1.BackColor = Color.Green;
        else
            lable_M1.BackColor = Color.Red;
    }

    private void lable_M2_Click(object sender, EventArgs e)
    {
        if (lable_M2.BackColor == Color.Red)
            lable_M2.BackColor = Color.Green;
        else
            lable_M2.BackColor = Color.Red;
    }

    private void lable_M3_Click(object sender, EventArgs e)
    {
        if (lable_M3.BackColor == Color.Red)
            lable_M3.BackColor = Color.Green;
        else
            lable_M3.BackColor = Color.Red;
    }

    private void lable_M4_Click(object sender, EventArgs e)
    {
        if (lable_M4.BackColor == Color.Red)
            lable_M4.BackColor = Color.Green;
        else
            lable_M4.BackColor = Color.Red;
    }

    private void lable_M5_Click(object sender, EventArgs e)
    {
        if (lable_M5.BackColor == Color.Red)
            lable_M5.BackColor = Color.Green;
        else
            lable_M5.BackColor = Color.Red;
    }

    private void lable_M6_Click(object sender, EventArgs e)
    {
        if (lable_M6.BackColor == Color.Red)
            lable_M6.BackColor = Color.Green;
        else
            lable_M6.BackColor = Color.Red;
    }

    private void lable_M7_Click(object sender, EventArgs e)
    {
        if (lable_M7.BackColor == Color.Red)
            lable_M7.BackColor = Color.Green;
        else
            lable_M7.BackColor = Color.Red;
    }

}

}

你可能感兴趣的:(三菱)