单片机通信问题 5
单片机在和上位机通信时需要借助一个串口小助手,那是不是在自己用单片机采集信号想要传给电脑并要显示数据在电脑上时还要编写一个串口小助手出来吗,如果不是那该怎么编写自己的软件...
单片机在和上位机通信时需要借助一个串口小助手,那是不是在自己用单片机采集信号想要传给电脑并要显示数据在电脑上时还要编写一个串口小助手出来吗,如果不是那该怎么编写自己的软件显示数据,本人会写MFC不知怎么样编写
展开
4个回答
展开全部
using System;
using System.IO.Ports;
namespace Midapexsoft.IO.Ports
{
/// <summary>
/// Interface of serial port
/// </summary>
public interface ISerialPort : IDisposable, Midapexsoft.IO.ITerminal
{
#region Property
string PortName
{
get;
set;
}
Parity Parity
{
get;
set;
}
int DataBits
{
get;
set;
}
StopBits StopBits
{
get;
set;
}
StandardBaudRate BaudRate
{
get;
set;
}
Handshake HandShake
{
get;
set;
}
#endregion
}
/// <summary>
/// Standard baud rate setting
/// </summary>
public enum StandardBaudRate
{
R110 = 110,
R300 = 300,
R600 = 600,
R1200 = 1200,
R2400 = 2400,
R4800 = 4800,
R9600 = 9600,
R14400 = 14400,
R19200 = 19200,
R38400 = 38400,
R57600 = 57600,
R76800 = 76800,
R115200 = 115200
};
}
//SerialPortX.h
#pragma once
#include "AsyncSerialPort.h"
#include "RuntimeException.h"
#include "SerialPortX.h"
#include "RuntimeException.h"
using namespace System;
using namespace System::Collections;
class CSerialPortObserver;
namespace Midapexsoft
{
namespace IO
{
namespace Port
{
public ref class SerialPortX: public IStartable, public ISerialPort
{
public:
SerialPortX(void);
~SerialPortX(void);
virtual void Stop();
virtual void SerialPortX::Start();
virtual void Write(array<byte> ^ data);
virtual event BytesEventHandler^ OnWriteData
{
void add(BytesEventHandler^ value)
{
writeDataHandler += value;
}
void remove(BytesEventHandler^ value)
{
writeDataHandler -= value;
}
void raise(Object^ sender, BytesEventArgs^ args)
{
if(writeDataHandler!=nullptr)
{
//writeDataHandler->BeginInvoke(sender, args,nullptr,nullptr);
writeDataHandler(sender,args);
}
}
}
virtual event BytesEventHandler^ OnReadData
{
void add(BytesEventHandler^ value)
{
readDataHandler += value;
}
void remove(BytesEventHandler^ value)
{
readDataHandler -= value;
}
void raise(Object^ sender, BytesEventArgs^ args)
{
if(readDataHandler!=nullptr)
{
//readDataHandler->BeginInvoke(sender, args,nullptr,nullptr);
readDataHandler(sender,args);
}
}
}
virtual event MessageEventHandler^ OnError
{
void add(MessageEventHandler^ value)
{
errorHandler += value;
}
void remove(MessageEventHandler^ value)
{
errorHandler -= value;
}
void raise(Object^ sender, MessageEventArgs^ args)
{
if(errorHandler!=nullptr)
{
//errorHandler->BeginInvoke(sender, args,nullptr,nullptr);
errorHandler(sender,args);
}
}
}
property String^ PortName
{
virtual String^ get() { return name;}
virtual void set(String^ newName)
{
//Get port number
int portNumberIndex = newName->IndexOf("COM");
if(portNumberIndex!=0)
{
throw gcnew Exception("Bad Port Name");
}
String^ portNumberS = newName->Substring(3);
portNumber = Convert::ToByte(portNumberS);
name= newName;
}
}
property Parity PortParity
{
virtual Parity get() { return parity;}
virtual void set(Parity parity)
{
try
{
if(IsOpen)
{
Hashtable^ parityTable = gcnew Hashtable();
parityTable->Add(L"None", 2);
parityTable->Add(L"Odd",3);
parityTable->Add(L"Even",gcnew int(0) );
parityTable->Add(L"Mark",1);
parityTable->Add(L"Space",4);
int parityMothord = (int) parityTable[parity.ToString()];
port->SetParity((CSerialPort::Parity)parityMothord);
}
this->parity = parity;
}
catch (CRuntimeException* e)
{
String^ info = gcnew String((LPCTSTR)e->GetErrorMessage());
throw gcnew Exception( info );
e->Delete();
}
}
}
property byte DataBytes
{
virtual byte get() { return dataBytes;}
virtual void set(byte dataBytes)
{
if(IsOpen)
{
port->SetDataBytes(dataBytes);
}
this->dataBytes = dataBytes;
}
}
property bool IsOpen
{
virtual bool get() { return port->IsOpen() != 0;}
}
property StopBits PortStopBits
{
virtual StopBits get() { return stopBits; }
virtual void set(StopBits stopBits)
{
try
{
if(IsOpen)
{
port->SetStopBits((CSerialPort::StopBits)(int)stopBits);
}
this->stopBits = stopBits;
}
catch (CRuntimeException* e)
{
String^ info = gcnew String((LPCTSTR)e->GetErrorMessage());
throw gcnew Exception( info );
e->Delete();
}
}
}
property StandardBaudRate BaudRate
{
virtual StandardBaudRate get() { return this->baudRate; }
virtual void set(StandardBaudRate newBaudRate)
{
try
{
if(IsOpen)
{
int baud = (int)newBaudRate;
port->SetBaud(baud);
}
this->baudRate = newBaudRate;
}
catch (CRuntimeException* e)
{
String^ info = gcnew String((LPCTSTR)e->GetErrorMessage());
throw gcnew Exception( info );
e->Delete();
}
}
}
property Handshake PortHandShake
{
virtual Handshake get() { return this->handshake;}
virtual void set( Handshake newHandShake)
{
try
{
if(IsOpen)
{
Hashtable ^ handshakeTable = gcnew Hashtable();
handshakeTable->Add("None",gcnew int(0));
handshakeTable->Add("XonXoff",5);
handshakeTable->Add("CtsRts",1);
handshakeTable->Add("DsrDtr",4);
//Handshake
int handshake = (int)handshakeTable[ newHandShake.ToString() ];
port->SetFlowControl((CSerialPort::FlowControl)handshake);
}
this->handshake = newHandShake;
}
catch (CRuntimeException* e)
{
String^ info = gcnew String((LPCTSTR)e->GetErrorMessage());
throw gcnew Exception( info );
e->Delete();
}
}
}
property bool CanStart
{
virtual bool get() { return canStart;}
virtual void set(bool canStart) { this->canStart = canStart; }
}
property bool IsRun
{
virtual bool get() { return isRun;}
}
virtual event EventHandler<StartableArgs^>^ OnStartableStatueChange
{
void add(EventHandler<StartableArgs^>^ value)
{
statusChangeHandler+= value;
}
void remove(EventHandler<StartableArgs^>^ value)
{
statusChangeHandler -= value;
}
void raise(Object^ sender, StartableArgs^ args)
{
if(statusChangeHandler!=nullptr)
{
statusChangeHandler->BeginInvoke(sender,args,nullptr,nullptr);
}
}
}
internal:
void OnPortWriteData(array<Byte>^ data)
{
OnWriteData(this, gcnew BytesEventArgs(data));
}
void OnPortReceiveData(array<Byte>^ data)
{
OnReadData(this, gcnew BytesEventArgs(data));
}
void ReportError(String^ errorMessage)
{
OnError(this, gcnew MessageEventArgs(errorMessage));
}
void SetRunStatus(bool isRun)
{
this->isRun = isRun;
OnStartableStatueChange(this, gcnew StartableArgs(this));
}
private:
CAsyncSerialPort* port;
CSerialPortObserver* observer;
private:
String^ name;
Parity parity;
byte dataBytes;
StandardBaudRate baudRate;
Handshake handshake;
StopBits stopBits;
byte portNumber;
bool canStart;
bool isRun;
BytesEventHandler^ writeDataHandler;
BytesEventHandler^ readDataHandler;
MessageEventHandler^ errorHandler;
EventHandler<StartableArgs^>^ statusChangeHandler;
};
}
}
}
希望对你有帮助
using System.IO.Ports;
namespace Midapexsoft.IO.Ports
{
/// <summary>
/// Interface of serial port
/// </summary>
public interface ISerialPort : IDisposable, Midapexsoft.IO.ITerminal
{
#region Property
string PortName
{
get;
set;
}
Parity Parity
{
get;
set;
}
int DataBits
{
get;
set;
}
StopBits StopBits
{
get;
set;
}
StandardBaudRate BaudRate
{
get;
set;
}
Handshake HandShake
{
get;
set;
}
#endregion
}
/// <summary>
/// Standard baud rate setting
/// </summary>
public enum StandardBaudRate
{
R110 = 110,
R300 = 300,
R600 = 600,
R1200 = 1200,
R2400 = 2400,
R4800 = 4800,
R9600 = 9600,
R14400 = 14400,
R19200 = 19200,
R38400 = 38400,
R57600 = 57600,
R76800 = 76800,
R115200 = 115200
};
}
//SerialPortX.h
#pragma once
#include "AsyncSerialPort.h"
#include "RuntimeException.h"
#include "SerialPortX.h"
#include "RuntimeException.h"
using namespace System;
using namespace System::Collections;
class CSerialPortObserver;
namespace Midapexsoft
{
namespace IO
{
namespace Port
{
public ref class SerialPortX: public IStartable, public ISerialPort
{
public:
SerialPortX(void);
~SerialPortX(void);
virtual void Stop();
virtual void SerialPortX::Start();
virtual void Write(array<byte> ^ data);
virtual event BytesEventHandler^ OnWriteData
{
void add(BytesEventHandler^ value)
{
writeDataHandler += value;
}
void remove(BytesEventHandler^ value)
{
writeDataHandler -= value;
}
void raise(Object^ sender, BytesEventArgs^ args)
{
if(writeDataHandler!=nullptr)
{
//writeDataHandler->BeginInvoke(sender, args,nullptr,nullptr);
writeDataHandler(sender,args);
}
}
}
virtual event BytesEventHandler^ OnReadData
{
void add(BytesEventHandler^ value)
{
readDataHandler += value;
}
void remove(BytesEventHandler^ value)
{
readDataHandler -= value;
}
void raise(Object^ sender, BytesEventArgs^ args)
{
if(readDataHandler!=nullptr)
{
//readDataHandler->BeginInvoke(sender, args,nullptr,nullptr);
readDataHandler(sender,args);
}
}
}
virtual event MessageEventHandler^ OnError
{
void add(MessageEventHandler^ value)
{
errorHandler += value;
}
void remove(MessageEventHandler^ value)
{
errorHandler -= value;
}
void raise(Object^ sender, MessageEventArgs^ args)
{
if(errorHandler!=nullptr)
{
//errorHandler->BeginInvoke(sender, args,nullptr,nullptr);
errorHandler(sender,args);
}
}
}
property String^ PortName
{
virtual String^ get() { return name;}
virtual void set(String^ newName)
{
//Get port number
int portNumberIndex = newName->IndexOf("COM");
if(portNumberIndex!=0)
{
throw gcnew Exception("Bad Port Name");
}
String^ portNumberS = newName->Substring(3);
portNumber = Convert::ToByte(portNumberS);
name= newName;
}
}
property Parity PortParity
{
virtual Parity get() { return parity;}
virtual void set(Parity parity)
{
try
{
if(IsOpen)
{
Hashtable^ parityTable = gcnew Hashtable();
parityTable->Add(L"None", 2);
parityTable->Add(L"Odd",3);
parityTable->Add(L"Even",gcnew int(0) );
parityTable->Add(L"Mark",1);
parityTable->Add(L"Space",4);
int parityMothord = (int) parityTable[parity.ToString()];
port->SetParity((CSerialPort::Parity)parityMothord);
}
this->parity = parity;
}
catch (CRuntimeException* e)
{
String^ info = gcnew String((LPCTSTR)e->GetErrorMessage());
throw gcnew Exception( info );
e->Delete();
}
}
}
property byte DataBytes
{
virtual byte get() { return dataBytes;}
virtual void set(byte dataBytes)
{
if(IsOpen)
{
port->SetDataBytes(dataBytes);
}
this->dataBytes = dataBytes;
}
}
property bool IsOpen
{
virtual bool get() { return port->IsOpen() != 0;}
}
property StopBits PortStopBits
{
virtual StopBits get() { return stopBits; }
virtual void set(StopBits stopBits)
{
try
{
if(IsOpen)
{
port->SetStopBits((CSerialPort::StopBits)(int)stopBits);
}
this->stopBits = stopBits;
}
catch (CRuntimeException* e)
{
String^ info = gcnew String((LPCTSTR)e->GetErrorMessage());
throw gcnew Exception( info );
e->Delete();
}
}
}
property StandardBaudRate BaudRate
{
virtual StandardBaudRate get() { return this->baudRate; }
virtual void set(StandardBaudRate newBaudRate)
{
try
{
if(IsOpen)
{
int baud = (int)newBaudRate;
port->SetBaud(baud);
}
this->baudRate = newBaudRate;
}
catch (CRuntimeException* e)
{
String^ info = gcnew String((LPCTSTR)e->GetErrorMessage());
throw gcnew Exception( info );
e->Delete();
}
}
}
property Handshake PortHandShake
{
virtual Handshake get() { return this->handshake;}
virtual void set( Handshake newHandShake)
{
try
{
if(IsOpen)
{
Hashtable ^ handshakeTable = gcnew Hashtable();
handshakeTable->Add("None",gcnew int(0));
handshakeTable->Add("XonXoff",5);
handshakeTable->Add("CtsRts",1);
handshakeTable->Add("DsrDtr",4);
//Handshake
int handshake = (int)handshakeTable[ newHandShake.ToString() ];
port->SetFlowControl((CSerialPort::FlowControl)handshake);
}
this->handshake = newHandShake;
}
catch (CRuntimeException* e)
{
String^ info = gcnew String((LPCTSTR)e->GetErrorMessage());
throw gcnew Exception( info );
e->Delete();
}
}
}
property bool CanStart
{
virtual bool get() { return canStart;}
virtual void set(bool canStart) { this->canStart = canStart; }
}
property bool IsRun
{
virtual bool get() { return isRun;}
}
virtual event EventHandler<StartableArgs^>^ OnStartableStatueChange
{
void add(EventHandler<StartableArgs^>^ value)
{
statusChangeHandler+= value;
}
void remove(EventHandler<StartableArgs^>^ value)
{
statusChangeHandler -= value;
}
void raise(Object^ sender, StartableArgs^ args)
{
if(statusChangeHandler!=nullptr)
{
statusChangeHandler->BeginInvoke(sender,args,nullptr,nullptr);
}
}
}
internal:
void OnPortWriteData(array<Byte>^ data)
{
OnWriteData(this, gcnew BytesEventArgs(data));
}
void OnPortReceiveData(array<Byte>^ data)
{
OnReadData(this, gcnew BytesEventArgs(data));
}
void ReportError(String^ errorMessage)
{
OnError(this, gcnew MessageEventArgs(errorMessage));
}
void SetRunStatus(bool isRun)
{
this->isRun = isRun;
OnStartableStatueChange(this, gcnew StartableArgs(this));
}
private:
CAsyncSerialPort* port;
CSerialPortObserver* observer;
private:
String^ name;
Parity parity;
byte dataBytes;
StandardBaudRate baudRate;
Handshake handshake;
StopBits stopBits;
byte portNumber;
bool canStart;
bool isRun;
BytesEventHandler^ writeDataHandler;
BytesEventHandler^ readDataHandler;
MessageEventHandler^ errorHandler;
EventHandler<StartableArgs^>^ statusChangeHandler;
};
}
}
}
希望对你有帮助
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给个比较麻烦的建议:
试着用用下述软件:力控(北京三维力控,很好的技术支持)、LabVIEW(NI,很强大)。其他的就不说了。力控的好处在于简单,很容易实现你期望的功能,而且有很好的技术支持。可以让你摆脱编程的烦恼,只是不容易得到盗版之类,使用试用版或者开发版是有限制的。LabVIEW很强大,可能会花费多点的时间,不过图形化的编程能给以后的工作带来很多的方便。
试着用用下述软件:力控(北京三维力控,很好的技术支持)、LabVIEW(NI,很强大)。其他的就不说了。力控的好处在于简单,很容易实现你期望的功能,而且有很好的技术支持。可以让你摆脱编程的烦恼,只是不容易得到盗版之类,使用试用版或者开发版是有限制的。LabVIEW很强大,可能会花费多点的时间,不过图形化的编程能给以后的工作带来很多的方便。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
网上有串口助手软件的MFC代码的,自己可以搜索一下,懂MFC的话应该很容易能看懂的。
http://dl.21ic.com/download/code/21ic-code-ic-7851.html
这个是我搜索的,还有很多其他版本的,弄懂一个就行了。
http://dl.21ic.com/download/code/21ic-code-ic-7851.html
这个是我搜索的,还有很多其他版本的,弄懂一个就行了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询