How to read data from RFIDControllerMfrc522 - c#

I'm trying to find substitution for my rfid card. The current script is made in Python and is using SimpleMFRC522 library which is working perfectly. However, I want to use C#.
I found one library Unosquare.RaspberryIO.Peripherals, but I can't find good documentation. The function below is returning data 4, which is not the data that is written on the card.
If there is any other good RFID library, let me know. Thank you in advance!
Pi.Init<BootstrapWiringPi>();
var _reader = new RFIDControllerMfrc522();
while (true)
{
if (_reader.DetectCard() == RFIDControllerMfrc522.Status.AllOk)
{
var uidResponse = _reader.ReadCardUniqueId();
if (uidResponse.Status == RFIDControllerMfrc522.Status.AllOk)
{
var cardUid = uidResponse.Data;
_reader.SelectCardUniqueId(cardUid);
try
{
if (_reader.AuthenticateCard1A(cardUid, 7) == RFIDControllerMfrc522.Status.AllOk)
{
var a = _reader.CardReadData(0);
foreach (var item in a.Data)
{
Console.WriteLine(item);
}
}
}
finally
{
_reader.ClearCardSelection();
}
}
}
}

Related

How to get real time of a midi event

I am reading a midi file with this parser.
But I cannot read the real time.
MidiFile midiFile = new MidiFile("/Jenkins.mid");
var ticksPerQuarterNote = _midiFile.TicksPerQuarterNote;
foreach (MidiTrack track in midiFile.Tracks)
{
foreach (MidiEvent midiEvent in track.MidiEvents)
{
if (midiEvent.MidiEventType != MidiEventType.NoteOn)
continue;
int note = midiEvent.Note;
int time = midiEvent.Time;
}
}
All the formulas I have seen on the internet use the tempo, but I can't find it.
You can use my DryWetMIDI library which does all these calculations for you:
var midiFile = MidiFile.Read("Jenkins.mid");
var tempoMap = midiFile.GetTempoMap();
foreach (var trackChunk in midiFile.GetTrackChunks())
{
foreach (var timedEvent in trackChunk.GetTimedEvents())
{
if (timedEvent.Event.MidiEventType != MidiEventType.NoteOn)
continue;
MetricTimeSpan metricTime = timedEvent.TimeAs<MetricTimeSpan>(tempoMap);
}
}
Here we get metric time (hours/minutes/seconds/ms), but the library provides several other formats you can convert MIDI time to. Please read the article of the library docs to learn more: Time and length.
More than that if you actually want to get notes instead of events, it's super easy with DryWetMIDI:
var notes = midiFile.GetNotes();
You can also use TimeAs method on notes.

Mail merge word docx in Azure app service

In an App Service running on Azure I need to replace mail merge fields in a word/docx-document with content.
As I understand interop can't be used because it needs word to be installed.
So how do I replace mail merge fields on Azure in a c# app service?
Maybe one can use the OpenXML SDK for this? But how?
[Update]
OpenXML worked, I created the following helper class to replace the mailmerge content:
public static void DocXReplaceMergeFields(Stream docStream, Dictionary<string, string> placeholder)
{
using (var docXml = WordprocessingDocument.Open(docStream, true))
{
//docXml.ChangeDocumentType(WordprocessingDocumentType.Document);
foreach (var run in docXml.MainDocumentPart.Document.Descendants<Run>())
{
foreach (var text in run.Descendants<Text>().Where(a => a.Text.StartsWith("«") && a.Text.EndsWith("»")))
{
var propertyName = text.Text.Substring(1, text.Text.Length - 2);
if (placeholder.TryGetValue(propertyName, out var propertyValue))
text.Text = propertyValue;
}
}
var settingsPart = docXml.MainDocumentPart.GetPartsOfType<DocumentSettingsPart>().First();
var oxeSettings = settingsPart.Settings.Where(a => a.LocalName == "mailMerge").FirstOrDefault();
if (oxeSettings != null)
{
settingsPart.Settings.RemoveChild(oxeSettings);
settingsPart.Settings.Save();
}
docXml.MainDocumentPart.Document.Save();
}
}
You can give a try using Open XML SDK:
https://learn.microsoft.com/en-us/office/open-xml/word-processing
If that doesn't work for some reason, try doing the same using an Azure Logic App:
https://medium.com/plumsail/create-complex-excel-and-word-documents-from-templates-in-microsoft-flow-azure-logic-apps-and-794334e59f0f

Find a word in PDF using PDFSharp

I am using PDFSharp. I need help. I need to check wether the document contains the word "abc". Example:
11abcee = true
444abcggw = true
778ab = false
I wrote this code, but it does not work as expected:
PdfDocument document = PdfReader.Open("c:\\abc.pdf");
PdfDictionary dictionary = new PdfDictionary(document);
string a = dictionary.Elements.GetString("MTZ");
if (a.Equals("MTZ"))
{
MessageBox.Show("OK", "");
}
else
{
MessageBox.Show("NO", "");
}
Am I missing something?
maybe this SO entry will help you: PDFSharp alter Text repositioning.
It links to here - text extraction example with PDFSharp.
Old question, but here is an example.
Note: c# 7.0+ is required to use IS new local variable assignment.
Note: This example uses PDFSharp installed from Package Manager.
"Install-Package PdfSharp -Version 1.50.5147"
Note: For my requirements, I only needed to search the first page of my PDFs, update if
needed.
using (PdfDocument inputDocument = PdfReader.Open(filePath, PdfDocumentOpenMode.Import))
{
if (searchPDFPage(ContentReader.ReadContent(inputDocument.Pages[0]), searchText))
{
// match found.
}
}
This code looks for a cString that starts with a pound sign, the OP would need to use a Contains string function.
private bool searchPDFPage(CObject cObject, string searchText)
{
if (cObject is COperator cOperator)
{
if (cOperator.OpCode.Name == OpCodeName.Tj.ToString() ||
cOperator.OpCode.Name == OpCodeName.TJ.ToString())
{
foreach (var cOperand in cOperator.Operands)
{
if (searchPDFPage(cOperand, searchText))
{
return true;
}
}
}
}
else if (cObject is CSequence cSequence)
{
foreach (var element in cSequence)
{
if (searchPDFPage(element, searchText))
{
return true;
}
}
}
else if (cObject is CString cString)
{
if (cString.Value.StartsWith("#"))
{
if (cString.Value.Substring(2) == searchText)
{
return true;
}
}
}
return false;
}
Credit: This example was modified based on this answer:
C# Extract text from PDF using PdfSharp

Examples of Quickbase API in C#

I am fairly new to the use of APIs and haven't touched Quickbase until today. I was researching the Quickbase API and it seemed as if all the examples I saw were written in XML or some similar variant. Is there a way to write code in C# that will do the same things that I saw could be done on the Quickbase website's API documentation? If you know of any code examples, please let me know.
There is a QuickBase C# SDK that might help get you started.
using System;
using Intuit.QuickBase.Client;
namespace MyProgram.QB.Interaction
{
class MyApplication
{
static void Main(string[] args)
{
var client = QuickBase.Client.QuickBase.Login("your_QB_username", "your_QB_password");
var application = client.Connect("your_app_dbid", "your_app_token");
var table = application.GetTable("your_table_dbid");
table.Query();
foreach(var record in table.Records)
{
Console.WriteLine(record["your_column_heading"]);
}
client.Logout();
}
}
}
There is also a QuickBase API Wrapper example as well.
Back in 2009 I wrote an .NET API for QuickBase which makes working with the platform easy, it also supports uploading and downloading of attached files.
IQuickBaseService svc = new QuickBaseService("user", "pass", "URL", "token");
Schema schema = svc.GetSchema("DBID");
Console.WriteLine("Schema : {0}", schema.Name);
Console.WriteLine("Variables - ");
for (KeyValuePair<string, string> ent in schema.Variables.OrderBy(en => en.Key)) {
Console.WriteLine("Var: {0} = {1}", ent.Key, ent.Value);
}
for (Query q : schema.Queries) {
// Work with queries.
}
// schema.Children
// schema.Fields
// ...
svc.SignOut();
Performing a query is simple.
QueryResult res;
res = svc.Query("tableid", 1); // Execute query number 1
res = svc.Query("tableid", "{{140.EX.'1'}}") // execute QB query text
foreach (QueryRow row in result.Rows) {
// Do something with row, use get<type>, not all shown here.
// row.GetBool(1);
// row.GetInt(1);
// row.GetLong(1);
// row.GetFloat(1);
// row.GetDouble(1);
// row.GetDecimal(1);
// row.GetString(1);
// row.GetDate(1);
// row.GetDateTime(1);
// row.GetObject(1);
}
QuickBase SDK Code is now moved to github https://github.com/QuickbaseAdmirer/QuickBase-C-Sharp-SDK

Get current ClickOnce's application publisher name?

Is it possible to read the publisher name of the currently running ClickOnce application (the one you set at Project Properties -> Publish -> Options -> Publisher name in Visual Studio)?
The reason why I need it is to run another instance of the currently running application as described in this article and pass parameters to it.
Of course I do know my application's publisher name, but if I hard code it and later on I decide to change my publisher's name I will most likely forget to update this piece of code.
Here is another option. Note that it will only get the publisher name for the currently running application, which is all I need.
I'm not sure if this is the safest way to parse the XML.
public static string GetPublisher()
{
XDocument xDocument;
using (MemoryStream memoryStream = new MemoryStream(AppDomain.CurrentDomain.ActivationContext.DeploymentManifestBytes))
using (XmlTextReader xmlTextReader = new XmlTextReader(memoryStream))
{
xDocument = XDocument.Load(xmlTextReader);
}
var description = xDocument.Root.Elements().Where(e => e.Name.LocalName == "description").First();
var publisher = description.Attributes().Where(a => a.Name.LocalName == "publisher").First();
return publisher.Value;
}
You would think this would be trivial, but I don't see anything in the framework that gives you this info.
If you want a hack, you can get the publisher from the registry.
Disclaimer - Code is ugly and untested...
...
var publisher = GetPublisher("My App Name");
...
public static string GetPublisher(string application)
{
using (var key = Registry.CurrentUser.OpenSubKey(#"Software\Microsoft\Windows\CurrentVersion\Uninstall"))
{
var appKey = key.GetSubKeyNames().FirstOrDefault(x => GetValue(key, x, "DisplayName") == application);
if (appKey == null) { return null; }
return GetValue(key, appKey, "Publisher");
}
}
private static string GetValue(RegistryKey key, string app, string value)
{
using (var subKey = key.OpenSubKey(app))
{
if (!subKey.GetValueNames().Contains(value)) { return null; }
return subKey.GetValue(value).ToString();
}
}
If you find a better solution, please follow-up.
I dont know about ClickOnce, but normally, you can read the assembly-info using the System.Reflection framework:
public string AssemblyCompany
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
if (attributes.Length == 0)
{
return "";
}
return ((AssemblyCompanyAttribute)attributes[0]).Company;
}
}
Unfortunately, theres no "publisher" custom-attribute, just throwing this out as a possible work-around

Categories

Resources