1

我对通过 ttyAMA0 串行连接的 FPS 传感器有疑问。它开机时的默认速度是9600,但有些功能只有在115200才能正常工作。

所以我的要求是如何在运行时更改串行波特率。我解释一下(我使用 C# 和 MonoDevelop):

1)我用 9600 波特率声明串口 2)我向 FPS 发送命令以将其波特率从 9600 更改为 115200 3)我将 RaspBerry ttyAMA0 波特率从 9600 更改为 115200 4)我继续向 FPS 发送其他命令

非常感谢您的关注。再见

卢卡·蒙吉尼

代码是(它很简单,不包括一些控件以使其更干净)

//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;

    }
4

1 回答 1

0

我自己解决了。这些是获得波特率变化的步骤(经过测试):

1) 在 9600 声明第一个串行 mySerialL 2) 在 115200 声明第二个串行 mySerialH 3) 打开 mySerialL 4) 向传感器发送命令 OPEN 5) 向传感器发送命令 CHANGEBAUDRATE(并验证响应 OK = ACK) 6) 打开 mySerialH 7) 清除串行通道 8) 关闭 mySerialL

从这一点到最后使用 mySerialL 发送消息

对我来说它就像一个魅力;-))))

再见

于 2015-04-27T06:58:54.190 回答