嵌入式串口测试程序代码
【1】首先将代码保存成t232.c,编译:
arm-eabi-gcc -o t232 t232.c -static
【2】执行(切记,波特率要设置成终端一样的波特率,否则执行后终端会死掉,一般是115200)
t232 115200 /dev/ttyS5
会提示:input example: H55AA to send hex 0x55,0xAA; A01 to send ascii '0', '1'
意思是可以输入16进制或者ascii码,例如键盘敲入:H55AA,或者:A01
可以接电脑端串口,打开串口调试程序看接收到的信息,电脑也可以发数据给设备。
【3】如果设备发出的数据无法被电脑的串口调试程序接收,有可能是设备的tty设备不能用。可以在设备控制端这样输入命令测试:
echo 123 > /dev/ttyS5
电脑端串口调试程序将显示123.
//This program test COM port on UT0500, it can receive any COM data and send HEX or ASCII data out
//To send HEX, type H followed by HEX data, for example: H55AA to send hex 0x55,0xAA;
//To send ASCII, type A plus ascii string, for example: A01 to send ascii '0', '1'\r\n;
#include <stdio.h>
//#include <asm/termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <ctype.h>
#include <sys/ioctl.h>
#define DEBUG
//for other baud rate, please change here:
static int BAUDRATE = B115200;
//use telnet's tty as console:
#define LCD_KEYPAD_DEVICE "/dev/tty"
static unsigned char * RS232_DEVICE = "/dev/s3c2410_serial0";
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1
volatile int STOP=FALSE;
static struct termios savedtio;
static void set_serial_raw(int fd, int with_hw_handshake) {
struct termios newtio;
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD ;
if (with_hw_handshake)
newtio.c_cflag |= CRTSCTS;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 1; /* inter-character timer unused */
newtio.c_cc[VMIN] = 0; /* no blocking, return immediately if none */
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
}
static int valid_baudrates[] = {9600, 19200, 38400, 57600, 115200, 230400, -1};
static int valid_baudsetting[] = {B9600, B19200, B38400, B57600, B115200, B230400, -1};
#define HEX(ch) ( isdigit(ch) ? (ch-'0') : (toupper(ch)-'A'+0x0A) )
int main(char argc, char** argv)
{
int fd485, fd_lk, c, res, i;
unsigned char buf[255], tmpstr[260], * tptr;
unsigned char obuf[512];
if (argc > 1) {
int ibaud = 0, idx;
if (!strcmp(argv[1], "-h")) {
puts("Usage: t232 [baudrate] [device]");
return -1;
}
if (sscanf(argv[1], "%d", &ibaud) == 1) {
for (idx = 0; valid_baudrates[idx] > 0; idx++) {
if (ibaud == valid_baudrates[idx])
break;
}
if (valid_baudrates[idx] < 0)
goto baud_error;
}
else {
baud_error:
puts("Valid baudrates:");
ibaud = 0;
while(1) {
printf("%d", valid_baudrates[ibaud]);
ibaud ++;
if (valid_baudrates[ibaud] > 0)
printf(", ");
else {
printf("\n");
break;
}
}
return -2;
}
BAUDRATE = valid_baudsetting[idx];
printf("setting baud rate to: %d\n", valid_baudrates[idx]);
if (argc > 2) {
RS232_DEVICE = argv[2];
printf("setting device to: %s\n", RS232_DEVICE);
}
}
else
printf("using default params:\n115200bps on %s\n", RS232_DEVICE);
tcgetattr(0,&savedtio);
fd_lk = open(LCD_KEYPAD_DEVICE, O_RDWR | O_NOCTTY );
if (fd_lk <0) {perror(LCD_KEYPAD_DEVICE); exit(-1); }
set_serial_raw(fd_lk, 0);
//please turn on dedicated tty's mux before running this program.
fd485 = open(RS232_DEVICE, O_RDWR | O_NOCTTY );
if (fd485 <0) {
perror(RS232_DEVICE);
tcsetattr(0,TCSANOW,&savedtio);
exit(-1);
}
#ifdef CTSRTS
#warning "with hard ware handshake"
set_serial_raw(fd485, 1);
#else
#warning "no hard ware handshake"
set_serial_raw(fd485, 0);
#endif
puts("MC2005 RS232 test, (C)Megawave Tech., ltd.(2005)\r\n");
puts("input example: H55AA to send hex 0x55,0xAA; A01 to send ascii '0', '1'\r\n");
tptr = tmpstr;
while (STOP==FALSE) { /* loop for input */
res = read(fd_lk,buf,255); /* return after 0~255 chars have been input */
if (res) {
for (c = 0; c < res; c++) {
int ch = buf[c];
putchar(ch);
if (ch == 8) { //<BackSpace >
if (tptr > tmpstr) {
tptr--;
putchar(' ');
putchar(ch);
}
}
else if (ch == 3) //<Ctrl-C>
STOP = TRUE;
else if (ch == 0x0D) { //<Enter>
int oidx = 0;
putchar(0x0A);
* tptr = 0;
printf("u'v entered: %s\r\n", tmpstr);
//input example: H55AA to send hex 0x55,0x55; A3031 to send ascii '0', '1'
if(toupper(*tmpstr)=='H') { //hex mode
for (i=0; i < (tptr-tmpstr-1) /2; i++) {
int ipp;
ipp = obuf[oidx++] = *(tmpstr+i) = HEX(*(tmpstr+1+2*i)) * 16 + HEX(*(tmpstr+1+2*i+1));
}
if ( !write(fd485, tmpstr, (tptr-tmpstr-1)/2) )
puts("232 HEX mode sent\r\n");
}
else { //ascii mode
memcpy(obuf+oidx, tmpstr+1, tptr-tmpstr-1);
oidx += tptr-tmpstr-1;
if ( !write(fd485, tmpstr+1, tptr-tmpstr-1) )
puts("232 ASCII mode sent\r\n");
}
{
int cline = oidx;
unsigned char * pob = obuf;
int lcnt;
printf("sending:\n\r");
while (cline > 0) {
if (cline >= 16)
lcnt = 16;
else
lcnt = cline;
cline -= lcnt;
for (i = 0; i < lcnt; i ++)
printf("%02X ", pob[i]);
for (;i<16;i++)
printf(" ");
printf(" ");
for (i = 0; i < lcnt; i ++)
printf("%c", isprint(pob[i])?pob[i]:'.');
printf("\n\r");
pob += lcnt;
}
}
tptr = tmpstr;
}
else if ((tptr - tmpstr) < 255)
*tptr++ = ch;
} //end for
fflush(stdout);
} //end if
res = read(fd485,buf,255); /* return after 0~255 chars have been input */
if (res) { //got sth. from RS485 line
buf[res]=0; /* so we can printf... */
printf("rx:%s:%d\r\n", buf,res);
for (c = 0; c < res; c++) {
int ch = buf[c];
printf("%02X ", ch);
}
printf("\r\n");
}
} //end of while
tcsetattr(0,TCSANOW,&savedtio);
return 0;
} // end of main.