---
title: "凌阳SPCE3200精简开发板：解决附带实验程序中printf无法输出串口调试日志信息的问题"
author: "mmdev"
date: "2010-12-31T01:05:00Z"
tags: []
slug: "spce3200-printf"
draft: false
lastmod: "2012-04-14T20:18:17Z"
id: 1486474
url: "https://mmdev.iteye.com/blog/1486474"
---

我在运行开发板光盘上的裸机实验程序时，在程序中写printf("xxxx/n")输出调试信息，但结果却并没有如愿地在串口终端看到输出。而使用ECOS的实验程序则大多数情况下printf都能正常地在串口输出，少部分实验不能输出时，也可以用diag\_printf输出。

为何裸机实验程序会有问题呢？我新建了一个SPCE3200标准工程，在里面写printf，结果又发现串口能正常输出。也就是说，我自己新建的工程是OK的，光盘上旧的工程反而不行。

继续查找原因。经过一番比较，终于发现，我新建的工程里的libgloss.c与光盘上裸机实验的很不一样。这个文件里定义了标准的输入输出实现。比如这个函数，光盘上的里面是直接返回-1：

/*

\* write -- write bytes to the serial port. Ignore fd, since

\* stdout and stderr are the same. Since we have no filesystem,

\* open will only return an error.

\*/

int

\_write\_r (struct \_reent \*r, int fd, char \*buf, int nbytes)

{

return -1;

}

而我新建的却有实际内容：

/*

\* write -- write bytes to the serial port. Ignore fd, since

\* stdout and stderr are the same. Since we have no filesystem,

\* open will only return an error.

\*/

int

\_write\_r (struct \_reent \*r, int fd, char \*buf, int nbytes)

{

int bk\_nbytes = nbytes;

if(isatty(fd))

{

while(bk\_nbytes-- &gt; 0)

{

\_putc\_r(\*buf++);

}

}

return nbytes;

}

于是把该文件直接复制到光盘裸机实验程序目录覆盖原有文件，再编译下载运行，一切正常，可以看到串口打印输出了。