首页 > 代码库 > (c#2.0)serialPort串口通讯

(c#2.0)serialPort串口通讯

原文:(c#2.0)serialPort串口通讯

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Data.SqlClient;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Configuration;using System.IO;using System.IO.Ports;namespace WindowsApplication2{     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }         int iCount;         int numbers;         byte rlenth;         bool bRLenth;         int bRStart;         bool bRParam;         byte Rchk;         int LParam;         byte[] RParam;         bool brcmd;         byte RCmd;         bool bRchk;         bool bfinish;         private void Form1_Load(object sender, EventArgs e)         {                         //Form1 Form1 = new Form1();             try             {             if (this.serialPort1.IsOpen)             {                 serialPort1.Close();             }             else             {                 serialPort1.PortName = "COM1"; //选择串口COM1                 serialPort1.BaudRate = 9600;    //设置通信口参数                 serialPort1.DataBits = 8;       //数据位                  serialPort1.Parity = System.IO.Ports.Parity.None;//校验位 无校验                 serialPort1.StopBits = System.IO.Ports.StopBits.One;//停止位1位                 serialPort1.ReadBufferSize = 1024;                  //接收缓冲区大小                 serialPort1.WriteBufferSize = 1024;                 //发送缓冲区大小                 serialPort1.Open();                 serialPort1.ReadExisting();                         //设置Input从接收缓冲读取全部数据                 serialPort1.ReceivedBytesThreshold = 1;             //设置引发OnComm事件的字节长度                 serialPort1.DiscardInBuffer() ;                     //清除接收缓冲区                 serialPort1.DiscardOutBuffer() ;                    //清除发送缓冲区             }                             string connstr = System.Configuration.ConfigurationManager.AppSettings["myConnectionString"];                 SqlConnection conn = new SqlConnection(connstr);                 conn.Open();                 string sql = "select   name   as 时间,price,card from test01";                 SqlDataAdapter da = new SqlDataAdapter(sql, conn);                 DataSet ds = new DataSet();                 da.Fill(ds, "baomin");                 ultraChart1.DataSource = ds.Tables[0].DefaultView;                 dataGridView1.DataSource = ds.Tables[0].DefaultView;                 ultraChart1.Axis.Y.LogBase = 1000;                 ultraChart1.Axis.Y.RangeMax = 30000;                 ultraChart1.Axis.Y.RangeMin = 0;                 Application.EnableVisualStyles();                 Application.DoEvents();             }             catch (Exception ex)             {                 MessageBox.Show("dd",ex.Message,MessageBoxButtons.OK,MessageBoxIcon.Error);             }             //ultraChart1.         }         /// <summary>         /// 取前后存储器两个值对比,应付电脑异常退出         /// </summary>         /// <param name="sender">XOR异域校验</param>         /// <param name="e"></param>         private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)         {                             try             {                 //Byte[] dataread = new Byte[8] ;                                 //serialPort1.Read(dataread, 0, dataread.Length);                 //int productline=dataread[0]; //接口号                 //iCount=dataread[5];                 string connstr = System.Configuration.ConfigurationManager.AppSettings["myConnectionString"];                 SqlConnection conn = new SqlConnection(connstr);                 conn.Open();                 //if (dataread[0] == 0xAA)                 //{                 //    // MessageBox.Show(productline.ToString(), "ddd", MessageBoxButtons.OK, MessageBoxIcon.Error);                 //     numbers = 0;                 //     numbers = Convert.ToInt32(dataread[4]) * 256 ^ 1 + Convert.ToInt32(dataread[5]);                  //     numbers = Convert.ToInt32(dataread[4]) * Convert.ToInt32(System.Math.Pow(256, 1)) + Convert.ToInt32(dataread[5]);                 //     SqlCommand cmd = new SqlCommand("SP_SC_IN_AUTONUMBER", conn);                 //     cmd.CommandType = CommandType.StoredProcedure;                 //     cmd.Parameters.Add("@quantity", SqlDbType.Int);                 //     cmd.Parameters["@quantity"].Value = http://www.mamicode.com/numbers;"@ports", SqlDbType.Int);                 //     cmd.Parameters["@ports"].Value = http://www.mamicode.com/dataread[3];"SP_SC_IN_AUTONUMBER", conn);                             cmd.CommandType = CommandType.StoredProcedure;                             cmd.Parameters.Add("@quantity", SqlDbType.Int);                             cmd.Parameters["@quantity"].Value = http://www.mamicode.com/iCount;"@ports", SqlDbType.Int);                             cmd.Parameters["@ports"].Value = http://www.mamicode.com/System.Convert.ToInt32(RCmd);>

其它相关:
      
转自:http://www.cnblogs.com/jyshi/

<script type="text/javascript">// </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// </script><script type="text/javascript">// </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// </script>