"Rayeager PX2 sharing" PX2 on the TTYS3 serial test program writing

Source: Internet
Author: User

In the PX2 Development Board has led to the TTYS3, if you use PX2 to develop or learn, from time to time will always need to use this serial port, and in the Android system, the way to use the serial port is also very simple, because in the PX2 source has been TTYS3 driver, All we need to do is compile the kernel (compile fangshihttp://bbs.chipspark.com/forum.p ... =%e4%b8%b2%e5%8f%a3) and manipulate the serial port just like the file, and then operate the serial port. All we need to do is start with an initial configuration, which is to set the baud rate, stop bit, data bit, parity check.

Note: Ttys3 connection if is DB9 foot, theoretically only connect Rx, TX,GND can work. The landlord is using this

1. The configuration of the serial port,

The configuration of the serial port is using the TERMIOS structure of POSIX terminal

The TERMIOS structure is defined as follows

struct Termios

{

tcflag_t C_iflag/* Lose entry Flag */

tcflag_t c_oflag/* OUTPUT Option flag */

tcflag_t c_cflag/* control option Flag */

tcflag_t c_lflag/* Local option Flag */

cc_t C_cc[nccs]/* Control features */

}

And the function of some parameters refer to the contents of the Attachment Linux under the serial port programming-very comprehensive-fine. Txt.zip

Core main configuration baud rate, check bit, data bit, stop bit,

And the landlord himself wrote the drive as follows, wherein the setting is baud rate 115200, invalid inspection, eight bits of data, a stop bit,


#include <stdio.h>/* standard input/output definition */

#include <stdlib.h>/* Standard function library definition */

#include <unistd.h>/*unix standard function definition */

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>/* File Control Definitions */

#include <termios.h>/*ppsix Terminal control definition */

#include <errno.h>/* error number Definition */

#include <linux/kernel.h>


int Opendev (char *dev) {


int fd = open (Dev, O_RDWR)

if ( -1 = = FD) {

Perror ("Can ' t Open Serial Port")

Return-1

}

Else

return FD

}

int set_opt (int fd,int nspeed,int Nbits,char nevent,int nstop) {

struct Termios Newios,oldios

if (Tcgetattr (FD, &oldios)!=0) {//Gets the endpoint value defined earlier

Perror ("Setupserial 1")

Return-1

}

Bzero (&newios,sizeof (Newios))//Reset the data in the Newios structure to 0

newios.c_cflag|=cread//enable to read and

newios.c_cflag&=~csize//Character Length Mask

Switch (nBits) {

Case 7:newios.c_cflag|=cs7break

Case 8:newios.c_cflag|=cs8break



}

Switch (nevent) {

Case ' 0 ':

Newios.c_cflag|=parenb

Newios.c_cflag|=parodd

Newios.c_cflag|= (inpck| Istrip)//even check

Break

Case ' 1 ':

Newios.c_cflag|=parenb

newios.c_cflag&=~parodd//parity Check

Break

Case ' N ':

newios.c_cflag&=~parenb//No Calibration

Break


}

Switch (nspeed) {//Set baud rate

Case 2400:

Cfsetispeed (&newios,b2400)

Cfsetospeed (&newios,b2400)

Break

Case 4800:

Cfsetispeed (&newios,b4800)

Cfsetospeed (&newios,b4800)

Break

Case 9600:

Cfsetispeed (&newios,b9600)

Cfsetospeed (&newios,b9600)

Break

Case 115200:

Cfsetispeed (&newios,b115200)

Cfsetospeed (&newios,b115200)

Break

Default

Cfsetispeed (&newios,b115200)

Cfsetospeed (&newios,b115200)

Break

}

if (nstop==1)

newios.c_cflag&=~cstopb//a Stop bit

else if (nstop==2)

{

newios.c_cflag|=cstopb//two Stop bit

newios.c_cc[vtime]=0//No delay

newios.c_cc[vmin]=0//no additional

Tcflush (Fd,tcioflush)//Refresh Output queue

}


Newios.c_lflag &= ~ (Icanon | Iexten | Isig | ECHO)//RAW data input

Newios.c_oflag &= ~opost//raw Data output

if (tcsetattr (Fd,tcsanow,&newios)!=0)//Assign configuration to POSIX terminal

{

Perror ("com set error")

Return-1


}


printf ("Set done\n")

return 0


}

int main (int argc, char **argv) {

int FD

int Nread,nwrite

Char Buff[8]

Char *dev = "/DEV/TTYS3"//serial Port 3

Char Bufsend[8]

FD = Opendev (dev)


if (set_opt (fd,115200,8, ' N ', 1) = = FALSE) {

printf ("Set Parity Errorn")

Exit (0)

}

memset (buff,0,8*sizeof (char))

if (strcmp (argv[1], "receive") ==0)

{

while (1) {

while ((nread = read (FD, buff, 8))//Read data, wait in the dead loop, output data,

{

printf ("Receive%d", nread)

printf ("\n%s", Buff)

}}}

memset (Bufsend, 0,8*sizeof (char))//emptying the data stack

if (strcmp (argv[1], "send") ==0) {//Send data, type character

printf ("Send message:")

Fgets (Bufsend,8,stdin)

printf ("\n%s", Bufsend)


Nwrite=write (fd,bufsend,8)


}


Close (FD)

Exit (0)

}


2. Program testing,

Confirm the connection is correct, the landlord began to send data, but, although the normal implementation of the serial port communication, but the data sent is garbled, the first landlord sent 111111111111, and received the data is GGGGGGGGGGG, it received is not ASCII code, and the corresponding hexadecimal is 67, the specific problem landlord is still in the study, estimated also to have a period of time, to solve this problem, according to the landlord's estimated probability there are two, one is the landlord is the use of DB9 interface, only a three line, and no VCC, it will be affected, two, landlord configuration serial port, Only the basic configuration, perhaps a configuration error. Here first put on the forum to share to everyone, follow up and continue to add, and everyone to encourage.

Serial Debugging assistant
Sscom32.rar

"Rayeager PX2 sharing" PX2 on the TTYS3 serial test program writing

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.