Example #1
0
 private void btnSetForce3_Click(object sender, EventArgs e)
 {
     if (USBSerial.advConfig.max_hall_value1 > 100)
     {
         int force = Int32.Parse(cbxForce3.GetItemText(cbxForce3.SelectedItem));
         USBSerial.SetServoAngle(2, (int)USBSerial.map(force, 0, 100, USBSerial.advConfig.min_servo_angle3, USBSerial.advConfig.max_servo_angle3));
     }
 }
Example #2
0
 private void btnSetMaxAngle3_Click(object sender, EventArgs e)
 {
     if (USBSerial.advConfig.max_hall_value1 > 100)
     {
         int angle = 0;
         if (Int32.TryParse(txtMaxServoAngle3.Text, out angle) && angle <= 180 && angle >= 0)
         {
             USBSerial.SetServoAngle(2, angle);
         }
     }
 }
Example #3
0
        private void tmrCheckSerial_Tick(object sender, EventArgs e)
        {
            // also check share memory update interval
            int value = 0;

            this.sharedMemoryInterval = Int32.TryParse(PCIConfig.NewBaseConfig["Interval"], out value) ? value : 100;

            // task
            if (!PCIConfig.NewBaseConfig.ContainsKey("SerialPort") || PCIConfig.NewBaseConfig["SerialPort"] == "")
            {
                if (USBSerial.ComDevice.IsOpen)
                {
                    // close port
                    if (CloseSerialPort())
                    {
                        lbSerialStatus.Text = "未设置USB端口,无法连接设备,请打开设置页进行配置";
                    }
                }
                return;
            }
            else
            {
                if (!USBSerial.ComDevice.IsOpen)
                {
                    if (portCountDown == 0)
                    {
                        if (!OpenSerialPort(PCIConfig.NewBaseConfig["SerialPort"], 115200))
                        {
                            portCountDown       = 5;
                            lbSerialStatus.Text = portErrorMessage + ", 5秒钟后重新连接..." + portCountDown.ToString();
                        }
                        else
                        {
                            lbSerialStatus.Text = "已连接";
                            // get adv config data
                            USBSerial.RequestAdvConfig();
                            USBSerial.RequestAdvConfig();
                        }
                    }
                    else
                    {
                        lbSerialStatus.Text = portErrorMessage + ", 5秒钟后重新连接..." + portCountDown.ToString();
                    }
                    portCountDown--;
                }
                else
                {
                    // send heartbeat data
                    USBSerial.SendHeartbeat();
                }
            }
        }
Example #4
0
        private void updateSharedMemory()
        {
            int force1 = -1;
            int force2 = -1;
            int force3 = -1;

            try
            {
                smMutex.WaitOne();
                // read force set data
                Marshal.Copy(smAddr, smData.data, 0, (int)SMPos.sm_set_length);
                force1 = smData.getAimForce1();
                force2 = smData.getAimForce2();
                force3 = smData.getAimForce3();
                // clear setting flag
                smData.setAimForce1(-1);
                smData.setAimForce2(-1);
                smData.setAimForce3(-1);
                smData.setData();
                Marshal.Copy(smData.data, 0, smAddr, (int)SMPos.sm_length);
                sharedMemoryLastTime = DateTime.Now.Ticks / 10000;
            }
            catch (Exception ex)
            {
                MessageBox.Show("更新共享内存出错:" + ex.Message);
            }
            finally
            {
                smMutex.ReleaseMutex();
            }

            // check if aim forces are set
            if (force1 >= 0)
            {
                USBSerial.SetServoAngle(0, (int)USBSerial.map(force1, 0, 100, USBSerial.advConfig.min_servo_angle1, USBSerial.advConfig.max_servo_angle1));
            }

            if (force2 >= 0)
            {
                USBSerial.SetServoAngle(1, (int)USBSerial.map(force2, 0, 100, USBSerial.advConfig.min_servo_angle2, USBSerial.advConfig.max_servo_angle2));
            }

            if (force3 >= 0)
            {
                USBSerial.SetServoAngle(2, (int)USBSerial.map(force3, 0, 100, USBSerial.advConfig.min_servo_angle3, USBSerial.advConfig.max_servo_angle3));
            }
        }
Example #5
0
        private void deviceDataReceived(DeviceData deviceData)
        {
            // update sm data received from serial device
            smData.pressure = deviceData.pressure;
            smData.switch1  = deviceData.switch1 == 1;
            smData.switch2  = deviceData.switch2 == 1;

            // check if device adv config is available
            if (USBSerial.advConfig.max_hall_value1 > 100)
            {
                smData.contrast = deviceData.contrast > USBSerial.advConfig.contrast_threshold;

                smData.force1 = Convert.ToInt32(USBSerial.map(deviceData.servo_angle1, USBSerial.advConfig.min_servo_angle1, USBSerial.advConfig.max_servo_angle1, 0, 100));
                smData.force2 = Convert.ToInt32(USBSerial.map(deviceData.servo_angle2, USBSerial.advConfig.min_servo_angle2, USBSerial.advConfig.max_servo_angle2, 0, 100));
                smData.force3 = Convert.ToInt32(USBSerial.map(deviceData.servo_angle3, USBSerial.advConfig.min_servo_angle3, USBSerial.advConfig.max_servo_angle3, 0, 100));

                smData.diam1 = Convert.ToSingle(Math.Round(USBSerial.map(deviceData.hall_value1, USBSerial.advConfig.min_hall_value1, USBSerial.advConfig.max_hall_value1, USBSerial.advConfig.min_hall_diam1, USBSerial.advConfig.max_hall_diam1) / 10.0, 1));
                smData.diam2 = Convert.ToSingle(Math.Round(USBSerial.map(deviceData.hall_value2, USBSerial.advConfig.min_hall_value2, USBSerial.advConfig.max_hall_value2, USBSerial.advConfig.min_hall_diam2, USBSerial.advConfig.max_hall_diam2) / 10.0, 1));
                smData.diam3 = Convert.ToSingle(Math.Round(USBSerial.map(deviceData.hall_value3, USBSerial.advConfig.min_hall_value3, USBSerial.advConfig.max_hall_value3, USBSerial.advConfig.min_hall_diam3, USBSerial.advConfig.max_hall_diam3) / 10.0, 1));

                if (DateTime.Now.Ticks / 10000 - sharedMemoryLastTime > sharedMemoryInterval)
                {
                    // update information
                    updateSharedMemory();
                }

                this.BeginInvoke(new MethodInvoker(delegate
                {
                    pbrPressure.Value    = smData.pressure;
                    lbPressureValue.Text = smData.pressure.ToString();
                    pbxSwitch1.BackColor = smData.switch1 ? Color.Green : Color.Gray;
                    pbxSwitch2.BackColor = smData.switch2 ? Color.Green : Color.Gray;

                    pbxContrast.BackColor = smData.contrast ? Color.Green : Color.Gray;

                    lbForceValue1.Text = smData.force1.ToString();
                    lbForceValue2.Text = smData.force2.ToString();
                    lbForceValue3.Text = smData.force3.ToString();

                    diameterList[0] = smData.diam1;
                    diameterList[1] = smData.diam2;
                    diameterList[2] = smData.diam3;

                    detectList[0] = true; // diameterList[0] > 1.3;
                    detectList[1] = true; // diameterList[1] > 0.2 && diameterList[1] < 0.6;
                    detectList[2] = true; // diameterList[2] > 0.6;

                    if (detectList[0])
                    {
                        pbxThickness1.BackColor = Color.Green;
                    }
                    else
                    {
                        pbxThickness1.BackColor = Color.Gray;
                        tbrTravel1.Value        = 0;
                        cpbRotation1.Value      = 0;
                    }
                    if (detectList[1])
                    {
                        pbxThickness2.BackColor = Color.Green;
                    }
                    else
                    {
                        pbxThickness2.BackColor = Color.Gray;
                        tbrTravel2.Value        = 0;
                        cpbRotation2.Value      = 0;
                    }
                    if (detectList[2])
                    {
                        pbxThickness3.BackColor = Color.Green;
                    }
                    else
                    {
                        pbxThickness3.BackColor = Color.Gray;
                        tbrTravel3.Value        = 0;
                        cpbRotation3.Value      = 0;
                    }

                    lbThicknessValue1.Text = Math.Round(diameterList[0], 1).ToString();
                    lbThicknessValue2.Text = Math.Round(diameterList[1], 1).ToString();
                    lbThicknessValue3.Text = Math.Round(diameterList[2], 1).ToString();
                }));
            }
        }
Example #6
0
 private void btnOK_Click(object sender, EventArgs e)
 {
     UpdateAdvConfig();
     USBSerial.UpdateAdvConfig();
     this.Close();
 }