矿石收音机论坛

 找回密码
 加入会员

QQ登录

只需一步,快速开始

搜索
查看: 2810|回复: 3

驱动

[复制链接]
     
发表于 2024-7-13 13:49:06 | 显示全部楼层 |阅读模式
本帖最后由 gxg0000 于 2024-7-13 13:46 编辑

闲暇无事,翻出多年前的飞凌2440嵌入式单片机,将其改成多功能仪表,其中有一中短波扫频仪功能,也算是废物利用吧。
扫频仪采用WINCE5.0系统运行,以下扫频仪流驱动程序,程序带注释,仅供参考!

开机界面
2.jpg

扫频仪全貌
6.jpg

扫频仪工作界面
1.jpg

扫频仪探头
7.jpg


  1. // SWEEP.cpp : 定义 DLL 应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #define REG_TYPE DWORD
  5. #include <ceddk.h>
  6. #include <s3c2440a_ioport.h>
  7. #include <s3c2440a_adc.h>
  8. #include <s3c2440a_base_regs.h>
  9. #define DLLAPI extern "C" __declspec(dllexport)        //指定C编译器编译
  10. static volatile S3C2440A_IOPORT_REG  *v_pIOPregs;         // S3C2440x GPIO registers
  11. static volatile S3C2440A_ADC_REG  *v_pADCPregs;         // S3C2440x ADC registers
  12. //IO控制DDS引脚定义
  13. #define ad9850_w_clk_clr                v_pIOPregs->GPGDAT &=~ (1 <<7)                //GPG7口接ad9850的w_clk脚
  14. #define ad9850_w_clk_set          v_pIOPregs->GPGDAT |= (1 << 7)
  15. #define ad9850_fq_up_clr                v_pIOPregs->GPGDAT &=~ (1 << 5)                //GPG5口接ad9850的fu_ud脚
  16. #define ad9850_fq_up_set          v_pIOPregs->GPGDAT |= (1 << 5)
  17. #define ad9850_bit_data_clr                v_pIOPregs->GPGDAT &=~ (1 << 2)        //GPG2口接ad9850的data脚
  18. #define ad9850_bit_data_set        v_pIOPregs->GPGDAT |= (1 << 2)
  19. #define ad9850_rest_clr                v_pIOPregs->GPEDAT &=~ (1 << 12)                        //GPE12口接ad9850的reset脚
  20. #define ad9850_rest_set        v_pIOPregs->GPEDAT |= (1 << 12)
  21. //
  22. bool flag=true;                 //读写标记
  23. unsigned int f_start =441750;        //扫描开始频率
  24. unsigned int step=150;                //步进
  25. unsigned int delay=1;                //采样速度1快2慢
  26. unsigned int ad_channel=1;//AD通道
  27. unsigned int adc[310];
  28. unsigned int Array1[32];
  29. unsigned int Array2[64];
  30. HANDLE hand;        //AD线程句柄
  31. DWORD WINAPI ADThread();        //AD线程函数

  32. /*-------------------------------------------------------------/
  33. 函数名称:        Delay_us
  34. 功能描述:        延时函数
  35. 传    参:        n*1微秒
  36. 返 回 值:        无
  37. --------------------------------------------------------------*/  
  38. void Delay_us(unsigned int n)
  39. {
  40.     volatile unsigned int i,j;
  41.     for(i = 0; i < n; i++)
  42.         for(j = 0; j < 78; j++);
  43. }
  44. /*-------------------------------------------------------------/
  45. 函数名称:        ad9850_reset_serial
  46. 功能描述:        ad9850复位(串口模式)
  47. 传    参:无
  48. 返 回 值:无
  49. --------------------------------------------------------------*/  
  50. void ad9850_reset_serial()
  51. {
  52.         ad9850_w_clk_clr;
  53.         ad9850_fq_up_clr;
  54.         ad9850_rest_clr;        //rest信号
  55.         ad9850_rest_set;
  56.         Delay_us(1);
  57.         ad9850_rest_clr;
  58.         ad9850_w_clk_clr;        //w_clk信号
  59.         ad9850_w_clk_set;
  60.         Delay_us(1);
  61.         ad9850_w_clk_clr;
  62.         ad9850_fq_up_clr;        //fu_ud信号
  63.         ad9850_fq_up_set;
  64.         Delay_us(1);
  65.         ad9850_fq_up_clr;
  66. }
  67. /*-------------------------------------------------------------/
  68. 函数名称:        ad9850_w
  69. 功能描述:        向ad9850串口写1字节函数
  70. 传    参:w
  71. 返 回 值:无
  72. --------------------------------------------------------------*/  
  73. void ad9850_w(BYTE w)
  74. {
  75.         for(unsigned char i=0;i<8;i++)
  76.         {
  77.                 (((w>>i)&1)==1)?(ad9850_bit_data_set):(ad9850_bit_data_clr);
  78.                 ad9850_w_clk_set;
  79.                 Delay_us(1);
  80.                 ad9850_w_clk_clr;
  81.         }
  82. }
  83. /*-------------------------------------------------------------/
  84. 函数名称:        ad9850_wr_serial
  85. 功能描述:        向ad9850中写命令与数据(串口)
  86. 传    参:w0,frequence
  87. 返 回 值:无
  88. --------------------------------------------------------------*/  
  89. void ad9850_wr_serial(BYTE w0,unsigned int frequence)
  90. {
  91.         unsigned int y=(unsigned int)(frequence*34.35973836);
  92.         ad9850_w(y);                //写w4数据
  93.         ad9850_w(y>>8);        //写w3数据
  94.         ad9850_w(y>>16);        //写w2数据
  95.         ad9850_w(y>>24);        //写w1数据
  96.         ad9850_w(w0);        //写w0数据
  97.         ad9850_fq_up_set;        //移入始能
  98.         Delay_us(1);
  99.         ad9850_fq_up_clr;
  100. }
  101. /*-------------------------------------------------------------/
  102. 函数名称:        ADC
  103. 功能描述:        单次模数转换函数
  104. 传    参:无
  105. 返 回 值:10位有效
  106. --------------------------------------------------------------*/  
  107. UINT32 ADC()
  108. {
  109.         v_pADCPregs-> ADCCON |= 0x1;        // 开始转换
  110.         while (        (v_pADCPregs-> ADCCON) & (1<<0));        //检测开始位是否为低电平
  111.         while(!((v_pADCPregs-> ADCCON) & (1<<15)));        //检测转换是否完毕标志位
  112.         return ((v_pADCPregs->ADCDAT0) & 0x3ff);
  113. }
  114. /*-------------------------------------------------------------/
  115. 函数名称:        ADC_DigitalFilter_1
  116. 功能描述:        模数转换+数字滤波函数(32次AD)
  117. 传    参:无
  118. 返 回 值:10位有效
  119. --------------------------------------------------------------*/  
  120. UINT32 ADC_DigitalFilter_1()
  121. {
  122.         v_pADCPregs-> ADCDLY = 10;        // ADC开始延时寄存器(延时时间最小)                                                        
  123.         for (unsigned int j=0; j<32; j++)         {Array1[j]=ADC();}
  124.         v_pADCPregs-> ADCDLY = 20000;                //ADC开始延时寄存器(恢复默认,否则要影响触摸屏)
  125.         qsort(Array1,32,4,comp);
  126.         unsigned int        add=0;
  127.         for (unsigned int j=0; j<16; j++) {add+=Array1[j+8];}
  128.         return add>>4;
  129. }
  130. /*-------------------------------------------------------------/
  131. 函数名称:        ADC_DigitalFilter_2
  132. 功能描述:        模数转换+数字滤波函数(64次AD)
  133. 传    参:无
  134. 返 回 值:10位有效
  135. --------------------------------------------------------------*/  
  136. UINT32 ADC_DigitalFilter_2()
  137. {
  138.         v_pADCPregs-> ADCDLY = 10;                               
  139.         for (unsigned int j=0; j<64; j++) {Array2[j]=ADC();}
  140.         v_pADCPregs-> ADCDLY = 20000;       
  141.         qsort(Array2,64,4,comp);
  142.         unsigned int        add=0;
  143.         for (unsigned int j=0; j<32; j++) {add+=Array2[j+16];}
  144.         return add>>5;
  145. }
  146. //-----------------------------------------------------------------------------
  147. //----------------------DllMain入口--------------------------------
  148. BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
  149. {
  150.     switch ( ul_reason_for_call )
  151.     {
  152.         case DLL_PROCESS_ATTACH:
  153.                         RETAILMSG(1, (_T("SEW_PROCESS_ATTACH\r\n")));        //DisableThreadLibraryCalls((HMODULE) hModule);
  154.         break;
  155.         case DLL_PROCESS_DETACH:
  156.                         RETAILMSG(1, (_T("SEW_PROCESS_DETACH\r\n")));
  157.         break;
  158.     }
  159.         return true;
  160. }
  161. //-----------------------------------------------------------------------------
  162. //-------------init-init-init-init-init-init-init-------------------
  163. DLLAPI DWORD SWE_Init(LPCTSTR pContext)
  164. {
  165.     RETAILMSG(1,(TEXT("***SWE_init\r\n")));
  166.         //IO寄存器地址映射
  167.     v_pIOPregs = (volatile S3C2440A_IOPORT_REG *)VirtualAlloc(0, sizeof(S3C2440A_IOPORT_REG), MEM_RESERVE,PAGE_NOACCESS);//为v_pIOPregs变量分配虚拟空间
  168.         if (v_pIOPregs == NULL)
  169.     {
  170.         ERRORMSG(1,(_T("SWE_InitAddrIO: VirtualAlloc failed!\r\n"))); //分配虚拟空间不成功
  171.         return 0;
  172.     }
  173.     if (!VirtualCopy((PVOID)v_pIOPregs, (PVOID)(S3C2440A_BASE_REG_PA_IOPORT>>8) , sizeof(S3C2440A_IOPORT_REG), PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE))  //把v_pIOPregs虚拟空间映射到物理内存
  174.     {
  175.         ERRORMSG(1,(_T("SWE_InitAddrIO: VirtualCopy failed!\r\n")));
  176.         VirtualFree((PVOID) v_pIOPregs, 0, MEM_RELEASE); //释放虚拟空间
  177.         v_pIOPregs = NULL;
  178.         return 0;
  179.     }
  180.         //ADC寄存器地址映射
  181.     v_pADCPregs = (volatile S3C2440A_ADC_REG *)VirtualAlloc(0, sizeof(S3C2440A_ADC_REG), MEM_RESERVE,PAGE_NOACCESS);
  182.     if (v_pADCPregs == NULL) //分配虚拟空间不成功
  183.         {
  184.                 ERRORMSG(1,(TEXT("SWE_InitAddrADC: VirtualAlloc failed!\r\n")));
  185.                 return 0;
  186.         }
  187.         if(!VirtualCopy((PVOID)v_pADCPregs, (PVOID)(S3C2440A_BASE_REG_PA_ADC>>8) , sizeof(S3C2440A_ADC_REG), PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE))  //把虚拟空间与物理空间绑定
  188.         {
  189.                 ERRORMSG(1,(TEXT("SWE_InitAddrADC: VirtualCopy failed!\r\n")));
  190.                 VirtualFree((PVOID) v_pADCPregs, 0, MEM_RELEASE); //释放虚拟空间
  191.                 v_pADCPregs = NULL;
  192.                 return 0;
  193.         }
  194.         return 1;
  195. }
  196. //-----------------------------------------------------------------------------
  197. //-------------Deinit-DeinitDeinit-DeinitDein------------
  198. DLLAPI BOOL SWE_Deinit(DWORD hDeviceContext)
  199. {
  200.         RETAILMSG(1,(TEXT("***SWE_Deinit\r\n")));
  201.      
  202.         if(v_pIOPregs)
  203.         {
  204.                 VirtualFree((PVOID) v_pIOPregs, 0, MEM_RELEASE); /*释放虚拟空间*/   
  205.                 v_pIOPregs = NULL;
  206.         }       
  207.         if(v_pADCPregs)
  208.         {
  209.                 VirtualFree((PVOID) v_pADCPregs, 0, MEM_RELEASE); /*释放虚拟空间*/   
  210.                 v_pADCPregs = NULL;
  211.         }
  212.         return true;
  213. }
  214. //-----------------------------------------------------------------------------
  215. //-----------------------------------------------------------------------------
  216. DLLAPI DWORD SWE_Open(DWORD hDeviceContext,DWORD AccessCode,DWORD ShareMode)
  217. {
  218.     //设置GPG7:GPG5:GPG2:GPE12:端口输出
  219.         v_pIOPregs->GPGCON &=~ (1<< 15);        //设置GPG7输出(15:14位)
  220.         v_pIOPregs->GPGCON |= (1<< 14);       
  221.         v_pIOPregs->GPGCON &=~ (1<< 11);        //设置GPG5输出(11:10位)
  222.         v_pIOPregs->GPGCON |= (1<< 10);       
  223.         v_pIOPregs->GPGCON &=~ (1<< 5);                //设置GPG2输出(5:4位)
  224.         v_pIOPregs->GPGCON |= (1<< 4);       
  225.         v_pIOPregs->GPECON &=~ (1<< 25);        //设置GPE12输出(25:24位)
  226.         v_pIOPregs->GPECON |= (1<< 24);       
  227.         //设置放电端口
  228.         v_pIOPregs->GPBCON &=~ (1<< 15);        //设置GPG7输入(15:14位)
  229.         v_pIOPregs->GPBCON &=~ (1<< 14);
  230.         v_pIOPregs->GPBUP |= (1<< 7);                                //取消GPB7端口上拉电阻
  231.         //创建AD线程
  232.     hand = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ADThread,NULL,0,NULL);                                                               
  233.         // 设置线程的优先级低于触控屏
  234.         if( !CeSetThreadPriority(hand, 130))
  235.         {
  236.                 RETAILMSG(1,(TEXT("SWE: Failed setting Thread Priority.\r\n")));
  237.                 return 0;
  238.         }
  239.         RETAILMSG(1,(TEXT("***SWE_OPEN\r\n")));
  240.         return (1);
  241. }
  242. //-----------------------------------------------------------------------------
  243. //-----------------------------------------------------------------------------
  244. DLLAPI BOOL SWE_Close(DWORD hOpenContext)
  245. {
  246.         RETAILMSG(1,(TEXT("***SWE_Close\r\n")));
  247.     TerminateThread(hand,-1); //在线程外终止一个线程,用于强制终止线程(终止线程句柄,退出码)
  248.     CloseHandle(hand); //关闭AD转换线程句柄
  249.         v_pIOPregs->GPGCON &=~ (1<< 15);        //设置GPG7输入(15:14位)
  250.         v_pIOPregs->GPGCON &=~ (1<< 14);       
  251.         v_pIOPregs->GPGCON &=~ (1<< 11);        //设置GPG5输入(11:10位)
  252.         v_pIOPregs->GPGCON &=~ (1<< 10);       
  253.         v_pIOPregs->GPGCON &=~ (1<< 5);                //设置GPG2输入(5:4位)
  254.         v_pIOPregs->GPGCON &=~ (1<< 4);       
  255.         v_pIOPregs->GPECON &=~ (1<< 25);        //设置GPE12输入(25:24位)
  256.         v_pIOPregs->GPECON &=~ (1<< 24);       
  257.         return (true);
  258. }
  259. //-----------------------------------------------------------------------------
  260. //----------IO-IO-IO-IO-IO-IO-IO-IO-IO-IO-IO-------------
  261. DLLAPI BOOL SWE_IOControl(DWORD hOpenContext, //DRV_Open()返回的设备打开上下文  
  262.                                                   DWORD dwCode,       //要发送的控制码(应用程序发往驱动控制命令,握手约定)
  263.                                                   PBYTE pBufIn,       //输入,指向输入缓冲区的指针   
  264.                                                   DWORD dwLenIn,      //输入缓冲区的长度   
  265.                                                   PDWORD pBufOut,      //输出,指向输出缓冲区的指针(将指针由BYTE型改成DWORD型)   
  266.                                                   DWORD dwLenOut,     //输出缓冲区的长度   
  267.                                                   PDWORD pdwActualOut)//输出,设备实际输出字符数   
  268. {
  269.         dwLenIn=16;   //输入缓冲区的长度
  270.         dwLenOut=310;     //输出缓冲区的长度
  271.         unsigned int data =0;       
  272.         f_start=0;
  273.         step=0;
  274.         delay=0;
  275.         ad_channel=0;
  276.         for(unsigned char i=0; i<16; i++)
  277.         {
  278.                 data=*pBufIn;
  279.                 if(i<4) f_start += data<<(8*i);
  280.                 else if(i<8) step += data<<(8*(i-4));
  281.                 else if(i<12) delay += data<<(8*(i-8));
  282.                 else if(i<16) ad_channel += data<<(8*(i-12));
  283.                 pBufIn++;
  284.         }
  285.         memcpy(pBufOut, adc,1240);        //数组拷贝到*pBufOut(310*4字节)
  286.         flag=true;        //设立AD转换标记,在线程内完成采集
  287.         RETAILMSG(1,(TEXT("***SWE_IOControl\n")));
  288.         return true;
  289. }
  290. //-----------------------------------------------------------------------------
  291. //-----------------------------------------------------------------------------
  292. DLLAPI DWORD SWE_Read(DWORD hOpenContext, LPVOID pBuffer, DWORD Count)
  293. {
  294.            RETAILMSG(1,(TEXT("***SWE_Read\n")));
  295.     return 1;
  296. }
  297. //-----------------------------------------------------------------------------
  298. //-----------------------------------------------------------------------------
  299. DLLAPI DWORD SWE_Write(DWORD hOpenContext, LPCVOID pBuffer, DWORD Count)
  300. {
  301.     RETAILMSG(1,(TEXT("***SWE_Write\n")));
  302.     return 1;
  303. }
  304. //-----------------------------------------------------------------------------
  305. //-----------------------------------------------------------------------------
  306. DLLAPI DWORD SWE_Seek(DWORD hOpenContext,long Amount,DWORD Type)
  307. {
  308.     DWORD dwRet = 0;
  309.     RETAILMSG(1,(TEXT("***SWE_Seek\n")));
  310.     return dwRet;
  311. }
  312. //-----------------------------------------------------------------------------
  313. //-----------------------------------------------------------------------------
  314. DLLAPI void SWE_PowerDown(DWORD hDeviceContext)
  315. {
  316.     RETAILMSG(1,(TEXT("***SWE_PowerDown\n")));
  317. }
  318. //-----------------------------------------------------------------------------
  319. //-----------------------------------------------------------------------------
  320. DLLAPI void SWE_PowerUp(DWORD hDeviceContext)
  321. {
  322.     RETAILMSG(1,(TEXT("***SWE_PowerUp\n")));
  323. }
  324. //-----------------------------------------------------------------------------
  325. //--------------------------AD线程---------------------------------------
  326. DWORD WINAPI ADThread()
  327. {
  328.     RETAILMSG(1,(_T("ADThread++\r\n")));
  329.         HWND hwnd;
  330.         if(!(hwnd = FindWindow(NULL,_T("扫频仪"))))        //获取窗口句柄
  331.         {RETAILMSG(1,(TEXT("Get handle failure\n")));}
  332.         int time1=0;        //设置电池电压发回窗口控制循环时间
  333.         while (1)
  334.         {
  335.                 if(flag==true)//查询读/写标记
  336.                 {
  337.                         DWORD dstart=GetTickCount();
  338.                         v_pADCPregs-> ADCCON = (1 << 14) | (98 << 6) | (ad_channel << 3);         //AD初始化(D14="1"分频有效, D13:6=98分频系数1Mhz()10us,建立通道号
  339.                         for (unsigned int i=0; i<310; i++)        // =1,等待310次转化完成
  340.                         {                               
  341.                                 if((step!=0)|(i==0))                        //步进=0, DDS频率修改第1次, 以后for循环内不修改DDS频率
  342.                                 {
  343.                                         ad9850_reset_serial();
  344.                                         ad9850_wr_serial(0x0, f_start+i*step);        //向ad9850中写命令与数据(串口)  
  345.                                 }       
  346.                                 Sleep(1);  //线程阻塞1ms
  347.                                 if(delay==1)
  348.                                         adc[i]=ADC_DigitalFilter_1();               
  349.                                 else                       
  350.                                         adc[i]=ADC_DigitalFilter_2();                       
  351.                         }
  352.                         RETAILMSG(1,(TEXT("time= %dms\n"), (GetTickCount() - dstart)));        //计数310次AD转换运行时间并显示
  353.                         //向ad9850写入扫描初始频率
  354.                         ad9850_wr_serial(0x0, f_start);
  355.                         //ch3通道电池电压监控
  356.                         time1=0;
  357.                         v_pADCPregs-> ADCCON = (1 << 14) | (98 << 6) | (3 << 3);
  358.                          int UV =ADC_DigitalFilter_1();       
  359.                         flag=false;
  360.                         if(hwnd != NULL)
  361.                         {
  362.                                 if (!(PostMessage(hwnd, 310, 0,UV)))        //将测量值发送到应用程序窗口
  363.                                         {RETAILMSG(1,(TEXT("Messaging failure\n")));}
  364.                         }
  365.                 }
  366.                 else
  367.                 {
  368.                         time1++;
  369.                         if(time1==100)
  370.                         {
  371.                                 time1=0;
  372.                                 int UV =ADC_DigitalFilter_1();       
  373.                                 flag=false;
  374.                                 if(hwnd != NULL)
  375.                                 {
  376.                                         if (!(PostMessage(hwnd, 3700, 0,UV)))        //将电池电压发送到应用程序窗口
  377.                                                 {RETAILMSG(1,(TEXT("Messaging failure\n")));}
  378.                                 }
  379.                         }
  380.                         else Sleep(10);       
  381.                 }
  382.         }
  383.         return true;
  384. }
复制代码


程序编译后生成SWEEP.dll驱动。




补充内容 (2024-7-13 16:56):
不小心排序回调函数删了,补上
/******排序回调函数******/
int comp(const void * p1,const void * p2)
{
        int n1 = *((const int *)p1);
        int n2 = *((const int *)p2);
        return n1 < n2 ?-1n1 > n2?1:0);
}

补充内容 (2024-7-25 15:37):
谢谢老师们加分鼓励!

评分

2

查看全部评分

     
 楼主| 发表于 2024-7-25 15:17:10 | 显示全部楼层
本帖最后由 gxg0000 于 2024-7-25 15:19 编辑

窗口应用程序C#书写,有注释,仅供参考!

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Runtime.InteropServices;
  9. using System.Threading;
  10. using System.Drawing.Imaging;
  11. using 琴键开关;
  12. using System.Diagnostics;


  13. namespace 扫频仪
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.         public const int GWL_WNDPROC = (-4); //nIndex的参数:为窗口过程设定一个新的地址
  22.         public IntPtr OldProc = IntPtr.Zero;
  23.         [DllImport("coredll.dll")] //API声明,将消息信息传送给指定的窗口过程
  24.         private extern static int CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hwnd, uint msg, uint wParam, int lParam);
  25.         [DllImport("coredll.dll")] //API声明,改变指定窗口的属性
  26.         public static extern IntPtr SetWindowLong(IntPtr hwnd, int nIndex, IntPtr dwNewLong);
  27.         public delegate int WndProcHandler(IntPtr hwnd, uint msg, uint wParam, int lParam);//定义委托(Delegate)的类型函数
  28.         WndProcHandler myproc = null; //声明委托类型对象

  29.         int hd; //声明流驱动设备句柄
  30.         int hDrv; //声明打开设备句柄
  31.         bool button1_state=true;
  32.         bool loop1= true;   //数据刷新标记
  33.         bool loop2 = false;  //图形上显示带宽标记
  34.         int dwCode; //控制码
  35.         uint dstart = 0;

  36.         public const int OPEN_EXISTING = 3;
  37.         public const int INVALID_HANDLE_VALUE = -1;
  38.         public const int GENERIC_READ = -0;
  39.         public const int GENERIC_WRITE = 0x40000000;

  40.         public static uint[] pBuffIn1 = new uint[16]; //声明写入数组
  41.         public static byte[] pBuffIn2 = new byte[2];
  42.         public static uint[] pBufOut1 = new uint[310];//声明读出数组
  43.         public static uint[] pBufOut2 = new uint[310];
  44.         uint[] f1 = { 455000, 465000, 4000000, 8000000, 10700000, 12000000, 16000000, 20000000, 24000000, 30000000 };
  45.         uint[] f2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 100 };
  46.         KeySwitch keySwitch5;
  47.         KeySwitch keySwitch6;
  48.         KeySwitch keySwitch7;

  49.         [DllImport("Coredll")]
  50.         public static extern uint GetTickCount();
  51.         [DllImport("Coredll")]
  52.         public static extern int CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFLagsAndAttributes, int hTemplateFile);
  53.         [DllImport("Coredll")]
  54.         public static extern bool WriteFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, byte[] pNumberOfBytesWritten, int lpOverlapped);
  55.         [DllImport("coredll")]
  56.         public static extern bool ReadFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToRead, byte[] lpNumberOfBytesRead, int lpOverlapped);
  57.         [DllImport("Coredll")]
  58.         public static extern int DeviceIoControl(int hDevice, int dwIoControlCode, uint[] lpInBuffer, int nInBufferSize, uint[] lpOutBuffer, int nOutBufferSize, IntPtr lpBytesReturned, int lpOverlapped);
  59.         [DllImport("coredll")]
  60.         public static extern bool CloseHandle(int hObject);
  61.         [DllImport("coredll")] //加载流驱动
  62.         public static extern int ActivateDeviceEx(string lpszDevKey, IntPtr lpRegEnts, UInt32 cRegEnts, IntPtr dwParam);
  63.         [DllImport("coredll")] //卸载流
  64.         public static extern bool DeactivateDevice(int hDevice);
  65.         
  66.         Pen p1 = new Pen(Color.LimeGreen, 1); //创建画笔p1(色,像数)
  67.         Pen p2 = new Pen(Color.DimGray, 1);
  68.         Pen p3 = new Pen(Color.FromArgb(30, 30, 30), 1);
  69.         Brush brush1= new SolidBrush(Color.Brown);

  70.         Bitmap tup1; Graphics g1; //画曲线
  71.         Bitmap tup2; Graphics g2; //画网格和坐标
  72.         
  73.         Thread ThreadAd;
  74.         /// <summary>
  75.         /// 画显示电池电压图形
  76.         /// </summary>
  77.         /// <param name="level">电量等级</param>
  78.         private void UV(int level)
  79.         {
  80.             if (level > 715) //欠压7.18V时对应AD值715
  81.             {
  82.                 level = (level - 715) / 17 + 1;
  83.                 level = (level > 4) ? 4 : level;   //限幅
  84.             }
  85.             else
  86.             {
  87.                 level = 0;
  88.             }
  89.             Bitmap tup3; Graphics g3; //画电池图形
  90.             tup3 = new Bitmap(22, 14); g3 = Graphics.FromImage(tup3);
  91.             Brush brush2 = new SolidBrush(Color.Gray);
  92.             g3.DrawRectangle(new Pen(Color.FromArgb(50, 50, 50), 1), 0, 4, 2, 5);
  93.             g3.DrawRectangle(new Pen(Color.FromArgb(50, 50, 50), 1), 2, 2, 19, 9);
  94.             g3.FillRectangle(brush2, 20 - 4 * level, 4, 4 * level, 6);
  95.             pictureBox2.Image = tup3;
  96.             g3.Dispose();
  97.         }
  98.         /// <summary>
  99.         /// 频率显示格式化
  100.         /// </summary>
  101.         /// <param name="f">频率</param>
  102.         /// <param name="str1">返回频率值</param>
  103.         /// <param name="str2">返回频率单位</param>
  104.         private void Freq_Format(int f, out string str1, out string str2)
  105.         {
  106.             string length1 = f.ToString();
  107.             if (length1.Length < 4) { str1 = length1; str2 = "Hz"; }
  108.             else if (length1.Length == 4) { str1 = length1.Insert(1, "."); str2 = "KHz"; }
  109.             else if (length1.Length == 5) { str1 = length1.Insert(2, ".").Remove(5, 1); str2 = "KHz"; }
  110.             else if (length1.Length == 6) { str1 = length1.Insert(3, ".").Remove(5, 2); str2 = "KHz"; }
  111.             else if (length1.Length == 7) { str1 = length1.Insert(1, ".").Remove(5, 3); str2 = "MHz"; }
  112.             else if (length1.Length == 8) { str1 = length1.Insert(2, ".").Remove(5, 4); str2 = "MHz"; }
  113.             else { str1 = "0"; str2 = "0"; }
  114.         }
  115.         /// <summary>
  116.         /// 图像提亮函数
  117.         /// </summary>
  118.         /// <param name="sur">源图像</param>
  119.         /// <param name="light">背景图像提亮(=0亮度不变、正数亮度增加、负数亮度降低</param>
  120.         /// <returns></returns>
  121.         private Bitmap ImageLight(Bitmap sur, int light)
  122.         {
  123.             if (light == 0) { return sur; }
  124.             Bitmap tempCur = (Bitmap)sur.Clone(); //创建临时背景位图副本
  125.             BitmapData bmpData1 = tempCur.LockBits(new Rectangle(0, 0, tempCur.Width, tempCur.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
  126.             int stride = bmpData1.Stride; //背景图像扫描宽度
  127.             byte[] Data1 = new byte[stride * tempCur.Height + 3]; //背景图像数组
  128.             Marshal.Copy(bmpData1.Scan0, Data1, 0, stride * tempCur.Height);//读取背景图像数据(拷贝到托管数组)
  129.             for (int i = 0; i < tempCur.Height; i++) //高内循环
  130.             {
  131.                 for (int j = 0; j < tempCur.Width; j++) //行内循环
  132.                 {
  133.                     int m = i * stride + j * 3;
  134.                     int b = Data1[m] + light;
  135.                     int g = Data1[m + 1] + light;
  136.                     int r = Data1[m + 2] + light;
  137.                     if (light > 0)
  138.                     {
  139.                         b = b > 255 ? 255 : b;
  140.                         g = g > 255 ? 255 : g;
  141.                         r = r > 255 ? 255 : r;
  142.                     }
  143.                     else if (light < 0)
  144.                     {
  145.                         b = b < 0 ? 0 : b;
  146.                         g = g < 0 ? 0 : g;
  147.                         r = r < 0 ? 0 : r;
  148.                     }
  149.                     Data1[m] = (byte)b;
  150.                     Data1[m + 1] = (byte)g;
  151.                     Data1[m + 2] = (byte)r;
  152.                 }
  153.             }
  154.             Marshal.Copy(Data1, 0, bmpData1.Scan0, stride * tempCur.Height); //将数据写入图像(拷贝到bmpData1)                                                     
  155.             tempCur.UnlockBits(bmpData1);
  156.             return tempCur;
  157.         }
  158.         /// <summary>
  159.         /// 生成窗口上固定的字符串
  160.         /// </summary>
  161.         /// <param name="sender"></param>
  162.         /// <param name="e"></param>
  163.         private void Form1_Paint(object sender, PaintEventArgs e)
  164.         {
  165.             Graphics g3 = e.Graphics;
  166.             Font font = new Font("Arial", 9f, FontStyle.Regular);
  167.             Brush brush = new SolidBrush(Color.DimGray);
  168.             //画显示图形区矩形框
  169.             g3.DrawRectangle(p2, 21, 19, 311, 217);
  170.             //y轴刻度字符串生成
  171.             g3.DrawString("电压(V)", font, brush, 1, 4);
  172.             g3.DrawString("  3", font, brush, 1, 18);
  173.             g3.DrawString("2.5", font, brush, 1, 51);
  174.             g3.DrawString("  2", font, brush, 1, 87);
  175.             g3.DrawString("1.5", font, brush, 1, 123);
  176.             g3.DrawString("  1", font, brush, 1, 159);
  177.             g3.DrawString("0.5", font, brush, 1, 195);
  178.             g3.DrawString("  0", font, brush, 1, 227);
  179.             //频率轴刻度字符串生成
  180.             g3.DrawString("0", font, brush, 19, 238);
  181.             g3.DrawString("1", font, brush, 51, 238);
  182.             g3.DrawString("2", font, brush, 82, 238);
  183.             g3.DrawString("3", font, brush, 113, 238);
  184.             g3.DrawString("4", font, brush, 144, 238);
  185.             g3.DrawString("5", font, brush, 175, 238);
  186.             g3.DrawString("6", font, brush, 206, 238);
  187.             g3.DrawString("7", font, brush, 237, 238);
  188.             g3.DrawString("8", font, brush, 268, 238);
  189.             g3.DrawString("9", font, brush, 299, 238);
  190.             g3.DrawString("10", font, brush, 326, 238);
  191.             //
  192.             //g3.DrawString("gxg0000", font, brush, 1, 252);
  193.             g3.DrawString("扫描频率", font, brush, 142, 252);
  194.             //
  195.             g3.FillRectangle(new SolidBrush(Color.DarkKhaki), 340, 0, 138, 98);
  196.             g3.DrawString("谐振频率", font, new SolidBrush(Color.DarkRed), 342, 5);
  197.             g3.DrawLine(new Pen(Color.MediumOrchid, 2), 346, 49, 472, 49);
  198.             g3.DrawString("峰值电压       V", font, new SolidBrush(Color.Indigo), 342, 55);
  199.             //
  200.             g3.FillRectangle(new SolidBrush(Color.YellowGreen), 340, 100, 138, 82);
  201.             g3.DrawString("带宽", font, new SolidBrush(Color.Purple), 342+24, 103);
  202.             g3.DrawString("下边频", font, new SolidBrush(Color.Purple), 342+12, 119);
  203.             g3.DrawString("上边频", font, new SolidBrush(Color.Purple), 342+12, 135);
  204.             g3.DrawString("Q值", font, new SolidBrush(Color.Purple), 342+30, 151);
  205.             g3.DrawString("矩形系数", font, new SolidBrush(Color.Purple), 342, 167);
  206.             //
  207.             g3.FillRectangle(new SolidBrush(Color.LightSeaGreen), 340, 184, 138, 51);
  208.             g3.DrawString("开始频率", font, new SolidBrush(Color.Navy), 342, 188);
  209.             g3.DrawString("步进", font, new SolidBrush(Color.Navy), 342+24, 203);
  210.             g3.DrawString("终止频率", font, new SolidBrush(Color.Navy), 342, 218);
  211.         }
  212.         private void Form1_Load(System.Object sender, System.EventArgs e) //初始化
  213.         {
  214.             Cursor.Hide();
  215.             this.Width = 480;
  216.             this.Height = 267;
  217.             label18.Width = 1;   //竖线
  218.             label18.Height = 216 - 12;
  219.             label18.Top = 20;
  220.             label18.Left = 22;
  221.             label18.Visible = false;
  222.             label19.Width = 310 - 32; //横向
  223.             label19.Height = 1;
  224.             label19.Top = 20;
  225.             label19.Left = 22 + 32;
  226.             label19.Visible = false;

  227.             label31.Width = 32;    //频率刻度
  228.             label31.Height = 12;
  229.             label31.Top = 20 - 12 + 216;
  230.             label31.Left = 22;
  231.             label31.Visible = false;
  232.             label32.Width = 32;    //增益刻度
  233.             label32.Height = 12;
  234.             label32.Top = 20;
  235.             label32.Left = 22;
  236.             label32.Visible = false;
  237.             hd = ActivateDeviceEx("\\Drivers\\SWEEP", IntPtr.Zero, 0, IntPtr.Zero); //加载驱动
  238.             hDrv = CreateFile("SWE1:", GENERIC_READ + GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
  239.             if (INVALID_HANDLE_VALUE == hDrv)   //获得句柄hDrv,句柄=-1,说明设备未打开
  240.             { MessageBox.Show("不能打开SWE1"); }
  241.             tup1 = new Bitmap(310, 216); g1 = Graphics.FromImage(tup1);
  242.             tup2 = new Bitmap(310, 216); g2 = Graphics.FromImage(tup2);
  243.             dwCode = 0; //控制码
  244.             pBuffIn1[0] = 441750;    //扫描开始频率
  245.             pBuffIn1[1] = 155;   //步进
  246.             pBuffIn1[2] = 1; //AD转换增加延时1ms
  247.             pBuffIn1[3] = 1; //AD转换选择1通道
  248.             label2.Text = "0";
  249.             label3.Text = "0";
  250.             label4.Text = "0 Hz";
  251.             label5.Text = "0 Hz";
  252.             label6.Text = "0 Hz";
  253.             label8.Text = "";
  254.             //生成预置频率控件
  255.             keySwitch5 = new KeySwitch();
  256.             keySwitch5.SwichValue = 2;
  257.             keySwitch5.Left = 1;
  258.             keySwitch5.Top = 17;
  259.             keySwitch5.SwichText = new string[] { "455KHz", "465KHz", "4MHz", "8MHz", "10.7MHz", "12MHz", "16MHz", "20MHz", "24MHz", "手动"};
  260.             this.panel1.Controls.Add(keySwitch5); //添加到控件集合
  261.             this.keySwitch5.MouseDown += new KeySwitch.MouseDownEventHandler(this.keySwitch5_MouseDown);
  262.             //生成扫频范围控件
  263.             keySwitch6 = new KeySwitch();
  264.             keySwitch6.SwichValue = 5;
  265.             keySwitch6.Left = 62;
  266.             keySwitch6.Top = 17;
  267.             keySwitch6.SwichText = new string[] { "±1%", "±2%", "±3%", "±4%", "±5%", "±6%", "±7%", "±8%", "±9%", "手动" };
  268.             this.panel1.Controls.Add(keySwitch6); //添加到控件集合
  269.             this.keySwitch6.MouseDown += new KeySwitch.MouseDownEventHandler(this.keySwitch6_MouseDown);
  270.             //生成AD转换通道号控件            
  271.             keySwitch7 = new KeySwitch();
  272.             keySwitch7.SwichNumber = 2;
  273.             keySwitch7.Left = 123;
  274.             keySwitch7.Top = 17;
  275.             keySwitch7.Height = 50;
  276.             keySwitch7.SwichText = new string[] { "ch1", "ch2" };
  277.             this.panel1.Controls.Add(keySwitch7); //添加到控件集合
  278.             //生成显示速度切换控件  
  279.             //keySwitch4 = new KeySwitch();
  280.             //keySwitch4.SwichNumber = 2;
  281.             //keySwitch4.Left = 184;
  282.             //keySwitch4.Top = 17;
  283.             //keySwitch4.Height = 50;
  284.             //keySwitch4.SwichText = new string[] { "快", "慢" };
  285.             //this.panel1.Controls.Add(keySwitch4); //添加到控件集合
  286.             //
  287.             ThreadAd = new Thread(myTask); //创建读取电压采集数据的线程
  288.             ThreadAd.Start(); //启动线程
  289.         }
复制代码


待续



回复 支持 反对

使用道具 举报

     
 楼主| 发表于 2024-7-25 15:20:56 | 显示全部楼层
  1. /// <summary>
  2.         /// 截获驱动程序发出完成电压采集的消息
  3.         /// </summary>
  4.         /// <param name="e"></param>
  5.         protected override void OnHandleCreated(EventArgs e) //引发事件时,会通过委托调用事件处理程序
  6.         {
  7.             base.OnHandleCreated(e);
  8.             myproc = new WndProcHandler(MyWndProc);
  9.             OldProc = SetWindowLong(Handle, GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate(myproc));
  10.         }
  11.         int MyWndProc(IntPtr hwnd, uint msg, uint wParam, int lParam)
  12.         {
  13.             if (msg == 310)
  14.             {
  15.                 loop1 = true;
  16.                 UV(lParam); //显示电池电量(频率扫描状态)
  17.             }
  18.             else if (msg == 3700)
  19.             {
  20.                 UV(lParam); //显示电池电量(暂停状态)
  21.             }
  22.             return CallWindowProc(OldProc, hwnd, msg, wParam, lParam);
  23.         }

  24.         private void myTask() //数据接收及界面显示方法(多线程)
  25.         {
  26.             this.Invoke(new EventHandler(myTask_Drawing)); //委托调用画界面图形方法
  27.             while (true)
  28.             {
  29.                  this.Invoke(new EventHandler(myTask_Show)); //委托调用接收数据和显示方法
  30.             }
  31.         }
  32.         private void myTask_Drawing(object sender, EventArgs e)  //被调用画界面图形方法
  33.         {
  34.             for (int i = 31; i < 310; i += 31)
  35.             {
  36.                 g2.DrawLine(p3, i, 0, i, 217); //画垂直格子线,间距31
  37.             }
  38.             for (int i = 36; i < 216; i += 36)
  39.             {
  40.                 g2.DrawLine(p3, 0, i, 310, i); //画水平格子线,间距31
  41.             }
  42.             pictureBox1.Image = tup2;
  43.         }
  44.         private void myTask_Show(object sender, EventArgs e)  //被调用接收数据和显示方法
  45.         {
  46.             if (pBuffIn1[2] == 1)
  47.                 label17.Text = "快";
  48.             else
  49.                 label17.Text = "慢";
  50.             if (loop1&(!button1_state) == true)
  51.             {
  52.                 label14.Text = (GetTickCount() - dstart).ToString() + "ms";  //显示一次循环时间
  53.                 dstart = GetTickCount();
  54.                 if (DeviceIoControl(hDrv, dwCode, pBuffIn1, 16, pBufOut1, 1, IntPtr.Zero, 0) == 0)
  55.                 {
  56.                     MessageBox.Show("读写SWE1出错");
  57.                 }
  58.                 for (int i = 0; i < 310; i++)
  59.                 {
  60.                     pBufOut2[i] = pBufOut1[i];  //复制副本
  61.                 }
  62.                 Array.Sort(pBufOut2); //副本排序
  63.                 uint Umax = pBufOut2[309];
  64.                 int fs = Array.IndexOf<uint>(pBufOut1, Umax, 0);  //索引中心频率位置
  65.                 //搜索增益下降-3dB时的带宽(可用于计算Q值等...)
  66.                int fL = fs;
  67.                while ((fL != 0) && (Convert.ToInt32(pBufOut1[fL]) > (Convert.ToInt32(pBufOut2[309]) * 0.708)))
  68.                 { fL--; } //搜索中心频率左边下降-3dB频率
  69.                int fH = fs;
  70.                while ((fH != 309) && (Convert.ToInt32(pBufOut1[fH]) > (Convert.ToInt32(pBufOut2[309]) * 0.708)))
  71.                 { fH++; } //搜索中心频率右边下降-3dB频率
  72.                int BW_3dB = 0;
  73.                if ((fL == 0) | (fH == 309)) { BW_3dB = 0; }//未搜索到+-3dB处频率
  74.                else { BW_3dB = fH - fL; }  //计算-3dB带宽
  75.                 int b1 = Convert.ToInt32(label9.Text.Replace(",", "").Replace(" Hz", ""));   //开始频率
  76.                 int b2 = Convert.ToInt32(label10.Text.Replace(",", "").Replace(" Hz", ""));  //步进         
  77.                 int fs_peak = b1 + fs * b2;   //谐振峰频率
  78.                 string s1 = "0";
  79.                 string s2 = "0";
  80.                 Freq_Format(fs_peak,out s1, out s2);
  81.                 label2.Text = s1;
  82.                 label1.Text = s2; //显示谐振频率
  83.                 uint u1 = (Umax > 930) ? 930 : Umax;
  84.                 label3.Text = String.Format("{0:N}", u1 * 3.3 / 1023); //显示谐振峰电压(限幅3V)
  85.                 label4.Text = String.Format("{0:N}", BW_3dB * b2).Replace(".00", "") + " Hz";//显示带宽
  86.                 label5.Text = String.Format("{0:N}", b1 + fL * b2).Replace(".00", "") + " Hz";//显示下边频
  87.                 label6.Text = String.Format("{0:N}", b1 + fH * b2).Replace(".00", "") + " Hz";//显示上边频
  88.                 //搜索增益下降-20dB时的带宽(与3dB带宽配合计算通道矩形系数)
  89.                 int BW_20dB = 0;
  90.                 int fL_20dB = fs;
  91.                 while ((fL_20dB != 0) && (Convert.ToInt32(pBufOut1[fL_20dB]) > (Convert.ToInt32(pBufOut2[309]) * 0.1)))
  92.                 { fL_20dB--; } //搜索中心频率左边下降-20dB频率
  93.                 int fH_20dB = fs;
  94.                 while ((fH_20dB != 309) && (Convert.ToInt32(pBufOut1[fH_20dB]) > (Convert.ToInt32(pBufOut2[309]) * 0.1)))
  95.                 { fH_20dB++; } //搜索中心频率右边下降-20dB频率
  96.                 if ((fL_20dB == 0) | (fH_20dB == 309)) { BW_20dB = 0; } //未搜索到+-20dB处频率
  97.                 else { BW_20dB = fH_20dB - fL_20dB; }  //计算-20dB带宽
  98.                 if (BW_3dB * b2 == 0) { label7.Text = "0"; }
  99.                 else { label7.Text = String.Format("{0:N}", (b1 + fs * b2) / (BW_3dB*b2)).Replace(".00", ""); } //显示Q值
  100.                 //计算矩形系数
  101.                 if ((BW_3dB != 0) & (BW_20dB != 0))//-3dB带宽或-20dB带宽=0,不计算矩形系数
  102.                 {
  103.                     label8.Text = String.Format("{0:N}", 1.0 * BW_3dB / BW_20dB);    //显示矩形系数
  104.                 }
  105.                 else { label8.Text = ""; }
  106.                 //下面是在这块内存画布上绘图(先在内存上画好图形,然后在刷新屏幕,降低屏闪)
  107.                 g1.Clear(Color.Black); //清除内存绘图区
  108.                 g1.DrawImage(tup2, new Rectangle(0, 0, 310, 216), new Rectangle(0, 0, 310, 216), GraphicsUnit.Pixel); //将tup2图像复制到tup1中
  109.                 int y = 0;
  110.                 List<Point> points = new List<Point>();//存直线连接的点
  111.                 for (int i = 0; i < 310; i++)
  112.                 {
  113.                     y = Convert.ToInt32(pBufOut1[i] * 216 / 930); //电压倍率调整(=3*1023/3.3v)
  114.                     points.Add(new Point(i, 215 - y));
  115.                 }
  116.                 g1.DrawLines(p1, points.ToArray());
  117.                 //显示带宽
  118.                 if (checkBox1.Checked  == true)
  119.                 {
  120.                     int width3 = Convert.ToInt32(pBufOut1[fL] * 216 / 930);  //图形宽度
  121.                     if ((BW_3dB != 0) & (width3 != 0))
  122.                     {
  123.                         Bitmap tup3 = new Bitmap(BW_3dB, width3);
  124.                         Graphics g3 = Graphics.FromImage(tup3);
  125.                         g3.DrawImage(tup1, 0, 0, new Rectangle((int)fL + 1, 216 - (int)(pBufOut1[fL] * 216 / 930), BW_3dB, (int)(pBufOut1[fL] * 216 / 930)), GraphicsUnit.Pixel);
  126.                         tup3 = ImageLight(tup3, 35); //带宽显示处提高亮度
  127.                         g1.DrawImage(tup3, (int)fL + 1, 216 - (int)(pBufOut1[fL] * 216 / 930));
  128.                         g3.Dispose();
  129.                      }
  130.                 }               
  131.                 pictureBox1.Image = tup1; //将内存画布画到图片框中
  132.                 g1.Dispose(); //释放资源
  133.                 g2.Dispose();
  134.                 loop1 = false;
  135.             }
  136.             else
  137.                 System.Threading.Thread.Sleep(10);
  138.         }

  139.         private void button1_Click(object sender, EventArgs e) //启动扫描
  140.         {
  141.             if (button1_state == true)
  142.             {
  143.                 button1.Text = "暂停";
  144.                 button1_state = false;
  145.                 button3.Enabled = false;
  146.             }
  147.             else
  148.             {
  149.                 button1.Text = "启动";
  150.                 button1_state = true;
  151.                 button3.Enabled = true;
  152.             }
  153.         }
  154.         private void button2_Click(object sender, EventArgs e) //退出
  155.         {
  156.             Cursor.Show();
  157.             ThreadAd.Abort();//终止线程
  158.             CloseHandle(hDrv); //关闭设备
  159.             DeactivateDevice(hd); //卸载驱动
  160.             this.Close();
  161.         }
  162.         /// <summary>
  163.         /// 打开设置界面
  164.         /// </summary>
  165.         /// <param name="sender"></param>
  166.         /// <param name="e"></param>
  167.         private void button3_Click(object sender, EventArgs e)
  168.         {
  169.             if (button3.Text == "设置")
  170.             {
  171.                 button3.Text = "返回";
  172.                 panel1.Visible = true;
  173.                 panel1.Top = 0;
  174.                 panel1.Left = 0;
  175.                 button1.Enabled = false;
  176.                 button2.Enabled = false;
  177.             }
  178.             else
  179.             {
  180.                 button3.Text = "设置";
  181.                 panel1.Visible = false;
  182.                 button1.Enabled = true;
  183.                 button2.Enabled = true;
  184.                 dwCode = 0; //控制码
  185.                 pBuffIn1[0] = Convert.ToUInt32(label9.Text.Replace(",", "").Replace(" Hz", ""));   //扫描开始频率
  186.                 pBuffIn1[1] = Convert.ToUInt32(label10.Text.Replace(",", "").Replace(" Hz", ""));  //步进
  187.                 //pBuffIn1[2] = (uint)(keySwitch4.SwichValue); //AD转换增加延时
  188.                 pBuffIn1[3] = (uint)(keySwitch7.SwichValue); //AD转换选择通道号(ch0通道用于电池电压检测)
  189.             }
  190.         }
  191.         #region 扫频预置
  192.       
  193.         /// <summary>
  194.         /// 频率预置函数
  195.         /// </summary>
  196.         private void settings()
  197.         {
  198.             UInt64 freq = f1[keySwitch5.SwichValue - 1];
  199.             if (keySwitch6.SwichValue != 10)
  200.             {
  201.                 label24.Text = String.Format("{0:N}", freq * (100 - f2[keySwitch6.SwichValue - 1]) / 100).Replace(".00", " Hz");
  202.                 label26.Text = String.Format("{0:N}", freq * 2 * f2[keySwitch6.SwichValue - 1] / 31000).Replace(".00", " Hz");
  203.                 label28.Text = String.Format("{0:N}", freq * (100 + f2[keySwitch6.SwichValue - 1]) / 100).Replace(".00", " Hz");
  204.             }
  205.             else
  206.             {
  207.                 label24.Text = String.Format("{0:N}", freq * (1000 - f2[9]) / 1000).Replace(".00", " Hz");
  208.                 label26.Text = String.Format("{0:N}", freq * 2 * f2[9] / 310000).Replace(".00", " Hz");
  209.                 label28.Text = String.Format("{0:N}", freq * (1000 + f2[9]) / 1000).Replace(".00", " Hz");
  210.             }
  211.         }
  212.         /// <summary>
  213.         /// 扫频频率预置
  214.         /// </summary>
  215.         /// <param name="sender"></param>
  216.         /// <param name="e"></param>
  217.         private void keySwitch5_MouseDown(object sender, MouseEventArgs e)
  218.         {
  219.             if (keySwitch5.SwichValue == 10)
  220.             {
  221.                 panel2.Visible = true;  //打开数字按键盘
  222.                 label20.Text = f1[9].ToString();
  223.                 label20.Visible = true;
  224.                 label21.Text = "Hz";
  225.                 label22.Visible = false;
  226.                 keySwitch6.Enabled = false;
  227.                 button4.Visible = false;
  228.             }
  229.             else
  230.             {
  231.                 panel2.Visible = false;  //隐藏数字按键盘
  232.                 keySwitch6.Enabled = true;
  233.                 settings();
  234.                 button4.Visible = true;
  235.             }
  236.         }
  237.         /// <summary>
  238.         /// 扫频范围预置
  239.         /// </summary>
  240.         /// <param name="sender"></param>
  241.         /// <param name="e"></param>
  242.         private void keySwitch6_MouseDown(object sender, MouseEventArgs e)
  243.         {
  244.             if (keySwitch6.SwichValue == 10)
  245.             {
  246.                 panel2.Visible = true;  //打开数字按键盘
  247.                 label22.Text = f2[9].ToString();
  248.                 label22.Visible = true;
  249.                 label21.Text = "‰";
  250.                 label20.Visible = false;
  251.                 keySwitch5.Enabled = false;
  252.                 button4.Visible = false;
  253.             }
  254.             else
  255.             {
  256.                 panel2.Visible = false ;  //隐藏数字按键盘
  257.                 keySwitch5.Enabled = true;
  258.                 settings();
  259.                 button4.Visible = true;
  260.             }
  261.         }
  262.         /// <summary>
  263.         /// 预置置入
  264.         /// </summary>
  265.         /// <param name="sender"></param>
  266.         /// <param name="e"></param>
  267.         private void button4_Click(object sender, EventArgs e)
  268.         {
  269.             label9.Text = label24.Text;
  270.             label10.Text = label26.Text;
  271.             label11.Text = label28.Text;
  272.             if (keySwitch5.SwichValue != 10)
  273.                 label13.Text = keySwitch5.SwichText[keySwitch5.SwichValue - 1]; //预置扫描中心频率显示
  274.             else
  275.             {
  276.                 string s1 = "0";
  277.                 string s2 = "0";
  278.                 Freq_Format(Convert.ToInt32(label20.Text.Replace(",", "")), out s1, out s2);//手动扫描中心频率显示
  279.                 label13.Text = s1+s2;
  280.             }
  281.         }

  282.         #endregion
复制代码


待续

回复 支持 反对

使用道具 举报

     
 楼主| 发表于 2024-7-25 15:21:57 | 显示全部楼层
  1.     #region 数字小键盘

  2.         /// <summary>
  3.         /// 确认
  4.         /// </summary>
  5.         /// <param name="sender"></param>
  6.         /// <param name="e"></param>
  7.         private void button15_Click(object sender, EventArgs e)
  8.         {
  9.             if (label21.Text == "Hz")
  10.                 if (label20.Text != "") { f1[9] = Convert.ToUInt32(label20.Text); }
  11.                 else
  12.                 {
  13.                     MessageBox.Show("频率预置不能为空");
  14.                     return;
  15.                 }
  16.                 else
  17.                 if (label22.Text != "") { f2[9] = Convert.ToUInt32(label22.Text); }
  18.                 else
  19.                 {
  20.                     MessageBox.Show("频率范围预置不能为空");
  21.                     return;
  22.                 }
  23.             settings();
  24.             keySwitch5.Enabled = true;
  25.             keySwitch6.Enabled = true;
  26.             panel2.Visible = false;
  27.             button4.Visible = true;
  28.         }
  29.         /// <summary>
  30.         /// 删除
  31.         /// </summary>
  32.         /// <param name="sender"></param>
  33.         /// <param name="e"></param>
  34.         private void button14_Click(object sender, EventArgs e)
  35.         {
  36.             int data = 0;
  37.             if (label21.Text == "Hz")
  38.             {
  39.                 data=label20.Text.Length;
  40.                 if (data != 0)
  41.                 label20.Text = label20.Text.Remove(data - 1, 1);
  42.             }
  43.             else
  44.             {
  45.                 data = label22.Text.Length;
  46.                 if (data != 0)
  47.                 label22.Text = label22.Text.Remove(data - 1, 1);
  48.             }
  49.         }
  50.         
  51.         /// <summary>
  52.         /// 数字键盘处理函数
  53.         /// </summary>
  54.         /// <param name="digit"></param>
  55.         private void keyboard(string  digit)
  56.         {
  57.             uint data = 0;
  58.             if (label21.Text == "Hz") //频率处理
  59.             {
  60.                 if (label20.Text == "")
  61.                     data = Convert.ToUInt32(digit);
  62.                 else
  63.                     data = Convert.ToUInt32(label20.Text) * 10 + Convert.ToUInt32(digit);
  64.                 if (data < 40000000) //频率设置限幅
  65.                     label20.Text += digit;
  66.                 else
  67.                     MessageBox.Show("频率预置超过40MHz");
  68.             }
  69.             else //%处理
  70.             {
  71.                 if (label22.Text == "")
  72.                     data = Convert.ToUInt32(digit);
  73.                 else
  74.                     data = Convert.ToUInt32(label22.Text) * 10 + Convert.ToUInt32(digit);
  75.                 if (data < 1000) //频率范围‰设置限幅
  76.                     label22.Text += digit;
  77.                 else
  78.                     MessageBox.Show("扫频范围预置超过1000‰");
  79.             }
  80.         }
  81.         /// <summary>
  82.         /// 数字键盘0-9事件
  83.         /// </summary>
  84.         /// <param name="sender"></param>
  85.         /// <param name="e"></param>
  86.         private void button16_Click(object sender, EventArgs e)
  87.         {
  88.             keyboard("0");
  89.         }

  90.         private void button5_Click(object sender, EventArgs e)
  91.         {
  92.             keyboard("1");
  93.         }

  94.         private void button6_Click(object sender, EventArgs e)
  95.         {
  96.             keyboard("2");
  97.         }

  98.         private void button7_Click(object sender, EventArgs e)
  99.         {
  100.             keyboard("3");
  101.         }

  102.         private void button8_Click(object sender, EventArgs e)
  103.         {
  104.             keyboard("4");
  105.         }

  106.         private void button10_Click(object sender, EventArgs e)
  107.         {
  108.             keyboard("5");
  109.         }

  110.         private void button9_Click(object sender, EventArgs e)
  111.         {
  112.             keyboard("6");
  113.         }

  114.         private void button11_Click(object sender, EventArgs e)
  115.         {
  116.             keyboard("7");
  117.         }

  118.         private void button13_Click(object sender, EventArgs e)
  119.         {
  120.             keyboard("8");
  121.         }

  122.         private void button12_Click(object sender, EventArgs e)
  123.         {
  124.             keyboard("9");
  125.         }

  126.         #endregion

  127.         private void pictureBox1_Click(object sender, EventArgs e)
  128.         {
  129.             if (loop2 == false) { loop2 = true; } //显示带宽标记
  130.             else { loop2 = false; }
  131.         }
  132.         /// <summary>
  133.         /// 十字线
  134.         /// </summary>
  135.         /// <param name="sender"></param>
  136.         /// <param name="e"></param>
  137.         private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  138.         {
  139.             int x = e.X;  //获取鼠标的竖直坐标,并将十字星焦点向左上方偏移1个像数
  140.             int y = 0;
  141.             if (e.X < 310)
  142.                 y = Convert.ToInt32(pBufOut1[e.X] * 216 / 930);   //横线位置(跟踪曲线的高度=3*1023/3.3v)
  143.             else
  144.                 y = Convert.ToInt32(pBufOut1[309] * 216 / 930);
  145.             label18.Left = x + 22;    //十字线竖线         
  146.             label19.Top = 20 + (216 - y);   //十字线横线
  147.             ///刻度
  148.             if (x < 16) { label31.Left = 23; } //16=32/2
  149.             else if (x > 294) { label31.Left = 300; }    //294=310-32/2
  150.             else { label31.Left = x + 22-16; }
  151.             if (y < 6) { label32.Top = 224; }
  152.             else if (y > 210) { label32.Top = 20; } //210=216-12/2
  153.             else { label32.Top = 14 + (216 - y); }
  154.             ///刻度值
  155.             string s1 = "0";
  156.             string s2 = "0";
  157.             int b1 = Convert.ToInt32(label9.Text.Replace(",", "").Replace(" Hz", ""));   //开始频率
  158.             int b2 = Convert.ToInt32(label10.Text.Replace(",", "").Replace(" Hz", ""));  //步进         
  159.             int f = b1 + e.X * b2;   //频率
  160.             Freq_Format(f, out s1, out s2);
  161.             label31.Text = String.Format("{0:0}",s1);
  162.             label15.Text = "(" + s2 + ")";
  163.             label32.Text = String.Format("{0:0.00}", 3.0 * y / 216);
  164.             //显示十字线
  165.             label18.Visible = true;
  166.             label19.Visible = true;
  167.             label31.Visible = true;
  168.             label32.Visible = true;
  169.             if (checkBox2.Checked == false)  //勾选十字线光标常量
  170.             {
  171.                 timer1.Interval = 3000;   //设置自动关闭十字线光标时间
  172.                 timer1.Enabled = true;
  173.             }
  174.         }
  175.         /// <summary>
  176.         /// 隐藏十字线
  177.         /// </summary>
  178.         /// <param name="sender"></param>
  179.         /// <param name="e"></param>
  180.         private void timer1_Tick(object sender, EventArgs e)
  181.         {
  182.             label18.Visible = false;
  183.             label19.Visible = false;
  184.             label31.Visible = false;
  185.             label32.Visible = false;
  186.             timer1.Enabled = false;
  187.         }
  188.         /// <summary>
  189.         /// 带宽单选
  190.         /// </summary>
  191.         /// <param name="sender"></param>
  192.         /// <param name="e"></param>
  193.         private void checkBox1_Click(object sender, EventArgs e)
  194.         {
  195.             this.Focus();
  196.         }
  197.         /// <summary>
  198.         /// 光标单选
  199.         /// </summary>
  200.         /// <param name="sender"></param>
  201.         /// <param name="e"></param>
  202.         private void checkBox2_Click(object sender, EventArgs e)
  203.         {
  204.             this.Focus();
  205.             if (checkBox2.Checked == true)    //显示十字线光标
  206.             {
  207.                 label18.Visible = true;
  208.                 label19.Visible = true;
  209.                 label31.Visible = true;
  210.                 label32.Visible = true;
  211.                 timer1.Enabled = false;
  212.             }
  213.             else       //隐藏十字线光标
  214.             {
  215.                 label18.Visible = false;
  216.                 label19.Visible = false;
  217.                 label31.Visible = false;
  218.                 label32.Visible = false;
  219.             }
  220.         }
  221.         /// <summary>
  222.         /// 采样速度单选
  223.         /// </summary>
  224.         /// <param name="sender"></param>
  225.         /// <param name="e"></param>
  226.         private void checkBox3_Click(object sender, EventArgs e)
  227.         {
  228.             this.Focus();
  229.             pBuffIn1[2] = (uint)(checkBox3.Checked ? 2 : 1);  //显示速度快1,慢2
  230.         }

  231.         private void button17_Click(object sender, EventArgs e)
  232.         {
  233.             Process.Start("\\ResidentFlash\\扫频仪\\扫频仪使用说明.txt", "");
  234.         }

  235.     }
  236. }
复制代码


完!


回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 加入会员

本版积分规则

小黑屋|手机版|矿石收音机 ( 蒙ICP备05000029号-1 )

蒙公网安备 15040402000005号

GMT+8, 2025-4-26 07:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表