c#串口开发源代码——这是我09年一个写的一个非常简陋的通用的串口调试程序

================================================================================================

2011-04-25:添加了SiXi.Logs.dll到项目中,原来是引用我本机的。在本机可以正常运行,后来网友反映无法找到DLL,我才发现了这个错误。对不住各位了。现已更新。

================================================================================================

09年因为一个项目要用到串口通信,我对串口通信没有什么了解,就一边学一边写了一个小工具,这个工具非常简陋,甚至非常丑陋,但是对于入门的想知道c#如何通过串口收发数据的童鞋来说,应该有一点点帮助。这个程序不包含任何通信协议的封装,我想,不同的设备都遵循不同的协议,我见过的最BT的协议就是我们的产品的通信协议。天,好几层封装,仅仅是为了那几个字节。不废话了,有应用程序和源代码,请选择下载。

 

开发环境:

语言:C#2.0

Microsoft Visual Studio 2005
版本 8.0.50727.762  (SP.050727-7600)
Microsoft .NET Framework
版本 2.0.50727 SP2

 

 

下载地址:

友情提示:如果您无法打开此解决方案,请升级您的Visual Studio 至VS2005SP1(这个升级过程要消耗很多时间,请耐心,并保持计算机不会突然断电)

 

COMServer通用串口测试程序源代码.7z

COMServer通用串口测试程序.7z 

 

 在编程中遇到问题的解决办法

 

c#开发交流:

裙: 125353043

 

 ================================================================

下面是Sixi .log.dll的源码:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace SiXi.Logs
{
    /// <summary>
    
/// 写日志的类
    
/// </summary>
    public class Log
    {
        #region 方法
        #region 写入日志
        /// <summary>
        
/// 写入日常日志
        
/// </summary>
        
/// <param name="msg">要写入日志的信息</param>
        public static void WriteNomalLog(string msg)
        {
            try
            {
                //如果是同一天的话,则打开文件在末尾写入。如果不是同一天,则创建文件写入文件
                
//新建文件
                
//写入文件
                if (!Directory.Exists("logs"))
                {
                    Directory.CreateDirectory("logs");
                }
                //判断是否存在文件
                if (File.Exists("logs/" + DateTime.Now.ToString("yyyyMMddHH") + "Nomal.log"))
                {
                    //如果存在文件,
                    using (StreamWriter sw = new StreamWriter("logs/" + DateTime.Now.ToString("yyyyMMddHH") + "Nomal.log"true))
                    {

                        lock (sw)
                        {
                            sw.WriteLine("============================================================================");
                            sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":");
                            sw.WriteLine(msg);
                            sw.Close();
                            sw.Dispose();
                        }
                    }
                    return;

                }
                using (StreamWriter sw2 = new StreamWriter("logs/" + DateTime.Now.ToString("yyyyMMddHH") + "Nomal.log"true))
                {
                    lock (sw2)
                    {
                        sw2.WriteLine("============================================================================");
                        sw2.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":");
                        sw2.WriteLine(msg);
                        sw2.Close();
                        sw2.Dispose();
                    }
                }
            }
            catch (IOException ex)
            {
                //throw ex;
            }
            catch (Exception ex)
            {
                //throw ex;
            }
           
        }//日志文件结束

        /// <summary>
        
/// 写异常日志
        
/// </summary>
        
/// <param name="e2">异常的实体</param>
        public static void WriteExceptionLog(Exception e2)
        {
            try
            {
            //如果是同一天的话,则打开文件在末尾写入。如果不是同一天,则创建文件写入文件
            
//新建文件
            
//写入文件
            if (!Directory.Exists("logs"))
            {
                Directory.CreateDirectory("logs");
            }
            //判断是否存在文件
            if (File.Exists("logs/" + DateTime.Now.ToString("yyyyMMddHH") + "Exception.log"))
            {
                //如果存在文件,
                using (StreamWriter sw = new StreamWriter("logs/" + DateTime.Now.ToString("yyyyMMddHH") + "Exception.log"true))
                {

                    lock (sw)
                    {
                        sw.WriteLine("============================================================================");
                        sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":");
                        sw.WriteLine(e2.ToString());
                        sw.Close();
                        sw.Dispose();
                    }
                }
                return;

            }
            using (StreamWriter sw2 = new StreamWriter("logs/" + DateTime.Now.ToString("yyyyMMddHH") + "Exception.log"true))
            {
                lock (sw2)
                {
                    sw2.WriteLine("============================================================================");
                    sw2.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":");
                    sw2.WriteLine(e2.ToString());
                    sw2.Close();
                    sw2.Dispose();
                }
            }
        }
        catch (IOException ex)
        {
            throw ex;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        }//日志文件结束

        /// <summary>
        
/// 写入调试日志
        
/// </summary>
        
/// <param name="msg">要写入日志的信息</param>
        public static void WriteDebugLog(string msg)
        {
            try
            {
                //如果是同一天的话,则打开文件在末尾写入。如果不是同一天,则创建文件写入文件
                
//新建文件
                
//写入文件
                if (!Directory.Exists("logs"))
                {
                    Directory.CreateDirectory("logs");
                }
                //判断是否存在文件
                if (File.Exists("logs/" + DateTime.Now.ToString("yyyyMMddHH") + "Debug.log"))
                {
                    //如果存在文件,
                    using (StreamWriter sw = new StreamWriter("logs/" + DateTime.Now.ToString("yyyyMMddHH") + "Debug.log"true))
                    {

                        lock (sw)
                        {
                            sw.WriteLine("============================================================================");
                            sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":");
                            sw.WriteLine(msg);
                            sw.Close();
                            sw.Dispose();
                        }
                    }
                    return;

                }
                using (StreamWriter sw2 = new StreamWriter("logs/" + DateTime.Now.ToString("yyyyMMddHH") + "Debug.log"true))
                {
                    lock (sw2)
                    {
                        sw2.WriteLine("============================================================================");
                        sw2.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":");
                        sw2.WriteLine(msg);
                        sw2.Close();
                        sw2.Dispose();
                    }
                }
            }
            catch (IOException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }//日志文件结束

        #endregion
        #endregion
    }

posted on 2010-11-23 15:40  sixiiweb  阅读(4777)  评论(2编辑  收藏  举报

导航