100 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C#
		
	
	
	
		
		
			
		
	
	
			100 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C#
		
	
	
	
| 
								 | 
							
								using System;
							 | 
						|||
| 
								 | 
							
								using System.Collections.Generic;
							 | 
						|||
| 
								 | 
							
								using System.Linq;
							 | 
						|||
| 
								 | 
							
								using System.Text;
							 | 
						|||
| 
								 | 
							
								using System.IO.Ports;
							 | 
						|||
| 
								 | 
							
								namespace elelab
							 | 
						|||
| 
								 | 
							
								{
							 | 
						|||
| 
								 | 
							
								   public class port
							 | 
						|||
| 
								 | 
							
								    {
							 | 
						|||
| 
								 | 
							
								        private SerialPort commPort = new SerialPort();
							 | 
						|||
| 
								 | 
							
								        public delegate void receive_data(byte[] port_data);
							 | 
						|||
| 
								 | 
							
								        public event receive_data port_event;
							 | 
						|||
| 
								 | 
							
								        //初始化端口 使用的时候必须先初始化
							 | 
						|||
| 
								 | 
							
								        //int com_count 端口号 1 - 100
							 | 
						|||
| 
								 | 
							
								        public bool init_wms_sys(int com_count)
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            commPort.PortName = "COM" + com_count.ToString();
							 | 
						|||
| 
								 | 
							
								            commPort.BaudRate = 9600;
							 | 
						|||
| 
								 | 
							
								            commPort.Parity = Parity.None;// Parity.Even;
							 | 
						|||
| 
								 | 
							
								            commPort.DataBits = 8;
							 | 
						|||
| 
								 | 
							
								            commPort.StopBits = StopBits.One;
							 | 
						|||
| 
								 | 
							
								            commPort.Encoding = Encoding.ASCII;
							 | 
						|||
| 
								 | 
							
								            commPort.WriteBufferSize = 0x5000;
							 | 
						|||
| 
								 | 
							
								            commPort.ReadBufferSize = 0x5000;//by dhwu 150130
							 | 
						|||
| 
								 | 
							
								            commPort.DataReceived += new SerialDataReceivedEventHandler(Sp_DataReceived);
							 | 
						|||
| 
								 | 
							
								            commPort.ReceivedBytesThreshold = 1;
							 | 
						|||
| 
								 | 
							
								            try
							 | 
						|||
| 
								 | 
							
								            {
							 | 
						|||
| 
								 | 
							
								                commPort.Open();
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            catch
							 | 
						|||
| 
								 | 
							
								            {
							 | 
						|||
| 
								 | 
							
								                return false;
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								          
							 | 
						|||
| 
								 | 
							
								            return true;
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        private void Sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            byte[] readBuffer = new byte[commPort.BytesToRead];
							 | 
						|||
| 
								 | 
							
								            commPort.Read(readBuffer, 0, readBuffer.Length);
							 | 
						|||
| 
								 | 
							
								            port_event(readBuffer);
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								       //发送数据
							 | 
						|||
| 
								 | 
							
								       //byte[] byt 字节数组 发送内容
							 | 
						|||
| 
								 | 
							
								       public bool write_all_byte(byte[] byt)
							 | 
						|||
| 
								 | 
							
								        {//
							 | 
						|||
| 
								 | 
							
								            for (int i = 0; i < byt.Length; i++)
							 | 
						|||
| 
								 | 
							
								            {
							 | 
						|||
| 
								 | 
							
								                if (write_byte(byt[i]) == false)
							 | 
						|||
| 
								 | 
							
								                {
							 | 
						|||
| 
								 | 
							
								                    return false;
							 | 
						|||
| 
								 | 
							
								                }
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            return true;
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								       private bool write_byte(byte byt)
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            byte[] byt1 = new byte[1];
							 | 
						|||
| 
								 | 
							
								            byt1[0] = byt;
							 | 
						|||
| 
								 | 
							
								            if (commPort.IsOpen)
							 | 
						|||
| 
								 | 
							
								            {
							 | 
						|||
| 
								 | 
							
								                try
							 | 
						|||
| 
								 | 
							
								                {
							 | 
						|||
| 
								 | 
							
								                    commPort.Write(byt1, 0, 1);
							 | 
						|||
| 
								 | 
							
								                }
							 | 
						|||
| 
								 | 
							
								                catch
							 | 
						|||
| 
								 | 
							
								                {
							 | 
						|||
| 
								 | 
							
								                    return false;
							 | 
						|||
| 
								 | 
							
								                }
							 | 
						|||
| 
								 | 
							
								                while (commPort.BytesToWrite != 0) ;
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            else
							 | 
						|||
| 
								 | 
							
								            {
							 | 
						|||
| 
								 | 
							
								                try
							 | 
						|||
| 
								 | 
							
								                {
							 | 
						|||
| 
								 | 
							
								                    commPort.Open();
							 | 
						|||
| 
								 | 
							
								                }
							 | 
						|||
| 
								 | 
							
								                catch
							 | 
						|||
| 
								 | 
							
								                {
							 | 
						|||
| 
								 | 
							
								                    return false;
							 | 
						|||
| 
								 | 
							
								                }
							 | 
						|||
| 
								 | 
							
								                if (commPort.IsOpen)
							 | 
						|||
| 
								 | 
							
								                {
							 | 
						|||
| 
								 | 
							
								                    try
							 | 
						|||
| 
								 | 
							
								                    {
							 | 
						|||
| 
								 | 
							
								                        commPort.Write(byt1, 0, 1);
							 | 
						|||
| 
								 | 
							
								                    }
							 | 
						|||
| 
								 | 
							
								                    catch
							 | 
						|||
| 
								 | 
							
								                    {
							 | 
						|||
| 
								 | 
							
								                        return false;
							 | 
						|||
| 
								 | 
							
								                    }
							 | 
						|||
| 
								 | 
							
								                    while (commPort.BytesToWrite != 0) ;
							 | 
						|||
| 
								 | 
							
								                }
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            return true;
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								}
							 |