日期:2014-05-16  浏览次数:20667 次

在linux下如何驱动本机的串口
在linux下如何驱动本机的串口,小弟急用

------解决方案--------------------

#include <stdio.h> /* perror, printf, puts, fprintf, fputs */
#include <unistd.h> /* read, write, close */
#include <fcntl.h> /* open */

#include <sys/types.h>
#include <string.h> /* bzero, memcpy */
#include <limits.h> /* CHAR_MAX */

//#include "vardef.h " /* int, INT16, INT8, Uint, UINT16, UINT8 */
#include <termios.h> /* tcgetattr, tcsetattr */
//#include <sys/signal.h>
class CSerialPort
{
public:
CSerialPort();
~CSerialPort();

int OpenComPort(int ComPort, int baudrate, int databit,
const char *stopbit, char parity);
void CloseComPort (void);
int ReadComPort (void *data, int datalength);
int WriteComPort (unsigned char * data, int datalength);
//int SendFile (const char *pathname);
virtual void GetData(double &RTData, double &TotalData);

private:
int fd; //File descriptor for the port
struct termios termios_old, termios_new;
fd_set fs_read, fs_write;
struct timeval tv_timeout;
//struct sigaction sigaction_io; /* definition of signal action */

//int SetPortSig ();
// static void signal_handler_IO (int status);
void SetBaudrate (int);
int GetBaudrate ();
void SetDataBit (int databit);
int BAUDRATE (int baudrate);
int _BAUDRATE (int baudrate);
int SetPortAttr (int baudrate, int databit,
const char *stopbit, char parity);
void SetStopBit (const char *stopbit);
void SetParityCheck (char parity);

};

#endif