مرتضا این یه کلاس برای پورت سریال هست که برای یکی از پروژه ها ساخته بودمش...
این کلاس رو توی پروژت اضافه کن شاید بتونی مشکلت رو با این حل کنی
[PHP]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace CEWorld.IO.Ports
{
public class SerialPort
{
private System.IO.Ports.SerialPort ComPort = new System.IO.Ports.SerialPort();
public string[] GetPortNames()
{
string[] PortNames = System.IO.Ports.SerialPort.GetPortNames();
string[] p = { string.Empty };
foreach (string Port in PortNames)
{
p[p.GetUpperBound(0)] = Port;
Array.Resize(ref p, p.Length + 1);
}
Array.Resize(ref p, p.Length - 1);
return p;
}
public bool OpenPort(string PortName)
{
if (ComPort.IsOpen)
ComPort.Close();
ComPort.PortName = PortName;
try
{
ComPort.Open();
return true;
}
catch
{
return false;
}
return true;
}
public bool IsPortOpen()
{
return ComPort.IsOpen;
}
public void ClosePort()
{
if (ComPort.IsOpen)
ComPort.Close();
}
public string Send(string value)
{
ComPort.DiscardInBuffer();
ComPort.NewLine = "\r";
ComPort.WriteLine(value);
int t = 0;
string retData = string.Empty;
while (t <= 50)
{
retData = ComPort.ReadExisting();
if (!string.IsNullOrEmpty(retData))
{
return retData.Replace("\r", string.Empty).Replace("\n", string.Empty);
}
System.Threading.Thread.Sleep(10);
t++;
}
ComPort.DiscardInBuffer();
return string.Empty;
}
}
}
[/PHP]