ButonClick调用扰乱了我的串口'readTo'功能 [英] ButonClick call is disturbing my Serial Port 'readTo' function
本文介绍了ButonClick调用扰乱了我的串口'readTo'功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好。我有这段代码,当我使用botonclick函数(botonMostrarTramaClick(null,null);)时,程序在readto()行中停止(Trama = PuertoSerie.ReadTo(cFinal);)。当我不使用butonclick调用时,该程序工作正常,但问题的目的是如何使用按钮点击调用使程序工作。
Hi all. I have this piece of code and when I use the botonclick function (botonMostrarTramaClick(null, null);), the program stops in the readto() line (Trama = PuertoSerie.ReadTo(cFinal);). When I don't use the butonclick call, the program Works perfectly, but the aim of the question is how I can make the program work using the buton click call.
void GrabarContadorClick(object sender, EventArgs e)
{
if((ConfigurationManager.AppSettings["Administrador"]) != "Administrador")
{
botonMostrarTramaClick(null, null);
}
barraProgreso.Value = 0;
etiquetaProceso.Text = "";
try
{
string numeroSerie = "";
if(rtbTexto.Text.StartsWith("VSEL"))
{
numeroSerie = rtbTexto.Text.Substring(4, 8);
}
else
{
numeroSerie = rtbTexto.Text.Substring(10, 8);
}
try
{
rtbTexto.Text = "";
string Trama = "";
string cInicio = "";
string cFinal = "";
int nCInicio, nCFinal;
string text = System.IO.File.ReadAllText("Trama_Intact.txt");
MatchCollection coincidenciasInicio = Regex.Matches(text, cInicio);
nCInicio = coincidenciasInicio.Count;
MatchCollection coincidenciasFinal = Regex.Matches(text, cFinal);
nCFinal = coincidenciasFinal.Count;
int intStartPos = text.IndexOf(cInicio) + cInicio.Length;
int intEndPos = text.IndexOf(cFinal);
string resto_del_texto = text;
for (int i = 0; i < nCInicio; i++)
{
string encontrado = resto_del_texto.Substring(intStartPos, intEndPos - intStartPos);
resto_del_texto = resto_del_texto.Substring(intEndPos + 1);
intStartPos = resto_del_texto.IndexOf(cInicio) + cInicio.Length;
intEndPos = resto_del_texto.IndexOf(cFinal);
System.Text.StringBuilder strBuilder = new System.Text.StringBuilder(encontrado);
strBuilder[14] = '7';
encontrado = strBuilder.ToString(); //Convierte el array en string
encontrado = encontrado.Remove(0,10);
encontrado = encontrado.Remove(encontrado.Length - 4);
if(radioMantener.Checked && i == 1)
{
string principioMM1 = encontrado.Substring(0, 19);
string finalMM1 = encontrado.Remove(0, 27);
encontrado = String.Format("{0}{1}{2}", principioMM1, numeroSerie, finalMM1);
}
barraProgreso.Increment(1);
string porcentaje = String.Format("Grabando... {0}%", (barraProgreso.Value-1)*100/7);
barraProgreso.CreateGraphics().DrawString(porcentaje, new Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(barraProgreso.Width / 2 - 50, barraProgreso.Height / 2 - 7));
encontrado = String.Format("1<?0000000{0}", encontrado); Trama = String.Format("{0}{1}{2}{3}", (char)2, encontrado, Calculadora_CRC.CRC(encontrado), (char)4);
PuertoSerie.WriteLine(Trama);
//Thread.Sleep(3000);
Trama = PuertoSerie.ReadTo(cFinal);
rtbTexto.Text = rtbTexto.Text + "\n\n" + Trama; //Muestra la trama que se ha enviado
log.DebugFormat("Grabado mapa de memoria {0}", i);
if (i == 1)
{
Trama = String.Format("{0}2>?000000010554;={1}", (char)2, (char)4);
PuertoSerie.Write(Trama);
//Thread.Sleep(3000);
Trama = PuertoSerie.ReadTo(cFinal);
rtbTexto.Text = rtbTexto.Text + "\n\n" + Trama;
}
}
etiquetaProceso.Text = String.Format("Grabado {0}%", barraProgreso.Value*100/7);
预先感谢您的帮助。
Thanks in advance for the help.
推荐答案
完成剩下的任务后,我关闭串口。在开始'readTo'任务之前,我重新打开串口然后继续该程序。
After finishing the rest of the tasks, I close the serial port. Before start with the 'readTo' task, I re-open the serial port and then I continue with the program.
这篇关于ButonClick调用扰乱了我的串口'readTo'功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文