第一个C程序,获取串行数据的问题 [英] First C Program, Problems getting serial data

查看:48
本文介绍了第一个C程序,获取串行数据的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调试我的第一个C程序(PIC MCU的固件)。问题

从我的设备上获取串行数据。我的get命令必须两次发送给
,以便PIC能够正确响应所需的数据。任何想法?

这里是有问题的代码,看看为什么命令不会触发的任何原因

''kbhit''第一次串口命令是发送了?:谢谢!


查克


******************** ****************************** **

while(1)//永远循环

{


//通过序列检查请求

if(kbhit())

{

得到(cmd);

//闪烁LED以显示正在处理的命令

output_high(LED_PIN);

delay_ms(200);

output_low(LED_PIN);

//检查正确的请求

strcpy(cmdcheck," CFGET");

if(strcmp(cmd,cmdcheck)= 1)//如果是我们的命令返回值

当前

//读取adc值

set_adc_channel(CURRENT_CHANNEL);

delay_us(ADC_DELAY);

s1 = read_adc();

current =(0.05 * s1); //检查这个缩放(10位全1'= 5.0)

//发送请求的数据

printf(" CF%3.1f\ r \\ n,当前);

}

delay_ms(50); //延迟50毫秒再做一个周期

} //结束时

}

*********** ****************************************

- -


问候,

Chuck Faranda
http://ccdastro.net

解决方案

文章< tt ****** ************************@comcast.com>,

Chuck Faranda< as ******* @ hotmail.com>写道:

我正在尝试调试我的第一个C程序(PIC MCU的固件)。问题
是从我的设备返回串行数据。我的get命令必须两次发送,以便PIC能够正确响应所需的数据。有什么想法吗?
这里有问题的代码,看看为什么命令不会在第一次发送串行命令时触发''kbhit'的原因?:谢谢!
********************************************** ****
while(1)//永远循环
//
//通过序列检查请求
if(kbhit())




kbhit()不是C库的标准部分,你没有为它提供

代码,也没有提供关于*的文件*确实如此。任何猜测我们可能会b / b $ b bb为什么你的代码没有达到预期的效果......好吧,猜测。

您需要查看特定于平台的文档。


狂猜:另一端是C程序,另一端设置缓冲

输出[可能是默认]而另一端确实没有具体的

fflush()强制输出从缓冲区发送到设备驱动程序。

-

有一些想法如此错误,只有一个非常聪明的人才能相信他们。 - 乔治奥威尔


哎呀,对不起。这是编译器附带的唯一文档:


缓冲区可能已满,我会看到可以做什么

关于刷新,谢谢。


KBHIT()

语法:

value = kbhit()


参数:




如果getc()返回:

0(或FALSE)将需要等待一个角色进来,1

(或TRUE)如果一个角色已准备好接受getc()


功能:

如果RS232受软件控制,如果

在RS232 RCV引脚上发送字符的起始位,则此函数返回TRUE。如果

RS232是硬件,则此函数返回TRUE是一个字符已经收到并且在硬件缓冲区中等待getc()读取。这个

函数可用于轮询数据而无需停止并等待

数据出现。请注意,对于软件RS232,此功能

应至少调用比特率的10倍,以确保传入的数据不会丢失。


可用性:

所有设备。


需要

#use rs232

示例:

char timed_getc(){


超时;


timeout_error = FALSE ;


超时= 0;


while(!kbhit()&&(++ timeout< 50000))// 1 / 2秒


delay_us(10);


if(kbhit())


return(getc());


else {


timeout_error = TRUE;


返回(0);


}


}


示例文件:

ex_tgetc.c


另见:

getc(),#USE RS232


-


问候,

Chuck Faranda
HTTP:// ccdastr o.net

" Walter Roberson" < RO ****** @ ibd.nrc-cnrc.gc.ca>在消息中写道

news:e1 ********** @ canopus.cc.umanitoba.ca ...

在文章< tt ** ****************************@comcast.com>,
Chuck Faranda< as ******* @ hotmail.com>写道:

我正在尝试调试我的第一个C程序(PIC MCU的固件)。
问题
从我的设备返回串行数据。我的get命令必须两次发送,以便PIC能够正确响应所需的数据。有什么想法吗?
这里有问题的代码,看看为什么命令不会在第一次发送串口命令时触发''kbhit'的原因?:谢谢!


********************************* *************** ****
while(1)//永远循环
//
//检查请求通过序列号
if(kbhit())



kbhit()不是C库的标准部分,也没有为它提供代码,也没有关于*确切*它的文档。任何猜测我们都可能会说明为什么你的代码没有达到预期的效果......好吧,
猜测。
您需要查看特定于平台的文档狂野猜测:另一端是C程序,另一端设置为
缓冲输出[可能是默认]而另一端没有专门
fflush()强制输出从缓冲区发送到设备
驱动程序。
-
有一些想法错误,只有一个非常聪明的人
可以相信他们。 - George Orwell



Chuck Faranda写道:

我正在尝试调试我的第一个C程序(PIC的固件) MCU)。
if(strcmp(cmd,cmdcheck)= 1)//如果是我们的命令返回值


你可能想要:


if(strcmp(cmd,cmdcheck)== 0)


即使用等于运算符而不是赋值运算符,

并将strcmp的返回值与零进行比较......


I''m trying to debug my first C program (firmware for PIC MCU). The problem
is getting serial data back from my device. My get commands have to be sent
twice for the PIC to respond properly with the needed data. Any ideas?
Here''s the code in question, see any reason why a command would not trigger
the ''kbhit'' the first time a serial command is sent?: Thanks!

Chuck

************************************************** **
while(1) // loop forever
{

// check for a request via serial
if (kbhit())
{
gets(cmd);
//flash the LED to show a command is being processed
output_high(LED_PIN);
delay_ms(200);
output_low(LED_PIN);
//check for proper request
strcpy(cmdcheck,"CFGET");
if (strcmp(cmd, cmdcheck)=1) // if it is our command return the value
of current
//read the adc value
set_adc_channel(CURRENT_CHANNEL);
delay_us(ADC_DELAY);
s1 = read_adc();
current = (0.05*s1); // check this scaling (10 bits all 1''s = 5.0)
//send the requested data
printf("CF%3.1f\r\n", current);
}
delay_ms(50); // delay 50 ms and do another cycle
} // end while
}
************************************************** **
--

Regards,
Chuck Faranda
http://ccdastro.net

解决方案

In article <tt******************************@comcast.com>,
Chuck Faranda <as*******@hotmail.com> wrote:

I''m trying to debug my first C program (firmware for PIC MCU). The problem
is getting serial data back from my device. My get commands have to be sent
twice for the PIC to respond properly with the needed data. Any ideas?
Here''s the code in question, see any reason why a command would not trigger
the ''kbhit'' the first time a serial command is sent?: Thanks! ************************************************* ***
while(1) // loop forever
{

// check for a request via serial
if (kbhit())



kbhit() is not a standard part of the C library, and you have not provided
code for it nor documentation on what *exactly* it does. Any guesses we might
make about why your code does not do what you expect would be... well, guesses.
You need to check out your platform-specific documentation.

Wild guess: the other end is a C program and the other end is set to buffer
output [probably by default] and the other end does not specifically
fflush() to force the output to be sent from the buffers to the device driver.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell


Oops, sorry. Here''s the only docs that came with the compiler:

It may be possible fot the buffers to be full, I''ll see what can be done
about flushing, thanks.

KBHIT()
Syntax:
value = kbhit()

Parameters:
None

Returns:
0 (or FALSE) if getc() will need to wait for a character to come in, 1
(or TRUE) if a character is ready for getc()

Function:
If the RS232 is under software control this function returns TRUE if
the start bit of a character is being sent on the RS232 RCV pin. If the
RS232 is hardware this function returns TRUE is a character has been
received and is waiting in the hardware buffer for getc() to read. This
function may be used to poll for data without stopping and waiting for the
data to appear. Note that in the case of software RS232 this function
should be called at least 10 times the bit rate to ensure incoming data is
not lost.

Availability:
All devices.

Requires
#use rs232

Examples:
char timed_getc() {

long timeout;

timeout_error=FALSE;

timeout=0;

while(!kbhit()&&(++timeout<50000)) // 1/2 second

delay_us(10);

if(kbhit())

return(getc());

else {

timeout_error=TRUE;

return(0);

}

}

Example Files:
ex_tgetc.c

Also See:
getc(), #USE RS232

--

Regards,
Chuck Faranda
http://ccdastro.net
"Walter Roberson" <ro******@ibd.nrc-cnrc.gc.ca> wrote in message
news:e1**********@canopus.cc.umanitoba.ca...

In article <tt******************************@comcast.com>,
Chuck Faranda <as*******@hotmail.com> wrote:

I''m trying to debug my first C program (firmware for PIC MCU). The
problem
is getting serial data back from my device. My get commands have to be
sent
twice for the PIC to respond properly with the needed data. Any ideas?
Here''s the code in question, see any reason why a command would not
trigger
the ''kbhit'' the first time a serial command is sent?: Thanks!


************************************************ ****
while(1) // loop forever
{

// check for a request via serial
if (kbhit())



kbhit() is not a standard part of the C library, and you have not provided
code for it nor documentation on what *exactly* it does. Any guesses we
might
make about why your code does not do what you expect would be... well,
guesses.
You need to check out your platform-specific documentation.

Wild guess: the other end is a C program and the other end is set to
buffer
output [probably by default] and the other end does not specifically
fflush() to force the output to be sent from the buffers to the device
driver.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell



Chuck Faranda wrote:

I''m trying to debug my first C program (firmware for PIC MCU). if (strcmp(cmd, cmdcheck)=1) // if it is our command return the value



you probably want :

if (strcmp(cmd, cmdcheck) == 0)

i.e. use the equality operator instead of the assignment operator,
and compare the return value of strcmp with zero...


这篇关于第一个C程序,获取串行数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆