串行数据接收多个数据
Serial data receiving multiple data
我有一个 C# 应用程序试图从步进控制器读取两个电机位置,但似乎变得很困惑 - 或者至少我是这样。如果我只要求一个电机位置,它会很好用。
为了让控制器发送数据,我必须向它发送一个命令@00PX 用于 X 位置,@00PY 用于 Y 位置。控制器 returns 一个 28 位数的位置 - 后跟一个 .
我需要在电机移动时实时获取此信息,并区分文本框的哪些数据是 X,哪些是 Y。
我已经使用定时器滴答事件发送@00PX 命令,然后每 50 毫秒发送一次@00PY 命令。数据接收事件(读取一个 CR 字符)似乎变得混乱,并将数据随机放置在任一文本框中。我还尝试在数据调用之前为 Xpos 和 Ypos 应用标志,然后在读取数据后重置,但它似乎无法始终如一地工作。
我可能做错了,但最终还想获得编码器值,这样数据就会更加不同。我可以使用任何建议来完成这项工作。
private void SerialPortController_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
ControllerData = sp.ReadTo("\r"); //reads up to the <CR> and then outputs data
{
labelStatus.Invoke(new MethodInvoker(delegate { labelStatus.Text = ControllerData; ; })); //testing to see what is all output from controller
Int32 numOnly = 0;
bool result = int.TryParse(ControllerData, out numOnly); //numOnly should be number only and bool set to true
if (result == true) //checks to ensure that indata is only numbers - if true, display in text box
{
if (XPos == true)
{
if (radioButtonMoveDetector.Checked == true)
{
textBoxTotalDetectorSteps.Invoke(new MethodInvoker(delegate { textBoxTotalDetectorSteps.Text = ControllerData; ; })); //only place numbers in Text box not control characters
}
labelXPos.Invoke(new MethodInvoker(delegate { labelXPos.Text = ControllerData; ; })); //Show X position
XPos = false;
YPos = true;
serialPortController.Write("@00PY\r");
}
else if (YPos == true)
{
if (radioButtonMoveSource.Checked == true)
{
textBoxTotalSourceSteps.Invoke(new MethodInvoker(delegate { textBoxTotalSourceSteps.Text = ControllerData; ; })); //only place numbers in Text box not control characters
}
labelYPos.Invoke(new MethodInvoker(delegate { labelYPos.Text = ControllerData; ; })); //Show Y position
YPos = false;
private void timerMotor_Tick(object sender, EventArgs e)
{
if (serialPortController.IsOpen) //Check to ensure serial port is open
{
//timerMotor.Stop();
XPos = true;
serialPortController.Write("@00PX\r");//Sends command to retrieve motor X step position - a 28 bit number followed with a CR
//YPos = true;
serialPortController.Write("@00PY\r");
我删除了 DataReceived 事件,并在从 timer_tick 调用的单独模块中按顺序编写了发送和接收事件。计时器在 SendReceive 模块开始时停止,然后在结束时启动。感谢 Stephen 的肯定并引导我朝着正确的方向前进。
我有一个 C# 应用程序试图从步进控制器读取两个电机位置,但似乎变得很困惑 - 或者至少我是这样。如果我只要求一个电机位置,它会很好用。
为了让控制器发送数据,我必须向它发送一个命令@00PX 用于 X 位置,@00PY 用于 Y 位置。控制器 returns 一个 28 位数的位置 - 后跟一个 .
我需要在电机移动时实时获取此信息,并区分文本框的哪些数据是 X,哪些是 Y。
我已经使用定时器滴答事件发送@00PX 命令,然后每 50 毫秒发送一次@00PY 命令。数据接收事件(读取一个 CR 字符)似乎变得混乱,并将数据随机放置在任一文本框中。我还尝试在数据调用之前为 Xpos 和 Ypos 应用标志,然后在读取数据后重置,但它似乎无法始终如一地工作。
我可能做错了,但最终还想获得编码器值,这样数据就会更加不同。我可以使用任何建议来完成这项工作。
private void SerialPortController_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
ControllerData = sp.ReadTo("\r"); //reads up to the <CR> and then outputs data
{
labelStatus.Invoke(new MethodInvoker(delegate { labelStatus.Text = ControllerData; ; })); //testing to see what is all output from controller
Int32 numOnly = 0;
bool result = int.TryParse(ControllerData, out numOnly); //numOnly should be number only and bool set to true
if (result == true) //checks to ensure that indata is only numbers - if true, display in text box
{
if (XPos == true)
{
if (radioButtonMoveDetector.Checked == true)
{
textBoxTotalDetectorSteps.Invoke(new MethodInvoker(delegate { textBoxTotalDetectorSteps.Text = ControllerData; ; })); //only place numbers in Text box not control characters
}
labelXPos.Invoke(new MethodInvoker(delegate { labelXPos.Text = ControllerData; ; })); //Show X position
XPos = false;
YPos = true;
serialPortController.Write("@00PY\r");
}
else if (YPos == true)
{
if (radioButtonMoveSource.Checked == true)
{
textBoxTotalSourceSteps.Invoke(new MethodInvoker(delegate { textBoxTotalSourceSteps.Text = ControllerData; ; })); //only place numbers in Text box not control characters
}
labelYPos.Invoke(new MethodInvoker(delegate { labelYPos.Text = ControllerData; ; })); //Show Y position
YPos = false;
private void timerMotor_Tick(object sender, EventArgs e)
{
if (serialPortController.IsOpen) //Check to ensure serial port is open
{
//timerMotor.Stop();
XPos = true;
serialPortController.Write("@00PX\r");//Sends command to retrieve motor X step position - a 28 bit number followed with a CR
//YPos = true;
serialPortController.Write("@00PY\r");
我删除了 DataReceived 事件,并在从 timer_tick 调用的单独模块中按顺序编写了发送和接收事件。计时器在 SendReceive 模块开始时停止,然后在结束时启动。感谢 Stephen 的肯定并引导我朝着正确的方向前进。