本文整理汇总了C++中SetupComm函数的典型用法代码示例。如果您正苦于以下问题:C++ SetupComm函数的具体用法?C++ SetupComm怎么用?C++ SetupComm使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SetupComm函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: SetupComm
HANDLE ControlUnit::OpenComPort(const char *name)
{
HANDLE handle = ::CreateFileA(name, GENERIC_READ|GENERIC_WRITE, 0/*exclusive access*/, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (handle != INVALID_HANDLE_VALUE) {
SetupComm(handle, 2048, 2048);
COMMTIMEOUTS com_time_outs;
com_time_outs.ReadIntervalTimeout = 0xFFFFFFFF;
com_time_outs.ReadTotalTimeoutMultiplier = 0;
com_time_outs.ReadTotalTimeoutConstant = 1000;
com_time_outs.WriteTotalTimeoutMultiplier = 0;
com_time_outs.WriteTotalTimeoutConstant = 1000;
if(!SetCommTimeouts(handle, &com_time_outs)) {
::CloseHandle(handle);
return INVALID_HANDLE_VALUE;
}
DCB com_param;
memset(&com_param, 0x00, sizeof(com_param));
if (!GetCommState(handle, &com_param)) {
::CloseHandle(handle);
return INVALID_HANDLE_VALUE;
}
com_param.BaudRate = CBR_9600;
com_param.ByteSize = 8;
com_param.StopBits = ONESTOPBIT;
com_param.Parity = NOPARITY;
com_param.fAbortOnError = TRUE;
com_param.fDtrControl = DTR_CONTROL_DISABLE;
com_param.fRtsControl = RTS_CONTROL_DISABLE;
com_param.fInX = FALSE;
com_param.fOutX = FALSE;
com_param.XonLim = 128;
com_param.XoffLim = 128;
com_param.fNull = FALSE;
if (!SetCommState(handle, &com_param)) {
::CloseHandle(handle);
return INVALID_HANDLE_VALUE;
}
::PurgeComm(handle, PURGE_RXCLEAR|PURGE_TXABORT);
::CancelIo(handle);
::Sleep(2000); // Без этого не работает. Скорее всего что ардуина не увспевает сконфигурироваться.
return handle;
}
return INVALID_HANDLE_VALUE;
}
示例2: serial_open
serial_t* serial_open(const char *device)
{
serial_t *h = calloc(sizeof(serial_t), 1);
//COMMTIMEOUTS timeouts = {MAXDWORD, MAXDWORD, 3000, 0, 0};
// shorter timeout
COMMTIMEOUTS timeouts = {MAXDWORD, MAXDWORD, 500, 0, 0};
/* Fix the device name if required */
char *devName;
if (strlen(device) > 4 && device[0] != '\\') {
devName = calloc(1, strlen(device) + 5);
sprintf(devName, "\\\\.\\%s", device);
} else {
devName = device;
}
/* Create file handle for port */
h->fd = CreateFile(devName, GENERIC_READ | GENERIC_WRITE,
0, /* Exclusive access */
NULL, /* No security */
OPEN_EXISTING,
0, //FILE_FLAG_OVERLAPPED,
NULL);
if (devName != device)
free(devName);
if(h->fd == INVALID_HANDLE_VALUE)
return NULL;
SetupComm(h->fd, 4096, 4096); /* Set input and output buffer size */
SetCommTimeouts(h->fd, &timeouts);
SetCommMask(h->fd, EV_ERR); /* Notify us of error events */
GetCommState(h->fd, &h->oldtio); /* Retrieve port parameters */
GetCommState(h->fd, &h->newtio); /* Retrieve port parameters */
PurgeComm(h->fd, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);
return h;
}
示例3: openCom
BOOL openCom(int com_n,DWORD baudrate,HANDLE *hdlr)
{
#ifdef _DEBUG
printf("[DEBUG] Connecting to COM%d at %d bauds : " ,com_n,(int)baudrate);
#endif
char com_name[16];
sprintf(com_name, "COM%d", com_n);
*hdlr = CreateFile(com_name, GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(*hdlr == INVALID_HANDLE_VALUE)
{
#ifdef _DEBUG
printf("ERROR - invalid handle (COM%d)\r\n", com_n);
#endif
return FALSE;
}
// size of RX and TX buffers
SetupComm(*hdlr, RX_SIZE, TX_SIZE);
// com port config
g_dcb.BaudRate=baudrate;
if(!SetCommTimeouts(*hdlr, &g_cto) || !SetCommState(*hdlr, &g_dcb))
{
#ifdef _DEBUG
printf("ERROR - setting timeouts (COM%d)\r\n", com_n);
#endif
CloseHandle(*hdlr);
return FALSE;
}
// empty the buffers
PurgeComm(*hdlr, PURGE_TXCLEAR|PURGE_RXCLEAR|PURGE_TXABORT|PURGE_RXABORT);
#ifdef _DEBUG
printf("OK\r\n");
#endif
return TRUE;
}
示例4: ser_open
// Open the serial port
ser_handler ser_open( const char* sername )
{
char portname[ WIN_MAX_PORT_NAME + 1 ];
HANDLE hComm;
portname[ 0 ] = portname[ WIN_MAX_PORT_NAME ] = '\0';
_snprintf( portname, WIN_MAX_PORT_NAME, "\\\\.\\%s", sername );
hComm = CreateFile( portname, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0 );
if( hComm == INVALID_HANDLE_VALUE )
return WIN_ERROR;
if( !SetupComm( hComm, 2048, 2048 ) )
return WIN_ERROR;
if( !FlushFileBuffers( hComm ) ||
!PurgeComm( hComm, PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR ) )
return WIN_ERROR;
return hComm;
}
示例5: CreateFile
bool StimThread::connectCOM(){
//Connect to serial port for stage
hCom = CreateFile(comName.utf16(),GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,0); //Zaber Stage Control
if (hCom == INVALID_HANDLE_VALUE) {
qDebug("Can't Connect to Stim");
return false;
}
//Configure Ports
DCB cfg;
GetCommState(hCom,&cfg);
//cfg.BaudRate = 1200000;
//cfg.BaudRate = 115200; //for magnus' board
cfg.BaudRate = 460800; //Lakshmi's system
//cfg.BaudRate = 230400; //Lakshmi's system
cfg.ByteSize = 8;
cfg.StopBits = ONESTOPBIT;
cfg.Parity = NOPARITY;
cfg.fDtrControl = DTR_CONTROL_DISABLE;
cfg.fRtsControl = RTS_CONTROL_DISABLE;
if(!SetCommState(hCom, &cfg )){
qDebug("Serial SetCommState Error %d - Stim ",GetLastError());
return false;
}
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = 0;
timeouts.ReadTotalTimeoutMultiplier = 100;
timeouts.ReadTotalTimeoutConstant = 100;
timeouts.WriteTotalTimeoutMultiplier = 100;
timeouts.WriteTotalTimeoutConstant = 100;
if (!SetCommTimeouts(hCom,&timeouts)) {
qDebug("SetCommTimeouts Error 1 - Stim");
return false;
}
if (!SetupComm(hCom,2048,2048)){
qDebug("SetupComm Error 1 - Stim");
return false;
}
return true;
};
示例6: OpenConnection
static BOOL OpenConnection(LPCOMMINFO hCom, LPCTSTR lpPortName)
{
COMMTIMEOUTS CommTimeOuts;
BOOL bRet;
if (NULL == hCom)
return (FALSE);
// open communication resource
hCom->hComPort = CreateFile(lpPortName,
GENERIC_READ | GENERIC_WRITE, // access mode
0, // exclusive share
NULL, // security attrs
OPEN_EXISTING,
0, // 0 FILE_ATTRIBUTE_NORMAL
NULL);
if (hCom->hComPort == INVALID_HANDLE_VALUE)
return (FALSE);
// setup device buffers
SetupComm(hCom->hComPort, 4096, 4096);
// purge any information in the buffer
PurgeComm(hCom->hComPort, PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR);
// setup timeouts
// CommTimeOuts.ReadIntervalTimeout = 10;//50;
// CommTimeOuts.ReadTotalTimeoutMultiplier = 10;//5;
// CommTimeOuts.ReadTotalTimeoutConstant = 200;//100;
// CommTimeOuts.WriteTotalTimeoutMultiplier = 10;//5;
// CommTimeOuts.WriteTotalTimeoutConstant = 50;//100;
CommTimeOuts.ReadIntervalTimeout = 3;//50;
CommTimeOuts.ReadTotalTimeoutMultiplier = 3;//5;
CommTimeOuts.ReadTotalTimeoutConstant = 2;//100;
CommTimeOuts.WriteTotalTimeoutMultiplier = 3;//5;
CommTimeOuts.WriteTotalTimeoutConstant = 2;//100;
bRet = SetCommTimeouts(hCom->hComPort, &CommTimeOuts);
return (TRUE);
} // end of OpenConnection()
示例7: port_buffers
static unsigned int
port_buffers(struct rs232_port_t *p, unsigned int rb, unsigned int wb)
{
struct rs232_windows_t *wx = p->pt;
DBG("p=%p p->pt=%p rb=%d wb=%d\n", (void *)p, p->pt, rb, wb);
if (!rs232_port_open(p))
return RS232_ERR_PORT_CLOSED;
if (!SetupComm(wx->fd, rb, wb)) {
DBG("SetupComm() %s\n", last_error());
return RS232_ERR_UNKNOWN;
}
wx->r_buffer = rb;
wx->w_buffer = wb;
return RS232_ERR_NOERROR;
}
示例8: rs232_open
// Open the rs232 port
int rs232_open(char *_port, int baud_rate, int byte_size, int parity, int stop_bits)
{
// Make sure another port is not already opened
ASSERT(!handle_com);
// Open the existing COMX file to open the port
handle_com = CreateFile(
_port,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_SYSTEM,
NULL);
// Make sure it is opened
if (handle_com == INVALID_HANDLE_VALUE)
return 0;
// buffer size for emission and reception
SetupComm(handle_com, RS232_RX_SIZE, RS232_TX_SIZE);
// COM port configuration
g_dcb.BaudRate = baud_rate;
g_dcb.ByteSize = byte_size;
g_dcb.Parity = parity;
g_dcb.StopBits = stop_bits;
if(!SetCommTimeouts(handle_com, &g_cto) || !SetCommState(handle_com, &g_dcb))
{
CloseHandle(handle_com);
return 0;
}
// Flush buffers for emission and reception
// DTR = 1
PurgeComm(handle_com, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT);
EscapeCommFunction(handle_com, SETDTR);
return 1;
}
示例9: serial_init
int serial_init(const char *port, unsigned long baud)
{
char fullPort[20];
sprintf(fullPort, "\\\\.\\%s", port);
hSerial = CreateFile(
fullPort,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hSerial == INVALID_HANDLE_VALUE)
return FALSE;
/* Set the baud rate. Always succeeds with mingw. */
if (!serial_baud(baud)) {
serial_done();
return 0;
}
GetCommTimeouts(hSerial, &original_timeouts);
timeouts = original_timeouts;
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
timeouts.WriteTotalTimeoutConstant = 1;
timeouts.WriteTotalTimeoutMultiplier = 1;
/* setup device buffers */
SetupComm(hSerial, 10000, 10000);
/* purge any information in the buffer */
PurgeComm(hSerial, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
return TRUE;
}
示例10: ms
/////////////////////////////////////////////////////////////////////////////////
// 函数:OpenPort //
// 说明:打开指定的串口 //
// 参数: //
// pPort: 串口名称或设备路径,可用"COM1"或"\\.\COM1"两种方式,建议用后者 //
// nBaudRate: 波特率 //
// nParity: 奇偶校验 //
// nByteSize: 数据字节宽度 //
// nStopBits: 停止位 //
// 返回值:打开的串口句柄 //
/////////////////////////////////////////////////////////////////////////////////
HANDLE CSerialPort::OpenPort(const char* pPort, int nBaudRate,
int nParity, int nByteSize, int nStopBits)
{
HANDLE hPort;
DCB dcb; // 串口控制块
COMMTIMEOUTS timeouts = { // 串口超时控制参数
100, // 读字符间隔超时时间: 100 ms
1, // 读操作时每字符的时间: 1 ms (n个字符总共为n ms)
500, // 基本的(额外的)读超时时间: 500 ms
1, // 写操作时每字符的时间: 1 ms (n个字符总共为n ms)
100}; // 基本的(额外的)写超时时间: 100 ms
hPort = CreateFile(pPort, // 串口名称或设备路径
GENERIC_READ | GENERIC_WRITE, // 读写方式
0, // 共享方式:独占
NULL, // 默认的安全描述符
OPEN_EXISTING, // 创建方式
0, // 不需设置文件属性
NULL); // 不需参照模板文件
if(hPort == INVALID_HANDLE_VALUE)
{
// printf("Open Port Failed\n");
return NULL; // 打开串口失败
}
GetCommState(hPort, &dcb); // 取DCB
dcb.BaudRate = nBaudRate;
dcb.ByteSize = nByteSize;
dcb.Parity = nParity;
dcb.StopBits = nStopBits;
SetCommState(hPort, &dcb); // 设置DCB
SetupComm(hPort, 4096, 1024); // 设置输入输出缓冲区大小
SetCommTimeouts(hPort, &timeouts); // 设置超时
return hPort;
}
示例11: CreateFile
int CComPrint::OpenCOM(int nCmdNum)
{
m_sComPort.Format(_T("COM%d"), nCmdNum);
m_hCom = CreateFile(m_sComPort,
GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security
OPEN_EXISTING,
0, // no overlapped I/O
NULL); // null template
if (NULL != m_hCom)
{
m_bPortReady = SetupComm(m_hCom, 128, 128);
m_bPortReady = GetCommState(m_hCom, &m_dcb);
m_dcb.BaudRate = 9600;
m_dcb.ByteSize = 8;
m_dcb.Parity = NOPARITY;
m_dcb.StopBits = ONESTOPBIT;
m_dcb.fAbortOnError = TRUE;
m_dcb.fBinary = TRUE;
m_bPortReady = SetCommState(m_hCom, &m_dcb);
m_bPortReady = GetCommTimeouts (m_hCom, &m_CommTimeouts);
m_CommTimeouts.ReadIntervalTimeout = 50;
m_CommTimeouts.ReadTotalTimeoutConstant = 50;
m_CommTimeouts.ReadTotalTimeoutMultiplier = 10;
m_CommTimeouts.WriteTotalTimeoutConstant = 50;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 10;
m_bPortReady = SetCommTimeouts (m_hCom, &m_CommTimeouts);
return 0;
}
return -1;
}
示例12: wprintf
int ConnPort::OpenPort(TCHAR *szPort,int iBaudrate,int iParity,int iDataBits,int iStopBits)
{
DWORD dwThreadID=0;
g_iExitFlag=0;
m_hPort=NULL;
m_hPort=CreateFile(szPort,
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
if(!m_hPort)
{
m_hPort=NULL;
wprintf(TEXT("open port failed!!!\r\n"));
return FALSE;
}
//指定端口检测的事件集
SetCommMask(m_hPort,EV_RXCHAR);
//设置缓冲区,内部输入、输出缓冲区大小
SetupComm(m_hPort,1024,1024);
//刷新缓冲区信息->输入、输出缓冲区
PurgeComm(m_hPort,PURGE_TXCLEAR|PURGE_RXCLEAR);
if(!ConfigPort(iBaudrate,iParity,iDataBits,iStopBits)) return FALSE;
if(!CommTimeouts())return FALSE;
//创建读写线程
m_hThreadRead = CreateThread(0,0,(LPTHREAD_START_ROUTINE)ReadThreadProc, (void*)this,0,&dwThreadID);
m_hThreadWrite = CreateThread(0,0,(LPTHREAD_START_ROUTINE)WriteThreadProc, (void*)this,0,&dwThreadID);
m_hDataParse = CreateThread(0,0,(LPTHREAD_START_ROUTINE)PareDataProc, (void*)this,0,&dwThreadID);
m_bIsConnect=TRUE;
return TRUE;
}
示例13: serial_init
void serial_init()
{
DCB dcbSerialParams = {0};
hSerial = CreateFile("COM6",GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if(hSerial==INVALID_HANDLE_VALUE)
{
//some other error occurred. Inform user.
printf("Open COM port fail.\n");
if(GetLastError()==ERROR_FILE_NOT_FOUND)
{
//serial port does not exist. Inform user.
printf("COM port not found.\n");
}
}
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
if (!GetCommState(hSerial, &dcbSerialParams))
{
//error getting state
printf("GetCommState Error\n");
}
dcbSerialParams.BaudRate=CBR_115200;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
if(!SetCommState(hSerial, &dcbSerialParams))
{
printf("SetCommState Error\n");
}
if (!SetupComm(hSerial,2048,2048))
{
printf("Set Buffer size Error\n");
}
//SetupComm(hSerial, 1024, 1024);
}
示例14: config
bool SerialPort::config(int baudRate, int byteSize) {
if(!SetupComm(hCom, 1024, 1024))
return false;
DCB dcbConfig;
if(GetCommState(hCom, &dcbConfig)) {
/* Configuring Serial Port Settings */
dcbConfig.BaudRate = baudRate;
dcbConfig.ByteSize = byteSize;
dcbConfig.Parity = NOPARITY;
dcbConfig.StopBits = ONESTOPBIT;
dcbConfig.fBinary = TRUE;
dcbConfig.fParity = FALSE;
} else
return false;
if(!SetCommState(hCom, &dcbConfig))
return false;
COMMTIMEOUTS commTimeout;
if(GetCommTimeouts(hCom, &commTimeout)) {
/* Configuring Read & Write Time Outs */
commTimeout.ReadIntervalTimeout = 1000; //One second
commTimeout.ReadTotalTimeoutConstant = 1000; //One second
commTimeout.ReadTotalTimeoutMultiplier = 0;
commTimeout.WriteTotalTimeoutConstant = 1000; //One second
commTimeout.WriteTotalTimeoutMultiplier = 0;
} else
return false;
if(SetCommTimeouts(hCom, &commTimeout))
return true;
else
return false;
}
示例15: sizeof
BOOL CSerial::Init(DWORD dwBaudRate)
{
DCB dcb;
if(!GetCommState(m_hDev, &dcb))
{
//RETAILMSG(DBGMSG, (L"Serial::Init() error GetCommState()\r\n") );
return FALSE;
}
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = dwBaudRate;
dcb.fParity = FALSE;
dcb.fNull = FALSE;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
dcb.ByteSize = 8;
// dcb.EvtChar = '@'; //×¢²áÒ»¸öʼþ×Ö·û
if(!SetCommState(m_hDev, &dcb))
{
//RETAILMSG(DBGMSG, (L"Serial::Init() error SetCommState()\r\n") );
return FALSE;
}
if(!SetupComm(m_hDev, 4096, 4096))
{
//RETAILMSG(DBGMSG, (L"Serial::Init() error SetupComm()\r\n") );
return FALSE;
}
SetReadTimeout(100,100);
return TRUE;
}