|
|
Posted on 2006-12-29 18:40 Hu Yong Yuan 阅读(310) 评论(0) 编辑 收藏
串口通訊兩種方式:
1.被動接收(我是這樣稀稱呼),即串口通訊設被不停的發送有起始位和接收位的數據.而程式收到數據後根據接收位和起始位,將數據剝出來.進行處理.我給地磅寫的就是利用這種(後面貼出原碼).
1 using System; 2 using System.Drawing; 3 using System.Collections; 4 using System.ComponentModel; 5 using System.Windows.Forms; 6 using System.Data; 7 using System.IO; 8 namespace weight 9  { 10 /**//// <summary> 11 /// Form1 的摘要描述。 12 /// </summary> 13 public class Form1 : System.Windows.Forms.Form 14 { 15 private AxMSCommLib.AxMSComm comm; 16 17 bool cop=false; 18 bool ctrlcop=false; 19 string strCollect=""; 20 int iswrite=0; 21 int btnum=0; 22 /**//// <summary> 23 /// 設計工具所需的變數。 24 /// </summary> 25 private System.ComponentModel.Container components = null; 26 public Form1() 27 { 28 // 29 // Windows Form 設計工具支援的必要項 30 // 31 InitializeComponent(); 32 33 // 34 // TODO: 在 InitializeComponent 呼叫之後加入任何建構函式程式碼 35 // 36 } 37 38 /**//// <summary> 39 /// 清除任何使用中的資源。 40 /// </summary> 41 protected override void Dispose( bool disposing ) 42 { 43 if( disposing ) 44 { 45 if (components != null) 46 { 47 components.Dispose(); 48 } 49 } 50 base.Dispose( disposing ); 51 } 52 53 Windows Form 設計工具產生的程式碼#region Windows Form 設計工具產生的程式碼 54 /**//// <summary> 55 /// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改 56 /// 這個方法的內容。 57 /// </summary> 58 private void InitializeComponent() 59 { 60 System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); 61 this.comm = new AxMSCommLib.AxMSComm(); 62 ((System.ComponentModel.ISupportInitialize)(this.comm)).BeginInit(); 63 this.SuspendLayout(); 64 // 65 // comm 66 // 67 this.comm.Enabled = true; 68 this.comm.Location = new System.Drawing.Point(72, 0); 69 this.comm.Name = "comm"; 70 this.comm.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("comm.OcxState"))); 71 this.comm.Size = new System.Drawing.Size(38, 38); 72 this.comm.TabIndex = 0; 73 this.comm.OnComm += new System.EventHandler(this.axMSComm1_OnComm); 74 // 75 // Form1 76 // 77 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 78 this.ClientSize = new System.Drawing.Size(256, 174); 79 this.Controls.Add(this.comm); 80 this.MaximizeBox = false; 81 this.Name = "Form1"; 82 this.Opacity = 0; 83 this.Text = "Form1"; 84 this.WindowState = System.Windows.Forms.FormWindowState.Minimized; 85 this.Disposed += new System.EventHandler(this.Form1_Disposed); 86 this.Load += new System.EventHandler(this.Form1_Load); 87 this.Activated += new System.EventHandler(this.Form1_Activated); 88 ((System.ComponentModel.ISupportInitialize)(this.comm)).EndInit(); 89 this.ResumeLayout(false); 90 91 } 92 #endregion 93 94 /**//// <summary> 95 /// 應用程式的主進入點。 96 /// </summary> 97 [STAThread] 98 static void Main() 99 { 100 Form f = new Form1(); 101 f.ShowInTaskbar=false; 102 Application.Run(f); 103 } 104 105 private void axMSComm1_OnComm(object sender, System.EventArgs e) 106 { 107 108 109 byte [] bt; 110 char chBegin='\u0002'; 111 char chEnd='\u0003'; 112 bool ctrlCp=true; 113 bt=(byte [])comm.Input; 114 comm.InBufferCount=0; 115 comm.OutBufferCount=0; 116 string strIn; 117 118 strIn=System.Text.ASCIIEncoding.ASCII.GetString(bt); 119 120 for(int i=0;i<bt.Length;i++) 121 { 122 123 if((char)bt[i]==chBegin) 124 { 125 strIn=System.Text.ASCIIEncoding.ASCII.GetString(bt,i+1,10); 126 strCollect+=strIn; 127 break; 128 } 129 130 131 132 } 133 134 135 if(strCollect.Length==10) 136 { 137 string str=""; 138 try 139 { 140 141 142 int wei=int.Parse(strCollect.Substring(7,1)); 143 144 145 switch(wei) 146 { 147 case 0: 148 str=strCollect.Substring(0,7); 149 break; 150 case 1: 151 str=strCollect.Substring(0,6)+"."+strCollect.Substring(6,1); 152 break; 153 case 2: 154 str=strCollect.Substring(0,5)+"."+strCollect.Substring(5,2); 155 break; 156 case 3: 157 str=strCollect.Substring(0,4)+"."+strCollect.Substring(4,3); 158 break; 159 case 4: 160 str=strCollect.Substring(0,3)+"."+strCollect.Substring(3,4); 161 break; 162 163 } 164 165 if(str.Length!=8) return; 166 167 FileStream fs=new FileStream("c:\\temp\\weight.txt",FileMode.Append,FileAccess.Write,FileShare.Write); 168 fs.Close(); 169 StreamWriter sw=new StreamWriter("c:\\temp\\weight.txt",false,System.Text.Encoding.ASCII); 170 sw.Write(str); 171 sw.Close(); 172 173 Application.Exit(); 174 175 } 176 catch 177 { 178 179 180 } 181 finally 182 { 183 184 185 186 187 } 188 } 189 else 190 { 191 192 193 194 195 } 196 197 198 } 199 200 private void Form1_Load(object sender, System.EventArgs e) 201 { 202 comm.PortOpen=true; 203 204 comm.OutBufferCount=0; 205 206 207 } 208 209 210 private void Form1_Disposed(object sender, EventArgs e) 211 { 212 213 } 214 215 private void Form1_Activated(object sender, System.EventArgs e) 216 { 217 this.Visible=false; 218 } 219 220 221 } 222 } 223
2.命令應答.這種方式是程式和通訊設備進行交互:程式向設備發出供應商指定的命令,一般在說明書上有,設備跟據命令向程式返饋數據(後有原碼).代碼中有鍵盤鉤子,用於偵測用戶鍵盤計錄(copy他人的,稼接過來.忘具體名字了).
1 1using System; 2 2using System.Drawing; 3 3using System.Collections; 4 4using System.ComponentModel; 5 5using System.Windows.Forms; 6 6using System.Data; 7 7using System.Threading; 8 8using System.Runtime.InteropServices; 9 9using System.Reflection; 10 10 11 11namespace ppcom 12 12 { 13 13 /**//**//**//// <summary> 14 14 /**//// Form1 的摘要描述。 15 15 /**//// </summary> 16 16 17 17 public class Form1 : System.Windows.Forms.Form 18 18 { 19 19 private System.Windows.Forms.TextBox textBox1; 20 20 private System.Windows.Forms.Button button1; 21 21 private System.Windows.Forms.Splitter splitter1; 22 22 string strIn; 23 23 string strNum=""; 24 24 private System.Windows.Forms.ComboBox cbRthread; 25 25 private System.Windows.Forms.Label label1; 26 26 private System.Windows.Forms.Label label2; 27 27 private System.Windows.Forms.ComboBox comboBox1; 28 28 private System.Windows.Forms.Label label3; 29 29 private System.Windows.Forms.Label label4; 30 30 private System.Windows.Forms.ComboBox comboBox2; 31 31 private System.Windows.Forms.Label label5; 32 32 bool clboard=true;//是否清空剪切板 33 33 bool ctrlreset=true; 34 34 35 35 bool ctrlKey=true,ctrlCaps=true,ctrlF1=true; 36 36 private System.Windows.Forms.NotifyIcon notifyIcon1; 37 37 private System.Windows.Forms.Label label6; 38 38 private System.Windows.Forms.ComboBox cbSign; 39 39 private AxMSCommLib.AxMSComm axMSComm1; 40 40 41 41 private System.ComponentModel.IContainer components; 42 42 43 43 public Form1() 44 44 { 45 45 // 46 46 // Windows Form 設計工具支援的必要項 47 47 // 48 48 InitializeComponent(); 49 49// tb.KeyPress += new KeyPressEventHandler(keypressed); 50 50 51 51 52 52 // 53 53 // TODO: 在 InitializeComponent 呼叫之後加入任何建構函式程式碼 54 54 // 55 55 HookStart(); 56 56 57 57 } 58 58 public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam); 59 59 static int hKeyboardHook = 0; 60 60 HookProc KeyboardHookProcedure; 61 61 62 62 public const int WH_KEYBOARD = 13; 63 63 [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] 64 64 public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, 65 65 IntPtr hInstance, int threadId); 66 66 67 67 [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] 68 68 public static extern bool UnhookWindowsHookEx(int idHook); 69 69 [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] 70 70 public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam); 71 71 [DllImport("kernel32.dll")] 72 72 static extern int GetCurrentThreadId(); 73 73 74 74 public struct KeyMSG 75 75 { 76 76 public int vkCode; 77 77 public int scanCode; 78 78 public int flags; 79 79 public int time; 80 80 public int dwExtraInfo; 81 81 } 82 82 83 83 /**//**//**//**//**//**//**//// <summary> 84 84 /**//// 85 85 /**//// </summary> 86 86 /**//// <param name="nCode"></param> 87 87 /**//// <param name="wParam"></param> 88 88 /**//// <param name="lParam"></param> 89 89 /**//// <returns></returns> 90 90 /**//// 91 91 private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) 92 92 { 93 93 94 94 KeyMSG m = (KeyMSG) Marshal.PtrToStructure(lParam, typeof(KeyMSG)); 95 95 96 96 if ((int) m.vkCode == 113) 97 97 { 98 98 99 99 100 100 axMSComm1.Output="CR00\r\n"; 101 101 axMSComm1.OutBufferCount=0; 102 102 103 103 } 104 104 if ((int) m.vkCode == 20) 105 105 { 106 106 if(ctrlCaps==true) 107 107 { 108 108 109 109 if(cbSign.SelectedIndex==0) 110 110 { 111 111 cbSign.SelectedIndex=1; 112 112 this.Text="否"; 113 113 114 114 } 115 115 else 116 116 { 117 117 cbSign.SelectedIndex=0; 118 118 this.Text="是"; 119 119 } 120 120 ctrlCaps=false; 121 121 } 122 122 else 123 123 { 124 124 125 125 126 126 ctrlCaps=true; 127 127 } 128 128 129 129 } 130 130 131 131 if ( (int) m.vkCode == 192) 132 132 { 133 133 if(ctrlKey==true) 134 134 { 135 135// axMSComm1.Output="IN"; 136 136// axMSComm1.Output="RES2"; 137 137// axMSComm1.Output="PRI\r\n"; 138 138// 139 139 axMSComm1.Output="GA00\r\n"; 140 140 ctrlKey=false; 141 141 ctrlreset=false; 142 142 143 143 } 144 144 else 145 145 { 146 146 ctrlKey=true; 147 147 } 148 148 return 1; 149 149 } 150 150 return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam); 151 151 } 152 152 153 153 /**//**//**//**//**//**//**//// <summary> 154 154 /**//// 安??子 155 155 /**//// </summary> 156 156 public void HookStart() 157 157 { 158 158 if(hKeyboardHook == 0) 159 159 { 160 160 KeyboardHookProcedure = new HookProc(KeyboardHookProc); 161 161 hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardHookProcedure, IntPtr.Zero,GetCurrentThreadId()); 162 162 hKeyboardHook = SetWindowsHookEx( WH_KEYBOARD,KeyboardHookProcedure,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0); 163 163 // 如果?置?子失? 164 164 if(hKeyboardHook == 0 ) 165 165 { 166 166 HookStop(); 167 167 throw new Exception("SetWindowsHookEx failed."); 168 168 } 169 169 } 170 170 } 171 171 172 172 /**//**//**//**//**//**//**//// <summary> 173 173 /**//// 卸??子 174 174 /**//// </summary> 175 175 public void HookStop() 176 176 { 177 177 bool retKeyboard = true; 178 178 if(hKeyboardHook != 0) 179 179 { 180 180 retKeyboard = UnhookWindowsHookEx(hKeyboardHook); 181 181 hKeyboardHook = 0; 182 182 } 183 183 if (!(retKeyboard)) 184 184 { 185 185 throw new Exception("UnhookWindowsHookEx failed."); 186 186 } 187 187 } 188 188 189 189 /**//**//**//// <summary> 190 190 /**//// 清除任何使用中的資源。 191 191 /**//// </summary> 192 192 protected override void Dispose( bool disposing ) 193 193 { 194 194 if( disposing ) 195 195 { 196 196 if (components != null) 197 197 { 198 198 components.Dispose(); 199 199 } 200 200 } 201 201 base.Dispose( disposing ); 202 202 } 203 203 204 204 Windows Form 設計工具產生的程式碼Windows Form 設計工具產生的程式碼#region Windows Form 設計工具產生的程式碼 205 205 /**//**//**//// <summary> 206 206 /**//// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改 207 207 /**//// 這個方法的內容。 208 208 /**//// </summary> 209 209 private void InitializeComponent() 210 210 { 211 211 this.components = new System.ComponentModel.Container(); 212 212 System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); 213 213 this.textBox1 = new System.Windows.Forms.TextBox(); 214 214 this.button1 = new System.Windows.Forms.Button(); 215 215 this.splitter1 = new System.Windows.Forms.Splitter(); 216 216 this.cbRthread = new System.Windows.Forms.ComboBox(); 217 217 this.label1 = new System.Windows.Forms.Label(); 218 218 this.label2 = new System.Windows.Forms.Label(); 219 219 this.comboBox1 = new System.Windows.Forms.ComboBox(); 220 220 this.label3 = new System.Windows.Forms.Label(); 221 221 this.label4 = new System.Windows.Forms.Label(); 222 222 this.comboBox2 = new System.Windows.Forms.ComboBox(); 223 223 this.label5 = new System.Windows.Forms.Label(); 224 224 this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); 225 225 this.label6 = new System.Windows.Forms.Label(); 226 226 this.cbSign = new System.Windows.Forms.ComboBox(); 227 227 this.axMSComm1 = new AxMSCommLib.AxMSComm(); 228 228 ((System.ComponentModel.ISupportInitialize)(this.axMSComm1)).BeginInit(); 229 229 this.SuspendLayout(); 230 230 // 231 231 // textBox1 232 232 // 233 233 this.textBox1.Location = new System.Drawing.Point(64, 16); 234 234 this.textBox1.Multiline = true; 235 235 this.textBox1.Name = "textBox1"; 236 236 this.textBox1.Size = new System.Drawing.Size(224, 56); 237 237 this.textBox1.TabIndex = 2; 238 238 this.textBox1.Text = ""; 239 239 // 240 240 // button1 241 241 // 242 242 this.button1.Location = new System.Drawing.Point(232, 160); 243 243 this.button1.Name = "button1"; 244 244 this.button1.TabIndex = 1; 245 245 this.button1.Text = "接收"; 246 246 this.button1.Click += new System.EventHandler(this.button1_Click); 247 247 // 248 248 // splitter1 249 249 // 250 250 this.splitter1.Location = new System.Drawing.Point(0, 0); 251 251 this.splitter1.Name = "splitter1"; 252 252 this.splitter1.Size = new System.Drawing.Size(3, 189); 253 253 this.splitter1.TabIndex = 5; 254 254 this.splitter1.TabStop = false; 255 255 // 256 256 // cbRthread 257 257 // 258 258 this.cbRthread.Items.AddRange(new object[] { 259 259 "1", 260 260 "2", 261 261 "3", 262 262 "4", 263 263 "5", 264 264 "6", 265 265 "7", 266 266 "8", 267 267 "9", 268 268 "10", 269 269 "11", 270 270 "12", 271 271 "13", 272 272 "14", 273 273 "15"}); 274 274 this.cbRthread.Location = new System.Drawing.Point(72, 88); 275 275 this.cbRthread.Name = "cbRthread"; 276 276 this.cbRthread.Size = new System.Drawing.Size(48, 21); 277 277 this.cbRthread.TabIndex = 6; 278 278 this.cbRthread.Text = "15"; 279 279 this.cbRthread.SelectedIndexChanged += new System.EventHandler(this.cbRthread_SelectedIndexChanged); 280 280 // 281 281 // label1 282 282 // 283 283 this.label1.Location = new System.Drawing.Point(16, 88); 284 284 this.label1.Name = "label1"; 285 285 this.label1.Size = new System.Drawing.Size(56, 23); 286 286 this.label1.TabIndex = 7; 287 287 this.label1.Text = "每次接收"; 288 288 // 289 289 // label2 290 290 // 291 291 this.label2.Location = new System.Drawing.Point(16, 120); 292 292 this.label2.Name = "label2"; 293 293 this.label2.Size = new System.Drawing.Size(160, 23); 294 294 this.label2.TabIndex = 8; 295 295 this.label2.Text = "關閉程序後是否清空剪切板"; 296 296 // 297 297 // comboBox1 298 298 // 299 299 this.comboBox1.Items.AddRange(new object[] { 300 300 "是", 301 301 "否"}); 302 302 this.comboBox1.Location = new System.Drawing.Point(176, 120); 303 303 this.comboBox1.Name = "comboBox1"; 304 304 this.comboBox1.Size = new System.Drawing.Size(48, 21); 305 305 this.comboBox1.TabIndex = 9; 306 306 this.comboBox1.Text = "否"; 307 307 this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged); 308 308 // 309 309 // label3 310 310 // 311 311 this.label3.Location = new System.Drawing.Point(128, 88); 312 312 this.label3.Name = "label3"; 313 313 this.label3.Size = new System.Drawing.Size(160, 23); 314 314 this.label3.TabIndex = 10; 315 315 this.label3.Text = "字符就放入剪切板"; 316 316 // 317 317 // label4 318 318 // 319 319 this.label4.Location = new System.Drawing.Point(16, 144); 320 320 this.label4.Name = "label4"; 321 321 this.label4.Size = new System.Drawing.Size(80, 23); 322 322 this.label4.TabIndex = 11; 323 323 this.label4.Text = "接收數字放大"; 324 324 // 325 325 // comboBox2 326 326 // 327 327 this.comboBox2.Items.AddRange(new object[] { 328 328 "1", 329 329 "10", 330 330 "100", 331 331 "1000", 332 332 "10000"}); 333 333 this.comboBox2.Location = new System.Drawing.Point(96, 144); 334 334 this.comboBox2.Name = "comboBox2"; 335 335 this.comboBox2.Size = new System.Drawing.Size(48, 21); 336 336 this.comboBox2.TabIndex = 12; 337 337 this.comboBox2.Text = "100"; 338 338 // 339 339 // label5 340 340 // 341 341 this.label5.Location = new System.Drawing.Point(144, 144); 342 342 this.label5.Name = "label5"; 343 343 this.label5.Size = new System.Drawing.Size(16, 23); 344 344 this.label5.TabIndex = 13; 345 345 this.label5.Text = "倍"; 346 346 // 347 347 // notifyIcon1 348 348 // 349 349 this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon"))); 350 350 this.notifyIcon1.Text = "COM1口連接程式(EF-11PRH)"; 351 351 this.notifyIcon1.Visible = true; 352 352 // 353 353 // label6 354 354 // 355 355 this.label6.Location = new System.Drawing.Point(16, 168); 356 356 this.label6.Name = "label6"; 357 357 this.label6.Size = new System.Drawing.Size(80, 23); 358 358 this.label6.TabIndex = 16; 359 359 this.label6.Text = "是否取絕對值"; 360 360 // 361 361 // cbSign 362 362 // 363 363 this.cbSign.Items.AddRange(new object[] { 364 364 "是", 365 365 "否"}); 366 366 this.cbSign.Location = new System.Drawing.Point(96, 168); 367 367 this.cbSign.Name = "cbSign"; 368 368 this.cbSign.Size = new System.Drawing.Size(48, 21); 369 369 this.cbSign.TabIndex = 17; 370 370 this.cbSign.Text = "是"; 371 371 // 372 372 // axMSComm1 373 373 // 374 374 this.axMSComm1.Enabled = true; 375 375 this.axMSComm1.Location = new System.Drawing.Point(16, 24); 376 376 this.axMSComm1.Name = "axMSComm1"; 377 377 this.axMSComm1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMSComm1.OcxState"))); 378 378 this.axMSComm1.Size = new System.Drawing.Size(38, 38); 379 379 this.axMSComm1.TabIndex = 18; 380 380 this.axMSComm1.OnComm += new System.EventHandler(this.axMSComm1_OnComm); 381 381 // 382 382 // Form1 383 383 // 384 384 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 385 385 this.ClientSize = new System.Drawing.Size(320, 189); 386 386 this.Controls.Add(this.axMSComm1); 387 387 this.Controls.Add(this.cbSign); 388 388 this.Controls.Add(this.label6); 389 389 this.Controls.Add(this.label5); 390 390 this.Controls.Add(this.comboBox2); 391 391 this.Controls.Add(this.label4); 392 392 this.Controls.Add(this.label3); 393 393 this.Controls.Add(this.comboBox1); 394 394 this.Controls.Add(this.label2); 395 395 this.Controls.Add(this.label1); 396 396 this.Controls.Add(this.cbRthread); 397 397 this.Controls.Add(this.splitter1); 398 398 this.Controls.Add(this.button1); 399 399 this.Controls.Add(this.textBox1); 400 400 this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); 401 401 this.MaximizeBox = false; 402 402 this.Name = "Form1"; 403 403 this.StartPosition = System.Windows.Forms.FormStartPosition.Manual; 404 404 this.Text = "是"; 405 405 this.WindowState = System.Windows.Forms.FormWindowState.Minimized; 406 406 this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); 407 407 this.Load += new System.EventHandler(this.Form1_Load); 408 408 ((System.ComponentModel.ISupportInitialize)(this.axMSComm1)).EndInit(); 409 409 this.ResumeLayout(false); 410 410 411 411 } 412 412 #endregion 413 413 414 414 /**//**//**//// <summary> 415 415 /**//// 應用程式的主進入點。 416 416 /**//// </summary> 417 417 [STAThread] 418 418 static void Main() 419 419 { 420 420 Application.Run(new Form1()); 421 421 } 422 422// void keypressed(Object o, KeyPressEventArgs e) 423 423// { 424 424// // The keypressed method uses the KeyChar property to check 425 425// // whether the ENTER key is pressed. 426 426// 427 427// // If the ENTER key is pressed, the Handled property is set to true, 428 428// // to indicate the event is handled. 429 429// if(e.KeyChar == (char)13) 430 430// e.Handled=true; 431 431// } 432 432 433 433 private void Form1_Load(object sender, System.EventArgs e) 434 434 { 435 435 axMSComm1.PortOpen=true; 436 436// axMSComm1.Output="RST"; 437 437// axMSComm1.Output="EXT3"; 438 438// axMSComm1.Output="IN"; 439 439// axMSComm1.Output="TOL1"; 440 440// axMSComm1.Output="RES2\r\n"; 441 441 } 442 442 443 443 private void axMSComm1_OnComm(object sender, System.EventArgs e) 444 444 { 445 445// MessageBox.Show("111"); 446 446 447 447 byte[] bytIn; 448 448 object objIn; 449 449 float fltC; 450 450 // int i; 451 451 452 452 axMSComm1.InputMode=MSCommLib.InputModeConstants.comInputModeBinary; 453 453 axMSComm1.InputLen =short.Parse(cbRthread.Text); 454 454 objIn=axMSComm1.Input; 455 455 bytIn =(byte [])objIn; 456 456 457 457 458 458 strIn = System.Text.ASCIIEncoding.ASCII.GetString(bytIn); 459 459 if(strIn.IndexOf(',')>0) strIn=strIn.Substring(strIn.IndexOf(',')+1); 460 460 textBox1.Text=strIn; 461 461 if(cbSign.SelectedIndex==0) 462 462 fltC=Math.Abs((float)Math.Round(float.Parse(strIn)*float.Parse(comboBox2.Text),2)); 463 463 else 464 464 fltC=(float)Math.Round(float.Parse(strIn)*float.Parse(comboBox2.Text),2); 465 465 466 466 System.Windows.Forms.Clipboard.SetDataObject(fltC.ToString(),clboard); 467 467 if(ctrlreset==false) 468 468 { 469 469 SendKeys.Send("^v"); 470 470 SendKeys.Send("{TAB}"); 471 471 472 472 axMSComm1.OutBufferCount=0; 473 473 ctrlreset=true; 474 474 475 475 476 476 // System.Threading.Thread.Sleep(500); 477 477 } 478 478 479 479 480 480 } 481 481 482 482 private void button1_Click(object sender, System.EventArgs e) 483 483 { 484 484 axMSComm1.Output="GA00\r\n"; 485 485 } 486 486 487 487 private void cbRthread_SelectedIndexChanged(object sender, System.EventArgs e) 488 488 { 489 489 axMSComm1.RThreshold=short.Parse(cbRthread.Text); 490 490 } 491 491 492 492 private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e) 493 493 { 494 494 if(comboBox1.Text.Trim()=="是") clboard=true; 495 495 else 496 496 clboard=false; 497 497 } 498 498 499 499 private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) 500 500 { 501 501// MessageBox.Show("11"); 502 502 } 503 503 504 504 505 505 506 506 507 507 508 508 509 509 510 510 511 511 512 512 } 513 513} 514 514 515
|