本文整理汇总了C++中SetCommConfig函数的典型用法代码示例。如果您正苦于以下问题:C++ SetCommConfig函数的具体用法?C++ SetCommConfig怎么用?C++ SetCommConfig使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SetCommConfig函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: LOCK_MUTEX
/*!
\fn void Win_QextSerialPort::setDataBits(DataBitsType dataBits)
Sets the number of data bits used by the serial port. Possible values of dataBits are:
\verbatim
DATA_5 5 data bits
DATA_6 6 data bits
DATA_7 7 data bits
DATA_8 8 data bits
\endverbatim
\note
This function is subject to the following restrictions:
\par
5 data bits cannot be used with 2 stop bits.
\par
1.5 stop bits can only be used with 5 data bits.
\par
8 data bits cannot be used with space parity on POSIX systems.
*/
void Win_QextSerialPort::setDataBits(DataBitsType dataBits) {
LOCK_MUTEX();
if (Settings.DataBits!=dataBits) {
if ((Settings.StopBits==STOP_2 && dataBits==DATA_5) ||
(Settings.StopBits==STOP_1_5 && dataBits!=DATA_5)) {
}
else {
Settings.DataBits=dataBits;
}
}
if (isOpen()) {
switch(dataBits) {
/*5 data bits*/
case DATA_5:
if (Settings.StopBits==STOP_2) {
TTY_WARNING("Win_QextSerialPort: 5 Data bits cannot be used with 2 stop bits.");
}
else {
Win_CommConfig.dcb.ByteSize=5;
SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
}
break;
/*6 data bits*/
case DATA_6:
if (Settings.StopBits==STOP_1_5) {
TTY_WARNING("Win_QextSerialPort: 6 Data bits cannot be used with 1.5 stop bits.");
}
else {
Win_CommConfig.dcb.ByteSize=6;
SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
}
break;
/*7 data bits*/
case DATA_7:
if (Settings.StopBits==STOP_1_5) {
TTY_WARNING("Win_QextSerialPort: 7 Data bits cannot be used with 1.5 stop bits.");
}
else {
Win_CommConfig.dcb.ByteSize=7;
SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
}
break;
/*8 data bits*/
case DATA_8:
if (Settings.StopBits==STOP_1_5) {
TTY_WARNING("Win_QextSerialPort: 8 Data bits cannot be used with 1.5 stop bits.");
}
else {
Win_CommConfig.dcb.ByteSize=8;
SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
}
break;
}
}
UNLOCK_MUTEX();
}
示例2: Hardware
/*!
\fn void Win_QextSerialPort::setFlowControl(FlowType flow)
Sets the flow control used by the port. Possible values of flow are:
\verbatim
FLOW_OFF No flow control
FLOW_HARDWARE Hardware (RTS/CTS) flow control
FLOW_XONXOFF Software (XON/XOFF) flow control
\endverbatim
*/
void Win_QextSerialPort::setFlowControl(FlowType flow)
{
LOCK_MUTEX();
if (Settings.FlowControl!=flow)
{
Settings.FlowControl=flow;
}
if (isOpen())
{
switch(flow)
{
/*no flow control*/
case FLOW_OFF:
{
m_WinCommConfig.dcb.fOutxCtsFlow=FALSE;
m_WinCommConfig.dcb.fRtsControl=RTS_CONTROL_DISABLE;
m_WinCommConfig.dcb.fInX=FALSE;
m_WinCommConfig.dcb.fOutX=FALSE;
SetCommConfig(m_WinHandle, &m_WinCommConfig, sizeof(COMMCONFIG));
}
break;
/*software (XON/XOFF) flow control*/
case FLOW_XONXOFF:
{
m_WinCommConfig.dcb.fOutxCtsFlow=FALSE;
m_WinCommConfig.dcb.fRtsControl=RTS_CONTROL_DISABLE;
m_WinCommConfig.dcb.fInX=TRUE;
m_WinCommConfig.dcb.fOutX=TRUE;
SetCommConfig(m_WinHandle, &m_WinCommConfig, sizeof(COMMCONFIG));
}
break;
case FLOW_HARDWARE:
{
m_WinCommConfig.dcb.fOutxCtsFlow=TRUE;
m_WinCommConfig.dcb.fRtsControl=RTS_CONTROL_HANDSHAKE;
m_WinCommConfig.dcb.fInX=FALSE;
m_WinCommConfig.dcb.fOutX=FALSE;
SetCommConfig(m_WinHandle, &m_WinCommConfig, sizeof(COMMCONFIG));
}
break;
default:
{
Assert(false);
}
break;
}
}
UNLOCK_MUTEX();
}
示例3: setRs232
/*-------------------------------------------
| Name:setRs2322
| Description:
| Parameters:
| Return Type:
| Comments:
| See:
---------------------------------------------*/
int setRs232(uart_config* config){
COMMCONFIG commconfig;
DWORD dwSize=sizeof(COMMCONFIG);
char buffer[256]={0};
sprintf(buffer,"%s: baud=%d parity=%c data=%d stop=%d",USE_COM,
config->speed,
config->parity,
config->data,
config->stop);
if(!GetCommConfig(hCom,&commconfig,&dwSize))
return -1;
BuildCommDCB(buffer,&commconfig.dcb);
//CommConfigDialog(USE_COM,NULL,&commconfig);
if(!SetCommConfig(hCom,&commconfig,dwSize))
return -1;
printf("com cfg: %s\n",buffer);
memcpy(¤t_config,config,sizeof(uart_config));
return 0;
}
示例4: sizeof
/*!
\fn bool Win_QextSerialPort::open(OpenMode mode)
Opens a serial port. Note that this function does not specify which device to open. If you need
to open a device by name, see Win_QextSerialPort::open(const char*). This function has no effect
if the port associated with the class is already open. The port is also configured to the current
settings, as stored in the Settings structure.
*/
bool Win_QextSerialPort::open(OpenMode mode) {
unsigned long confSize = sizeof(COMMCONFIG);
Win_CommConfig.dwSize = confSize;
DWORD dwFlagsAndAttributes = 0;
if (queryMode() == QextSerialBase::EventDriven)
dwFlagsAndAttributes += FILE_FLAG_OVERLAPPED;
LOCK_MUTEX();
if (mode == QIODevice::NotOpen)
return isOpen();
if (!isOpen()) {
/*open the port*/
Win_Handle=CreateFileA(port.toLatin1(), GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, dwFlagsAndAttributes, NULL);
if (Win_Handle!=INVALID_HANDLE_VALUE)
{
/*configure port settings*/
GetCommConfig(Win_Handle, &Win_CommConfig, &confSize);
GetCommState(Win_Handle, &(Win_CommConfig.dcb));
/*set up parameters*/
Win_CommConfig.dcb.fBinary=TRUE;
Win_CommConfig.dcb.fInX=FALSE;
Win_CommConfig.dcb.fOutX=FALSE;
Win_CommConfig.dcb.fAbortOnError=FALSE;
Win_CommConfig.dcb.fNull=FALSE;
setBaudRate(Settings.BaudRate);
setDataBits(Settings.DataBits);
setStopBits(Settings.StopBits);
setParity(Settings.Parity);
setFlowControl(Settings.FlowControl);
setTimeout(Settings.Timeout_Millisec);
SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
//init event driven approach
if (queryMode() == QextSerialBase::EventDriven) {
Win_CommTimeouts.ReadIntervalTimeout = MAXDWORD;
Win_CommTimeouts.ReadTotalTimeoutMultiplier = 0;
Win_CommTimeouts.ReadTotalTimeoutConstant = 0;
Win_CommTimeouts.WriteTotalTimeoutMultiplier = 0;
Win_CommTimeouts.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(Win_Handle, &Win_CommTimeouts);
if (!SetCommMask( Win_Handle, EV_TXEMPTY | EV_RXCHAR | EV_DSR)) {
qWarning("Failed to set Comm Mask. Error code: %ld", GetLastError());
UNLOCK_MUTEX();
return false;
}
overlapThread->start();
}
QIODevice::open(mode);
}
} else {
UNLOCK_MUTEX();
return false;
}
UNLOCK_MUTEX();
return isOpen();
}
示例5: sizeof
/*!
Opens a serial port. Note that this function does not specify which device to open. If you need
to open a device by name, see QextSerialPort::open(const char*). This function has no effect
if the port associated with the class is already open. The port is also configured to the current
settings, as stored in the Settings structure.
*/
bool QextSerialPort::open(OpenMode mode) {
unsigned long confSize = sizeof(COMMCONFIG);
Win_CommConfig.dwSize = confSize;
DWORD dwFlagsAndAttributes = 0;
if (queryMode() == QextSerialPort::EventDriven)
dwFlagsAndAttributes += FILE_FLAG_OVERLAPPED;
QMutexLocker lock(mutex);
if (mode == QIODevice::NotOpen)
return isOpen();
if (!isOpen()) {
/*open the port*/
Win_Handle=CreateFileA(port.toAscii(), GENERIC_READ|GENERIC_WRITE,
0, NULL, OPEN_EXISTING, dwFlagsAndAttributes, NULL);
if (Win_Handle!=INVALID_HANDLE_VALUE) {
QIODevice::open(mode);
/*configure port settings*/
GetCommConfig(Win_Handle, &Win_CommConfig, &confSize);
GetCommState(Win_Handle, &(Win_CommConfig.dcb));
/*set up parameters*/
Win_CommConfig.dcb.fBinary=TRUE;
Win_CommConfig.dcb.fInX=FALSE;
Win_CommConfig.dcb.fOutX=FALSE;
Win_CommConfig.dcb.fAbortOnError=FALSE;
Win_CommConfig.dcb.fNull=FALSE;
setBaudRate(Settings.BaudRate);
setDataBits(Settings.DataBits);
setStopBits(Settings.StopBits);
setParity(Settings.Parity);
setFlowControl(Settings.FlowControl);
setTimeout(Settings.Timeout_Millisec);
SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
//init event driven approach
if (queryMode() == QextSerialPort::EventDriven) {
Win_CommTimeouts.ReadIntervalTimeout = MAXDWORD;
Win_CommTimeouts.ReadTotalTimeoutMultiplier = 0;
Win_CommTimeouts.ReadTotalTimeoutConstant = 0;
Win_CommTimeouts.WriteTotalTimeoutMultiplier = 0;
Win_CommTimeouts.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(Win_Handle, &Win_CommTimeouts);
if (!SetCommMask( Win_Handle, EV_TXEMPTY | EV_RXCHAR | EV_DSR)) {
qWarning() << "failed to set Comm Mask. Error code:", GetLastError();
return false;
}
winEventNotifier = new QWinEventNotifier(overlap.hEvent, this);
connect(winEventNotifier, SIGNAL(activated(HANDLE)), this, SLOT(onWinEvent(HANDLE)));
WaitCommEvent(Win_Handle, &eventMask, &overlap);
}
}
} else {
return false;
}
return isOpen();
}
示例6: ASSERT
void CSerialPort::SetConfig(COMMCONFIG& config)
{
ASSERT(IsOpen());
DWORD dwSize = sizeof(COMMCONFIG);
if (!SetCommConfig(m_hComm, &config, dwSize))
{
TRACE(_T("Failed in call to SetCommConfig\n"));
AfxThrowSerialException();
}
}
示例7: Hardware
/*!
\fn void Win_QextSerialPort::setFlowControl(FlowType flow)
Sets the flow control used by the port. Possible values of flow are:
\verbatim
FLOW_OFF No flow control
FLOW_HARDWARE Hardware (RTS/CTS) flow control
FLOW_XONXOFF Software (XON/XOFF) flow control
\endverbatim
*/
void Win_QextSerialPort::setFlowControl(FlowType flow) {
LOCK_MUTEX();
if (Settings.FlowControl!=flow) {
Settings.FlowControl=flow;
}
if (1) {
switch(flow) {
/*no flow control*/
case FLOW_OFF:
Win_CommConfig.dcb.fOutxCtsFlow=FALSE;
Win_CommConfig.dcb.fRtsControl=RTS_CONTROL_DISABLE;
Win_CommConfig.dcb.fInX=FALSE;
Win_CommConfig.dcb.fOutX=FALSE;
SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
break;
/*software (XON/XOFF) flow control*/
case FLOW_XONXOFF:
Win_CommConfig.dcb.fOutxCtsFlow=FALSE;
Win_CommConfig.dcb.fRtsControl=RTS_CONTROL_DISABLE;
Win_CommConfig.dcb.fInX=TRUE;
Win_CommConfig.dcb.fOutX=TRUE;
SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
break;
case FLOW_HARDWARE:
Win_CommConfig.dcb.fOutxCtsFlow=TRUE;
Win_CommConfig.dcb.fRtsControl=RTS_CONTROL_HANDSHAKE;
Win_CommConfig.dcb.fInX=FALSE;
Win_CommConfig.dcb.fOutX=FALSE;
SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
break;
}
}
UNLOCK_MUTEX();
}
示例8: strdup
WindowsSimPort::WindowsSimPort(const char *port) {
portname = NULL;
if(NULL == port || 0 == strlen(port)) {
return;
}
portname = strdup(port);
char fullportname[512];
_snprintf(fullportname, sizeof(fullportname), "\\\\.\\%s", portname);
portHandle=CreateFileA(fullportname, GENERIC_READ|GENERIC_WRITE,0, NULL, OPEN_EXISTING, 0, NULL);
if (portHandle == INVALID_HANDLE_VALUE) {
fprintf(stderr, "Invalid handle returned by CreateFileA [port \"%s\"]\n", fullportname);
return;
}
COMMCONFIG Win_CommConfig;
COMMTIMEOUTS Win_CommTimeouts;
unsigned long confSize = sizeof(COMMCONFIG);
Win_CommConfig.dwSize = confSize;
GetCommConfig(portHandle, &Win_CommConfig, &confSize);
Win_CommConfig.dcb.Parity = 0;
Win_CommConfig.dcb.fRtsControl = RTS_CONTROL_DISABLE;
Win_CommConfig.dcb.fOutxCtsFlow = FALSE;
Win_CommConfig.dcb.fOutxDsrFlow = FALSE;
Win_CommConfig.dcb.fDtrControl = DTR_CONTROL_DISABLE;
Win_CommConfig.dcb.fDsrSensitivity = FALSE;
Win_CommConfig.dcb.fNull=FALSE;
Win_CommConfig.dcb.fTXContinueOnXoff = FALSE;
Win_CommConfig.dcb.fInX=FALSE;
Win_CommConfig.dcb.fOutX=FALSE;
Win_CommConfig.dcb.fBinary=TRUE;
Win_CommConfig.dcb.DCBlength = sizeof(DCB);
Win_CommConfig.dcb.BaudRate = 9600;
Win_CommConfig.dcb.ByteSize = 8;
Win_CommTimeouts.ReadIntervalTimeout = 50;
Win_CommTimeouts.ReadTotalTimeoutMultiplier = 0;
Win_CommTimeouts.ReadTotalTimeoutConstant = 110;
Win_CommTimeouts.WriteTotalTimeoutMultiplier = 0;
Win_CommTimeouts.WriteTotalTimeoutConstant = 110;
SetCommConfig(portHandle, &Win_CommConfig, sizeof(COMMCONFIG));
SetCommTimeouts(portHandle,&Win_CommTimeouts);
readbuf_pos = 0;
memset(readbuf, '\0', sizeof(readbuf));
memset(lastread, '\0', sizeof(lastread));
setUsable(1);
}
示例9: setCommConfiguration
bool setCommConfiguration(const char* device, RssconwindowsPortdata* pdata, DCB* port) {
COMMCONFIG commConfig = {0};
DWORD dwSize = sizeof(commConfig);
commConfig.dwSize = dwSize;
if (!GetDefaultCommConfig(device, &commConfig, &dwSize)) {
fputs("Failed to get default port settings.\n", stderr);
pdata->lastError = GetLastError();
return false;
}
if (!SetCommConfig(pdata->portHandle, &commConfig, dwSize)) {
fputs("Failed to set default port settings.\n", stderr);
pdata->lastError = GetLastError();
return false;
}
return true;
}
示例10: sizeof
/*!
\fn bool Win_QextSerialPort::open(OpenMode mode)
Opens a serial port. Note that this function does not specify which device to open. If you need
to open a device by name, see Win_QextSerialPort::open(const char*). This function has no effect
if the port associated with the class is already open. The port is also configured to the current
settings, as stored in the Settings structure.
*/
bool Win_QextSerialPort::open(OpenMode mode) {
unsigned long confSize = sizeof(COMMCONFIG);
Win_CommConfig.dwSize = confSize;
LOCK_MUTEX();
if (mode == QIODevice::NotOpen)
return isOpen();
if (!isOpen()) {
/*open the port*/
Win_Handle=CreateFileA(port.toLatin1(), GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (Win_Handle!=INVALID_HANDLE_VALUE) {
/*set open mode*/
QIODevice::open(mode);
/*configure port settings*/
GetCommConfig(Win_Handle, &Win_CommConfig, &confSize);
GetCommState(Win_Handle, &(Win_CommConfig.dcb));
/*set up parameters*/
Win_CommConfig.dcb.fBinary=TRUE;
Win_CommConfig.dcb.fInX=FALSE;
Win_CommConfig.dcb.fOutX=FALSE;
Win_CommConfig.dcb.fAbortOnError=FALSE;
Win_CommConfig.dcb.fNull=FALSE;
setBaudRate(Settings.BaudRate);
setDataBits(Settings.DataBits);
setStopBits(Settings.StopBits);
setParity(Settings.Parity);
setFlowControl(Settings.FlowControl);
setTimeout(Settings.Timeout_Sec, Settings.Timeout_Millisec);
SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
}
}
UNLOCK_MUTEX();
return isOpen();
}
示例11: CommPort_Init
static void CommPort_Init(HANDLE hComFile)
{
if(!hComFile)
return;
COMMCONFIG CommConfig;
DWORD ConfigSize = sizeof(CommConfig);
memset(&CommConfig, 0, ConfigSize);
GetCommConfig(hComFile, &CommConfig, &ConfigSize);
if(1)
{
CommConfig.dcb.BaudRate = CBR_57600;
CommConfig.dcb.ByteSize = 8;
CommConfig.dcb.Parity = NOPARITY;
CommConfig.dcb.StopBits = ONESTOPBIT;
/*
CommConfig.dcb.fTXContinueOnXoff = 0;
CommConfig.dcb.fInX = 0;
CommConfig.dcb.fOutX = 0;
CommConfig.dcb.fOutxCtsFlow = 0;
CommConfig.dcb.fOutxDsrFlow = 0;
CommConfig.dcb.fDsrSensitivity = 0;
CommConfig.dcb.fDtrControl = 0;
CommConfig.dcb.fRtsControl = 0;*/
/*CommConfig.dcb.StopBits = 0;
(&CommConfig.dcb.BaudRate)[1] = 0x5091;*/
}
SetCommConfig(hComFile, &CommConfig, ConfigSize);
PurgeComm(hComFile, PURGE_TXCLEAR|PURGE_RXCLEAR);
COMMTIMEOUTS Timeout;
GetCommTimeouts(hComFile, &Timeout);
Timeout.ReadIntervalTimeout = 1;
Timeout.ReadTotalTimeoutMultiplier = 1;
Timeout.ReadTotalTimeoutConstant = 1;
SetCommTimeouts(hComFile, &Timeout);
}
示例12: setOption
//.........这里部分代码省略.........
}
else if (epicsStrCaseCmp(val, "even") == 0) {
tty->commConfig.dcb.Parity = 2;
}
else {
epicsSnprintf(pasynUser->errorMessage,pasynUser->errorMessageSize,
"Invalid parity.");
return asynError;
}
}
else if (epicsStrCaseCmp(key, "stop") == 0) {
if (epicsStrCaseCmp(val, "1") == 0) {
tty->commConfig.dcb.StopBits = ONESTOPBIT;
}
else if (epicsStrCaseCmp(val, "2") == 0) {
tty->commConfig.dcb.StopBits = TWOSTOPBITS;
}
else {
epicsSnprintf(pasynUser->errorMessage,pasynUser->errorMessageSize,
"Invalid number of stop bits.");
return asynError;
}
}
else if (epicsStrCaseCmp(key, "clocal") == 0) {
if (epicsStrCaseCmp(val, "Y") == 0) {
tty->commConfig.dcb.fOutxDsrFlow = FALSE;
tty->commConfig.dcb.fDsrSensitivity = FALSE;
tty->commConfig.dcb.fDtrControl = DTR_CONTROL_ENABLE;
}
else if (epicsStrCaseCmp(val, "N") == 0) {
tty->commConfig.dcb.fOutxDsrFlow = TRUE;
tty->commConfig.dcb.fDsrSensitivity = TRUE;
tty->commConfig.dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
}
else {
epicsSnprintf(pasynUser->errorMessage,pasynUser->errorMessageSize,
"Invalid clocal value.");
return asynError;
}
}
else if (epicsStrCaseCmp(key, "crtscts") == 0) {
if (epicsStrCaseCmp(val, "Y") == 0) {
tty->commConfig.dcb.fOutxCtsFlow = TRUE;
tty->commConfig.dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
}
else if (epicsStrCaseCmp(val, "N") == 0) {
tty->commConfig.dcb.fOutxCtsFlow = FALSE;
tty->commConfig.dcb.fRtsControl = RTS_CONTROL_ENABLE;
}
else {
epicsSnprintf(pasynUser->errorMessage,pasynUser->errorMessageSize,
"Invalid crtscts value.");
return asynError;
}
}
else if (epicsStrCaseCmp(key, "ixon") == 0) {
if (epicsStrCaseCmp(val, "Y") == 0) {
tty->commConfig.dcb.fOutX = TRUE ;
}
else if (epicsStrCaseCmp(val, "N") == 0) {
tty->commConfig.dcb.fOutX = FALSE;
}
else {
epicsSnprintf(pasynUser->errorMessage,pasynUser->errorMessageSize,
"Invalid ixon value.");
return asynError;
}
}
else if (epicsStrCaseCmp(key, "ixany") == 0) {
epicsSnprintf(pasynUser->errorMessage,pasynUser->errorMessageSize,
"Option ixany not supported on Windows");
return asynError;
}
else if (epicsStrCaseCmp(key, "ixoff") == 0) {
if (epicsStrCaseCmp(val, "Y") == 0) {
tty->commConfig.dcb.fInX = TRUE;
}
else if (epicsStrCaseCmp(val, "N") == 0) {
tty->commConfig.dcb.fInX = FALSE;
}
else {
epicsSnprintf(pasynUser->errorMessage,pasynUser->errorMessageSize,
"Invalid ixoff value.");
return asynError;
}
}
else if (epicsStrCaseCmp(key, "") != 0) {
epicsSnprintf(pasynUser->errorMessage,pasynUser->errorMessageSize,
"Unsupported key \"%s\"", key);
return asynError;
}
ret = SetCommConfig(tty->commHandle, &tty->commConfig, commConfigSize);
if (ret == 0) {
error = GetLastError();
epicsSnprintf(pasynUser->errorMessage,pasynUser->errorMessageSize,
"%s error calling SetCommConfig %d", tty->serialDeviceName, error);
return asynError;
}
return asynSuccess;
}
示例13: open_port_and_set_baud_or_die
PORTTYPE open_port_and_set_baud_or_die(const char *name, long baud)
{
PORTTYPE fd;
#if defined(MACOSX)
struct termios tinfo;
fd = open(name, O_RDWR | O_NONBLOCK);
if (fd < 0) die("unable to open port %s\n", name);
if (tcgetattr(fd, &tinfo) < 0) die("unable to get serial parms\n");
cfmakeraw(&tinfo);
if (cfsetspeed(&tinfo, baud) < 0) die("error in cfsetspeed\n");
tinfo.c_cflag |= CLOCAL;
if (tcsetattr(fd, TCSANOW, &tinfo) < 0) die("unable to set baud rate\n");
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
#elif defined(LINUX)
struct termios tinfo;
memset (&tinfo, 0, sizeof tinfo);
struct serial_struct kernel_serial_settings;
int r;
fd = open(name, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd < 0) die("unable to open port %s\n", name);
if (tcgetattr(fd, &tinfo) < 0) die("unable to get serial parms\n");
tinfo.c_cflag &= ~PARENB; // Make 8n1
tinfo.c_cflag &= ~CSTOPB;
tinfo.c_cflag &= ~CSIZE;
tinfo.c_cflag |= CS8;
tinfo.c_cflag &= ~CRTSCTS; // no flow control
tinfo.c_cc[VMIN] = 1; // read doesn't block
tinfo.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tinfo.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
cfmakeraw(&tinfo);
tcflush( fd, TCIFLUSH );
if (cfsetspeed(&tinfo, baud) < 0) die("error in cfsetspeed\n");
if (tcsetattr(fd, TCSANOW, &tinfo) < 0) die("unable to set baud rate\n");
/* r = ioctl(fd, TIOCGSERIAL, &kernel_serial_settings); */
/* if (r >= 0) { */
/* kernel_serial_settings.flags |= ASYNC_LOW_LATENCY; */
/* r = ioctl(fd, TIOCSSERIAL, &kernel_serial_settings); */
/* if (r >= 0) printf("set linux low latency mode\n"); */
/* } */
#elif defined(WINDOWS)
COMMCONFIG cfg;
COMMTIMEOUTS timeout;
DWORD n;
char portname[256];
int num;
if (sscanf(name, "COM%d", &num) == 1) {
sprintf(portname, "\\\\.\\COM%d", num); // Microsoft KB115831
} else {
strncpy(portname, name, sizeof(portname)-1);
portname[n-1] = 0;
}
fd = CreateFile(portname, GENERIC_READ | GENERIC_WRITE,
0, 0, OPEN_EXISTING, 0, NULL);
if (fd == INVALID_HANDLE_VALUE) die("unable to open port %s\n", name);
GetCommConfig(fd, &cfg, &n);
//cfg.dcb.BaudRate = baud;
cfg.dcb.BaudRate = 115200;
cfg.dcb.fBinary = TRUE;
cfg.dcb.fParity = FALSE;
cfg.dcb.fOutxCtsFlow = FALSE;
cfg.dcb.fOutxDsrFlow = FALSE;
cfg.dcb.fOutX = FALSE;
cfg.dcb.fInX = FALSE;
cfg.dcb.fErrorChar = FALSE;
cfg.dcb.fNull = FALSE;
cfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
cfg.dcb.fAbortOnError = FALSE;
cfg.dcb.ByteSize = 8;
cfg.dcb.Parity = NOPARITY;
cfg.dcb.StopBits = ONESTOPBIT;
cfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
SetCommConfig(fd, &cfg, n);
GetCommTimeouts(fd, &timeout);
timeout.ReadIntervalTimeout = 0;
timeout.ReadTotalTimeoutMultiplier = 0;
timeout.ReadTotalTimeoutConstant = 1000;
timeout.WriteTotalTimeoutConstant = 0;
timeout.WriteTotalTimeoutMultiplier = 0;
SetCommTimeouts(fd, &timeout);
#endif
return fd;
}
示例14: closeSerial
//.........这里部分代码省略.........
p = strstr(buf, "COM");
if (p && sscanf(p + 3, "%d", &port_num) == 1) {
printf("port_num = %d\n", port_num);
snprintf(name_createfile, sizeof(name_createfile), "\\\\.\\COM%d", port_num);
snprintf(name_commconfig, sizeof(name_commconfig), "COM%d", port_num);
} else {
snprintf(name_createfile, sizeof(name_createfile), "%s", _portName.c_str());
snprintf(name_commconfig, sizeof(name_commconfig), "%s", _portName.c_str());
}
len = sizeof(COMMCONFIG);
if (GetDefaultCommConfig(name_commconfig, &cfg, &len)) {
// this prevents unintentionally raising DTR when opening
// might only work on COM1 to COM9
got_default_cfg = 1;
memcpy(&port_cfg_orig, &cfg, sizeof(COMMCONFIG));
cfg.dcb.fDtrControl = DTR_CONTROL_DISABLE;
cfg.dcb.fRtsControl = RTS_CONTROL_DISABLE;
SetDefaultCommConfig(name_commconfig, &cfg, sizeof(COMMCONFIG));
} else {
printf("error with GetDefaultCommConfig\n");
}
port_handle = CreateFile(name_createfile, GENERIC_READ | GENERIC_WRITE,
0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (port_handle == INVALID_HANDLE_VALUE) {
win32_err(buf);
//error_msg = "Unable to open " + _portName + ", " + buf;
return -1;
}
len = sizeof(COMMCONFIG);
if (!GetCommConfig(port_handle, &port_cfg, &len)) {
CloseHandle(port_handle);
win32_err(buf);
//error_msg = "Unable to read communication config on " + _portName + ", " + buf;
return -1;
}
if (!got_default_cfg) {
memcpy(&port_cfg_orig, &port_cfg, sizeof(COMMCONFIG));
}
// http://msdn2.microsoft.com/en-us/library/aa363188(VS.85).aspx
port_cfg.dcb.BaudRate = 230400;
port_cfg.dcb.fBinary = TRUE;
port_cfg.dcb.fParity = FALSE;
port_cfg.dcb.fOutxCtsFlow = FALSE;
port_cfg.dcb.fOutxDsrFlow = FALSE;
port_cfg.dcb.fDtrControl = DTR_CONTROL_DISABLE;
port_cfg.dcb.fDsrSensitivity = FALSE;
port_cfg.dcb.fTXContinueOnXoff = TRUE; // ???
port_cfg.dcb.fOutX = FALSE;
port_cfg.dcb.fInX = FALSE;
port_cfg.dcb.fErrorChar = FALSE;
port_cfg.dcb.fNull = FALSE;
port_cfg.dcb.fRtsControl = RTS_CONTROL_DISABLE;
port_cfg.dcb.fAbortOnError = FALSE;
port_cfg.dcb.ByteSize = 8;
port_cfg.dcb.Parity = NOPARITY;
port_cfg.dcb.StopBits = ONESTOPBIT;
if (!SetCommConfig(port_handle, &port_cfg, sizeof(COMMCONFIG))) {
CloseHandle(port_handle);
win32_err(buf);
//error_msg = "Unable to write communication config to " + name + ", " + buf;
return -1;
}
if (!EscapeCommFunction(port_handle, CLRDTR | CLRRTS)) {
CloseHandle(port_handle);
win32_err(buf);
//error_msg = "Unable to control serial port signals on " + name + ", " + buf;
return -1;
}
// http://msdn2.microsoft.com/en-us/library/aa363190(VS.85).aspx
// setting to all zeros means timeouts are not used
//timeouts.ReadIntervalTimeout = 0;
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
if (!SetCommTimeouts(port_handle, &timeouts)) {
CloseHandle(port_handle);
win32_err(buf);
//error_msg = "Unable to write timeout settings to " + name + ", " + buf;
return -1;
}
#endif // _WIN32
circularBuffer[0] = '\n';
cBufHead = 0;
cBufTail = 0;
serialCounter = 0;
_portOpened = true;
setNumberOfChannelsAndSamplingRate(1, maxSamplingRate());
return fd;
}
示例15: Windows
//.........这里部分代码省略.........
TTY_WARNING("Win_QextSerialPort: Windows does not support 150 baud operation. Switching to 110 baud.");
Win_CommConfig.dcb.BaudRate=CBR_110;
break;
/*200 baud*/
case BAUD200:
TTY_WARNING("Win_QextSerialPort: Windows does not support 200 baud operation. Switching to 110 baud.");
Win_CommConfig.dcb.BaudRate=CBR_110;
break;
/*300 baud*/
case BAUD300:
Win_CommConfig.dcb.BaudRate=CBR_300;
break;
/*600 baud*/
case BAUD600:
Win_CommConfig.dcb.BaudRate=CBR_600;
break;
/*1200 baud*/
case BAUD1200:
Win_CommConfig.dcb.BaudRate=CBR_1200;
break;
/*1800 baud*/
case BAUD1800:
TTY_WARNING("Win_QextSerialPort: Windows does not support 1800 baud operation. Switching to 1200 baud.");
Win_CommConfig.dcb.BaudRate=CBR_1200;
break;
/*2400 baud*/
case BAUD2400:
Win_CommConfig.dcb.BaudRate=CBR_2400;
break;
/*4800 baud*/
case BAUD4800:
Win_CommConfig.dcb.BaudRate=CBR_4800;
break;
/*9600 baud*/
case BAUD9600:
Win_CommConfig.dcb.BaudRate=CBR_9600;
break;
/*14400 baud*/
case BAUD14400:
TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: POSIX does not support 14400 baud operation.");
Win_CommConfig.dcb.BaudRate=CBR_14400;
break;
/*19200 baud*/
case BAUD19200:
Win_CommConfig.dcb.BaudRate=CBR_19200;
break;
/*38400 baud*/
case BAUD38400:
Win_CommConfig.dcb.BaudRate=CBR_38400;
break;
/*56000 baud*/
case BAUD56000:
TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: POSIX does not support 56000 baud operation.");
Win_CommConfig.dcb.BaudRate=CBR_56000;
break;
/*57600 baud*/
case BAUD57600:
Win_CommConfig.dcb.BaudRate=CBR_57600;
break;
/*76800 baud*/
case BAUD76800:
TTY_WARNING("Win_QextSerialPort: Windows does not support 76800 baud operation. Switching to 57600 baud.");
Win_CommConfig.dcb.BaudRate=CBR_57600;
break;
/*115200 baud*/
case BAUD115200:
Win_CommConfig.dcb.BaudRate=CBR_115200;
break;
/*128000 baud*/
case BAUD128000:
TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: POSIX does not support 128000 baud operation.");
Win_CommConfig.dcb.BaudRate=CBR_128000;
break;
/*256000 baud*/
case BAUD256000:
TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: POSIX does not support 256000 baud operation.");
Win_CommConfig.dcb.BaudRate=CBR_256000;
break;
}
SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
}
UNLOCK_MUTEX();
}