[顯示setup serialport 1:bad file descriptor哪里錯了啊][
#include "serialPort.h"
#include <string.h>
#include <sys/ioctl.h>
using namespace std;
serialPort::serialPort()
{
fd = -1;
}
bool serialPort::OpenPort(const char* dev)
{
char* _dev = new char[32];
strcpy(_dev, dev);
fd = open(_dev, O_RDWR| O_NOCTTY | O_NDELAY ); //| O_NOCTTY | O_NDELAY
if (-1 == fd)
{
perror("Can't Open Serial Port");
return false;
}
int DTR_flag;
DTR_flag = TIOCM_DTR;
ioctl(fd, TIOCMBIS, &DTR_flag);//Set RTS pin
return true;
}
int serialPort::setup(int speed, int flow_ctrl, int databits, int stopbits, int parity)
{
int i;
int status;
struct termios options;
/*tcgetattr(fd,&options)得到與fd指向物件的相關引數,并將它們保存于options,該函式還可以測驗配置是否正確,該串口是否可用等。若呼叫成功,函式回傳值為0,若呼叫失敗,函式回傳值為1.
*/
if (tcgetattr(fd, &options) != 0)
{
perror("SetupSerial 1");
return(false);
}
int speed_arr[14] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[14] = { 38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
19200, 9600, 4800, 2400, 1200, 300, };
//設定串口輸入波特率和輸出波特率
for (i = 0; i < sizeof(speed_arr) / sizeof(int); i++)
{
if (speed == name_arr[i])
{
cfsetispeed(&options, speed_arr[i]);
cfsetospeed(&options, speed_arr[i]);
}
}
//修改控制模式,保證程式不會占用串口
options.c_cflag |= CLOCAL;
//修改控制模式,使得能夠從串口中讀取輸入資料
options.c_cflag |= CREAD;
//設定資料流控制
switch (flow_ctrl)
{
case 0://不使用流控制
options.c_cflag &= ~CRTSCTS;
break;
case 1://使用硬體流控制
options.c_cflag |= CRTSCTS;
break;
case 2://使用軟體流控制
options.c_cflag |= IXON | IXOFF | IXANY;
break;
}
//設定資料位
//屏蔽其他標志位
options.c_cflag &= ~CSIZE;
switch (databits)
{
case 5:
options.c_cflag |= CS5;
break;
case 6:
options.c_cflag |= CS6;
break;
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr, "Unsupported data size\n");
return (false);
}
//設定校驗位
switch (parity)
{
case 'n':
case 'N': //無奇偶校驗位。
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case 'o':
case 'O'://設定為奇校驗
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK;
break;
case 'e':
case 'E'://設定為偶校驗
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;// INPCK:打開輸入奇偶校驗
break;
case 's':
case 'S': //設定為空格
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr, "Unsupported parity\n");
return (false);
}
// 設定停止位
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB; break;
case 2:
options.c_cflag |= CSTOPB; break;
default:
fprintf(stderr, "Unsupported stop bits\n");
return (false);
}
//修改輸出模式,原始資料輸出
options.c_oflag &= ~OPOST;// OPOST:執行輸出處理;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);// ICANON:規范輸入;ECHO:進行回送;ECHOE:可見擦除符;ISIG:使終端產生的信號起作用;
//options.c_lflag &= ~(ISIG | ICANON);
//設定等待時間和最小接收字符
options.c_cc[VTIME] = 1; /* 讀取一個字符等待1*(1/10)s */
options.c_cc[VMIN] = 1; /* 讀取字符的最少個數為1 */
//如果發生資料溢位,接收資料,但是不再讀取 重繪收到的資料但是不讀
tcflush(fd, TCIFLUSH);
//激活配置 (將修改后的termios資料設定到串口中)
if (tcsetattr(fd, TCSANOW, &options) != 0)//tcsetattr函式用于設定終端的相關引數
{
perror("com set error!\n");
return (false);
}
return (true);
}
int serialPort::readBuffer(unsigned char *buffer, int size)
{
return read(fd, buffer, size);
}
int serialPort::writeBuffer(unsigned char *buffer, int size)
{
return write(fd, buffer, size);
}
uint8_t serialPort::getchar()
{
uint8_t t;
read(fd, &t, 1);
return t;
}
void serialPort::ClosePort()
{
close(fd);
}
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/239722.html
標籤:CPU和硬件區
上一篇:docker里已經存在容器A和容器B,怎么用link 將A和B關聯起來。是A和B都已經存在的情況下哈
下一篇:ubuntu使用w3m問題
