VC2008中如何编程才能向PC串口发送一个字符? 不要用串口调试助手...
发布网友
发布时间:2024-02-13 03:17
我来回答
共3个回答
热心网友
时间:2024-03-17 13:37
VC2008 当然是 用 serialport 控件
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 9600;
serialPort1.Open();
byte[] data = Encoding.Unicode.GetBytes(textBox1.Text);
string str = Convert.ToBase64String(data);
serialPort1.WriteLine(str);
MessageBox.Show("数据发送成功!","系统提示");
}
串口程序 调试 可以 用 虚拟串口软件 vspd
请看 武汉鸿伟光电
有 虚拟串口软件 vspd 下载
热心网友
时间:2024-03-17 13:37
代码转自:《51单片机C语言创新教程》温子祺等编著,书籍中的界面开发篇!
#include "StdAfx.h"
#include "CSerial.h"
#include <assert.h> //使用断言
CSerial::CSerial(void)
{
m_bInit=FALSE;
m_hSerial=NULL;
}
CSerial::~CSerial(void)
{
Close();
}
BOOL CSerial::Ready(void)const
{
return m_bInit;
}
BOOL CSerial::Close(void)
{
if (m_hSerial)
{
CloseHandle(m_hSerial);
m_hSerial=NULL;
}
if (m_bInit)
{
m_bInit=FALSE;
}
return TRUE;
}
BOOL CSerial::Init(UINT portnr,
UINT baud,
UINT parity,
UINT databits,
UINT stopbits)
{
if (Ready())
{
Close();
}
COMMTIMEOUTS CommTimeOuts;
DCB dcb;
LPSTR sz= new CHAR[64];
LPWSTR wsz= new WCHAR[64];
// 打开串口
//wprintf_s(wsz,64,"COM%d",portnr);//用wprintf无效
sprintf_s(sz,64,"COM%d",portnr);
MultiByteToWideChar(CP_ACP,
0,
sz,
64,
wsz,
64);
m_hSerial = CreateFile(wsz,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
delete []sz;
delete []wsz;
if(m_hSerial == INVALID_HANDLE_VALUE)
{
return FALSE;
}
GetCommState(m_hSerial, &dcb); /* 读取串口的DCB */
dcb.BaudRate = baud; //波特率
dcb.ByteSize = databits; //数据位
dcb.Parity = parity; //校验位
dcb.StopBits = stopbits; //停止位
dcb.fParity = FALSE; /* 禁止奇偶校验 */
dcb.fBinary = TRUE;
dcb.fDtrControl = 0; /* 禁止流量控制 */
dcb.fRtsControl = 0;
dcb.fOutX = 0;
dcb.fInX = 0;
dcb.fTXContinueOnXoff = 0;
dcb.EvtChar='q'; // 这个一定要,否则大于127的数值会接收不了啊
//设置状态参数
SetCommMask(m_hSerial, EV_RXFLAG|EV_RXCHAR); /* 串口事件:接收到一个字符 */
SetupComm(m_hSerial, 1024, 1024); /* 设置接收与发送的缓冲区大小 */
if(!SetCommState(m_hSerial, &dcb)) /* 设置串口的DCB */
{
return FALSE;
}
//设置超时参数
GetCommTimeouts(m_hSerial, &CommTimeOuts);
CommTimeOuts.ReadIntervalTimeout = 100; /* 接收字符间最大时间间隔 */
CommTimeOuts.ReadTotalTimeoutMultiplier = 1;
CommTimeOuts.ReadTotalTimeoutConstant = 100; /* 读数据总超时常量 */
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 0;
if(!SetCommTimeouts(m_hSerial, &CommTimeOuts))
{
return FALSE;
}
PurgeComm(m_hSerial, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); /* 清除收/发缓冲区 */
m_bInit = TRUE;
return TRUE;
}
UINT CSerial::Send(UCHAR *pbuf,UINT len)
{
if (!Ready())
{
//AfxMessageBox(_T("请确保串口已正常打开"));
return 0;
}
BOOL rt=FALSE;
DWORD dwBytesSend=0,dwCnt=0;
while (len>dwCnt)
{
rt=WriteFile(m_hSerial,pbuf+dwCnt,len-dwCnt,&dwBytesSend,NULL);
if (!rt)
{
return FALSE;
}
dwCnt+=dwBytesSend;
if (len > dwCnt)
{
Sleep(100); //有可能I/O挂起
}
}
return (UINT)dwCnt;
}
UINT CSerial::Recv(UCHAR *pbuf,UINT len)
{
if (!Ready())
{
//AfxMessageBox(_T("请确保串口已正常打开"));
return 0;
}
BOOL rt=FALSE;
DWORD dwBytesRecv=0;
rt=ReadFile(m_hSerial,pbuf,len,&dwBytesRecv,NULL);
if (!rt)
{
return FALSE;
}
return (UINT)dwBytesRecv;
}
热心网友
时间:2024-03-17 13:34
bSuccess = WriteFile(m_hCom, // handle to file to write to
buffer, // pointer to data to write to file
dwBytesWrite, // number of bytes to write
&dwBytesWrite, // pointer to number of bytes written
&m_OverlappedWrite);