//public delegate void ReceiveDelegate(string StringReceive); //public event ReceiveDelegate ReceiveTCPEvent; /// <summary> /// Hàm khởi tạo /// Mở kết nối tới cổng TCP /// Tạo Thread nhận dữ liệu từ cổng TCP /// Khai báo timer 1s để gửi lệnh duy trì kết nối /// </summary> /// <param name="ipAddress"></param> /// <param name="port"></param> public TouqueMObj(frmProductCheckHistory1 form, string ipAddress, int port, int rowIndex, int workingID, string MaSungLuc) { try { _Form = form; _rowIndex = rowIndex; _WorkingID = workingID; //_maSungLuc = MaSungLuc; _maSungLuc = "0000000000000000000000001"; SocketTCP = new TcpClient(); SocketTCP.Connect(ipAddress, port); if (SocketTCP.Connected) { //Console.WriteLine("Khởi tạo kết nối - IP :" + ipAddress + " - Port: " + port.ToString()); TouqueTCP = SocketTCP.GetStream(); ReceiveTCPThread = new Thread(ReceiveDataTCP); ReceiveTCPThread.IsBackground = true; ReceiveTCPThread.Start(); timerSentCommand = new System.Timers.Timer(); //timerSentCommand.Tick += ProcessTimer; timerSentCommand.Interval = 1000; timerSentCommand.Elapsed += ProcessTimer; timerSentCommand.Start(); } else { //MessageBox.Show("Không kết nối được đến thiết bị: " + ipAddress + " - Port: " + port.ToString()); } } catch (Exception ex) { } }
public GetComportValue(frmProductCheckHistory1 ControlData, string comPort, int rowIndex, int workingID) { _Form = ControlData; _rowIndex = rowIndex; _WorkingID = workingID; try { COMPortInitial(comPort); _Form.SetConnectCom(rowIndex, true); //DataInitial(); } catch { //_Form.SetConnectCom(rowIndex, false); } }
// Khai báo Event //public delegate void BalanceCalReturn(float Value); //public event BalanceCalReturn BalanceChangeValueEvent; #endregion /// <summary> /// Khởi tạo đối tượng Cân mỡ /// </summary> /// <param name="COMPort">Tên cổng Com dạng string</param> /// <param name="numberValue">Số lượng phần tử của mảng trích mẫu giá trị cân</param> /// <param name="limitWeigh">Khoảng thay đổi trả ra lớn nhất của cân</param> public BalanceObj(frmProductCheckHistory1 form, string COMPort, int numberValue, float limitWeigh, int rowIndex, int workingID) { _Form = form; _rowIndex = rowIndex; _WorkingID = workingID; // Khai báo đối tượng cân try { COM_Kern = new SerialPort(COMPort, 9600, Parity.None, 8, StopBits.One); COM_Kern.DataReceived += ProcessWeighData; COM_Kern.Open(); //_Form.SetConnectCom(rowIndex, true); } catch (Exception) { //_Form.SetConnectCom(rowIndex, false); return; } // Khai báo mảng lưu giá trị cân valueArr = new float[numberValue]; // Khởi tạo giá trị cân tối đa if (limitWeigh > 0) { limitValue = limitWeigh; } // Khởi tạo đối tượng hiển thị cân LbdWeighRead = new WeighObj(); LbdWeighTakeOut = new WeighObj(); // Khởi tạo mảng lưu trữ mặc định for (int i = 0; i < valueArr.Length; i++) { valueArr[i] = 0; } // Timer kiểm tra trạng thái Stable 0 (không gửi dữ liệu về PC) wZeroTimer = new System.Timers.Timer(500); wZeroTimer.Elapsed += CheckWeighValue; wZeroTimer.Start(); }