【原创】mini2440下com1测试

板子上的位置为con2
linux /dev下为ttySAC1
串口助手为:串口调试助手V2.2
ps:max232+DB9母=转接板,usb2串口
现象:
串口助手发送的数据如数被mini2440发送回来。
 
#include
#include
#include
#include
#include
// # include
#include
#include
#include

static void Error(const char *Msg)
{
    fprintf (stderr, "%s\n", Msg);
    fprintf (stderr, "strerror() is %s\n", strerror(errno));
    exit(1);
}

static void Warning(const char *Msg)
{
     fprintf (stderr, "Warning: %s\n", Msg);
}

static inline void WaitFdWriteable(int Fd)
{
    fd_set WriteSetFD;
    FD_ZERO(&WriteSetFD);
    FD_SET(Fd, &WriteSetFD);
    if (select(Fd + 1, NULL, &WriteSetFD, NULL, NULL) < 0) {
 Error(strerror(errno));
    }
}

//int main(int argc, char **argv)
int main(void)
{
    int CommFd;

    struct termios TtyAttr;
//    struct termios BackupTtyAttr;

    int DeviceSpeed = B9600;
    int ByteBits = CS8;
    const char *DeviceName = "/dev/ttySAC1";

    CommFd = open(DeviceName, O_RDWR, 0);
    if (CommFd < 0)
Error("Unable to open device");
    if (fcntl(CommFd, F_SETFL, O_NONBLOCK) < 0)
      Error("Unable set to NONBLOCK mode");

    memset(&TtyAttr, 0, sizeof(struct termios));
    TtyAttr.c_iflag = IGNPAR;
    TtyAttr.c_cflag = DeviceSpeed | HUPCL | ByteBits | CREAD | CLOCAL;
    TtyAttr.c_cc[VMIN] = 1;

    if (tcsetattr(CommFd, TCSANOW, &TtyAttr) < 0)
        Warning("Unable to set comm port");

    for (;;) {
unsigned char Char = 0;
fd_set ReadSetFD;
write(CommFd, &Char, 1);
FD_ZERO(&ReadSetFD);
FD_SET(CommFd, &ReadSetFD);
if (select(CommFd + 1, &ReadSetFD, NULL, NULL, NULL) < 0) {
   Error(strerror(errno));
}

while (read(CommFd, &Char, 1) == 1) {
WaitFdWriteable(CommFd);
if (write(CommFd, &Char, 1) < 0) {
Error(strerror(errno));
}
}

    }
    return 0;
}

 

你可能感兴趣的:(【原创】mini2440下com1测试)