Reputation: 11
I have a problem with a FPS sensor connected via ttyAMA0 serial. Its default speed when it switchs on is 9600 but some functions work properly only at 115200.
So my request is how to change the serial baudrate at Runtime. I explain (I use C# with MonoDevelop):
1) I declare the serial port with 9600 baudrate 2) I send a command to FPS to change its baudrate from 9600 to 115200 3) I change RaspBerry ttyAMA0 baudrate from 9600 to 115200 4) I continue to send to FPS other commands
Thank you very much for your attention. Bye
Luca Menghini
the code is (it was simplyfied excluding some controls to make it more clean for you)
//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;
}
Upvotes: 1
Views: 523
Reputation: 11
I solved by myself. these are the steps order to obtain a Baudrate change (tested):
1) Declare first serial mySerialL at 9600 2) Declare second serial mySerialH at 115200 3) open mySerialL 4) send command OPEN to sensor 5) send command CHANGEBAUDRATE to sensor (and verify response is OK = ACK) 6) open mySerialH 7) clear the serial channel 8) close mySerialL
from this point to the end send messages using mySerialL
for my it works like a charm ;-))))
Bye
Upvotes: 0