存档

‘CSharp’ 分类的存档

大灰狼串口调试软件V1.0

2009年8月26日

工作需要, 同事需要一个串口调试的小软件,网上的大多没刚好合适自己需要的功能, 于是帮他写了一个. :)
截图:
snap1
下载地址:
http://www.suiyuan.org/download/SerialPort.exe
这是C#写的,用的C#自带的SerialPort,软件很简单,有需要的可以下载用一下. :)

admin CSharp

一个存储过程

2009年6月15日

用的是SQL SERVER2K
现在有一张表,
大概字段
id(自增长), SNumber(站号, 001, 002, etc.), SName(站名),GTime(datetime,获取测站数据时间),Level(float, 测站数据)

现在大概有7,80个站, 每隔6分钟就会同时向数据库里添加一条记录.

现在想实现把同一天内相同时间过来的数据先按照SNumber排列, 再按gtime倒序排列,然后对数据重新编号(1,2,3,etc) 请问如何实现?

例如原始记录
id snumber sname gtime level
66 023 测站1 2009-12-17 10:00:00 50.12
65 058 测站2 2009-12-17 10:00:00 30.24
64 018 测站3 2009-12-17 10:00:00 83.01
63 018 测站3 2009-12-17 09:54:00 50.12
62 058 测站2 2009-12-17 09:54:00 30.24
61 023 测站1 2009-12-17 09:54:00 83.01

按照要求排序后结果为
newid snumber sname gtime level
1 018 测站3 2009-12-17 10:00:00 83.01
2 023 测站1 2009-12-17 10:00:00 50.12
3 058 测站2 2009-12-17 10:00:00 30.24
1 018 测站3 2009-12-17 09:54:00 83.01
2 023 测站1 2009-12-17 09:54:00 50.12
3 058 测站2 2009-12-17 09:54:00 30.24

正解:

CREATE TABLE taoistong 
(id int identity(1,1)
,snumber char(3)
,sname nvarchar(4)
,gtime smalldatetime
,level float )
INSERT INTO taoistong SELECT 
'023',  N'测站1', '2009-12-17 10:00:00', 50.12 
INSERT INTO taoistong SELECT 
 '058', N'测站2', '2009-12-17 10:00:00', 30.24 
INSERT INTO taoistong SELECT 
 '018', N'测站3', '2009-12-17 10:00:00', 83.01 
INSERT INTO taoistong SELECT 
 '018', N'测站3', '2009-12-17 09:54:00', 50.12 
INSERT INTO taoistong SELECT 
 '058', N'测站2', '2009-12-17 09:54:00', 30.24 
INSERT INTO taoistong SELECT 
 '023', N'测站1', '2009-12-17 09:54:00', 83.01 
 
SELECT (SELECT count(1) FROM taoistong b WHERE a.gtime=b.gtime AND a.snumber>=b.snumber)
,snumber,sname,gtime,level  
FROM taoistong a ORDER BY gtime DESC,snumber

admin CSharp

使用C#调用外部命令重启IIS应用程序池

2009年5月4日

在这里我们通过.net的Process类调用外部命令, 通过重定向输入, 输出获取执行结果.
添加应用using System.Diagnostics;
核心代码如下

                                p.StartInfo.FileName = "cmd.exe";
                                p.StartInfo.UseShellExecute = false;
                                p.StartInfo.RedirectStandardInput = true;
                                p.StartInfo.RedirectStandardOutput = true;
                                p.StartInfo.RedirectStandardError = true;
                                p.StartInfo.CreateNoWindow = true;
                                p.Start();
                                p.StandardInput.WriteLine(_command);
                                p.StandardInput.WriteLine("exit");
                                //string command = "cscript.exe";
                                //p.StartInfo.Arguments = command + " c:\\windows\\system32\\iisapp.vbs /a \"www.xxx.net\" ";
 
                                //p.WaitForExit();
                                string s = p.StandardOutput.ReadToEnd();
                                p.Close();
                                LogEvent(s, LogType.normal);

_command写到配置文件中, 内容是

<add key="Command" value="cscript.exe c:\\windows\\system32\\iisapp.vbs /a www.xxx.net" />

这样就可以简单的实现重新启动指定的应用程序池了.
您可以通过命令iisapp -a来查看当前服务器上的所有应用程序.

最后可以根据自己的需要把这个代码写成windows服务, 安装到服务器上. 既可实现定时重新启动应用程序池功能.

admin CSharp

通过C#实现报警续

2009年4月17日

前一篇c#蜂鸣报警系列声音函数
今天要说的是如何实现连续播放报警声音和停止报警.

        #region 报警
        [DllImport("winmm.dll", EntryPoint = "PlaySound")]
        private static extern bool Win32_PlaySound(string pszSound, IntPtr hmod, uint fdwSound);   
 
        /// <summary>
        /// 播放一个wav音频文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="asynchronous"></param>
        /// <param name="loop"></param>
        /// <param name="doNotStopPlay"></param>
        public static void PlaySound(string path, bool asynchronous, bool loop, bool doNotStopPlay)
        {
            Win32_PlaySound(path, IntPtr.Zero, (uint)((asynchronous ?
          PlaySoundMessage.SND_ASYNC : PlaySoundMessage.SND_SYNC) | (loop ?
          PlaySoundMessage.SND_LOOP : 0) | (doNotStopPlay ?
          PlaySoundMessage.SND_NOSTOP : 0) | PlaySoundMessage.SND_FILENAME));
        }
 
        /// <summary>
        /// 停止播放
        /// </summary> 
        public static void StopSound()
        {
            Win32_PlaySound(null, IntPtr.Zero, 0);
        }
 
        [Flags()]
        internal enum PlaySoundMessage
        {
            SND_SYNC = 0x0000,
            SND_ASYNC = 0x0001,
            SND_LOOP = 0x0008,
            SND_NOSTOP = 0x0010,
            SND_FILENAME = 0x00020000
        }
        #endregion

当例如遥测水位中测量的水位大于预警水位, 通过PlaySound(”alarm8.wav”, true, true, false)方法实现连续报警, 你还可以同时弹出窗口提醒之类的信息.
关闭报警通过方法StopSound()实现就可以了.

admin CSharp

c#蜂鸣报警系列声音函数.

2009年3月20日

在C#中可以通过以下四种方式来实现蜂鸣或者报警,播放声音之类的功能.
1). Beep的报警实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   // 添加引用
   using System.Runtime.InteropServices;
 
   [DllImport("user32.dll")]
   public static extern int MessageBeep(uint uType);
   uint beepI = 0x00000030;
 
    //发出不同类型的声音的参数如下:
    //Ok = 0x00000000,
    //Error = 0x00000010,
    //Question = 0x00000020,
    //Warning = 0x00000030,
    //Information = 0x00000040
 
    // 然后在程序中调用
    MessageBeep(beepI);

2) 调用Microsoft.ViualBase中的Beep():
1.先引入命名空间:using Microsoft.VisualBasic;
2.调用:Interaction.Beep();
在编译时注意要引用Microsoft.VisualBasic.dll程序集,否则编译不能通过。
另外,如果是Console应用,可以用Console.WriteLine(”\a”);来代替Beep()。

3) 通过主板蜂鸣器发出蜂鸣

1
2
3
4
5
6
7
8
9
10
[DllImport("kernel32.dll",   EntryPoint="Beep")]   
   // 第一个参数是指频率的高低,越大越高,第二个参数是指响的时间多长
  public   static   extern   int   Beep   (   
  int   dwFreq,   
  int   dwDuration   
  );   
 
  private   void   button1_Click(object   sender,   System.EventArgs   e)   {   
  Beep(500,100);   
  }

4) 调用PlaySound(string pszSound,int hmod,int fdwSound)来播放声音

1
2
3
4
5
6
  [DllImport("winmm.dll")]
  public static extern bool PlaySound(string pszSound,int hmod,int fdwSound);
  public const int SND_FILENAME = 0x00020000;
  public const int SND_ASYNC = 0x0001; 
 
  PlaySound("alarm8.wav",0,SND_ASYNC|SND_FILENAME);

admin CSharp

C#下SOCKET异步调用的TCP SERVER实例

2009年2月21日

单位一个项目需要做一个TCP SOCKET通讯的东西, 上网搜索了一下, 发现一片文章不错, 转到这里来了.

我已经打包了, 需要的朋友请从这里下载, VS2005打开.

原文地址:http://dev.csdn.net/article/65/65798.shtm.

附: 当客户端连接上服务器端之后除非主动发出指令退出,否则会一直保持与SERVER的连接状态,在这个例子中,SERVER端可以设置最大的客户端连接数, 当然如果我们想让客户端在发呆一段时间之后断开连接的话,可以在Session类中添加一个DateTime字段, 然后当服务器端在AcceptCallBack(IAsyncResult ar)方法中接收客户端连接时进行判断,使用方法CloseIdleClient(int min);关闭掉已经超时的CLIENT连接.

        /// <summary>
        /// 关闭所有发呆的客户端
        /// </summary>
        /// <param name="min">发呆时间</param>
        public virtual void CloseIdleClient(int min)
        {
            List<SessionId> list = new List<SessionId>();
            foreach (Session client in _sessionTable.Values)
            {
                TimeSpan ts = DateTime.Now.Subtract(client.ConnDate);
                if (ts.Minutes >= min)
                {
                    client.Close();
                    list.Add(client.ID);
                }
            }
 
            foreach (SessionId id in list)
                _sessionTable.Remove(id);
        }

admin CSharp