RS232 baudrate setting
#include <stdio.h>
int getbaud(int fd)
{
struct termios termAttr;
int inputSpeed = -1;
speed_t baudRate;
tcgetattr(fd, &termAttr);
/* Get the input speed. */
baudRate = cfgetispeed(&termAttr);
switch (baudRate)
{
case B0: inputSpeed = 0; break;
case B50: inputSpeed = 50; break;
case B110: inputSpeed = 110; break;
case B134: inputSpeed = 134; break;
case B150: inputSpeed = 150; break;
case B200: inputSpeed = 200; break;
case B300: inputSpeed = 300; break;
case B600: inputSpeed = 600; break;
case B1200: inputSpeed = 1200; break;
case B1800: inputSpeed = 1800; break;
case B2400: inputSpeed = 2400; break;
case B4800: inputSpeed = 4800; break;
case B9600: inputSpeed = 9600; break;
case B19200: inputSpeed = 19200; break;
case B38400: inputSpeed = 38400; break;
case B57600: inputSpeed = 57600; break;
case B115200: inputSpeed = 115200; break;
}
return inputSpeed;
}
int initport(int fd)
{
struct termios options;
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
int fd=-1;
int getbaud(int fd)
{
struct termios termAttr;
int inputSpeed = -1;
speed_t baudRate;
tcgetattr(fd, &termAttr);
/* Get the input speed. */
baudRate = cfgetispeed(&termAttr);
switch (baudRate)
{
case B0: inputSpeed = 0; break;
case B50: inputSpeed = 50; break;
case B110: inputSpeed = 110; break;
case B134: inputSpeed = 134; break;
case B150: inputSpeed = 150; break;
case B200: inputSpeed = 200; break;
case B300: inputSpeed = 300; break;
case B600: inputSpeed = 600; break;
case B1200: inputSpeed = 1200; break;
case B1800: inputSpeed = 1800; break;
case B2400: inputSpeed = 2400; break;
case B4800: inputSpeed = 4800; break;
case B9600: inputSpeed = 9600; break;
case B19200: inputSpeed = 19200; break;
case B38400: inputSpeed = 38400; break;
case B57600: inputSpeed = 57600; break;
case B115200: inputSpeed = 115200; break;
}
return inputSpeed;
}
int initport(int fd)
{
struct termios options;
// Get the current options for the port...
tcgetattr(fd, &options);
// Set the baud rates to 38400...
cfsetispeed(&options, B38400);
cfsetospeed(&options, B38400);
// Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
sprintf(devicename, "/dev/ttyS%d", atoi(argv[1]));
printf("devicename: %s \n", devicename);
fd = open(devicename, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
printf("open_port: Unable to open %s \n", devicename);
tcgetattr(fd, &options);
// Set the baud rates to 38400...
cfsetispeed(&options, B38400);
cfsetospeed(&options, B38400);
// Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
// Set the new options for the port...
tcsetattr(fd, TCSANOW, &options);
return 1;
}
int main(int argc, char ** argv)
{
char devicename[32];
tcsetattr(fd, TCSANOW, &options);
return 1;
}
int main(int argc, char ** argv)
{
char devicename[32];
if( argv[1] == NULL )
{
{
printf("please input ttys unmber .......\n");
return 1;
}
sprintf(devicename, "/dev/ttyS%d", atoi(argv[1]));
printf("devicename: %s \n", devicename);
fd = open(devicename, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
printf("open_port: Unable to open %s \n", devicename);
return 1;
}
else
{
printf("open_port: open %s success \n", devicename);
printf("open_port: open %s success \n", devicename);
fcntl(fd, F_SETFL, 0);
}
printf("baud=%d\n", getbaud(fd));
initport(fd);
close(fd);
}
printf("baud=%d\n", getbaud(fd));
initport(fd);
close(fd);
}
你好不好意思打擾一下
回覆刪除Windows下Cygwin Serial port RS-232的問題
小弟想在cygwin下透過rs232,用C寫一個可以跟我開發板通訊的程式.
參照http://tldp.org/HOWTO/Serial-Programming-HOWTO/
的sample code 還是無法寫出....請板上先進幫幫忙指導我一下.
另外在code裡面
.....
#define MODEMDEVICE "/dev/ttyS1" //在我的cygwin並沒有這個個路徑的檔案??
我的最終目的是想讀取開發版的資料再拿來利用,可行嗎?