用VC6.0实现上位机串口通信

article/2025/9/19 23:36:24

串口是常用的计算机与外部串行设备之间的数据传输通道,由于串行通信方便易行,所以应用广泛。我们可以利用Windows API 提供的通信函数编写出高可移植性的串行通信程序。本实例介绍在Visual C++6.0下如何利用Win32 API 实现串行通信程序。程序编译运行后的界面效果如图一所示: 
 


图一、串口通信示例程序


  一、实现方法

  在Win16中,可以利用OpenComm()、CloseComm()和WriteComm()等函数打开、关闭和读写串口。但在Win32中,串口和其他通信设备均被作为文件处理,串口的打开、关闭和读写等操作所用的API函数与操作文件的函数相同。可通过CreateFile()函数打开串口;通过CloseFile()函数关闭串口;通过DCB结构、CommProp()、GetCommProperties()、SetCommProperties()、GetCommState()及SetCommState()等函数设置串口状态,通过函数ReadFile()和WritFile()等函数读写串口。下面来详细介绍其实现原理。 

  对于串行通信设备,Win32 API支持同步和异步两种I/O操作。同步操作方式的程序设计相对比较简单,但I/O操作函数在I/O操作结束前不能返回,这将挂起调用线程,直到I/O操作结束。异步操作方式相对要复杂一些,但它可让耗时的I/O操作在后台进行,不会挂起调用线程,这在大数据量通信的情况下对改善调用线程的响应速度是相当有效的。异步操作方式特别适合同时对多个串行设备进行I/O操作和同时对一个串行设备进行读/写操作。

  串行设备的初始化 

  串行设备的初始化是利用CreateFile()函数实现的。该函数获得串行设备句柄并对其进行通信参数设置,包括设置输出/接收缓冲区大小、超时控制和事件监视等。 例如下面的代码实现了串口的初始化:

复制代码

//串行设备句柄; HANDLE hComDev=0; //串口打开标志; BOOL bOpen=FALSE; //线程同步事件句柄; HANDLE hEvent=0; 
DCB dcb; 
COMMTIMEOUTS timeouts; 
//设备已打开 
if(bOpen) return FALSE;  
//打开COM1 
if((hComDev=CreateFile(“COM1”,GENERICREAD|GENERICWRITE,0,NULL,OPENEXISTING,FILEATTRIBUTENORMAL,NULL))==INVALIDHANDLEVALUE) return FALSE; 
//设置超时控制 
SetCommTimeouts(hComDev,&timeouts); 
//设置接收缓冲区和输出缓冲区的大小 
SetupComm(hComDev,1024,512); 
//获取缺省的DCB结构的值 
GetCommState(hComDev,&dcb); 
//设定波特率为9600 bps dcb.BaudRate=CBR9600; //设定无奇偶校验 dcb.fParity=NOPARITY; //设定数据位为8 dcb.ByteSize=8; //设定一个停止位 dcb.StopBits=ONESTOPBIT; //监视串口的错误和接收到字符两种事件 SetCommMask(hComDev,EVERR|EVRXCHAR); //设置串行设备控制参数 SetCommState(hComDev,&dcb); //设备已打开 bOpen=TRUE; //创建人工重设、未发信号的事件 hEvent=CreateEvent(NULL,FALSE,FALSE, “WatchEvent”); //创建一个事件监视线程来监视串口事件 AfxBeginThread(CommWatchProc,pParam); }

复制代码

 

  在设置串口DCB结构的参数时,不必设置每一个值。首先读出DCB缺省的参数设置,然后只修改必要的参数,其他参数都取缺省值。由于对串口进行的是同步I/O操作,所以除非指定进行监测的事件发生,否则WaitCommEvent()函数不会返回。在串行设备初始化的最后要建立一个单独的监视线程来监视串口事件,以免挂起当前调用线程,其中pParam可以是一个对事件进行处理的窗口类指针。 
 
  如果要进行异步I/O操作,打开设备句柄时,CreateFile的第6个参数应增加FILEFLAGOVERLAPPED 标志。 

  数据发送 

  数据发送利用WriteFile()函数实现。对于同步I/O操作,它的最后一个参数可为NULL;而对异步I/O操作,它的最后一个参数必需是一个指向OVERLAPPED结构的指针,通过OVERLAPPED结构来获得当前的操作状态。 

复制代码

BOOL WriteComm(LPCVOID lpSndBuffer,DWORD dwBytesToWrite) {//lpSndBuffer为发送数据缓冲区指针, dwBytesToWrite为将要发送的字节长度 //设备已打开 BOOL bWriteState; //实际发送的字节数 DWORD dwBytesWritten; //设备未打开 if(!bOpen) return FALSE; bWriteState=WriteFile(hComDev,lpSndBuffer,dwBytesToWrite,&dwBytesWritten,NULL);if(!bWriteState || dwBytesToWrite!=dwBytesWritten) //发送失败 return FALSE; else //发送成功 return TRUE; 
}

复制代码

 


  数据接收 

  接收数据的任务由ReadFile函数完成。该函数从串口接收缓冲区中读取数据,读取数据前,先用ClearCommError函数获得接收缓冲区中的字节数。接收数据时,同步和异步读取的差别同发送数据是一样的。 

复制代码

DWORD ReadComm(LPVOID lpInBuffer,DWORD dwBytesToRead) {//lpInBuffer为接收数据的缓冲区指针, dwBytesToRead为准备读取的数据长度(字节数) //串行设备状态结构 COMSTAT ComStat; DWORD dwBytesRead,dwErrorFlags;  //设备未打开 if(!bOpen) return 0; //读取串行设备的当前状态 ClearCommError(hComDev,&dwErrorFlags,&ComStat); //应该读取的数据长度 dwBytesRead=min(dwBytesToRead,ComStat.cbInQue); if(dwBytesRead>0) //读取数据 if(!ReadFile(hComDev,lpInBuffer,dwBytesRead,&dwBytesRead,NULL)) dwBytesRead=0; return dwBytesRead; 
}

复制代码

 


  事件监视线程 

  事件监视线程对串口事件进行监视,当监视的事件发生时,监视线程可将这个事件发送(SendMessage)或登记(PostMessage)到对事件进行处理的窗口类(由pParam指定)中。 

复制代码

UINT CommWatchProc(LPVOID pParam) 
{DWORD dwEventMask=0; //发生的事件; while(bOpen) {//等待监视的事件发生 WaitCommEvent(hComDev, &dwEventMask,NULL); if ((dwEventMask & EVRXCHAR)==EVRXCHAR) ……//接收到字符事件后,可以将此消息登记到由pParam有指定的窗口类中进行处理 if(dwEventMask & EVERR)==EVERROR) ……//发生错误时的处理 } SetEvent(hEvent); //发信号,指示监视线程结束 return 0; 
}

复制代码

 

  关闭串行设备 

  在整个应用程序结束或不再使用串行设备时,应将串行设备关闭,包括取消事件监视,将设备打开标志bOpen置为FALSE以使事件监视线程结束,清除发送/接收缓冲区和关闭设备句柄。 

复制代码

void CloseSynComm() 
{ if(!bOpen) return; //结束事件监视线程 bOpen=FALSE; SetCommMask(hComDev,0); //取消事件监视,此时监视线程中的WaitCommEvent将返回 WaitForSingleObject(hEvent,INFINITE); //等待监视线程结束 CloseHandle(hEvent); //关闭事件句柄 //停止发送和接收数据,并清除发送和接收缓冲区 PurgeComm(hComDev,PURGETXABORT| PURGERXABORT|PURGETXCLEAR|PURGERXCLEAR); //关闭设备句柄 CloseHandle(hComDev); 
}

复制代码

 


  二、编程步骤

  1、 启动Visual C++6.0,生成一个基于对话框的的应用程序,将该程序命名为“SerealCom”;

  2、 按照图一的界面设计对话框,具体设置参见代码部分;

  3、 使用Class Wizard为对话框的按钮添加鼠标单击消息响应函数;

  4、 添加代码,编译运行程序。

复制代码

  1 #if !defined(_COMM_ACCESS_FUNCTIONS_AND_DATA)2 #define _COMM_ACCESS_FUNCTIONS_AND_DATA3 #if _MSC_VER > 10004 #pragma once5 #endif // _MSC_VER > 10006 #define EVENTCHAR 0x0d7 #define MAXBLOCKLENGTH 598 9 extern BYTE XwCom;10 extern BYTE sCom1[5],sCom2[MAXBLOCKLENGTH+12];11 extern sCom3[MAXBLOCKLENGTH+12];12 extern BYTE opation;13 extern short ComNum;14 15 #define FC_DTRDSR 0x0116 #define FC_RTSCTS 0x0217 #define FC_XONXOFF 0x0418 #define ASCII_BEL 0x0719 #define ASCII_BS 0x0820 #define ASCII_LF 0x0A21 #define ASCII_CR 0x0D22 #define ASCII_XON 0x1123 #define ASCII_XOFF 0x1324 25 class CComStatus26 {27  public:28   HANDLE m_hCom;29   BYTE m_bComId;30   BYTE m_bByteSize;31   BYTE m_bStopBits;32   BYTE m_bParity;33   DWORD m_dwBaudRate;34 35   //WORD m_fChEvt;36 37   char m_bEvtChar;38   DWORD m_fBinary;39   BOOL m_bConnected;40   BOOL m_fXonXoff;41   BOOL m_bFlowCtrl;42   OVERLAPPED m_rdos;43   OVERLAPPED m_wtos;44 45   //functions46 47   CComStatus();48   CComStatus(BYTE bComId,BYTE bByteSize,BYTE bStopBits,BYTE bParity,49     DWORD dwBaudRate,/*WORD fChEvt,*/char bEvtChar,DWORD fBinary);50   BOOL OpenConnection();51   BOOL CloseConnection();52   BOOL SetupConnection();53   BOOL IsConnected();54 };55 56 UINT CommWatchProc( LPVOID lpData );57 BOOL WriteCommBlock( CComStatus& comDev, LPSTR lpByte , DWORD dwBytesToWrite);58 int ReadCommBlock(CComStatus& comDev,LPSTR lpszBlock, int nMaxLength );59 int ReadCommBlockEx(CComStatus& comDev,LPSTR lpszBlock, int nMaxLength,DWORD dwTimeOut);60 #endif61 62 ///63 64 #include "stdafx.h"65 #include "com232.h"66 67 BYTE XwCom=0x40;68 BYTE sCom1[5],sCom2[MAXBLOCKLENGTH+12],sCom3[MAXBLOCKLENGTH+12];69 BYTE opation;70 short ComNum;71 CComStatus::CComStatus()72 {73  m_hCom = NULL;74  m_bComId = (char)ComNum;//COM175  m_bByteSize=8;76  m_bStopBits=ONESTOPBIT;77  m_bParity=NOPARITY;78  m_dwBaudRate=9600;79  m_bEvtChar=EVENTCHAR;80  m_fBinary=1;81  m_bConnected = FALSE;82  m_bFlowCtrl = FC_XONXOFF ;83  m_fXonXoff = FALSE;84 }85 86 CComStatus::CComStatus(BYTE bComId,BYTE bByteSize,BYTE bStopBits,BYTE bParity,DWORD dwBaudRate,/*WORD fChEvt,*/char bEvtChar,DWORD fBinary)87 {88  m_hCom = NULL;89  m_bComId = bComId;90  m_bByteSize=bByteSize;91  m_bStopBits=bStopBits;92  m_bParity=bParity;93  m_dwBaudRate=dwBaudRate;94  m_bEvtChar=bEvtChar;95  m_fBinary=fBinary;96  m_bConnected = FALSE;97  m_bFlowCtrl = FC_XONXOFF ;98  m_fXonXoff = FALSE;99 }
100 
101 BOOL CComStatus::OpenConnection()
102 {
103  char csCom[10];
104  COMMTIMEOUTS CommTimeOuts ;
105  if((m_bComId < 0) || (m_bComId > 4))
106   return FALSE;//从COM1到COM4
107  if(m_hCom)//if already open
108  return FALSE;
109 
110  //OVERLAPPED包含异步I/O信息
111 
112  m_rdos.Offset = 0;
113  m_rdos.OffsetHigh = 0;
114  m_rdos.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
115  if(m_rdos.hEvent == NULL)
116   return FALSE;
117  m_wtos.Offset = 0;
118  m_wtos.OffsetHigh = 0;
119  m_wtos.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
120  if(m_wtos.hEvent == NULL)
121  {
122   CloseHandle(m_rdos.hEvent);
123   return FALSE;
124  }
125 
126  wsprintf(csCom,"COM%d",m_bComId);
127 
128  m_hCom = CreateFile(csCom,GENERIC_READ | GENERIC_WRITE, 0,NULL, OPEN_EXISTING,ILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,NULL);
129 
130  if(m_hCom == INVALID_HANDLE_VALUE) {
131   //dwError = GetLastError();
132   // handle error 
133   return FALSE;
134  }
135  else
136  {
137   SetCommMask( m_hCom, EV_RXCHAR ) ; // get any early notifications
138   SetupComm( m_hCom, 4096, 4096 ) ; // setup device buffers
139   // purge any information in the buffer
140 
141   PurgeComm( m_hCom, PURGE_TXABORT | PURGE_RXABORT |PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
142 
143   // set up for overlapped I/O
144 
145   DWORD dwTemp = 1000 / (this->m_dwBaudRate / 8);
146   CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF ;
147   CommTimeOuts.ReadTotalTimeoutMultiplier = 0;//((dwTemp > 0) ? dwTemp : 1);
148   CommTimeOuts.ReadTotalTimeoutConstant = 1000 ;
149 
150   // CBR_9600 is approximately 1byte/ms. For our purposes, allow
151   // double the expected time per character for a fudge factor.
152 
153   CommTimeOuts.WriteTotalTimeoutMultiplier =2*CBR_9600/this->m_dwBaudRate;//( npTTYInfo ) ;
154   CommTimeOuts.WriteTotalTimeoutConstant = 0;//1000 ;
155 
156   SetCommTimeouts( m_hCom, &CommTimeOuts ) ;
157  }
158  if(!SetupConnection())
159  {
160   CloseConnection();
161   return FALSE;
162  }
163  EscapeCommFunction( m_hCom, SETDTR );
164  m_bConnected = TRUE;
165  return TRUE;
166 }
167 
168 BOOL CComStatus::CloseConnection()
169 {
170  if (NULL == m_hCom)
171   return ( TRUE ) ;
172  // set connected flag to FALSE
173  m_bConnected = FALSE;
174  // disable event notification and wait for thread
175  // to halt
176  SetCommMask( m_hCom, 0 ) ;
177  EscapeCommFunction( m_hCom, CLRDTR ) ;
178  // purge any outstanding reads/writes and close device handle
179  PurgeComm( m_hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
180  CloseHandle( m_hCom ) ;
181  m_hCom = NULL;
182 
183  // change the selectable items in the menu
184 
185  CloseHandle(m_rdos.hEvent);
186  CloseHandle(m_wtos.hEvent);
187  return ( TRUE ) ;
188 }
189 
190 BOOL CComStatus::SetupConnection()
191 {
192  BOOL fRetVal ;
193  BYTE bSet ;
194  DCB dcb ;
195  if(m_hCom == NULL)
196   return FALSE; 
197  dcb.DCBlength = sizeof( DCB ) ;
198  GetCommState( m_hCom, &dcb ) ;
199  dcb.BaudRate = this->m_dwBaudRate;
200  dcb.ByteSize = this->m_bByteSize;
201  dcb.Parity = this->m_bParity;
202  dcb.StopBits = this->m_bStopBits ;
203  dcb.EvtChar = this->m_bEvtChar ;
204  // setup hardware flow control
205  bSet = (BYTE) ((m_bFlowCtrl & FC_DTRDSR) != 0) ;
206  dcb.fOutxDsrFlow = bSet ;
207  if (bSet)
208   dcb.fDtrControl = DTR_CONTROL_HANDSHAKE ;
209  else
210   dcb.fDtrControl = DTR_CONTROL_ENABLE ;
211  bSet = (BYTE) ((m_bFlowCtrl & FC_RTSCTS) != 0) ;
212  dcb.fOutxCtsFlow = bSet ;
213  if (bSet)
214   dcb.fRtsControl = RTS_CONTROL_HANDSHAKE ;
215  else
216   dcb.fRtsControl = RTS_CONTROL_ENABLE ;
217  // setup software flow control
218  bSet = (BYTE) ((m_bFlowCtrl & FC_XONXOFF) != 0) ;
219  dcb.fInX = dcb.fOutX = bSet ;
220  dcb.XonChar = ASCII_XON ;
221  char xon = ASCII_XON ;
222  dcb.XoffChar = ASCII_XOFF ;
223  char xoff = ASCII_XOFF ;
224  dcb.XonLim = 100 ;
225  dcb.XoffLim = 100 ;
226  // other various settings
227  dcb.fBinary = TRUE ;
228  dcb.fParity = TRUE ;
229  fRetVal = SetCommState( m_hCom, &dcb ) ;
230  return ( fRetVal ) ;
231 } // end of SetupConnection()
232 
233 BOOL CComStatus::IsConnected()
234 {
235  return m_bConnected;
236 }
237 
238 UINT CommWatchProc( LPVOID lpData )
239 {
240  DWORD dwEvtMask ;
241  //NPTTYINFO npTTYInfo = (NPTTYINFO) lpData ;
242  OVERLAPPED os ;
243  int nLength ;
244  //BYTE abIn[ MAXBLOCK + 1] ;
245 
246  CComStatus * pCom = (CComStatus *)lpData;
247  memset( &os, 0, sizeof( OVERLAPPED ) ) ;
248  // create I/O event used for overlapped read
249 
250  os.hEvent = CreateEvent( NULL, // no security
251   TRUE, // explicit reset req
252   FALSE, // initial event reset
253   NULL ) ; // no name
254 
255  if (os.hEvent == NULL)
256  {
257   MessageBox( NULL, "Failed to create event for thread!", "TTY Error!",MB_ICONEXCLAMATION | MB_OK ) ;
258   return ( FALSE ) ;
259  }
260  if (!SetCommMask( pCom->m_hCom, EV_RXCHAR ))
261   return ( FALSE ) ;
262  char buf[256];
263  while ( pCom->m_bConnected )
264  {
265   dwEvtMask = 0 ;
266   WaitCommEvent( pCom->m_hCom, &dwEvtMask, NULL );
267   if ((dwEvtMask & EV_RXCHAR) == EV_RXCHAR)
268   {
269    if ((nLength = ReadCommBlock( *pCom, (LPSTR) buf, 255 )))
270    {
271     //WriteTTYBlock( hTTYWnd, (LPSTR) abIn, nLength ) ;
272     buf[nLength]='\0';
273     AfxMessageBox(buf);
274    }
275   }
276  }
277  CloseHandle( os.hEvent ) ;
278  return( TRUE ) ;
279 } // end of CommWatchProc()
280 
281 int ReadCommBlock(CComStatus& comDev,LPSTR lpszBlock, int nMaxLength )
282 {
283  BOOL fReadStat ;
284  COMSTAT ComStat ;
285  DWORD dwErrorFlags;
286  DWORD dwLength;
287  DWORD dwError;
288 
289  char szError[ 10 ] ;
290 
291  // only try to read number of bytes in queue
292 
293  ClearCommError( comDev.m_hCom, &dwErrorFlags, &ComStat ) ;
294  dwLength = min( (DWORD) nMaxLength, ComStat.cbInQue ) ;
295 
296  if (dwLength > 0)
297  {
298   fReadStat = ReadFile( comDev.m_hCom, lpszBlock,dwLength, &dwLength, &(comDev.m_rdos) ) ;
299   if (!fReadStat)
300   {
301    if (GetLastError() == ERROR_IO_PENDING)
302    {
303     OutputDebugString("\n\rIO Pending");
304     while(!GetOverlappedResult( comDev.m_hCom ,&(comDev.m_rdos), &dwLength, TRUE ))
305     {
306      dwError = GetLastError();
307      if(dwError == ERROR_IO_INCOMPLETE)
308       // normal result if not finished
309       continue;
310      else
311      {
312       // an error occurred, try to recover
313       wsprintf( szError, "<CE-%u>", dwError ) ;
314       ClearCommError( comDev.m_hCom , &dwErrorFlags, &ComStat ) ;
315       break;
316      }
317     }
318    }
319    else
320    {
321     // some other error occurred
322     dwLength = 0 ;
323     ClearCommError( comDev.m_hCom , &dwErrorFlags, &ComStat ) ;
324    }
325   }
326  }
327  return ( dwLength ) ;
328 } // end of ReadCommBlock()
329 
330 int ReadCommBlockEx(CComStatus& comDev,LPSTR lpszBlock, int nMaxLength,DWORD dwTimeOut)
331 {
332  LPSTR lpOffset=lpszBlock;
333  int nReadCount = 0;
334  char chBuf;
335  //time_t beginTime,endTime;
336  if(!comDev.m_hCom)
337   return 0;
338  if(dwTimeOut <= 0)
339   return 0;
340  MSG msg;
341  //time(&beginTime);
342  DWORD dwLastTick,dwNowTick,dwGoneTime;
343  dwGoneTime = 0;
344  dwLastTick = GetTickCount();
345  dwNowTick = dwLastTick;
346  // double diftime;
347  do
348  {
349   if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
350   {
351    ::TranslateMessage(&msg);
352    ::DispatchMessage(&msg);
353   }
354   if(ReadCommBlock(comDev,&chBuf,1) > 0)
355   {
356    //TRACE("----get a char----\n");
357    *lpOffset = chBuf;
358    lpOffset ++;
359    nReadCount ++;
360   }
361   dwNowTick = GetTickCount();
362   if(dwNowTick < dwLastTick)
363   {
364    dwLastTick = dwNowTick;
365   }
366 
367   dwGoneTime = dwNowTick - dwLastTick;
368 
369   //TRACE("gon time = %lu\n",dwGoneTime);
370 
371  }while((nReadCount < nMaxLength) && (dwGoneTime < dwTimeOut));
372  return (nReadCount);
373 }//end ReadCommBlockEx
374 
375 BOOL WriteCommBlock( CComStatus& comDev, LPSTR lpByte , DWORD dwBytesToWrite)
376 {
377  BOOL fWriteStat ;
378  DWORD dwBytesWritten ;
379  DWORD dwErrorFlags;
380  DWORD dwError;
381  DWORD dwBytesSent=0;
382  COMSTAT ComStat;
383 
384  char szError[ 128 ] ;
385 
386  fWriteStat = WriteFile( comDev.m_hCom , lpByte, dwBytesToWrite,&dwBytesWritten, &( comDev.m_wtos) ) ;
387  if (!fWriteStat)
388  {
389   if(GetLastError() == ERROR_IO_PENDING)
390   {
391    while(!GetOverlappedResult( comDev.m_hCom,&(comDev.m_wtos), &dwBytesWritten, TRUE ))
392    {
393     dwError = GetLastError();
394     if(dwError == ERROR_IO_INCOMPLETE)
395     {
396      // normal result if not finished
397      dwBytesSent += dwBytesWritten;
398      continue;
399     }
400     else
401     {
402      // an error occurred, try to recover
403      wsprintf( szError, "<CE-%u>", dwError ) ;
404      ClearCommError( comDev.m_hCom, &dwErrorFlags, &ComStat ) ;
405      break;
406     }
407    }
408    dwBytesSent += dwBytesWritten;
409    if( dwBytesSent != dwBytesToWrite )
410     wsprintf(szError,"\nProbable Write Timeout: Total of %ld bytes sent", dwBytesSent);
411    else
412     wsprintf(szError,"\n%ld bytes written", dwBytesSent);
413    OutputDebugString(szError);
414   }
415   else
416   {
417    // some other error occurred
418    ClearCommError( comDev.m_hCom, &dwErrorFlags, &ComStat ) ;
419    return ( FALSE );
420   }
421  }
422  return ( TRUE ) ;
423 } // end of WriteCommBlock()

复制代码

四、小结 

  以上给出了用Win32 API设计串行通信的基本思路,在实际应用中,我们可以利用Win32 API设计出满足各种需要的串行通信程序。

欢迎各位电子爱好者转载。

PS原文出自http://soft.yesky.com/50/2214050_2.shtml


http://chatgpt.dhexx.cn/article/1JmfyKbm.shtml

相关文章

数据库连接异常: HikariPool-1 - Connection is not available, request timed out after 30000ms.

记一次生产环境数据库连接数导致的报错问题:Failed to obtain JDBC Connection; nested exception is java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30000ms. 1. 复现&#xff0c;定时任务失败会有错误邮件…

Could not create connection to database server. Attempted reconnect 3 times. Giving up.

Nacos集群启动一直报错 查看Nacos启动日志 Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.【解决办法】 1、在Nacos目录下创建plugins/mysql…

解决Hbase连接hdfs失败java.net.ConnectException: Connection refused

昨天hbase安装好之后一直连接不到hdfs上&#xff0c;十分费解。 错误如下&#xff1a; 2021-04-15 07:04:32,844 WARN [master:16000.activeMasterManager] ipc.Client: Failed to connect to server: master/192.168.110.129:9000: try once and fail. java.net.ConnectExc…

hbase1.2.1配置kerberos

今天需要在hbase上配置kerberos认证&#xff0c;所以需要安装kerberos&#xff0c;安装配置过程如下&#xff1a; kerberos简介 kerberos简单来说就是一套完全控制机制&#xff0c;它有一个中心服务器&#xff08;KDC&#xff09;&#xff0c;KDC中有数据库&#xff0c;你可以往…

setup maven plugin connection

setup maven plugin connection discover and map eclipse plugins to maven plugin goal executions 今天在创建maven工程时遇到了一个问题,工程在创建后有如图的报错,而且工程在创建后的也有很大的不同,丢失了很多的文件,问了下度娘居然没有相关的解决方法,所以在自己解决后…

Android Q Data Setup for Short Connection

直接上流程图 可参考之前的博客&#xff1a;Android N Data Setup & Disconnect For Short Connection 转载请注明出处。

Android Q Data Setup For Long Connection

直接上流程图 可参考之前的博客&#xff1a;Android N Data Setup For Long Connection 转载请注明出处。

hadoop集群安装配置Kerberos(三):hadoop集群配置 kerberos 认证

目录 前言 一、配置 SASL 认证证书 二、修改集群配置文件 1.hdfs添加以下配置 2.yarn添加以下配置 3.hive添加以下配置 4.hbase添加以下配置 三、kerberos相关命令 四、快速测试 五、问题解决 1、Caused by: java.io.IOException: Failed on local exception: java.…

IDEA连接kerberos环境的HDFS集群报错整理

连接hdfs代码 public class HdfsTest {public static void main(String[] args) throws IOException {System.setProperty("java.security.krb5.conf", "hdfs-conf-kerberos\\krb5.conf");Configuration conf new Configuration();conf.set("fs.def…

Failed to create/setup connection: 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。

情况描述&#xff1a;用hutool的DB工具测试连接SQL server数据库。结果返回异常。 尝试网上的一些方法&#xff0c;但均未成功。 如&#xff1a; 1、更换JDK&#xff0c;这是不可能的。现在用的是1.8 2、jre/lib/ext目录增加bcprov-ext-jdk15on-1.54.jar和bcprov-jdk15on-1…

当刷机工具遇到SetupConnection时的解决方法

当刷机工具遇到SetupConnection时&#xff0c;解决方法 首先&#xff0c;在此贴上原文地址&#xff0c;已表感激之情。 http://blog.sina.com.cn/s/blog_636fd7d90101drak.html http://bbs.gfan.com/android-4032375-1-1.html 据说程序员是比较挑剔的&#xff0c;骨子里难…

电子元件二极管封装SMA,SMB,SMC的区别

某个电路使用二极管&#xff08;典型的如肖特基二极管SS14 SS24 SS34&#xff09;&#xff0c;发现居然有三个规格&#xff0c;SMA, SMB, SMC&#xff0c; 找了一下其区别&#xff0c;记录如下&#xff0c; 从下面图片的来看&#xff0c;可以看出主要体积上不同&#xff1a;S…

Windows Server之浅谈SMB以及SMB小案例分享

Windows Server之浅谈SMB以及SMB小案例分享 gs_h关注4人评论89230人阅读2017-01-23 14:45:45 SMB由来 服务器消息区块&#xff08;英语&#xff1a;Server Message Block&#xff0c;缩写为SMB&#xff0c;服务器消息区块&#xff09;&#xff0c;又称网络文件共享系统&#x…

linux——SMB文件共享及应用实例

SMB文件共享 Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件&#xff0c;由服务器及客户端程序构成。SMB(Server Messages Block&#xff0c;信息服务块)是一种在局域网上共享文件和打印机的一种通信协议&#xff0c;它为局域网内的不同计算机之间提供文件及打印机等资源…

Linux-smb服务器搭建

Linux-smb服务器搭建 wget安装 rpm源获取地址&#xff1a;https://mirrors.163.com/centos/7.9.2009/os/x86_64/Packages/ 阿里云Yum源配置 1.可以移除默认的yum仓库&#xff0c;也就是删除 /etc/yum.repos.d/底下所有的.repo文件&#xff08;踢出国外的yum源&#xff09; 2…

Windows搭建SMB服务

Windows搭建SMB服务 本文介绍在windows本地环境上搭建SMB服务实现文件共享 配置服务 在本地机上以Windows10举例 &#xff1a;在控制面板 -->程序–>程序和功能–>启用或关闭Windows功能–>SMB 1.0/cifs file sharing support勾选SMB 1.0/CIFS Client和SMB 1.0/CI…

windows中的文件共享(SMB服务)

目录 SMB 如何进行文件共享 默认共享 默认共享盘符挂载 关闭默认共享 关闭共享服务 net sahre 查看共享 net share /del 共享名 删除共享 \\ip地址 访问共享 \\ip地址\c$ …

win10 smb共享硬盘

如果一台电脑里存放了电影&#xff0c;其它设备想要播放&#xff0c;可以通过远程看&#xff08;会卡&#xff09;、ftp下载后看&#xff08;麻烦&#xff09;、U盘拷贝&#xff08;麻烦&#xff09; 怎么共享win10的内容给其它设备&#xff0c;像访问本地硬盘似的呢 通过smb共…

Windows电脑SMB共享设置方法

SMB简介&#xff1a; SMB&#xff08;Server Message Block&#xff09;(*nix平台和Win NT4.0又称CIFS)协议是Windows平台标准文件共享协议&#xff0c;Linux平台通过samba来支持。SMB最新版本v3.0&#xff0c;在v2.0基础上针对WAN和分布式有改进。 建议使用原版wind…

Ubuntu搭建SMB服务器,并共享文件夹

一、Ubuntu安装步骤&#xff1a; 1.ubuntu安装samba sudo apt-get install samba 2.创建文件夹/home/share&#xff0c;并修改权限为777 mkdir /home/share chmod 777 /home/share 3.修改Samba配置文件&#xff0c;无账号密码直接访问共享文件夹 sudo vim /etc/samba/smb.co…