|
写的一个接收两个串口手柄的程序,总是乱码,大虾帮忙.....
代码如下:
const int DataNum=1;
const int rst = 9; //板子的RST脚接Arduino的D4口(自定义)
const int clk = 16; //板子的CLK脚接Arduino的D3口(自定义)
const int data1[] = {2,3}; //板子的DATA脚接Arduino的D2口(自定义)
void setup()
{
Serial.begin(9600);
pinMode(rst, OUTPUT);
pinMode(clk, OUTPUT);
for (int i=0;i<=DataNum;i++)
pinMode(data1, INPUT);
//pinMode(data2,INPUT); //如果有第二块板子的话要定义IO
Serial.print("1-");
}
void loop()
{
Serial.print("-2-");
delay(100);
unsigned int key[DataNum] = {0};
String SendStr="$";
digitalWrite(rst, HIGH);
delayMicroseconds(10); //所有delayMicroseconds(10);均是给4017一个反应时间。
digitalWrite(rst, LOW);
delayMicroseconds(10);
Serial.print("-3-");
delay(100);
for(int i = 0; i < 10; i++)
{
for (int ii=0;ii<=DataNum;ii++){
key[ii]<<=1;
bitWrite(key[ii],0,digitalRead(data1[ii]));
delayMicroseconds(110);
}
digitalWrite(clk, HIGH);
delayMicroseconds(10);
digitalWrite(clk, LOW);
delayMicroseconds(10);
}
Serial.print("-4-");
delay(100);
for (int ii=0;ii<=DataNum;ii++){
SendStr=SendStr+GetKey(key[ii]);
}
SendStr=SendStr+"$";
Serial.print(SendStr.length());
Serial.print(" - ");
Serial.print(SendStr);
Serial.print("\t");
delay(100);
}
//k1=key k2=keybak k3=keybak1
String GetKey(unsigned int k1)
{
Serial.print("-5-");
delay(100);
String lsstr="";
switch (k1)
{
case 1015:
lsstr= "a";//left
break;
case 1019:
lsstr= "d"; //right
break;
case 1017:
lsstr= "s";//up
break;
case 991:
lsstr= "w";//down
break;
case 895:
lsstr= "f";//select
break;
case 959:
lsstr= "r";//start
break;
case 767:
lsstr= "j";//a
break;
case 511:
lsstr= "k";//b
break;
// case 1023:
// lsstr= "q";//stop
// break;
default:
lsstr="q";//null
break;
}
return lsstr;
}
乱码如下:
如还有什么要求请告诉我!
|
|