/// <summary> /// /// </summary> /// <param name="modbus"></param> /// <param name="address"></param> /// <param name="sender"></param> /// <param name="numberBytes">字节的个数</param> public SettingResistance(PLCSerialPort modbus, ushort address, TextBox sender, int numberBytes = 2) { this.modbus = modbus; this._address = address; this._sender = sender; this._numberByte = numberBytes; }
/// <summary> /// /// </summary> /// <param name="address"></param> /// <param name="textBox"></param> /// <param name="modbus"></param> /// <param name="zoomFlag"></param> /// <param name="numberByte">读取的字节数据</param> private void InitValue(int address, TextBox textBox, PLCSerialPort modbus, int zoomFlag = 1, int numberByte = 2) { byte[] value = modbus.ReadDataFromPLC(address, numberByte, ReadTimeOut); //Int16 dataValue = BitConverter.ToInt16(value, 0); //Int16 dataValue = (Int16)value[0]; GetReadValue(out int dataValue, numberByte, value); this.Invoke(new Action(() => { textBox.Text = ((decimal)(dataValue / (zoomFlag * 1.0f))) + ""; })); Console.WriteLine($"读取的数据:{dataValue} 转化后的数据:{textBox.Text}"); }
private void btnExport_Click(object sender, EventArgs e) { try { var portName = cmbSerialPort.Text; var rate = int.Parse(comboBoxRate.Text); Console.WriteLine("rate:" + rate); //首先判断端口是否打开 if (null != _plcSerialPort) { _plcSerialPort.Close(); _plcSerialPort = null; } //判断串口是否存在 if (string.IsNullOrEmpty(portName)) { MessageBox.Show("请选择串口"); return; } Console.WriteLine(rate); _plcSerialPort = new PLCSerialPort(portName, rate, null); //打开串口 if (!_plcSerialPort.Open()) { throw new Exception("打开串口失败"); } //判断是否已经从设备中读取参数 // if (false == this.hasInit) { if (false == InitParameter()) { return; } hasInit = true; //保存设置的参数 AppConfig.SetValue("com", portName); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void GetInitValue(PLCSerialPort moduleBus) { //角度 InitValue((ushort)RegisterSetting.角度, this.tbAllAngle, _plcSerialPort, 100); //点数 InitValue((ushort)RegisterSetting.测试点数, this.tbNumber, _plcSerialPort); //电阻 InitValue((ushort)RegisterSetting.总阻设定, this.tbResistance, _plcSerialPort, 100); //角度1 InitValue((ushort)RegisterSetting.角度下限, this.tbAngleOne, _plcSerialPort, 10); //角度2 InitValue((ushort)RegisterSetting.角度下限, this.tbAngleTwo, _plcSerialPort, 10); //旋转速度 InitValue((ushort)RegisterSetting.旋转速度, this.tbAngleTransfer, _plcSerialPort, 10); //斜率 InitValue((ushort)RegisterSetting.斜率, this.tbSlope, _plcSerialPort, 100); //最大允许误差 InitValue((ushort)RegisterSetting.线性允许误差, this.tbOffset, _plcSerialPort, 100); //极限电压 //InitValue((ushort)RegisterSetting.极限电压,this.tbV, _plcSerialPort, 100); //测试角度差 InitValue((ushort)RegisterSetting.零位偏差值, this.offsetAngle, _plcSerialPort); //正向误差 InitValue((ushort)RegisterSetting.总阻最大正向允许误差, this.forwardError, _plcSerialPort, 100); //负向误差 InitValue((ushort)RegisterSetting.总阻最大负向允许误差, this.backwardError, _plcSerialPort, 100); //零阻误差 InitValue((ushort)RegisterSetting.零阻最大允许误差, this.zeroError, _plcSerialPort); //测量间隔 InitValue((ushort)RegisterSetting.测量间隔, this.tbIntervalAngle, _plcSerialPort); //上行程 InitValue((ushort)RegisterSetting.行程, this.upValue, _plcSerialPort, 100); this._upValue = 10 * decimal.Parse(upValue.Text); //下行程 InitValue((ushort)RegisterSetting.行程, this.downValue, _plcSerialPort, 100); this._downValue = 10 * (1 - decimal.Parse(downValue.Text)); }
/// <summary> /// 设置系统的参数 /// </summary> /// <param name="address"></param> /// <param name="value"></param> /// <param name="modbus"></param> private void SetValue(ushort address, byte value, PLCSerialPort modbus) { // modbus.WriteMultipleRegisters(ConstPara.SlaveId,address,new ushort[]{value}); _plcSerialPort.WriteDatasEx(address, new byte[] { value }, 3000); }
private void ReadTestData() { this.progressBar.Value = 0; //MessageBox.Show("正在采集数据,请稍后。。。"); if (false == hasInit) { MessageBox.Show("请先读取参数"); return; } //开始检测之前首先要清空数据 ClearDrawData(); var portName = cmbSerialPort.Text; var rate = int.Parse(comboBoxRate.Text); //首先判断端口是否打开 if (null != _plcSerialPort) { _plcSerialPort.Close(); _plcSerialPort = null; } //判断串口是否存在 if (string.IsNullOrEmpty(portName)) { MessageBox.Show("请选择串口"); return; } _plcSerialPort = new PLCSerialPort(portName, rate, null); //打开串口 if (!_plcSerialPort.Open()) { throw new Exception("打开串口失败"); } Task.Factory.StartNew(() => { try { //开启检测的工 this.Invoke(new Action(() => { this.cmbSerialPort.Enabled = false; this.btnConfirm.Enabled = false; })); //在开始测试之前需要发送测试命令 SetValue((ushort)RegisterSetting.测试命令, 1, _plcSerialPort); Thread.Sleep(200); //开始读取,读取采集的状态, 如果采集到数据就把数据呈现出来 SetSystemStatus("等待测试"); try { //开始检测之前首先要清空数据 ClearDrawData(); this.Invoke(new Action(() => { btnConfirm.Enabled = false; })); this.isReadingStatus = true; // var valueIndex = 0; while (isReadingStatus) { valueIndex++; this.Invoke(new Action(() => { this.progressBar.Value = valueIndex; })); byte value = _plcSerialPort.GetD10Status(); Console.WriteLine($"D10状态:{value}"); //呈现测试的结果 ShowTestResult(value); //已经检测完毕 if (value == 2) { //获取运行的参数,开始计算 GetRunParameter(); //开始检测 BeginWork(); break; } if (value == 6) { MessageBox.Show("长时间没有响应"); break; } Thread.Sleep(2000); } } catch (Exception ex) { this.Invoke(new Action(() => { MessageBox.Show(ex.Message); })); } finally { isReadingStatus = false; //按钮恢复状态 this.Invoke(new Action(() => { this.btnConfirm.Enabled = true; })); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { this.btnConfirm.Enabled = true; } }); }