⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 我写的一个C#串口通信
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace myservice
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Hide();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            /*String str="1234567*";
            int str1;
            String str2 = "";
            for (int i = 0; i < 8; i++)
            {
                str1= str[(i/2)*2 + (i + 1) % 2];
                if (i % 2 == 0)
                {
                    str2 += "0x";
                }
                str2 +=String.Format("{0:X}", str1);
            }
           // str2 += "0x2A";
             * */ 
            if (mycomm.PortOpen)
            {
                mycomm.PortOpen = false;
                return;
            }
            mycomm.CommPort = 1;
            if (!mycomm.PortOpen)
            {
                mycomm.PortOpen = true;
            }
            else
            {
                MessageBox.Show("error");
                return;
            }
            mycomm.Settings ="57600,n,8,1";
            mycomm.RThreshold = 1;
            mycomm.InputLen = 0;
        }
        private void mycomm_OnComm(object sender, EventArgs e)
        {
            
            Thread.Sleep(50);
            if (receive.Text.Length > 1000)
            {
                receive.Text = "";
            }
            String str = mycomm.Input.ToString();
            receive.Text += str + "\r\n";
            String result = "";
            /*int start = str.IndexOf("Number: ") + 8;
            if (start > 8)
            {
                str = str.Substring(start);
                result += str.Substring(5, 1);
                result += str.Substring(3, 1);
                result += str.Substring(11, 1);
                result += str.Substring(9, 1);
                result += str.Substring(17, 1);
                result += str.Substring(15, 1);
                result += str.Substring(23, 1);
                result += str.Substring(21, 1);
                String output = validate(result);
                while (output.Length < 7)
                {
                    output += "0";
                }
                output =output + "*z";
                mycomm.Output = output;
                receive.Text += output + "\r\n";
            }
	     */
            if (str.Length >= 8)
            {
                result = str.Substring(0, 8);
                receive.Text += result + "\r\n";
                String output = validate(result);
                while (output.Length < 7)
                {
                    output += "0";
                }
                output += "*";
                int str1;
                String str2 = "";
                for (int i = 0; i < 8; i++)
                {
                    str1 = output[(i / 2) * 2 + (i + 1) % 2];
                    if (i % 2 == 0)
                    {
                        str2 += "0x";
                    }
                    str2 += String.Format("{0:X}", str1);
                }
                receive.Text += str2 + "\r\n";
                String tmp = str2;
                mycomm.Output = str2;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            mycomm.Output = send.Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            receive.Text = "";
        }
        private void room_TextChanged(object sender, EventArgs e)
        {

        }
        private String validate(String stu_id)
        {
            String conn_str = dbdir.Text;
            String rsv_term_start = dataProcess.getResult("select new_term_start from rsv_sets order by 编号", "new_term_start", conn_str);
            String record_code = dataProcess.getrsvcode(rsv_term_start, stu_id, conn_str);
            String d_record_code = dataProcess.getResult("select * from rsv_record where  (rsv_record_code-(rsv_record_code mod 10)) =" + Convert.ToInt32(record_code) + " and rsv_stu_id = '" + stu_id + "'", "rsv_record_code", conn_str);
            if(d_record_code!="")
            {
                int room = Convert.ToInt32(d_record_code)%10;
                String computer_id = dataProcess.getResult("select rsv_computer_id from rsv_record where rsv_record_code = '" + d_record_code + "' and rsv_stu_id = '" + stu_id + "'", "rsv_computer_id", conn_str);
                if (dataProcess.getCount("select count(*) from room_status where rsv_code = '" + d_record_code + "' and stu_id = '" + stu_id + "'",conn_str)==0)
                {
                    String stu_name = dataProcess.getResult("select stu_name from stu_info where stu_id  = '" + stu_id + "'", "stu_name", conn_str);
                    String stu_class = dataProcess.getResult("select stu_class from stu_info where stu_id  = '" + stu_id + "'", "stu_class", conn_str);
                    String stu_room = dataProcess.getRoom(room, conn_str);
                    dataProcess.Exec("update rsv_record set rsv_status = '已完成' where rsv_stu_id = '" + stu_id + "' and rsv_record_code = '" + d_record_code + "'", conn_str);
                    dataProcess.Exec("insert into room_status(room,stu_id,stu_name,stu_class," + "stu_computer,rsv_code) values ('" + stu_room + "','" + stu_id + "','" + stu_name + "','" + stu_class + "','" +
                    computer_id + "','" + d_record_code + "')", conn_str);
                }
                if (computer_id.Length < 2) return ("0" + room + "0" + computer_id);
                else return ("0" + room + computer_id);
            }
            else
            {
                Thread.Sleep(1);
                return "00";
            }
        }
        private void test(String stu_id)
        {
            String conn_str = dbdir.Text;
            String rsv_term_start = dataProcess.getResult("select new_term_start from rsv_sets order by 编号", "new_term_start", conn_str);
            String record_code = dataProcess.getrsvcode(rsv_term_start, stu_id, conn_str);
            DateTime dt1 = Convert.ToDateTime(rsv_term_start);
            int rrc = Int32.Parse(record_code);
            int week = rrc / 1000;
            int weekday = (rrc / 100) % 10;
            int timespan = week * 7 + weekday;
            int time = (rrc / 10) % 10;
            DateTime now = DateTime.Now;
            TimeSpan tp = now.Subtract(dt1);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            clear_table();
        }
        private void clear_table()
        {
            String conn_str = dbdir.Text;
            String cmd_str = "select DISTINCT rsv_code from room_status";
            OleDbConnection oledconn = new OleDbConnection(conn_str);
            OleDbCommand cmd = new OleDbCommand(cmd_str, oledconn);
            String rsv_term_start = dataProcess.getResult("select new_term_start from rsv_sets order by 编号", "new_term_start", conn_str);
            try
            {
                oledconn.Open();
                OleDbDataReader oled_reader = cmd.ExecuteReader();
                while (oled_reader.Read())
                {
                    if (dataProcess.judge_time(oled_reader["rsv_code"].ToString(), rsv_term_start) == 0)
                    {
                        dataProcess.Exec("delete from room_status where rsv_code = '" + oled_reader["rsv_code"].ToString() + "'",conn_str);
                    }
                }
            }
            catch(Exception ex)
            {
                throw (ex);
            }
            finally
            {
                oledconn.Close();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            clear_table();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -