C#获取扫码枪扫描数据并处理
开发原因:工厂产品需要频繁扫描产品SN进行产品踢转处理不良以及工单结多产品
直接上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; namespace WindowsFormsApplication6 { internal class ScanerHook { public delegate void ScanerDelegate(ScanerCodes codes); public event ScanerDelegate ScanerEvent; //private const int WM_KEYDOWN = 0x100;//KEYDOWN //private const int WM_KEYUP = 0x101;//KEYUP //private const int WM_SYSKEYDOWN = 0x104;//SYSKEYDOWN //private const int WM_SYSKEYUP = 0x105;//SYSKEYUP //private static int HookProc(int nCode, Int32 wParam, IntPtr lParam); private int hKeyboardHook = 0; //声明键盘钩子处理的初始值 private ScanerCodes codes = new ScanerCodes(); //13为键盘钩子 //定义成静态,这样不会抛出回收异常 private static HookProc hookproc; delegate int HookProc( int nCode, Int32 wParam, IntPtr lParam); [DllImport( "user32.dll" , CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] //设置钩子 private static extern int SetWindowsHookEx( int idHook, HookProc lpfn, IntPtr hInstance, int threadId); [DllImport( "user32.dll" , CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] //卸载钩子 private static extern bool UnhookWindowsHookEx( int idHook); [DllImport( "user32.dll" , CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] //继续下个钩子 private static extern int CallNextHookEx( int idHook, int nCode, Int32 wParam, IntPtr lParam); [DllImport( "user32" , EntryPoint = "GetKeyNameText" )] private static extern int GetKeyNameText( int IParam, StringBuilder lpBuffer, int nSize); [DllImport( "user32" , EntryPoint = "GetKeyboardState" )] //获取按键的状态 private static extern int GetKeyboardState( byte [] pbKeyState); [DllImport( "user32" , EntryPoint = "ToAscii" )] //ToAscii职能的转换指定的虚拟键码和键盘状态的相应字符或字符 private static extern bool ToAscii( int VirtualKey, int ScanCode, byte [] lpKeySate, ref uint lpChar, int uFlags); //int VirtualKey //[in] 指定虚拟关键代码进行翻译。 //int uScanCode, // [in] 指定的硬件扫描码的关键须翻译成英文。高阶位的这个值设定的关键,如果是(不压) //byte[] lpbKeyState, // [in] 指针,以256字节数组,包含当前键盘的状态。每个元素(字节)的数组包含状态的一个关键。如果高阶位的字节是一套,关键是下跌(按下)。在低比特,如/果设置表明,关键是对切换。在此功能,只有肘位的CAPS LOCK键是相关的。在切换状态的NUM个锁和滚动锁定键被忽略。 //byte[] lpwTransKey, // [out] 指针的缓冲区收到翻译字符或字符。 //uint fuState); // [in] Specifies whether a menu is active. This parameter must be 1 if a menu is active, or 0 otherwise. [DllImport( "kernel32.dll" )] //使用WINDOWS API函数代替获取当前实例的函数,防止钩子失效 public static extern IntPtr GetModuleHandle( string name); public ScanerHook() { } public bool Start() { if (hKeyboardHook == 0) { hookproc = new HookProc(KeyboardHookProc); //GetModuleHandle 函数 替代 Marshal.GetHINSTANCE //防止在 framework4.0中 注册钩子不成功 IntPtr modulePtr = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName); //WH_KEYBOARD_LL=13 //全局钩子 WH_KEYBOARD_LL // hKeyboardHook = SetWindowsHookEx(13, hookproc, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); hKeyboardHook = SetWindowsHookEx(13, hookproc, modulePtr, 0); } return (hKeyboardHook != 0); } public bool Stop() { if (hKeyboardHook != 0) { bool retKeyboard = UnhookWindowsHookEx(hKeyboardHook); hKeyboardHook = 0; return retKeyboard; } return true ; } private int KeyboardHookProc( int nCode, Int32 wParam, IntPtr lParam) { EventMsg msg = (EventMsg)Marshal.PtrToStructure(lParam, typeof (EventMsg)); codes.Add(msg); if (ScanerEvent != null && msg.message == 13 && msg.paramH > 0 && ! string .IsNullOrEmpty(codes.Result)) { ScanerEvent(codes); } return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam); } public class ScanerCodes { private int ts = 300; // 指定输入间隔为300毫秒以内时为连续输入 private List<List<EventMsg>> _keys = new List<List<EventMsg>>(); private List< int > _keydown = new List< int >(); // 保存组合键状态 private List< string > _result = new List< string >(); // 返回结果集 private DateTime _last = DateTime.Now; private byte [] _state = new byte [256]; private string _key = string .Empty; private string _cur = string .Empty; public EventMsg Event { get { if (_keys.Count == 0) { return new EventMsg(); } else { return _keys[_keys.Count - 1][_keys[_keys.Count - 1].Count - 1]; } } } public List< int > KeyDowns { get { return _keydown; } } public DateTime LastInput { get { return _last; } } public byte [] KeyboardState { get { return _state; } } public int KeyDownCount { get { return _keydown.Count; } } public string Result { get { if (_result.Count > 0) { return _result[_result.Count - 1].Trim(); } else { return null ; } } } public string CurrentKey { get { return _key; } } public string CurrentChar { get { return _cur; } } public bool isShift { get { return _keydown.Contains(160); } } public void Add(EventMsg msg) { #region 记录按键信息 // 首次按下按键 if (_keys.Count == 0) { _keys.Add( new List<EventMsg>()); _keys[0].Add(msg); _result.Add( string .Empty); } // 未释放其他按键时按下按键 else if (_keydown.Count > 0) { _keys[_keys.Count - 1].Add(msg); } // 单位时间内按下按键 else if (((TimeSpan)(DateTime.Now - _last)).TotalMilliseconds < ts) { _keys[_keys.Count - 1].Add(msg); } // 从新记录输入内容 else { _keys.Add( new List<EventMsg>()); _keys[_keys.Count - 1].Add(msg); _result.Add( string .Empty); } #endregion _last = DateTime.Now; #region 获取键盘状态 // 记录正在按下的按键 if (msg.paramH == 0 && !_keydown.Contains(msg.message)) { _keydown.Add(msg.message); } // 清除已松开的按键 if (msg.paramH > 0 && _keydown.Contains(msg.message)) { _keydown.Remove(msg.message); } #endregion #region 计算按键信息 int v = msg.message & 0xff; int c = msg.paramL & 0xff; StringBuilder strKeyName = new StringBuilder(500); if (GetKeyNameText(c * 65536, strKeyName, 255) > 0) { _key = strKeyName.ToString().Trim( new char [] { ' ' , '\0' }); GetKeyboardState(_state); if (_key.Length == 1 && msg.paramH == 0) // && msg.paramH == 0 { // 根据键盘状态和shift缓存判断输出字符 _cur = ShiftChar(_key, isShift, _state).ToString(); _result[_result.Count - 1] += _cur; } // 备选 else { _cur = string .Empty; } } #endregion } private char ShiftChar( string k, bool isShiftDown, byte [] state) { bool capslock = state[0x14] == 1; bool numlock = state[0x90] == 1; bool scrolllock = state[0x91] == 1; bool shiftdown = state[0xa0] == 1; char chr = (capslock ? k.ToUpper() : k.ToLower()).ToCharArray()[0]; if (isShiftDown) { if (chr >= 'a' && chr <= 'z' ) { chr = ( char )(( int )chr - 32); } else if (chr >= 'A' && chr <= 'Z' ) { if (chr == 'Z' ) { string s = "" ; } chr = ( char )(( int )chr + 32); } else { string s = "`1234567890-=[];',./" ; string u = "~!@#$%^&*()_+{}:\"<>?" ; if (s.IndexOf(chr) >= 0) { return (u.ToCharArray())[s.IndexOf(chr)]; } } } return chr; } } public struct EventMsg { public int message; public int paramL; public int paramH; public int Time; public int hwnd; } } } |
上面是获取扫码枪扫描数据的具体代码,扫描过快的话会导致条码粘连,不过因为条码长度都一样,所以可以获取数据后再进行加工处理。下面是获取数据后并处理的过程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | using System; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Collections; using System.Collections.Generic; namespace WindowsFormsApplication6 { public partial class Form1 : Form { private ScanerHook listener = new ScanerHook(); KeyboardHook k_hook; public Form1() { InitializeComponent(); listener.ScanerEvent += Listener_ScanerEvent; k_hook = new KeyboardHook(); k_hook.KeyDownEvent += K_hook_KeyDownEvent; k_hook.Start(); } HashSet< string > set = new HashSet< string >(); int count = 0; private void Listener_ScanerEvent(ScanerHook.ScanerCodes codes) { count++; DataGridViewRow row = new DataGridViewRow(); int index = dataGridView1.Rows.Add(row); int i = dataGridView1.Rows.Count - 1; dataGridView1.CurrentCell = dataGridView1[0, i]; dataGridView1.Rows[i].Selected = true ; dataGridView1.FirstDisplayedCell = dataGridView1.Rows[i].Cells[0]; dataGridView1.Rows[index].Cells[0].Value= codes.Result.ToUpper(); // System.Console.WriteLine(codes.Result.ToUpper()); if (codes.Result.ToUpper().Length % 14 == 0) { if (! set .Contains(codes.Result.ToUpper())) { int h = 14; int k = codes.Result.ToUpper().Length; string n = codes.Result.ToUpper(); if (k >= 14) { int m = k / h; for ( int x = 0; x < m; x++) { int t = x * h; string b = n.Substring(t, h); set .Add(b); } } } } label1.Text = "有效數量:" + set .Count.ToString(); label2.Text = "縂數量:" +count; } private void Form1_Load( object sender, EventArgs e) { listener.Start(); } private void K_hook_KeyDownEvent( object sender, KeyEventArgs e) { if (e.KeyCode == Keys.F4) { SendMsg sendMsg = new SendMsg(); foreach (Object s in set ) { sendMsg.SendText(s+ "\r" ); } // sendMsg.SendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss\r")); if (MessageBox.Show( "do you?" , "Confirm Message" , MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { set .Clear(); dataGridView1.Rows.Clear(); label1.Text = "有效數量:0" ; label2.Text = "縂數量:0" ; count = 0; } } } private void button1_Click( object sender, EventArgs e) { set .Clear(); dataGridView1.Rows.Clear(); label1.Text = "有效數量:0" ; label2.Text = "縂數量:0" ; count = 0; } } internal class SendMsg { [DllImport( "user32.dll" )] public static extern IntPtr GetForegroundWindow(); [DllImport( "user32.dll" , CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); [DllImport( "user32.dll" )] static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId); [DllImport( "user32.dll" )] static extern bool GetGUIThreadInfo( uint idThread, ref GUITHREADINFO lpgui); [StructLayout(LayoutKind.Sequential)] public struct GUITHREADINFO { public int cbSize; public int flags; public IntPtr hwndActive; public IntPtr hwndFocus; public IntPtr hwndCapture; public IntPtr hwndMenuOwner; public IntPtr hwndMoveSize; public IntPtr hwndCaret; public RECT rectCaret; } [StructLayout(LayoutKind.Sequential)] public struct RECT { int left; int top; int right; int bottom; } public GUITHREADINFO? GetGuiThreadInfo(IntPtr hwnd) { if (hwnd != IntPtr.Zero) { uint threadId = GetWindowThreadProcessId(hwnd, IntPtr.Zero); GUITHREADINFO guiThreadInfo = new GUITHREADINFO(); guiThreadInfo.cbSize = Marshal.SizeOf(guiThreadInfo); if (GetGUIThreadInfo(threadId, ref guiThreadInfo) == false ) return null ; return guiThreadInfo; } return null ; } public void SendText( string text) { IntPtr hwnd = GetForegroundWindow(); if (String.IsNullOrEmpty(text)) return ; GUITHREADINFO? guiInfo = GetGuiThreadInfo(hwnd); if (guiInfo != null ) { for ( int i = 0; i < text.Length; i++) { SendMessage(guiInfo.Value.hwndFocus, 0x0102, (IntPtr)( int )text[i], IntPtr.Zero); } } } } internal class KeyboardHook { public event KeyEventHandler KeyDownEvent; public event KeyPressEventHandler KeyPressEvent; public event KeyEventHandler KeyUpEvent; public delegate int HookProc( int nCode, Int32 wParam, IntPtr lParam); static int hKeyboardHook = 0; //声明键盘钩子处理的初始值 //值在Microsoft SDK的Winuser.h里查询 public const int WH_KEYBOARD_LL = 13; //线程键盘钩子监听鼠标消息设为2,全局键盘监听鼠标消息设为13 HookProc KeyboardHookProcedure; //声明KeyboardHookProcedure作为HookProc类型 //键盘结构 [StructLayout(LayoutKind.Sequential)] public class KeyboardHookStruct { public int vkCode; //定一个虚拟键码。该代码必须有一个价值的范围1至254 public int scanCode; // 指定的硬件扫描码的关键 public int flags; // 键标志 public int time; // 指定的时间戳记的这个讯息 public int dwExtraInfo; // 指定额外信息相关的信息 } //使用此功能,安装了一个钩子 [DllImport( "user32.dll" , CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern int SetWindowsHookEx( int idHook, HookProc lpfn, IntPtr hInstance, int threadId); //调用此函数卸载钩子 [DllImport( "user32.dll" , CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern bool UnhookWindowsHookEx( int idHook); //使用此功能,通过信息钩子继续下一个钩子 [DllImport( "user32.dll" , CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern int CallNextHookEx( int idHook, int nCode, Int32 wParam, IntPtr lParam); // 取得当前线程编号(线程钩子需要用到) [DllImport( "kernel32.dll" )] static extern int GetCurrentThreadId(); //使用WINDOWS API函数代替获取当前实例的函数,防止钩子失效 [DllImport( "kernel32.dll" )] public static extern IntPtr GetModuleHandle( string name); public void Start() { // 安装键盘钩子 if (hKeyboardHook == 0) { KeyboardHookProcedure = new HookProc(KeyboardHookProc); hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, GetModuleHandle(System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName), 0); //hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); //************************************ //键盘线程钩子 SetWindowsHookEx(13, KeyboardHookProcedure, IntPtr.Zero, GetCurrentThreadId()); //指定要监听的线程idGetCurrentThreadId(), //键盘全局钩子,需要引用空间(using System.Reflection;) //SetWindowsHookEx( 13,MouseHookProcedure,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0); // //关于SetWindowsHookEx (int idHook, HookProc lpfn, IntPtr hInstance, int threadId)函数将钩子加入到钩子链表中,说明一下四个参数: //idHook 钩子类型,即确定钩子监听何种消息,上面的代码中设为2,即监听键盘消息并且是线程钩子,如果是全局钩子监听键盘消息应设为13, //线程钩子监听鼠标消息设为7,全局钩子监听鼠标消息设为14。lpfn 钩子子程的地址指针。如果dwThreadId参数为0 或是一个由别的进程创建的 //线程的标识,lpfn必须指向DLL中的钩子子程。 除此以外,lpfn可以指向当前进程的一段钩子子程代码。钩子函数的入口地址,当钩子钩到任何 //消息后便调用这个函数。hInstance应用程序实例的句柄。标识包含lpfn所指的子程的DLL。如果threadId 标识当前进程创建的一个线程,而且子 //程代码位于当前进程,hInstance必须为NULL。可以很简单的设定其为本应用程序的实例句柄。threaded 与安装的钩子子程相关联的线程的标识符 //如果为0,钩子子程与所有的线程关联,即为全局钩子 //************************************ //如果SetWindowsHookEx失败 if (hKeyboardHook == 0) { // Stop(); throw new Exception( "安装键盘钩子失败" ); } } } public void Stop() { bool retKeyboard = true ; if (hKeyboardHook != 0) { retKeyboard = UnhookWindowsHookEx(hKeyboardHook); hKeyboardHook = 0; } try { if (!(retKeyboard)) { // throw new Exception("卸载钩子失败!"); } } catch (Exception) { throw ; } // if (!(retKeyboard)) throw new Exception("卸载钩子失败!"); } //ToAscii职能的转换指定的虚拟键码和键盘状态的相应字符或字符 [DllImport( "user32" )] public static extern int ToAscii( int uVirtKey, //[in] 指定虚拟关键代码进行翻译。 int uScanCode, // [in] 指定的硬件扫描码的关键须翻译成英文。高阶位的这个值设定的关键,如果是(不压) byte [] lpbKeyState, // [in] 指针,以256字节数组,包含当前键盘的状态。每个元素(字节)的数组包含状态的一个关键。如果高阶位的字节是一套,关键是下跌(按下)。在低比特,如果设置表明,关键是对切换。在此功能,只有肘位的CAPS LOCK键是相关的。在切换状态的NUM个锁和滚动锁定键被忽略。 byte [] lpwTransKey, // [out] 指针的缓冲区收到翻译字符或字符。 int fuState); // [in] Specifies whether a menu is active. This parameter must be 1 if a menu is active, or 0 otherwise. //获取按键的状态 [DllImport( "user32" )] public static extern int GetKeyboardState( byte [] pbKeyState); [DllImport( "user32.dll" , CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] private static extern short GetKeyState( int vKey); private const int WM_KEYDOWN = 0x100; //KEYDOWN private const int WM_KEYUP = 0x101; //KEYUP private const int WM_SYSKEYDOWN = 0x104; //SYSKEYDOWN private const int WM_SYSKEYUP = 0x105; //SYSKEYUP private int KeyboardHookProc( int nCode, Int32 wParam, IntPtr lParam) { // 侦听键盘事件 if ((nCode >= 0) && (KeyDownEvent != null || KeyUpEvent != null || KeyPressEvent != null )) { KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof (KeyboardHookStruct)); // raise KeyDown if (KeyDownEvent != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)) { Keys keyData = (Keys)MyKeyboardHookStruct.vkCode; KeyEventArgs e = new KeyEventArgs(keyData); KeyDownEvent( this , e); } //键盘按下 if (KeyPressEvent != null && wParam == WM_KEYDOWN) { byte [] keyState = new byte [256]; GetKeyboardState(keyState); byte [] inBuffer = new byte [2]; if (ToAscii(MyKeyboardHookStruct.vkCode, MyKeyboardHookStruct.scanCode, keyState, inBuffer, MyKeyboardHookStruct.flags) == 1) { KeyPressEventArgs e = new KeyPressEventArgs(( char )inBuffer[0]); KeyPressEvent( this , e); } } // 键盘抬起 if (KeyUpEvent != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP)) { Keys keyData = (Keys)MyKeyboardHookStruct.vkCode; KeyEventArgs e = new KeyEventArgs(keyData); KeyUpEvent( this , e); } } //如果返回1,则结束消息,这个消息到此为止,不再传递。 //如果返回0或调用CallNextHookEx函数则消息出了这个钩子继续往下传递,也就是传给消息真正的接受者 return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam); } ~KeyboardHook() { Stop(); } } }  |
部分代码引用源文连接:[源文链接,如有侵权请联系删除(https://blog.csdn.net/baidu_19356259/article/details/121998523)
本文来自博客园,作者:willamyao,转载请注明原文链接:https://www.cnblogs.com/robertyao/p/16945212.html
代码改变世界
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2018-12-02 AdminLTE 文档