PDA

View Full Version : ارسال دیتا وقتی اطلاعاتی وارد می شود



omidan321
یک شنبه 28 مهر 1392, 13:57 عصر
سلام به همگی

من اطلاعاتی از پورت سریال دریافت میکنم تحلیلی روش انجام میدم و توی همون متد datareceive میخام یه اطلاعات جدید رو ارسال کنم اما مشکل دارم -- کد رو میزارم اساتید اشکالات رو بهم بگید ممنون میشم.

private void Port_DataReceived(object sender,System.IO.Ports.SerialDataReceivedEventArgs e)

{
string cf,cf_prm,cf_fcv,cf_fun;
reciving = Port.ReadExisting();
this.Invoke(new EventHandler(DisplayText));
str= textBox3 .Text ;
int pos=str.IndexOf ("16");
if ((pos < 10) && (str.Length <15))
{

MessageBox.Show("اطلاعات شما با موفقیت دریافت شد", "اطلاع");
cf = str.Substring(3, 2);
textBox2.Text = cf;
string cf_bin = Convert.ToString(Convert.ToInt32(cf, 16), 2);
cf_prm = cf_bin.Substring(2, 1);
cf_fcv = cf_bin.Substring(4, 1);
cf_fun = cf_bin.Substring(5, 4);
if (cf_prm == "1")
{
if ((cf_fun == "0000") && (cf_fcv == "0"))
{
//
}

if ((cf_fun == "0001") && (cf_fcv == "0"))
{
//
}

if ((cf_fun == "1001") && (cf_fcv == "0"))
{
sendstr ();

}
if ((cf_fun == "1010") && (cf_fcv == "1"))
{
//
}
if ((cf_fun == "1011") && (cf_fcv == "1"))
{
//
}


}


}
else if (str .Length>16 )

{
MessageBox.Show("اطلاعات شما با موفقیت دریافت نشده", "اطلاع");
}
}

private void sendstr()
{
throw new Exception("The method or operation is not implemented.");
byte[] bytesToSend = new byte[200] { Convert.ToByte(str) };
Port.Write(bytesToSend, 0, bytesToSend.Length);
}

private void DisplayText(object sender, EventArgs e)
{

textBox2.Text = textBox2.Text + reciving ;
textBox3.Text = convertAsciiTextToHex(textBox2.Text);

}

private String convertAsciiTextToHex(String i_asciiText)
{
StringBuilder sBuffer = new StringBuilder();
for (int i = 0; i < i_asciiText.Length; i++)
{
sBuffer.Append(Convert.ToInt32(i_asciiText[i]).ToString("x"));
}
return sBuffer.ToString().ToUpper();
}


منتظر نظرات اساتید هستم