//20201120 读取字符串 public string getChars(string plcIp, int Rack, int Slot, int DbNum, int Start, int Length) { byte[] Buffer = new byte[Length]; Client.ConnectTo(plcIp, Rack, Slot); Client.DBRead(DbNum, Start, Length, Buffer);//读取DbwNum所对应的字的值 return(S7.GetCharsAt(Buffer, 0, Length)); }
//20200915 添加读取字符串指令 public string getCharValue(string ip, int dbnum, int start, int size) { byte[] Buffer = new byte[20]; Client.ConnectTo(ip, 0, 3); Client.DBRead(dbnum, start, size, Buffer); Client.Disconnect(); return(S7.GetCharsAt(Buffer, 0, size)); }
// public string getChars(int DbNum, int Start, int Length) { byte[] Buffer = new byte[Length]; if (Client.ConnectTo(plcIp, Convert.ToInt16(plcRack), Convert.ToInt16(plcSlot)) != 0) { return("PLC连接失败!"); } Client.DBRead(DbNum, Start, Length, Buffer);//读取DbwNum所对应的字的值 return(S7.GetCharsAt(Buffer, Start, Length)); }
public string GetCharS(byte[] buffer) { string s = ""; if (buffer.Length > 0) { s = S7.GetCharsAt(buffer, 0, buffer.Length); } return(s); }
public void ReadASCIIString(int dbNumber, int start, int size, out string message) { message = string.Empty; byte[] buf = new byte[size]; lock (syncLock) { int code = _s7Client.DBRead(dbNumber, start, size, buf); Validate(code); Thread.Sleep(300); } message = S7.GetCharsAt(buf, 0, size); }
public StructDB0 Lectura(int DbNumber, int offSetStart) { StructDB0 Result = new StructDB0(); byte[] readBuffer = new byte[21]; client.DBRead(DbNumber, offSetStart, readBuffer.Length, readBuffer);//Lectura de buffer //Extraer datos y meterlos en Struct Result.SerialNumber = S7.GetCharsAt(readBuffer, 0, 12); Result.TestResult = S7.GetIntAt(readBuffer, 12); Result.RealDetector = S7.GetRealAt(readBuffer, 14); Result.byte20 = S7.GetByteAt(readBuffer, 20); return(Result);// Devuelve un objeto del tipo StructDB0 }
private void ReadPLC() { // Connect if disconnected if (!client.Connected) { client.Connect(); } //Do this for each DB in the configuration foreach (var db in configService.ActiveConfig.DBReads) { Byte[] buffer = new byte[db.DBLengthByte]; var ret = client.DBRead(db.DBNumber, db.DBOffsetByte, db.DBLengthByte, buffer); var errorText = client.ErrorText(ret); if (ret == 0) { Console.WriteLine($"Reading DB{db.DBNumber} success."); } else { Console.WriteLine($"Reading DB{db.DBNumber} failed, error {ret}:, {errorText}"); } //Parse the db into parts foreach (var tag in db.Tags) { switch (tag.Type) { case "Bool": tag.Data = S7.GetBitAt(buffer, tag.OffsetByte, tag.OffsetBit); break; case "Int8": tag.Data = S7.GetByteAt(buffer, tag.OffsetByte); break; case "Int16": tag.Data = S7.GetIntAt(buffer, tag.OffsetByte); break; case "Int32": tag.Data = S7.GetDIntAt(buffer, tag.OffsetByte); break; case "Int64": tag.Data = S7.GetLIntAt(buffer, tag.OffsetByte); break; case "Float": tag.Data = S7.GetRealAt(buffer, tag.OffsetByte); break; case "Double": tag.Data = S7.GetLRealAt(buffer, tag.OffsetByte); break; case "String": tag.Data = S7.GetCharsAt(buffer, tag.OffsetByte, tag.StringLength); break; default: Console.WriteLine($"Unknown type for tag {tag.Name}"); break; } } } }
/// <summary> /// The main entry point for the application. /// </summary> /// <param name="args">The command line arguments</param> /// static void Main(string[] args) { string fileName = string.Empty; // The FileName to store the image bool printHelp = false; // Output help? string cameraID = null; // The camera ID int tellerNaam = 0; while (true) { var clientLaminator = new S7Client(); int connectionResult = clientLaminator.ConnectTo("10.0.0.151", 0, 1); if (connectionResult == 0) { Console.WriteLine("con ok"); } else { Console.WriteLine("err"); } //PARAMETERS // var Parameterbuffer = new byte[8]; // int readRest = clientLaminator.DBRead(2, 0, Parameterbuffer.Length, Parameterbuffer); // if (readRest == 0) // { // Console.WriteLine("dB2 ok"); // } // double Temperatuur = S7.GetRealAt(Parameterbuffer, 0); // Console.WriteLine(Temperatuur.ToString()); // int Druk = S7.GetIntAt(Parameterbuffer, 4); // Console.WriteLine(Druk.ToString()); // int LamiSucces = S7.GetIntAt(Parameterbuffer, 6); // Console.WriteLine(LamiSucces.ToString()); //FOTO var FotoBuffer = new byte[1]; int readReste = clientLaminator.DBRead(2000, 0, FotoBuffer.Length, FotoBuffer); if (readReste == 0) { Console.WriteLine("dB3 ok"); } //S7.SetBitAt(ref FotoBuffer, Pos: 0, Bit: 0, Value: false); bool FotoNemen = S7.GetBitAt(FotoBuffer, 0, 0); Console.WriteLine(FotoNemen.ToString()); if (FotoNemen == true) { // neem foto //CHARS var CharSerienummerbuffer = new byte[10]; int readRes = clientLaminator.DBRead(1, 0, CharSerienummerbuffer.Length, CharSerienummerbuffer); string Letter1; Letter1 = S7.GetCharsAt(CharSerienummerbuffer, 0, 1); //Console.WriteLine(Letter1); string Letter2; Letter2 = S7.GetCharsAt(CharSerienummerbuffer, 1, 1); //Console.WriteLine(Letter2); string Letter3; Letter3 = S7.GetCharsAt(CharSerienummerbuffer, 2, 1); //Console.WriteLine(Letter3); string Letter4; Letter4 = S7.GetCharsAt(CharSerienummerbuffer, 3, 1); //Console.WriteLine(Letter4); string Letter5; Letter5 = S7.GetCharsAt(CharSerienummerbuffer, 4, 1); //Console.WriteLine(Letter5); string Letter6; Letter6 = S7.GetCharsAt(CharSerienummerbuffer, 5, 1); //Console.WriteLine(Letter6); string Letter7; Letter7 = S7.GetCharsAt(CharSerienummerbuffer, 6, 1); //Console.WriteLine(Letter7); string Letter8; Letter8 = S7.GetCharsAt(CharSerienummerbuffer, 7, 1); //Console.WriteLine(Letter8); string Letter9; Letter9 = S7.GetCharsAt(CharSerienummerbuffer, 8, 1); //Console.WriteLine(Letter9); string Letter10; Letter10 = S7.GetCharsAt(CharSerienummerbuffer, 9, 1); //Console.WriteLine(Letter10); string Serienummer = Letter1 + Letter2 + Letter3 + Letter4 + Letter5 + Letter6 + Letter7 + Letter8 + Letter9 + Letter10; //Console.WriteLine(Serienummer); try { ParseCommandLine(args, ref fileName, ref printHelp, ref cameraID); if (fileName == string.Empty) { fileName = "SynchronousGrab.bmp"; } // Print out help and end program if (printHelp) { // Console.WriteLine("Usage: AsynchronousGrab [CameraID] [/i] [/h]"); // Console.WriteLine("Parameters: CameraID ID of the camera to use (using first camera if not specified)"); // Console.WriteLine(" /f FileName to save the image"); // Console.WriteLine(" /h Print out help"); } else { // Create a new Vimba entry object VimbaHelper vimbaHelper = new VimbaHelper(); vimbaHelper.Startup(); // Startup API // Open camera try { Console.WriteLine("Vimba .NET API Version {0}", vimbaHelper.GetVersion()); if (null == cameraID) { // Open first available camera // Fetch all cameras known to Vimba List <Camera> cameras = vimbaHelper.CameraList; if (cameras.Count < 0) { throw new Exception("No camera available."); } foreach (Camera currentCamera in cameras) { // Check if we can open the camera in full mode VmbAccessModeType accessMode = currentCamera.PermittedAccess; if (VmbAccessModeType.VmbAccessModeFull == (VmbAccessModeType.VmbAccessModeFull & accessMode)) { // Now get the camera ID cameraID = currentCamera.Id; } } if (null == cameraID) { throw new Exception("Could not open any camera."); } } Console.WriteLine("Opening camera with ID: " + cameraID); System.Drawing.Image img = vimbaHelper.AcquireSingleImage(cameraID); string lastDatetime = DateTime.Now.ToLongDateString(); if (File.Exists("C:\\Fotologs\\" + tellerNaam + lastDatetime + "voor laminatie" + ".bmp")) { img.Save("C:\\Fotologs\\" + tellerNaam + lastDatetime + "na laminatie" + ".bmp"); tellerNaam++; } else { img.Save("C:\\Fotologs\\" + tellerNaam + lastDatetime + "voor laminatie" + ".bmp"); } Console.WriteLine("Image is saved as: " + Serienummer); } finally { // shutdown the vimba API vimbaHelper.Shutdown(); } } } catch (VimbaException ve) { // Output in case of a vimba Exception Console.WriteLine(ve.Message); Console.Write("Return Code: " + ve.ReturnCode.ToString() + " (" + ve.MapReturnCodeToString() + ")"); } catch (Exception e) { // Output in case of a System.Exception Console.WriteLine(e.Message); } Console.WriteLine("foto genomen"); S7.GetBitAt(FotoBuffer, 0, 0); Console.WriteLine("External: " + FotoBuffer[0]); S7.SetBitAt(ref FotoBuffer, Pos: 0, Bit: 0, Value: false); Console.WriteLine("Internal: " + FotoBuffer[0]); clientLaminator.DBWrite(2000, 0, FotoBuffer.Length, FotoBuffer); } clientLaminator.Disconnect(); } }
public void TestGetCharsAt(byte[] buffer, string expected) { S7.GetCharsAt(buffer, 0, buffer.Length).ShouldBe(expected); }