我遇到通过ttyAMA0串行连接的FPS传感器出现问题。 打开时的默认速度为9600,但某些功能仅在115200时正常工作。
所以我的请求是如何在运行时更改串行波特率。 我解释一下(我使用C#和MonoDevelop):
1)我声明串口为9600波特率 2)我向FPS发送命令,将其波特率从9600改为115200 3)我将RaspBerry ttyAMA0波特率从9600更改为115200 4)我继续向FPS发送其他命令
非常感谢你的关注。 再见
Luca Menghini
代码是(它被简单地排除了一些控件以使它更干净)
//DECLARING SERIAL
public static SerialPort cbrSerial = new SerialPort("/dev/ttyAMA0", 9600, Parity.None, 8, StopBits.One);
cbrSerial.ReadTimeout = 20000;
//OPENING SERIAL
Console.WriteLine("OPEN Serial");
cbrSerial.Open();
//OPEN FPS COMMAND (MUST "OPEN FPS" BEFORE CHANGE ITSBAUDRATE!!!)
Console.WriteLine("OPEN FPS");
cbrSerial.Write (new byte[] {0x55,0xaa,0x01,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x02,0x01}, 0, 12);
Thread.Sleep(1000);
Console.WriteLine ("execute Baudrate change from default to 115200");
cbrSerial.Write (new byte[] {0x55,0xaa,0x01,0x00,0x80,0x25,0x00,0x00,0x04,0x00,0xa9,0x01}, 0, 12);
Thread.Sleep (3000);
//clearing the serial buffer
cbrSerial.DiscardInBuffer();
// HERE I'VE GOT TO CHANGE THE SERIALBAUDRATE FROM 9600 TO 115200
// BUT THIS COMMAND DOESN'T WORK!!!
cbrSerial.BaudRate=115200;
//THE LED SWITCH ON
Console.WriteLine("LED IS ON");
cbrSerial.Write (new byte[] {0x55,0xaa,0x01,0x00,0x01,0x00,0x00,0x00,0x12,0x00,0x13,0x01}, 0, 12);
Thread.Sleep(1000);
int loops =0;
string resp="";
// looping the procedure (30 TIMES TO DEBUG IT)
while (loops<30)
{
// ISPRESSFINGER command - verify if there is a finger on the FPS
cbrSerial.Write (new byte[] {0x55,0xaa,0x01,0x00,0x01,0x00,0x00,0x00,0x26,0x00,0x27,0x01}, 0, 12);
Thread.Sleep(1000);
resp=check_get_data();
Console.WriteLine(resp);
// if a finger is on the FPS
if (resp == "85|170|1|0|0|0|0|0|48|0|48|1|")
{
cbrSerial.DiscardInBuffer();
//CAPTURE THE FINGER IMPRESSION AND STORE IT IN FPS INTERNAL RAM
Console.WriteLine ("CAPTUREFINGER");
cbrSerial.Write (new byte[] {0x55,0xaa,0x01,0x00,0x01,0x00,0x00,0x00,0x60,0x00,0x61,0x01}, 0, 12);
Thread.Sleep(1000);
//COMMAND THE TRANSMISSION BACK OF THE IMAGE
Console.WriteLine("GETIMAGE");
cbrSerial.Write (new byte[] {0x55,0xaa,0x01,0x00,0x01,0x00,0x00,0x00,0x62,0x00,0x63,0x01}, 0, 12);
Thread.Sleep(7000);
//WRITE THE BYTE INTO A FILE ON THE RASPBERRY DESKTOP
string strTemplate="";
strTemplate=check_get_data();
Console.WriteLine(strTemplate);
StreamWriter a = new StreamWriter("/home/pi/Desktop/ConsoleResult.txt",true);
a.Write (strTemplate);
a.Close ();
}
loops = loops + 1;
Thread.Sleep(1000);
}
//AT THE END OF THE LOOP SWITCH OFF THE LED
Console.WriteLine("LED OFF");
cbrSerial.Write (new byte[] {0x55,0xAA,0x01,0x00,0x00,0x00,0x00,0x00,0x12,0x00,0x12,0x01}, 0, 12);
Thread.Sleep(3000);
//COMMAND TO CLOSE FPS
Console.WriteLine("FPS CLOSED");
cbrSerial.Write (new byte[] {0x55,0xAA,0x01,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x02,0x01}, 0, 12);
//SERIAL CLOSE
cbrSerial.Close ();
public static string check_get_data()
{
Console.WriteLine("READ BYTE:");
byte tmpByte=0;
string letto="";
while (cbrSerial.BytesToRead !=0) {
tmpByte=(byte)cbrSerial.ReadByte();
letto=letto + System.Convert.ToString (tmpByte) + '|';
}
return letto;
}
答案 0 :(得分:0)
我自己解决了。 这些是获取波特率变化的步骤(测试):
1)在9600声明第一个串行mySerialL 2)在115200声明第二个串行mySerialH 3)打开mySerialL 4)向传感器发送命令OPEN 5)向传感器发送命令CHANGEBAUDRATE(并验证响应是OK = ACK) 6)打开mySerialH 7)清除串行通道 8)关闭mySerialL
从这一点到结束使用mySerialL发送消息
对于我来说,它就像一个魅力; - ))))
再见