1.C51单片机发送字符串程序怎么写
#include<reg52.h>; //包含头文件,
#include"delay.h"
/*------------------------------------------------
函数声明
------------------------------------------------*/
void SendStr(unsigned char *s);
/*------------------------------------------------
串口初始化
------------------------------------------------*/
void InitUART (void)
{
SCON = 0x50; // SCON: 模式 1, 8-bit UART, 使能接收
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit 重装
TH1 = 0xFD; // TH1: 重装值 9600 波特率 晶振 11.0592MHz
TR1 = 1; // TR1: timer 1 打开
EA = 1; //打开总中断
//ES = 1; //打开串口中断
}
/*------------------------------------------------
主函数
------------------------------------------------*/
void main (void)
{
InitUART();
while (1)
{
SendStr("UART test!");
DelayMs(240);//延时循环发送
DelayMs(240);
}
}
/*------------------------------------------------
发送一个字节
------------------------------------------------*/
void SendByte(unsigned char dat)
{
SBUF = dat;
while(!TI);
TI = 0;
}
/*------------------------------------------------
发送一个字符串
------------------------------------------------*/
void SendStr(unsigned char *s)
{
while(*s!='\0')// \0 表示字符串结束标志,
//通过检测是否字符串末尾
{
SendByte(*s);
s++;
}
}
2.sendmessage这个发送消息的函数如何发送字符串
首先说怎么发送,然后说怎么接收:
自己定义一个消息,加上一个消息响应函数。然后用SendMessage把字符串的地址发过去。
比如VC环境:
在stdafx.h中,
#define MY_MESSAGE USER+1
定义一个函数用来发送,可以是定时器,可以是按钮。
void send()
{
CString str;
str = "12345";
::SendMessage(hWnd,MY_MESSAGE ,(WPARAM)&str);
}
在你的类头文件.h中:
afx_msg void MyFunction(WPARAM wParam,LPARAM lParam);
在.cpp中
BEGIN_MESSAGE_MAP下面加入:
ON_MESSAGE(MY_MESSAGE ,MyFunction)
在后面加上你的函数体
void 你的类名::MyFunction(WPARAM wParam,LPARAM lParam)
{
//如果你发送的字符串的地址在WPARAM中,则用WPARAM来接收
CString* pStr = (CString*)wParam.
AfxMessageBox(*pStr);
}
你要注意的是:这只能适合SendMessage,如果你要使用PostMessage,就要把你发送的字符串设定为类的成员,或者全局的,不能是临时变量。因为SendMessage是要让消息响应函数执行完了才会返回,所以str是不会被系统释放的。PostMessage是直接返回,于是你传了一个str的地址过去,但是消息响应函数执行的时候,str已经被删除了,这个时候的地址是一个不可知的内容,程序就会发生内存泄露,所以还是推荐你定义一个字符串专门用来发送。
3.字符串 自定义函数怎么写
#include <stdio.h>
#include <string.h>
///字符串是不方便直接return的,
///常用方法是输入串和输出串都传入函数,
///在函数里给输出串赋值。
void go(const char* input, const char* output)
{
int i = 0;
if (input && output)
{
while (input[i])
{
if (input[i] >= 'a' && input[i] <= 'z')
{
output[i] = input[i] - 'a' + 'A';
}
else
{
output[i] = input[i];
}
}
}
}
int main()
{
const int len = 16;
char a[len] = "abcde";
char b[len];
go(a, b);
printf("%s\n", a);
printf("%s\n", b);
return 0;
}
4.C语言中如何调用一个函数输入字符串.这个函数怎么写
#include <string.h>
#include <stdio.h>
main()
{char a[100];
gets(a);
printf("%s\n",a);
}
gets()函数用来从标准输入设备(键盘)读取字符串直到换行符结束,但换行符会被丢弃,然后在末尾添加'\0'字符。其调用格式为: gets(s); 其中s为字符串变量(字符串数组名或字符串指针)。 gets(s)函数与scanf("%s:",&s)/* scanf("%s",s) */相似,但不完全相同,使用scanf("%s",&s);函数输入字符串时存在一个问题,就是如果输入了空格会认为字符串结束,空格后的字符将作为下一个输入项处理,但gets()函数将接收输入的整个字符串直到遇到换行为止。
要函数就这样:
#include <string.h>
#include <stdio.h>
void sr(char *a)
{ gets(a);
}
main()
{char a[100];
sr(a);
printf("%s\n",a);
}
哦哦O(∩_∩)O^_^
5.编写连接字符串函数
public String fun1(String str1,String str2){
return str1+str2;
}
public int fun2(String str){
String temp="";
for(int i = 0; i < str.length()-1; i ++){
try{
String ch = str.substring(i,i+1);
int num = Integer.parseInt(ch);
temp += ch;
}catch(Exception e){}
}
return Integer.parseInt(temp.trim());
}
6.单片机发送一个13位字符串该怎么写代码啊
#include<reg52.h>
unsigned char kz[]={0x0, 0x02, 0x85 ,0x03 ,0x00 ,0x00 ,0x00 ,0x8d ,0x40 ,0x10,0x 03 0x,21 ,0xea};
void SendByte(unsigned char dat)
{
SBUF = dat;
while(!TI);
TI = 0;
}
void InitUART (void)
{
SCON = 0x50; // SCON: 模式 1, 8-bit UART, 使能接收
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit 重装
TH1 = 0xF3; // TH1: 重装值2400
TR1 = 1; // TR1: timer 1 打开
EA = 1; //打开总中断
//ES = 1; //可以不要,如果不接入中断函数的话
}
void main (void)
{
unsigned char q;
InitUART();
for(q=0;q<13;q++)
{
SendByte(kz[q]);
}
while();
}
7.字符串的复制如何写
头文件必须加上 #include
下面为string.h文件中函数的详细用法,附加实例:
1、strcpy
函数名: strcpy
功 能: 拷贝一个字符串到另一个
用 法: char *strcpy(char *destin, char *source);
程序例:
#include
#include
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
stpcrpy(string, str1);
printf("%s\n", string);
return 0;
}
其他string.h文件中函数的详细用法看链接
转载请注明出处育才学习网 » 发送字符串函数怎么写