1、做两个窗体程序,一个代表电视机,一个代表遥控器(里面的功能可通过按钮实现)
2、两个窗体间如何通信,这是这个程序的关键点,网上有也有很多实现两个程序通信,不过你可选择Socket进行实验。具体方法:在表视机中加入Socket监听,等待接收遥控器的信号,并且解析遥控器的信号,进行相应的处理;在遥控器程序当中,根据不同的功能向电视机发送信号。
3、至于打开、关闭等功能,仔细考虑设计好每个功能的相应的顺序和关系即可搞定
4、自动搜索,这个只要设定一个最大值,Random生成一些数值即可
总体思路可以这样,且体的思节再考虑吧,这个也是一个挺好的题目。
下面是一个用C写的遥控器程序.能在数码管上显示键码.#include <reg52.h>
#define c(x) (x*110592/120000)
sbit Ir_Pin=P3^3
unsigned char code Led_Tab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,
0xf8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E} //共阳极数码显示码0-F.
unsigned char code Led_Sel[]={0xe,0xd,0xb,0x7}
unsigned char Led_Buf[4]//显示缓冲区
char Led_Index //位选
unsigned char Ir_Buf[4]//用于保存解码结果
//==============================================================
//数码管扫描
timer0() interrupt 1 using 1
{
TL0=65536-1000
TH0=(65536-1000)/256//定时器0设定约1000us中断一次,用于数码管扫描
P0=0xff
P2=Led_Sel[Led_Index] //位选
P0=Led_Tab[Led_Buf[Led_Index]] //段选
if(++Led_Index>3) Led_Index=0 //四个扫描完了,到第一个数码管
}
//==============================================================
unsigned int Ir_Get_Low()
{
TL1=0
TH1=0
TR1=1
while(!Ir_Pin &&(TH1&0x80)==0)
TR1=0
return TH1*256+TL1
}
//=============================================================
unsigned int Ir_Get_High()
{
TL1=0
TH1=0
TR1=1
while(Ir_Pin &&(TH1&0x80)==0)
TR1=0
return TH1*256+TL1
}
//==============================================================
main()
{
unsigned int temp
char i,j
Led_Index=1
TMOD=0x11
TL0=65536-1000
TH0=(65536-1000)/256//定时器0设定约1000us中断一次,用于数码管扫描
EA=1
ET0=1
TR0=1
Led_Buf[0]=0
Led_Buf[1]=0
Led_Buf[2]=0
Led_Buf[3]=0//显示区设成0
do{
restart:
while(Ir_Pin)
temp=Ir_Get_Low()
if(temp<c(8500) || temp>c(9500)) continue//引导脉冲低电平9000
temp=Ir_Get_High()
if(temp<c(4000) || temp>c(5000)) continue//引导脉冲高电平4500
for(i=0i<4i++) //4个字节
for(j=0j<8j++) //每个字节8位
{
temp=Ir_Get_Low()
if(temp<c(200) || temp>c(800)) goto restart
temp=Ir_Get_High()
if(temp<c(200) || temp>c(2000)) goto restart
Ir_Buf[i]>>=1
if(temp>c(1120)) Ir_Buf[i]|=0x80
}
Led_Buf[0]=Ir_Buf[2]&0xf
Led_Buf[1]=(Ir_Buf[2]/16)&0xf
Led_Buf[2]=Ir_Buf[3]&0xf
Led_Buf[3]=(Ir_Buf[3]/16)&0xf//显示结果
}while(1)
}
写好了,两个类分别为:
public enum Mode{
Heating,
Cooling
}
public enum UpDown
{
Up,
Down
}
public class AirConditioner
{
private bool powerOn
private Mode currentMode
private int currentTemp
public AirConditioner()
{
this.powerOn = false
this.currentMode = Mode.Cooling
this.currentTemp = 16
}
public void SwichPower()
{
this.powerOn = !powerOn
}
internal void SetMode(Mode mode)
{
this.currentMode = mode
}
internal void SetTemp(UpDown upDown)
{
switch (upDown)
{
case UpDown.Up:
if (this.currentTemp <= 30)
{
this.currentTemp++
}
break
case UpDown.Down:
if (this.currentTemp >= 16)
{
this.currentTemp--
}
break
}
}
public override string ToString()
{
return string.Format("Current Status:\r\nPower: {0}\r\nMode: {1}\r\nTemp: {2}", this.powerOn ? "On" : "Off", this.currentMode, this.currentTemp)
}
} public class RemoteController
{
private AirConditioner conditioner
public RemoteController()
{
this.conditioner = new AirConditioner()
}
public void SwitchPower()
{
this.conditioner.SwichPower()
Console.WriteLine(this.conditioner)
}
public void SetMode(Mode mode)
{
this.conditioner.SetMode(mode)
Console.WriteLine(this.conditioner)
}
public void SetTemp(UpDown upDown)
{
this.conditioner.SetTemp(upDown)
Console.WriteLine(this.conditioner)
}
}
值得注意的是,“通过直接 *** 作空调不能进行调节温度、改变模式(制热、制冷)。”这一条说明这些方法应该不允许被声明为public,但是又要能够被遥控器访问,所以应该声明为internal
测试代码:
static void Main(string[] args){
var controller = new RemoteController()
Console.WriteLine("Turn on the conditioner...\r\n")
controller.SwitchPower()
Console.WriteLine("================================================================")
Console.WriteLine("Turn off the conditioner...\r\n")
controller.SwitchPower()
Console.WriteLine("================================================================")
Console.WriteLine("Set the mode as \"cooling\"...\r\n")
controller.SetMode(Mode.Cooling)
Console.WriteLine("================================================================")
Console.WriteLine("Set the mode as \"Heating\"...\r\n")
controller.SetMode(Mode.Heating)
Console.WriteLine("================================================================")
Console.WriteLine("Turn up temp...\r\n")
controller.SetTemp(UpDown.Up)
Console.WriteLine("================================================================")
Console.WriteLine("Turn down temp...\r\n")
controller.SetTemp(UpDown.Down)
}
运行结果:
源码在附件中,如有疑问,欢迎追问。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)