bool WinApit232::OpenComm()
{
//bool opencom=false;
m_hCom = CreateFile(_T("COM1"),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL); // 重叠方式
if(m_hCom != INVALID_HANDLE_VALUE)
{
SetupComm(m_hCom, MAXBLOCK, MAXBLOCK);
COMMTIMEOUTS TimeOuts;
//设定读超时
TimeOuts.ReadIntervalTimeout=1000;
TimeOuts.ReadTotalTimeoutMultiplier=500;
TimeOuts.ReadTotalTimeoutConstant=5000;
//设定写超时
TimeOuts.WriteTotalTimeoutMultiplier=500;
TimeOuts.WriteTotalTimeoutConstant=2000;
SetCommTimeouts(m_hCom,&TimeOuts); //设置超时 SetCommMask(m_hCom, EV_RXCHAR);
DCB myDCB;
GetCommState( m_hCom, &myDCB);
myDCB.BaudRate=CBR_115200;
myDCB.fBinary=TRUE;
myDCB.fParity=TRUE;
myDCB.ByteSize=8;
myDCB.Parity=ODDPARITY;
myDCB.StopBits=ONESTOPBIT;
SetCommState(m_hCom,&myDCB);
PurgeComm(m_hCom,PURGE_TXCLEAR|PURGE_RXCLEAR); opencom=true;
}
else
{
opencom=false;
}
return opencom;
}bool WinApit232::SendToCom(unsigned char *buffer)
{
bool fState=false;
//unsigned char *buffer;
unsigned long length=16;
BYTE masend[9];
//strncpy(buffer,sendstring.c_str(),sendstring.length());
if (OpenComm())
{
PurgeComm(m_hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
EnterCriticalSection(&crit_sect);
fState=WriteFile(m_hCom,buffer,length,NULL,&m_osWrite);
if (!fState)
{
MessageBox(NULL,L"向串口写入失败",L"失败",NULL);
if(GetLastError()==ERROR_IO_PENDING)
{
MessageBox(NULL,L"写入失败",L"但是非失败",NULL);
WaitForSingleObject(m_osWrite.hEvent,1000);
}
else
{
MessageBox(NULL,L"向串口写入失败",L"失败再次确认",NULL);
}
}
LeaveCriticalSection(&crit_sect);
//strncpy((char *)buf, (const char *)sendstring, 16);
//System.Text.Encoding.Default.GetBytes();//
/*int strlen=sendstring.length();
memcpy(buf, sendstring.c_str(),strlen);
for (int i=0;i<strlen;i++)
{
EnterCriticalSection(&crit_sect);
fState=WriteFile(m_hCom,
&buf[i],
1,
NULL,
&m_osWrite);
LeaveCriticalSection(&crit_sect);
}*/
}
return fState;
}发送数据一直失败!!!!急求大侠们解决下!
是在是问题多了分都给完了!
有兴趣的朋友可加我QQ768170275
帮我弄哈这串口通信!
{
//bool opencom=false;
m_hCom = CreateFile(_T("COM1"),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL); // 重叠方式
if(m_hCom != INVALID_HANDLE_VALUE)
{
SetupComm(m_hCom, MAXBLOCK, MAXBLOCK);
COMMTIMEOUTS TimeOuts;
//设定读超时
TimeOuts.ReadIntervalTimeout=1000;
TimeOuts.ReadTotalTimeoutMultiplier=500;
TimeOuts.ReadTotalTimeoutConstant=5000;
//设定写超时
TimeOuts.WriteTotalTimeoutMultiplier=500;
TimeOuts.WriteTotalTimeoutConstant=2000;
SetCommTimeouts(m_hCom,&TimeOuts); //设置超时 SetCommMask(m_hCom, EV_RXCHAR);
DCB myDCB;
GetCommState( m_hCom, &myDCB);
myDCB.BaudRate=CBR_115200;
myDCB.fBinary=TRUE;
myDCB.fParity=TRUE;
myDCB.ByteSize=8;
myDCB.Parity=ODDPARITY;
myDCB.StopBits=ONESTOPBIT;
SetCommState(m_hCom,&myDCB);
PurgeComm(m_hCom,PURGE_TXCLEAR|PURGE_RXCLEAR); opencom=true;
}
else
{
opencom=false;
}
return opencom;
}bool WinApit232::SendToCom(unsigned char *buffer)
{
bool fState=false;
//unsigned char *buffer;
unsigned long length=16;
BYTE masend[9];
//strncpy(buffer,sendstring.c_str(),sendstring.length());
if (OpenComm())
{
PurgeComm(m_hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
EnterCriticalSection(&crit_sect);
fState=WriteFile(m_hCom,buffer,length,NULL,&m_osWrite);
if (!fState)
{
MessageBox(NULL,L"向串口写入失败",L"失败",NULL);
if(GetLastError()==ERROR_IO_PENDING)
{
MessageBox(NULL,L"写入失败",L"但是非失败",NULL);
WaitForSingleObject(m_osWrite.hEvent,1000);
}
else
{
MessageBox(NULL,L"向串口写入失败",L"失败再次确认",NULL);
}
}
LeaveCriticalSection(&crit_sect);
//strncpy((char *)buf, (const char *)sendstring, 16);
//System.Text.Encoding.Default.GetBytes();//
/*int strlen=sendstring.length();
memcpy(buf, sendstring.c_str(),strlen);
for (int i=0;i<strlen;i++)
{
EnterCriticalSection(&crit_sect);
fState=WriteFile(m_hCom,
&buf[i],
1,
NULL,
&m_osWrite);
LeaveCriticalSection(&crit_sect);
}*/
}
return fState;
}发送数据一直失败!!!!急求大侠们解决下!
是在是问题多了分都给完了!
有兴趣的朋友可加我QQ768170275
帮我弄哈这串口通信!
解决方案 »
- 急急急,在线等
- 问:OnEraseBkgnd函数的问题?
- VPN L2TP的"pre-shared key"如何设置??
- 关于内存的问题
- 对话框句柄?
- 惑矣,求解困惑之路
- 请问各位大侠一个编译的问题,#pragma (pop,1)..............................................
- CAsyncSocket会自动把收到的两个包合成一个,请问有什么办法解决?在线等~谢谢
- 如何改变Button的Caption
- 如何给button等按钮和控件加背景色或者图片?还有dialog如何增加背景色和图片背景,请帮忙?感谢!!
- C Socket封装成COM 方便调用
- sprintf组合字符串,哪儿错了?
DWORD dwBytesWritten;
WriteFile(m_hCom,buffer,length,&dwBytesWritten,&m_osWrite);
DWORD dwBytesWritten;
WriteFile(m_hCom,buffer,length,&dwBytesWritten,&m_osWrite);
{
DCB PortDCB;
DWORD dwError;
PortDCB.DCBlength = sizeof (DCB);
//得到端口的默认设置信息
GetCommState (CPublic::hPort, &PortDCB);
//改变DCB结构设置
PortDCB.BaudRate =9600; //波特率
PortDCB.fBinary = TRUE; //Win32不支持非二进制串行传输模式,必须为TRUE
PortDCB.fParity = TRUE; //启用奇偶校验
PortDCB.fOutxCtsFlow = FALSE; //关闭串行端口CTS线控制
PortDCB.fOutxDsrFlow = FALSE; //关闭串行端口的DSR流控制
PortDCB.fDtrControl = DTR_CONTROL_ENABLE; //启用DTR线
PortDCB.fDsrSensitivity = FALSE; //如果设为TRUE将忽略任何输入的字节,除非DSR线被启用
//PortDCB.fTXContinueOnXoff = TRUE; //当为TRUE时,如果接收缓冲区已满且驱动程序已传送XOFF字符,将使驱动程序停止传输字符
PortDCB.fTXContinueOnXoff = FALSE;
PortDCB.fOutX = FALSE; //设为TRUE指定XON/XOFF控制被用于控制串行输出
PortDCB.fInX = FALSE; //设为TRUE指定XON/XOFF控制被用于控制串行输入
PortDCB.fErrorChar = FALSE; //WINCE串行驱动程序的默认执行将忽略这个字段
PortDCB.fNull = FALSE; //设为TRUE将使串行驱动程序忽略收到的空字节
PortDCB.fRtsControl = RTS_CONTROL_ENABLE; //启用RTS线
PortDCB.fAbortOnError = FALSE; //WINCE串行驱动程序的默认执行将忽略这个字段
PortDCB.ByteSize = 8; //每字节的位数
PortDCB.Parity = NOPARITY; //无奇偶校验
PortDCB.StopBits = ONESTOPBIT; //每字节一位停止位
//根据DCB结构配置端口
if (!SetCommState (CPublic::hPort, &PortDCB))
{
//不能配置串行端口
MessageBox (NULL, TEXT("Unable to configure the serial port"),TEXT("Error"), MB_OK);
dwError = GetLastError ();
return FALSE;
}
return TRUE;
}BOOL CSerial::InitCommTimeouts()
{
COMMTIMEOUTS CommTimeouts;
DWORD dwError;
//得到超时参数
GetCommTimeouts (CPublic::hPort, &CommTimeouts);
//改变COMMTIMEOUTS结构设置
CommTimeouts.ReadIntervalTimeout = 100;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
//CommTimeouts.WriteTotalTimeoutMultiplier = 10;
//CommTimeouts.WriteTotalTimeoutConstant = 1000;
CommTimeouts.WriteTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 0;
//设置端口超时值
if (!SetCommTimeouts (CPublic::hPort, &CommTimeouts))
{
//不能设置超时值
MessageBox (NULL, TEXT("Unable to set the time-out parameters"),TEXT("Error"), MB_OK);
dwError = GetLastError ();
return FALSE;
}
GetCommTimeouts(CPublic::hPort,&CommTimeouts);
return TRUE;
}
BOOL CSerial::OpenPort(LPTSTR lpszPortName)
{
DWORD dwError,dwThreadID;
// h_mainwin=mainwin;
if(CPublic::hPort)
{
return FALSE;
}
//打开串口
CPublic::hPort = CreateFile (lpszPortName, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING,0,NULL);
//如果打开端口出错, 返回FALSE
if ( CPublic::hPort == INVALID_HANDLE_VALUE )
{
//不能打开端口
CString strError;
strError.Format(_T("Unable to open %s, Error No.=%d"),lpszPortName, GetLastError());
MessageBox (NULL, strError,TEXT("Error"), MB_OK);
return FALSE;
}
//指定端口监测的事件集
SetCommMask (CPublic::hPort, EV_RXCHAR);
//分配设备缓冲区
SetupComm(CPublic::hPort,4096,4096);
//初始化缓冲区中的信息
PurgeComm(CPublic::hPort,PURGE_TXCLEAR|PURGE_RXCLEAR);
//配置串行端口
if(!InitDCB())
return FALSE;
//设置端口超时值
if(!InitCommTimeouts())
return FALSE;
//设置端口上指定信号的状态
// SETDTR: 发送DTR (data-terminal-ready)信号
// SETRTS: 发送RTS (request-to-send)信号
EscapeCommFunction (CPublic::hPort, SETDTR);
EscapeCommFunction (CPublic::hPort, SETRTS);
//创建一个从串口读取数据的线程
if (hReadThread = CreateThread (NULL,0,ReadPortThread, 0, 0,&dwThreadID))
{
}
else
{
MessageBox (NULL, TEXT("Unable to create the read thread"),TEXT("Error"), MB_OK);
dwError = GetLastError ();
return FALSE;
}
m_bConnected=TRUE;
return TRUE;
}
BOOL CSerial::WritePort(char * buf,int writenum)
{
BOOL fWriteState;
int i=0;
DWORD cnt=0;
LPDWORD lp_cnt=&cnt;
//写入数据
//Sleep(20);
fWriteState=WriteFile(CPublic::hPort,buf,writenum,lp_cnt,NULL);//CPublic::hPort为串口句柄
Sleep(20);
if(!fWriteState)
{
//不能写数据
MessageBox(NULL,TEXT("Can't Write String to Comm"),TEXT("Error"),MB_OK);
}
// else AfxMessageBox(_T("Write succeed"));
return TRUE;
}