this.ShowMessageEvent += new MessageEventHandler 改为(没Copy上): this.ShowMessageEvent += new MessageEventHandler(aClass1.ShowMessage);
你是不是写了构造函数 public delegate void ChangedEventHandler(object sender, string c); public class ComTerm { MSComm com = new MSComm(); public event ChangedEventHandler Changed; //定义一个事件用委托来处理
public ComTerm() { // // TODO: 在此处添加构造函数逻辑 // com.OnComm += new MSCommLib.DMSCommEvents_OnCommEventHandler(this.Receive); InitComPort(2,9600); } //to call underlying com class/method to send command to com port ** public bool Send(string sSendMsg) // { // if(sSendMsg != "") // { // com.Output = sSendMsg; // return true; // } // else // return false; // } // //*************调用以下 com类/以将命令发送到 com 口 *****************
//close com port //Receive method to receive data from underlying class from com port public void Receive() // {
private void OnComm(object sender, EventArgs e) // MSCommLib OnComm Event Handler { // If data is waiting in the buffer, process it. // Note: This is using the string method for simple data, be sure // to use byte arrays (described below) for more generic data. if (com.InBufferCount > 0) com.Input.ToString(); } //PortSetting to setup port parameter such as portNumber and baudRate etc //设置端口参数,如端口号和波特率等 public void InitComPort(short iPort,int iBaudRate) { // This port is already open, close it to reset it. /*if(com.PortOpen) { throw(new ApplicationException("This port has already been opened!")); }*/ if (com.PortOpen) com.PortOpen = false; // Set the com port to be 1 //设置端口号 com.CommPort = iPort; // Trigger the OnComm event whenever data is received com.RThreshold = 1; // Set the port to 9600 baud, no parity bit, 8 data bits, 1 stop bit (all standard) com.Settings = iBaudRate + ",n,8,1"; // Force the DTR line high, used sometimes to hang up modems com.DTREnable = true;
// No handshaking is used com.Handshaking = MSCommLib.HandshakeConstants.comNone; // Don't mess with byte arrays, only works with simple data (characters A-Z and numbers) com.InputMode = MSCommLib.InputModeConstants.comInputModeText;
// Use this line instead for byte array input, best for most communications com.InputMode = MSCommLib.InputModeConstants.comInputModeText;
// Read the entire waiting data when com.Input is used com.InputLen = 0; com.OutBufferSize = 1024; // Don't discard nulls, 0x00 is a useful byte com.NullDiscard = false; // Attach the event handler //com.OnComm += new System.EventHandler(this.OnComm); //com.OnComm += new MSCommLib.DMSCommEvents_OnCommEventHandler(this.OnComm); // Open the com port try { com.PortOpen = true; com.OnComm += new MSCommLib.DMSCommEvents_OnCommEventHandler(this.Receive); } catch (Exception e) { Console.WriteLine("{0} Exception caught.", e); }
//com.Changed += new ChangedEventHandler(ReceiveData); } } } 窗体调用的时候private ComTerm com = new ComTerm(); ReceivePacket rep = new ReceivePacket(); public Mainfrm() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); com.Changed += new ChangedEventHandler(this.OnComm); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } private void OnComm(object sender, string s) { //s += s; if(s.Length == 173) { com.InBufferCountClear(); s = "$AMD" + s; string bbc = "101010"; int rrr = int.Parse(bbc.Substring(0,4)); rep.Analyze(s); }
改为(没Copy上):
this.ShowMessageEvent += new MessageEventHandler(aClass1.ShowMessage);
public delegate void ChangedEventHandler(object sender, string c);
public class ComTerm
{
MSComm com = new MSComm();
public event ChangedEventHandler Changed; //定义一个事件用委托来处理
public ComTerm()
{
//
// TODO: 在此处添加构造函数逻辑
//
com.OnComm += new MSCommLib.DMSCommEvents_OnCommEventHandler(this.Receive);
InitComPort(2,9600);
} //to call underlying com class/method to send command to com port **
public bool Send(string sSendMsg) //
{ //
if(sSendMsg != "") //
{ //
com.Output = sSendMsg; //
return true; //
} //
else //
return false; //
} //
//*************调用以下 com类/以将命令发送到 com 口 *****************
//close com port
//Receive method to receive data from underlying class from com port
public void Receive() //
{
if (Changed != null)
{
if(com.InBufferCount > 0)////
{
string strAnalyze = com.Input.ToString();
Changed(this,strAnalyze);
}
}
} public void InBufferCountClear()
{
com.InBufferCount = 0;
}
//叫串口滚
public void comClose()
{
if(com.PortOpen)
com.PortOpen = false;
}
//} public string comdata(string input)
{
string putin = (input + "\n");
return putin;
}
//************* 接收方法从一个串口的优先类接收数据 ****************
private void OnComm(object sender, EventArgs e) // MSCommLib OnComm Event Handler
{
// If data is waiting in the buffer, process it.
// Note: This is using the string method for simple data, be sure
// to use byte arrays (described below) for more generic data.
if (com.InBufferCount > 0)
com.Input.ToString();
}
//PortSetting to setup port parameter such as portNumber and baudRate etc
//设置端口参数,如端口号和波特率等
public void InitComPort(short iPort,int iBaudRate)
{
// This port is already open, close it to reset it.
/*if(com.PortOpen)
{
throw(new ApplicationException("This port has already been opened!"));
}*/
if (com.PortOpen)
com.PortOpen = false; // Set the com port to be 1
//设置端口号
com.CommPort = iPort;
// Trigger the OnComm event whenever data is received
com.RThreshold = 1;
// Set the port to 9600 baud, no parity bit, 8 data bits, 1 stop bit (all standard)
com.Settings = iBaudRate + ",n,8,1"; // Force the DTR line high, used sometimes to hang up modems
com.DTREnable = true;
// No handshaking is used
com.Handshaking = MSCommLib.HandshakeConstants.comNone; // Don't mess with byte arrays, only works with simple data (characters A-Z and numbers)
com.InputMode = MSCommLib.InputModeConstants.comInputModeText;
// Use this line instead for byte array input, best for most communications
com.InputMode = MSCommLib.InputModeConstants.comInputModeText;
// Read the entire waiting data when com.Input is used
com.InputLen = 0; com.OutBufferSize = 1024; // Don't discard nulls, 0x00 is a useful byte
com.NullDiscard = false;
// Attach the event handler
//com.OnComm += new System.EventHandler(this.OnComm);
//com.OnComm += new MSCommLib.DMSCommEvents_OnCommEventHandler(this.OnComm);
// Open the com port
try
{
com.PortOpen = true;
com.OnComm += new MSCommLib.DMSCommEvents_OnCommEventHandler(this.Receive);
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
//com.Changed += new ChangedEventHandler(ReceiveData);
}
}
}
窗体调用的时候private ComTerm com = new ComTerm();
ReceivePacket rep = new ReceivePacket();
public Mainfrm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
com.Changed += new ChangedEventHandler(this.OnComm); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
private void OnComm(object sender, string s)
{
//s += s;
if(s.Length == 173)
{
com.InBufferCountClear();
s = "$AMD" + s;
string bbc = "101010";
int rrr = int.Parse(bbc.Substring(0,4));
rep.Analyze(s);
}
}
特别是brightheroes(闭关|那一剑的风情)CSDN真是大家交流思想答疑解惑的好地方,祝大家共同进步!