本文整理汇总了C++中cfsetospeed函数的典型用法代码示例。如果您正苦于以下问题:C++ cfsetospeed函数的具体用法?C++ cfsetospeed怎么用?C++ cfsetospeed使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了cfsetospeed函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: lcterm_init
/**
* Initialize the driver.
* Open com port and set baud correctly...
* \param drvthis Pointer to driver structure.
* \retval 0 Success.
* \retval <0 Error.
*/
MODULE_EXPORT int
lcterm_init (Driver *drvthis)
{
char device[200];
int speed=B9600;
struct termios portset;
PrivateData *p;
debug(RPT_INFO, "LCTERM: init(%p)", drvthis);
// Alocate and store private data
p = (PrivateData *) calloc(1, sizeof(PrivateData));
if (p == NULL)
return -1;
if (drvthis->store_private_ptr(drvthis, p))
return -1;
// initialize private data
p->fd = -1;
p->ccmode = p->last_ccmode = standard;
// READ CONFIG FILE:
// which serial device should be used
strncpy(device, drvthis->config_get_string(drvthis->name , "Device" , 0 , DEFAULT_DEVICE),
sizeof(device));
device[sizeof(device)-1] = '\0';
report(RPT_INFO, "%s: using Device %s", drvthis->name, device);
/* Get and parse size */
{
int w, h;
const char *s = drvthis->config_get_string(drvthis->name, "Size", 0, "16x2");
debug(RPT_DEBUG, "%s: reading size: %s", __FUNCTION__, s);
if ((sscanf(s, "%dx%d", &w, &h) != 2)
|| (w <= 0) || (w > LCD_MAX_WIDTH)
|| (h <= 0) || (h > LCD_MAX_HEIGHT))
{
report(RPT_WARNING, "%s: cannot read Size: %s; using default %s",
drvthis->name, s, "16x2");
sscanf("16x2", "%dx%d", &w, &h);
}
p->width = w;
p->height = h;
}
report(RPT_INFO, "%s: using Size: %dx%d", drvthis->name, p->width, p->height);
p->framebuf = malloc(p->width * p->height);
p->last_framebuf = malloc(p->width * p->height);
if ((p->framebuf == NULL) || (p->last_framebuf == NULL)) {
report(RPT_ERR, "%s: unable to create framebuffer", drvthis->name);
return -1;
}
memset(p->framebuf, ' ', p->width * p->height);
memset(p->last_framebuf, ' ', p->width * p->height);
// Set up io port correctly, and open it...
debug(RPT_DEBUG, "%s: Opening serial device: %s", drvthis->name, device);
p->fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
if (p->fd == -1) {
report(RPT_ERR, "%s: open(%) failed (%s)", drvthis->name, device, strerror(errno));
if (errno == EACCES)
report(RPT_ERR, "%s: make sure you have rw access to %s!", drvthis->name, device);
return -1;
}
report(RPT_INFO, "%s: opened display on %s", drvthis->name, device);
tcgetattr(p->fd, &portset);
#ifdef HAVE_CFMAKERAW
/* The easy way: */
cfmakeraw(&portset);
#else
/* The hard way: */
portset.c_iflag &= ~( IGNBRK | BRKINT | PARMRK | ISTRIP
| INLCR | IGNCR | ICRNL | IXON );
portset.c_oflag &= ~OPOST;
portset.c_lflag &= ~( ECHO | ECHONL | ICANON | ISIG | IEXTEN );
portset.c_cflag &= ~( CSIZE | PARENB | CRTSCTS );
portset.c_cflag |= CS8 | CREAD | CLOCAL ;
#endif
cfsetospeed(&portset, speed);
cfsetispeed(&portset, speed);
tcsetattr(p->fd, TCSANOW, &portset);
tcflush(p->fd, TCIOFLUSH);
// clear the display, disable cursor, disable key scanning
write(p->fd, "\x1a\x16\x1bK", 4);
report(RPT_DEBUG, "%s: init() done", drvthis->name);
return 0;
}
示例2: posix_cfsetospeed
EIF_INTEGER posix_cfsetospeed(struct termios *ptr, EIF_INTEGER speed)
{
return cfsetospeed(ptr, speed);
}
示例3: serial_setup
static int serial_setup(serial_control_t *ctrl)
{
/* just setup serial at debug level= 0, 1, 4,
for debug level = 2, 3 is reserved for simulation test locally */
if ((debug <2) || (debug >3)) {
struct termios newtio, oldtio;
/*
* Here we setup the standard input tty settings. We have to do it here,
* because we need to restore them when this application exits (so that the Linux
* shell continues to work as expected.)
* The tty settings of the external tty are set in the remote build.
*/
if (tcgetattr(ctrl->fd, &oldtio) != 0) {
error_printf("tcgetattr failed: %s\n", strerror(errno));
return EXIT_FAILURE;
}
memset(&newtio, 0, sizeof(newtio));
memcpy(&newtio, &oldtio, sizeof(struct termios));
if (ctrl->mode == RAW_MODE) {
/* Set raw mode: the remote application will handle all terminal characters */
cfmakeraw(&newtio);
}
switch (ctrl->bits) {
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}
switch (ctrl->parity) {
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'E':
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'N':
newtio.c_cflag &= ~PARENB;
break;
}
switch (ctrl->speed) {
case 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
if (ctrl->stop == 1)
newtio.c_cflag &= ~CSTOPB;
else if (ctrl->stop == 2)
newtio.c_cflag |= CSTOPB;
// important
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
if (ctrl->mode == RAW_MODE) {
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
newtio.c_oflag &= ~OPOST;
newtio.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
}
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(ctrl->fd, TCIFLUSH);
if (tcsetattr(ctrl->fd, TCSANOW, &newtio) < 0) {
error_printf("tcsetattr failed: %s\n", strerror(errno));
return EXIT_FAILURE;
}
debug_printf("serial_control: setting has done!\n");
}
return EXIT_SUCCESS;
}
示例4: serialport_set_baudrate
void serialport_set_baudrate(unsigned int baudrate)
{
switch(baudrate)
{
case 2400:
cfsetispeed(&term,B2400);
cfsetospeed(&term,B2400);
break;
case 4800:
cfsetispeed(&term,B4800);
cfsetospeed(&term,B4800);
break;
case 9600:
cfsetispeed(&term,B9600);
cfsetospeed(&term,B9600);
break;
case 19200:
cfsetispeed(&term,B19200);
cfsetospeed(&term,B19200);
break;
case 38400:
cfsetispeed(&term,B38400);
cfsetospeed(&term,B38400);
break;
case 57600:
cfsetispeed(&term,B57600);
cfsetospeed(&term,B57600);
break;
case 115200:
cfsetispeed(&term,B115200);
cfsetospeed(&term,B115200);
break;
case 230400:
cfsetispeed(&term,B230400);
cfsetospeed(&term,B230400);
break;
#ifndef __APPLE__
case 460800:
cfsetispeed(&term,B460800);
cfsetospeed(&term,B460800);
break;
case 921600:
cfsetispeed(&term,B921600);
cfsetospeed(&term,B921600);
break;
#endif
default:
LOGWARN("serialport_set_baudrate: baud rate %d may not work", baudrate);
cfsetispeed(&term,baudrate);
cfsetospeed(&term,baudrate);
break;
}
}
示例5: _modbus_rtu_connect
//.........这里部分代码省略.........
break;
#endif
#ifdef B2500000
case 2500000:
speed = B2500000;
break;
#endif
#ifdef B3000000
case 3000000:
speed = B3000000;
break;
#endif
#ifdef B3500000
case 3500000:
speed = B3500000;
break;
#endif
#ifdef B4000000
case 4000000:
speed = B4000000;
break;
#endif
default:
speed = B9600;
if (ctx->debug) {
fprintf(stderr,
"WARNING Unknown baud rate %d for %s (B9600 used)\n",
ctx_rtu->baud, ctx_rtu->device);
}
}
/* Set the baud rate */
if ((cfsetispeed(&tios, speed) < 0) ||
(cfsetospeed(&tios, speed) < 0)) {
close(ctx->s);
ctx->s = -1;
return -1;
}
/* C_CFLAG Control options
CLOCAL Local line - do not change "owner" of port
CREAD Enable receiver
*/
tios.c_cflag |= (CREAD | CLOCAL);
/* CSIZE, HUPCL, CRTSCTS (hardware flow control) */
/* Set data bits (5, 6, 7, 8 bits)
CSIZE Bit mask for data bits
*/
tios.c_cflag &= ~CSIZE;
switch (ctx_rtu->data_bit) {
case 5:
tios.c_cflag |= CS5;
break;
case 6:
tios.c_cflag |= CS6;
break;
case 7:
tios.c_cflag |= CS7;
break;
case 8:
default:
tios.c_cflag |= CS8;
break;
}
示例6: open
bool RawChannel::Open()
{
#ifndef WIN32
sp = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(sp, F_SETFL, 0);
//fcntl(sp, F_SETFL, FNDELAY);
if(sp < 0)
{
printf("RawChannel::Open: open(): %s: %d: %s\n", port, errno, strerror(errno));
return false;
}
if(tcgetattr(sp, &tty) != 0)
{
printf("RawChannel::Open: Tcgetattr: %d: %s\n", errno, strerror(errno));
return false;
}
cfsetospeed(&tty, baud_rate);
cfsetispeed(&tty, baud_rate);
tty.c_oflag &= ~(OCRNL | ONLCR | ONLRET | ONOCR | OFILL | OPOST);
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
tty.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK| INPCK| ISTRIP | IXON| IXOFF | IXANY);
tty.c_cflag &= ~(PARENB | CSIZE);
tty.c_cflag |= CS8;
tty.c_cc[VMIN] = 0;
tty.c_cc[VTIME] =50;
if(tcsetattr(sp, TCSANOW, &tty) !=0)
{
printf("RawChannel::Open: tcsetattr: %d: %s\n", errno, strerror(errno));
return false;
}
printf("RawChannel::Open: %s opened\n", port);
#else
hComm = ::CreateFile(port, GENERIC_READ|GENERIC_WRITE, 0,NULL,OPEN_EXISTING, 0, NULL);
if(hComm == INVALID_HANDLE_VALUE)
{
printf("Error opening serial port: %s\n", port);
return false;
}
if(!::GetCommState(hComm, &dcb))
{
printf("Could not setup comm port: %s\n", port);
return false;
}
dcb.BaudRate = CBR_57600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.fRtsControl = ONESTOPBIT;
//dcb.EofChar = '\r';
if(!::SetCommState(hComm, &dcb))
{
printf("Could not setup comm port: %s\n", port);
return false;
}
COMMTIMEOUTS cto;
cto.ReadIntervalTimeout = 100000;
cto.ReadTotalTimeoutConstant = 100;
cto.ReadTotalTimeoutMultiplier = 256;
cto.WriteTotalTimeoutConstant = 0;
cto.WriteTotalTimeoutMultiplier = 0;
::SetCommTimeouts(hComm, &cto);
return true;
#endif
return true;
}
示例7: throw
ASerial::ASerial(std::string serialport, int baudrate) throw (std::string){
std::string exc;
this->error = 0;
this->serialport = serialport;
this->baudrate = baudrate;
this->fd = open(this->serialport.c_str(), O_RDWR | O_NONBLOCK);
if(this->fd == -1){
this->error = 1;
exc = "ASerial: Could not open serialport: " + serialport + ".";
throw exc;
}
if (tcgetattr(fd, &this->toptions) < 0){
this->error = 2;
exc = "ASerial: Could not get term attributes.";
throw exc;
}
speed_t brate = this->baudrate; // let you override switch below if needed
switch(baudrate) {
case 4800:
brate=B4800;
break;
case 9600:
brate=B9600;
break;
#ifdef B14400
case 14400:
brate=B14400;
break;
#endif
case 19200:
brate=B19200;
break;
#ifdef B28800
case 28800:
brate=B28800;
break;
#endif
case 38400:
brate=B38400;
break;
case 57600:
brate=B57600;
break;
case 115200:
brate=B115200;
break;
#ifdef B230400
case 230400:
brate = B230400;
break;
#endif
}
cfsetispeed(&this->toptions, brate);
cfsetospeed(&this->toptions, brate);
// 8N1
toptions.c_cflag &= ~PARENB;
toptions.c_cflag &= ~CSTOPB;
toptions.c_cflag &= ~CSIZE;
toptions.c_cflag |= CS8;
// no flow control
toptions.c_cflag &= ~CRTSCTS;
toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
toptions.c_oflag &= ~OPOST; // make raw
toptions.c_cc[VMIN] = 0;
toptions.c_cc[VTIME] = 0;
tcsetattr(fd, TCSANOW, &this->toptions);
if(tcsetattr(fd, TCSAFLUSH, &this->toptions) < 0){
this->error = 3;
exc = "ASerial: Couldn't set term attributes.";
throw exc;
}
}
示例8: cmserial_create
COMMNG
cmserial_create(UINT port, BYTE param, UINT32 speed)
{
static const int cmserial_cflag[10] = {
B110, B300, B1200, B2400, B4800,
B9600, B19200, B38400, B57600, B115200
};
static const int csize[] = { CS5, CS6, CS7, CS8 };
struct termios options, origopt;
COMMNG ret;
CMSER serial;
int hdl;
UINT i;
VERBOSE(("cmserial_create: port = %d, param = %02x, speed = %d", port, param, speed));
if (port == 0 || port > MAX_SERIAL_PORT_NUM) {
VERBOSE(("cmserial_create: port is invalid"));
goto cscre_failure;
}
port--;
if (np2oscfg.com[port].mout[0] == '\0') {
VERBOSE(("cmserial_create: com device file is disable"));
goto cscre_failure;
}
hdl = open(np2oscfg.com[port].mout, O_RDWR | O_NOCTTY | O_NDELAY);
if (hdl == -1) {
VERBOSE(("cmserial_create: open failure %s, %s", np2oscfg.com[port].mout, strerror(errno)));
goto cscre_failure;
}
if (!isatty(hdl)) {
VERBOSE(("cmserial_create: not terminal file descriptor (%s)", strerror(errno)));
goto cscre_close;
}
/* get current options for the port */
tcgetattr(hdl, &options);
origopt = options;
/* baud rates */
for (i = 0; i < NELEMENTS(cmserial_speed); i++) {
if (cmserial_speed[i] >= speed) {
VERBOSE(("cmserial_create: spped = %d", cmserial_speed[i]));
break;
}
}
if (i >= NELEMENTS(cmserial_speed)) {
VERBOSE(("cmserial_create: speed is invaild"));
goto cscre_close;
}
cfsetispeed(&options, cmserial_cflag[i]);
cfsetospeed(&options, cmserial_cflag[i]);
/* character size bits */
options.c_cflag &= ~CSIZE;
options.c_cflag |= csize[(param >> 2) & 3];
VERBOSE(("cmserial_create: charactor size = %d", csize[(param >> 2) & 3]));
/* parity check */
switch (param & 0x30) {
case 0x10:
VERBOSE(("cmserial_create: odd parity"));
options.c_cflag |= PARENB | PARODD;
options.c_iflag |= INPCK | ISTRIP;
break;
case 0x30:
VERBOSE(("cmserial_create: even parity"));
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK | ISTRIP;
break;
default:
VERBOSE(("cmserial_create: non parity"));
options.c_cflag &= ~PARENB;
options.c_iflag &= ~(INPCK | ISTRIP);
break;
}
/* stop bits */
switch (param & 0xc0) {
case 0x80:
VERBOSE(("cmserial_create: stop bits: 1.5"));
break;
case 0xc0:
VERBOSE(("cmserial_create: stop bits: 2"));
options.c_cflag |= CSTOPB;
break;
default:
VERBOSE(("cmserial_create: stop bits: 1"));
options.c_cflag &= ~CSTOPB;
break;
}
//.........这里部分代码省略.........
示例9: serialOpen
int serialOpen (char *device, int baud)
{
struct termios options ;
speed_t myBaud ;
int status, fd ;
switch (baud)
{
case 50: myBaud = B50 ; break ;
case 75: myBaud = B75 ; break ;
case 110: myBaud = B110 ; break ;
case 134: myBaud = B134 ; break ;
case 150: myBaud = B150 ; break ;
case 200: myBaud = B200 ; break ;
case 300: myBaud = B300 ; break ;
case 600: myBaud = B600 ; break ;
case 1200: myBaud = B1200 ; break ;
case 1800: myBaud = B1800 ; break ;
case 2400: myBaud = B2400 ; break ;
case 4800: myBaud = B4800 ; break ;
case 9600: myBaud = B9600 ; break ;
case 19200: myBaud = B19200 ; break ;
case 38400: myBaud = B38400 ; break ;
case 57600: myBaud = B57600 ; break ;
case 115200: myBaud = B115200 ; break ;
case 230400: myBaud = B230400 ; break ;
default:
return -2 ;
}
if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
return -1 ;
fcntl (fd, F_SETFL, O_RDWR) ;
// Get and modify current options:
tcgetattr (fd, &options) ;
cfmakeraw (&options) ;
cfsetispeed (&options, myBaud) ;
cfsetospeed (&options, myBaud) ;
options.c_cflag |= (CLOCAL | CREAD) ;
options.c_cflag &= ~PARENB ;
options.c_cflag &= ~CSTOPB ;
options.c_cflag &= ~CSIZE ;
options.c_cflag |= CS8 ;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
options.c_oflag &= ~OPOST ;
options.c_cc [VMIN] = 0 ;
options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds)
tcsetattr (fd, TCSANOW | TCSAFLUSH, &options) ;
ioctl (fd, TIOCMGET, &status);
status |= TIOCM_DTR ;
status |= TIOCM_RTS ;
ioctl (fd, TIOCMSET, &status);
usleep (10000) ; // 10mS
return fd ;
}
示例10: csr_open_bcsp
int csr_open_bcsp(char *device)
{
struct termios ti;
uint8_t delay, activity = 0x00;
int timeout = 0;
if (!device)
device = "/dev/ttyS0";
fd = open(device, O_RDWR | O_NOCTTY);
if (fd < 0) {
fprintf(stderr, "Can't open serial port: %s (%d)\n",
strerror(errno), errno);
return -1;
}
tcflush(fd, TCIOFLUSH);
if (tcgetattr(fd, &ti) < 0) {
fprintf(stderr, "Can't get port settings: %s (%d)\n",
strerror(errno), errno);
close(fd);
return -1;
}
cfmakeraw(&ti);
ti.c_cflag |= CLOCAL;
ti.c_cflag &= ~CRTSCTS;
ti.c_cflag |= PARENB;
ti.c_cflag &= ~PARODD;
ti.c_cflag &= ~CSIZE;
ti.c_cflag |= CS8;
ti.c_cflag &= ~CSTOPB;
ti.c_cc[VMIN] = 1;
ti.c_cc[VTIME] = 0;
cfsetospeed(&ti, B38400);
if (tcsetattr(fd, TCSANOW, &ti) < 0) {
fprintf(stderr, "Can't change port settings: %s (%d)\n",
strerror(errno), errno);
close(fd);
return -1;
}
tcflush(fd, TCIOFLUSH);
if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK) < 0) {
fprintf(stderr, "Can't set non blocking mode: %s (%d)\n",
strerror(errno), errno);
close(fd);
return -1;
}
memset(&send_packet, 0, sizeof(send_packet));
memset(&receive_packet, 0, sizeof(receive_packet));
ubcsp_initialize();
send_packet.length = 512;
send_packet.payload = send_buffer;
receive_packet.length = 512;
receive_packet.payload = receive_buffer;
ubcsp_receive_packet(&receive_packet);
while (1) {
delay = ubcsp_poll(&activity);
if (activity & UBCSP_PACKET_RECEIVED)
break;
if (delay) {
usleep(delay * 100);
if (timeout++ > 5000) {
fprintf(stderr, "Initialization timed out\n");
return -1;
}
}
}
return 0;
}
示例11: perror
int CComDev::set_opt(int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio, oldtio;
/*保存测试现有串口参数设置,在这里如果串口号等出错,会有相关的出错信息*/
if (tcgetattr(m_nFd, &oldtio) != 0) {
perror("SetupSerial 1");
return FALSE;
}
bzero(&newtio, sizeof(newtio));
/*步骤一,设置字符大小*/
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
/*设置停止位*/
switch (nBits)
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}
/*设置奇偶校验位*/
switch (nEvent)
{
case 'O': //奇数
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'E': //偶数
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'N': //无奇偶校验位
newtio.c_cflag &= ~PARENB;
break;
}
/*设置波特率*/
switch (nSpeed)
{
case 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
case 460800:
cfsetispeed(&newtio, B460800);
cfsetospeed(&newtio, B460800);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
/*设置停止位*/
if (nStop == 1)
newtio.c_cflag &= ~CSTOPB;
else if (nStop == 2)
newtio.c_cflag |= CSTOPB;
/*设置等待时间和最小接收字符*/
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
/*处理未接收字符*/
tcflush(m_nFd, TCIFLUSH);
/*激活新配置*/
if ((tcsetattr(m_nFd, TCSANOW, &newtio)) != 0)
{
perror("Com set error");
return FALSE;
}
printf("Com set done!\n");
return TRUE;
}
示例12: comm_init
/*
* Arguments: fd_udata, [options ...:
* reset (string: "reset"),
* baud_rate (number),
* character_size (string: "cs5".."cs8"),
* parity (string: "parno", "parodd", "pareven"),
* stop_bits (string: "sb1", "sb2"),
* flow_controls (string: "foff", "frtscts", "fxio")]
* Returns: [fd_udata]
*/
static int
comm_init (lua_State *L)
{
const fd_t fd = (fd_t) lua_unboxinteger(L, 1, FD_TYPENAME);
const int narg = lua_gettop(L);
int i;
#ifndef _WIN32
struct termios tio;
if (tcgetattr(fd, &tio) == -1) goto err;
#else
DCB dcb;
dcb.DCBlength = sizeof(DCB);
if (!GetCommState(fd, &dcb)) goto err;
#endif
for (i = 2; i <= narg; ++i) {
#ifndef _WIN32
tcflag_t mask = 0, flag = 0;
#endif
if (lua_isnumber(L, i)) {
const int baud_rate = lua_tointeger(L, i);
#ifndef _WIN32
switch (baud_rate) {
case 9600: flag = B9600; break;
case 19200: flag = B19200; break;
case 38400: flag = B38400; break;
case 57600: flag = B57600; break;
case 115200: flag = B115200; break;
}
if (cfsetispeed(&tio, flag) == -1
|| cfsetospeed(&tio, flag) == -1)
goto err;
#else
dcb.BaudRate = baud_rate;
#endif
} else {
const char *opt = lua_tostring(L, i);
const char *endp = opt + lua_rawlen(L, i) - 1;
if (!opt) continue;
switch (*opt) {
case 'r': /* reset */
#ifndef _WIN32
memset(&tio, 0, sizeof(struct termios));
#else
memset(&dcb, 0, sizeof(DCB));
#endif
continue;
case 'c': /* character size */
#ifndef _WIN32
switch (*endp) {
case '5': flag = CS5; break;
case '6': flag = CS6; break;
case '7': flag = CS7; break;
default: flag = CS8;
}
mask = CSIZE;
#else
dcb.ByteSize = (char) (*endp - '0');
#endif
break;
case 'p': /* parity */
#ifndef _WIN32
switch (*endp) {
case 'd': flag = PARODD;
case 'n': flag |= PARENB; break;
default: flag = 0; /* no parity */
}
mask = PARENB | PARODD;
#else
{
int parity;
switch (*endp) {
case 'd': parity = ODDPARITY; break;
case 'n': parity = EVENPARITY; break;
default: parity = 0; /* no parity */
}
dcb.Parity = (char) parity;
dcb.fParity = (parity != 0);
}
#endif
break;
case 's': /* stop bits */
#ifndef _WIN32
if (*endp == '2') flag = CSTOPB; /* else: one stop bit */
mask = CSTOPB;
//.........这里部分代码省略.........
示例13: ssh_tty_parse_modes
/*
* Decodes terminal modes for the terminal referenced by fd in a portable
* manner from a packet being read.
*/
void
ssh_tty_parse_modes(struct ssh *ssh, int fd)
{
struct termios tio;
struct sshbuf *buf;
const u_char *data;
u_char opcode;
u_int baud, u;
int r, failure = 0;
size_t len;
if ((r = sshpkt_get_string_direct(ssh, &data, &len)) != 0)
fatal("%s: packet error: %s", __func__, ssh_err(r));
if (len == 0)
return;
if ((buf = sshbuf_from(data, len)) == NULL) {
error("%s: sshbuf_from failed", __func__);
return;
}
/*
* Get old attributes for the terminal. We will modify these
* flags. I am hoping that if there are any machine-specific
* modes, they will initially have reasonable values.
*/
if (tcgetattr(fd, &tio) == -1) {
logit("tcgetattr: %.100s", strerror(errno));
failure = -1;
}
while (sshbuf_len(buf) > 0) {
if ((r = sshbuf_get_u8(buf, &opcode)) != 0)
fatal("%s: packet error: %s", __func__, ssh_err(r));
switch (opcode) {
case TTY_OP_END:
goto set;
case TTY_OP_ISPEED:
if ((r = sshbuf_get_u32(buf, &baud)) != 0)
fatal("%s: packet error: %s",
__func__, ssh_err(r));
if (failure != -1 &&
cfsetispeed(&tio, baud_to_speed(baud)) == -1)
error("cfsetispeed failed for %d", baud);
break;
case TTY_OP_OSPEED:
if ((r = sshbuf_get_u32(buf, &baud)) != 0)
fatal("%s: packet error: %s",
__func__, ssh_err(r));
if (failure != -1 &&
cfsetospeed(&tio, baud_to_speed(baud)) == -1)
error("cfsetospeed failed for %d", baud);
break;
#define TTYCHAR(NAME, OP) \
case OP: \
if ((r = sshbuf_get_u32(buf, &u)) != 0) \
fatal("%s: packet error: %s", __func__, \
ssh_err(r)); \
tio.c_cc[NAME] = u; \
break;
#define TTYMODE(NAME, FIELD, OP) \
case OP: \
if ((r = sshbuf_get_u32(buf, &u)) != 0) \
fatal("%s: packet error: %s", __func__, \
ssh_err(r)); \
if (u) \
tio.FIELD |= NAME; \
else \
tio.FIELD &= ~NAME; \
break;
#include "ttymodes.h"
#undef TTYCHAR
#undef TTYMODE
default:
debug("Ignoring unsupported tty mode opcode %d (0x%x)",
opcode, opcode);
/*
* SSH2:
* Opcodes 1 to 159 are defined to have a uint32
* argument.
* Opcodes 160 to 255 are undefined and cause parsing
* to stop.
*/
if (opcode > 0 && opcode < 160) {
if ((r = sshbuf_get_u32(buf, NULL)) != 0)
fatal("%s: packet error: %s", __func__,
ssh_err(r));
break;
} else {
logit("%s: unknown opcode %d", __func__,
opcode);
//.........这里部分代码省略.........
示例14: OpenConnection
int OpenConnection(void)
{
/*
UART_Start();
*/
int speed;
if (serial_port == NULL)
{
if ((serial_port = strdup(MODEMDEV)) == NULL) {
printf("[ERROR] malloc failed\n");
return(CYRET_ERR_FILE);
}
}
// tty_fd=open(argv[1], O_RDWR | O_NONBLOCK);
tty_fd=open(serial_port, O_RDWR | O_NOCTTY | O_SYNC);
if ( tty_fd == -1) {
printf("[ERROR] opening %s : %s\n",serial_port,strerror(errno));
exit(1);
// return (CYRET_ERR_FILE);
}
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_oflag=0;
tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=5;
switch(serial_speed) {
case 9600:
speed = B9600;
break;
case 19200:
speed = B19200;
break;
case 38400:
speed = B38400;
break;
case 57600:
speed = B57600;
break;
case 115200:
speed = B115200;
break;
default:
speed = COMSPEED;
printf("[INFO] unsupported speed %d, defaulting to %d\n",serial_speed,speed);
break;
}
if ( cfsetospeed(&tio,speed) == -1) { // 115200 baud
printf("[ERROR] %s\n",strerror(errno));
return (CYRET_ERR_FILE);
}
if (cfsetispeed(&tio,speed) == -1) { // 115200 baud
printf("[ERROR] %s\n",strerror(errno));
return (CYRET_ERR_FILE);
}
if (tcsetattr(tty_fd,TCSANOW,&tio) == -1) {
printf("[ERROR] %s\n",strerror(errno));
return (CYRET_ERR_FILE);
}
return(CYRET_SUCCESS);
}
示例15: _setup_port
// ------------------------------------------------------------------------------
// Helper Function - Setup Serial Port
// ------------------------------------------------------------------------------
// Sets configuration, flags, and baud rate
bool
Serial_Port::
_setup_port(int baud, int data_bits, int stop_bits, bool parity, bool hardware_control)
{
// Check file descriptor
if(!isatty(fd))
{
fprintf(stderr, "\nERROR: file descriptor %d is NOT a serial port\n", fd);
return false;
}
// Read file descritor configuration
struct termios config;
if(tcgetattr(fd, &config) < 0)
{
fprintf(stderr, "\nERROR: could not read configuration of fd %d\n", fd);
return false;
}
// Input flags - Turn off input processing
// convert break to null byte, no CR to NL translation,
// no NL to CR translation, don't mark parity errors or breaks
// no input parity check, don't strip high bit off,
// no XON/XOFF software flow control
config.c_iflag &= ~(IGNBRK | BRKINT | ICRNL |
INLCR | PARMRK | INPCK | ISTRIP | IXON);
// Output flags - Turn off output processing
// no CR to NL translation, no NL to CR-NL translation,
// no NL to CR translation, no column 0 CR suppression,
// no Ctrl-D suppression, no fill characters, no case mapping,
// no local output processing
config.c_oflag &= ~(OCRNL | ONLCR | ONLRET |
ONOCR | OFILL | OPOST);
#ifdef OLCUC
config.c_oflag &= ~OLCUC;
#endif
#ifdef ONOEOT
config.c_oflag &= ~ONOEOT;
#endif
// No line processing:
// echo off, echo newline off, canonical mode off,
// extended input processing off, signal chars off
config.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
// Turn off character processing
// clear current char size mask, no parity checking,
// no output processing, force 8 bit input
config.c_cflag &= ~(CSIZE | PARENB);
config.c_cflag |= CS8;
// One input byte is enough to return from read()
// Inter-character timer off
config.c_cc[VMIN] = 1;
config.c_cc[VTIME] = 10; // was 0
// Get the current options for the port
////struct termios options;
////tcgetattr(fd, &options);
// Apply baudrate
switch (baud)
{
case 1200:
if (cfsetispeed(&config, B1200) < 0 || cfsetospeed(&config, B1200) < 0)
{
fprintf(stderr, "\nERROR: Could not set desired baud rate of %d Baud\n", baud);
return false;
}
break;
case 1800:
cfsetispeed(&config, B1800);
cfsetospeed(&config, B1800);
break;
case 9600:
cfsetispeed(&config, B9600);
cfsetospeed(&config, B9600);
break;
case 19200:
cfsetispeed(&config, B19200);
cfsetospeed(&config, B19200);
break;
case 38400:
if (cfsetispeed(&config, B38400) < 0 || cfsetospeed(&config, B38400) < 0)
{
fprintf(stderr, "\nERROR: Could not set desired baud rate of %d Baud\n", baud);
return false;
}
break;
case 57600:
if (cfsetispeed(&config, B57600) < 0 || cfsetospeed(&config, B57600) < 0)
{
fprintf(stderr, "\nERROR: Could not set desired baud rate of %d Baud\n", baud);
//.........这里部分代码省略.........