Code running problems !!!

Source: Internet
Author: User
Code running problems !!! -- Linux general technology-Linux programming and kernel information. The following is a detailed description. When I am running the following code, I will not stop. Please check it !!!!

# Include /* Standard input/output definition */
# Include /* Standard function library definition */
# Include /* Unix standard function definition */
# Include
# Include
# Include /* File control definition */
# Include /* PPSIX terminal control definition */
# Include /* Error Code definition */
# Define COM1 1 // "/dev/ttyS0" file: // Serial Port 1
# Define COM2 2 // "/dev/ttyS1" file: // Serial Port 2
# Define TURE 0
# Define FALSE-1
Void OpenSerialPort (); // open the serial port
Void CloseSerialPort (); // close the serial port
Void ReadSerialPort (); // read data from the serial port
Void WriteSerialPort (); // write data to the serial port
Void SetSerialPort ();
Struct termios opt;
Int fd;
Char buffR [512];
Char buffW [512];
Int LenR;
Int LenW;
Int OpenDev (char * Dev );
Void setspeed (int fd, int speed );
Int setParity (int fd, int databits, int stopbits, int parity );

/* ===================================================== ======================================
Private function initialization serial port
========================================================== ======================================= */


Int OpenDev (char * Dev)
{
Int fd = open (Dev, O_RDWR );
If (fd =-1)
{
Perror ("Can't Open Serial Port"); // an error occurs when the Serial Port file is opened.
Return-1;
}
Else
{
Printf ("open ");
Return fd; // the ID of the returned object.
}
}





/* ===================================================== ==========================================
Set the serial communication rate

File handle for opening a serial port of the fd type int
Speed int serial port speed
Return void
========================================================== ========================================= */
Void setspeed (int fd, int speed)
{
Int I;
Int status;
Int name_arr [] = {38400,19200, 9600,4800, 2400,1200, 300,384 300 ,19200, 9600,4800, 2400,1200 ,};
Int speed_arr [] = {B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, B4800, B2400, B1200, B300 ,};
Struct termios Opt;
Tcgetattr (fd, & Opt );
For (I = 0; I {
If (speed = name_arr )
{
Tcflush (fd, TCIOFLUSH );
Cfsetispeed (& Opt, speed_arr);
Cfsetospeed (& Opt, speed_arr);
Status = tcsetattr (fd, TCSANOW, & Opt );
If (status! = 0)
Perror ("tcsetattr fd1 ");
Return;
}
Tcflush (fd, TCIOFLUSH );
}
}
/* ===================================================== =========================================== */



/* ===================================================== ======================================

Set the checkpoint for private functions


* @ Brief set the serial data bit, stop bit and verify bit
* @ Param fd type int indicates the serial port file handle opened
* @ Param databits type int data bit value: 7 or 8
* @ Param stopbits type int stop bit value: 1 or 2
* @ Param parity type int valid type values: N, E, O, and S
========================================================== ===================================== */
Int setParity (int fd, int databits, int stopbits, int parity)
{
Struct termios options;

If (tcgetattr (fd, & options )! = 0)
{
Perror ("SetupSerial 1 ");
Return (FALSE );
}
Options. c_cflag & = ~ CSIZE;

Switch (databits)
{
Case 7:
Options. c_cflag | = CS7;
Break;
Case 8:
Options. c_cflag | = CS8;
Break;
Default:
Fprintf (stderr, "Unsuipported 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;
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 );
}

If (parity! = 'N ')
Options. c_iflag | = INPCK;

Options. c_cc [VTIME] = 150;
Options. c_cc [VMIN] = 0;

Tcflush (fd, TCIFLUSH );

If (tcsetattr (fd, TCSANOW, & options )! = 0)
{
Perror ("SetupSerial 3 ");
Return (FALSE );
}

Return (TURE );
}

/* ===================================================== =========================================== */

/*---------------------------------------------------------
Open the serial port using public functions
-------------------------------------------------------*/
Void OpenSerialPort (int com)
{
Char * dev;
Switch (com)
{
Case COM1:
Dev = "/dev/ttyS0"; break;
Case COM2:
Dev = "/dev/ttyS1"; break;
Default:
Exit (-1 );
}
Fd = OpenDev (dev );
Setspeed (fd, B9600 );

If (setParity (fd, 8, 1, 'n') =-1)
{
Printf ("Set Parity Error \ n ");
Exit (-1 );
}
Printf ("open shecff ");
}


/*---------------------------------------------------------
========================================================== ============================
Public Functions read serial port information
========================================================== ==============================
-------------------------------------------------------*/
Void ReadSerialPort ()
{
Int nread;
While (nread! = 0)
{
While (nread = read (fd, buffR, 511)> 0 ))
{
Printf ("\ nLen % d \ n", nread );
BuffR [nread + 1] = '\ 0 ';
Printf ("\ n % s", buffR );
}
If (nread =-1)
{
Printf ("read error! \ N ");
Exit (-1 );
}
}
}



/*---------------------------------------------------------
========================================================== ============================
Public Functions send serial port information
========================================================== ==============================
-------------------------------------------------------*/
Void WriteSerialPort (char * p)
{

Char * buffw;



Buffw = p;
If (write (fd, buffw, 512 ))
{
Printf ("write is suff ");
}

}



/*---------------------------------------------------------
========================================================== ============================
Public Function close serial port
========================================================== ==============================
-------------------------------------------------------*/

Void CloseSerialPort ()
{
Close (fd );
}
Int main (void)
{

Char * l = "fdfdfdfdfdf ";
OpenSerialPort (COM1 );
WriteSerialPort (l );
ReadSerialPort ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.