using System; using System.Collections.Generic; using System.Text; using System.IO.Ports; using System.IO; using System.Threading; namespace InterfaceKit_simple { class LSY_201 { private static SerialPort ComPort = new SerialPort(); private byte incomingbyte; private int a= 0x0000, j = 0, k = 0, count = 0; //Read Starting address private byte MH, ML; private bool EndFlag; public LSY_201(int port) { ComPort.PortName = "COM" + port.ToString(); ComPort.BaudRate = 38400; ComPort.DataBits = 8; ComPort.Parity = Parity.None; ComPort.StopBits = StopBits.One; ComPort.Open(); while (!ComPort.IsOpen) ; } public List takePicture() { List output = new List(); EndFlag = false; a = 0x0000; SendResetCmd(); Thread.Sleep(1800); while (ComPort.BytesToRead > 0) { incomingbyte = (byte)ComPort.ReadByte(); Console.Write(incomingbyte.ToString()); Console.Write(" "); } Console.Write("\n"); SetSizeCmd(); Thread.Sleep(20); while (ComPort.BytesToRead > 0) { incomingbyte = (byte)ComPort.ReadByte(); Console.Write(incomingbyte.ToString()); Console.Write(" "); } Console.Write("\n"); SetBaudRateCmd(); Thread.Sleep(20); while (ComPort.BytesToRead > 0) { incomingbyte = (byte)ComPort.ReadByte(); Console.Write(incomingbyte.ToString()); Console.Write(" "); } Console.Write("\n"); Console.WriteLine("finish initializing"); //After reset, wait 2-3 second to send take picture command byte[] a1 = new byte[32]; SendTakePhotoCmd(); Thread.Sleep(20); while (ComPort.BytesToRead > 0) { incomingbyte = (byte)ComPort.ReadByte(); Console.Write(incomingbyte.ToString()); Console.Write(" "); } Console.Write("\n"); Console.WriteLine("took picture"); while (ComPort.BytesToRead > 0) { incomingbyte = (byte)ComPort.ReadByte(); } while (!EndFlag) { j = 0; k = 0; count = 0; SendReadDataCmd(); Thread.Sleep(25); while (ComPort.BytesToRead > 0) { incomingbyte = (byte)ComPort.ReadByte(); //Console.Write(incomingbyte.ToString()); k++; if ((k > 5) && (j < 32) && (!EndFlag)) { a1[j] = incomingbyte; if ((a1[Math.Max(j - 1,0)] == 0xFF) && (a1[j] == 0xD9)) //Check if the picture is over EndFlag = true; j++; count++; } } for (j = 0; j < count; j++) { output.Add(a1[j]); } //Send jpeg picture over the serial port } StopTakePhotoCmd(); Thread.Sleep(20); while (ComPort.BytesToRead > 0) { incomingbyte = (byte)ComPort.ReadByte(); Console.Write(incomingbyte.ToString()); Console.Write(" "); } Console.Write("\n"); return output; } private void SendResetCmd() { byte[] info = new byte[] {0x56,0x00,0x26,0x00}; ComPort.Write(info, 0, 4); } private void SetSizeCmd() { byte[] info = new byte[] {0x56, 0x00, 0x31, 0x05, 0x04, 0x01, 0x00, 0x19, 0x11}; ComPort.Write(info, 0, 9); } private void SetBaudRateCmd() { byte[] info = new byte[] { 0x56, 0x00, 0x24, 0x03, 0x01, 0x2A, 0xF2}; ComPort.Write(info, 0, 7); } private void ReadFileSizeCmd() { byte[] info = new byte[] { 0x56, 0x00, 0x34, 0x01, 0x00}; ComPort.Write(info, 0, 5); } private void SendTakePhotoCmd() { byte[] info = new byte[] { 0x56, 0x00, 0x36, 0x01, 0x00 }; ComPort.Write(info, 0, 5); } //Read data private void SendReadDataCmd() { MH = (byte)(a / 0x100); ML = (byte)(a % 0x100); byte[] info = new byte[] { 0x56, 0x00, 0x32, 0x0c, 0x00, 0x0a, 0x00, 0x00, MH, ML, 0x00, 0x00, 0x00, 0x20, 0x00, 0x0a }; ComPort.Write(info, 0, 16); a += 0x20; //address increases 32£¬set according to buffer size } private void StopTakePhotoCmd() { byte[] info = new byte[] { 0x56, 0x00, 0x36, 0x01, 0x03 }; ComPort.Write(info, 0, 5); } } }