/* Frequency Generator with SCPI parser setup: 1. programm your Arduino 2. addapt the serial port in your python script to the serial port used on your PC 3. run the python script PC: python with tkinter and pyserial MC: any Arduino 22.05.06 first version by mchris 22.05.08 V0.2, SCPI parser lib included by mchris Use SCPI library from https://github.com/Vrekrer/Vrekrer_scpi_parser */ #include "Arduino.h" #include "Vrekrer_scpi_parser.h" SCPI_Parser my_instrument; #define FREQENCY_PIN 8 void setup() { //We change the `hash_magic_number` variable before registering the commands my_instrument.hash_magic_number = 16; //16 will generate hash crashes //The default value is 37 and good values are prime numbers (up to 113) my_instrument.RegisterCommand(F("*IDN?"), &Identify); //*IDN? my_instrument.SetCommandTreeBase(F("GENErator:")); my_instrument.RegisterCommand(F(":FREQency"), &SetFrequency); //GENE:FREQ 100 my_instrument.RegisterCommand(F(":FREQency?"), &GetFrequency); //GENE:FREQ? Serial.begin(115200); while (!Serial) {;} //my_instrument.PrintDebugInfo(); } void loop() { my_instrument.ProcessInput(Serial, "\n"); } void Identify(SCPI_C commands, SCPI_P parameters, Stream& interface) { interface.println(F("MCHRIS, Arduino Frequency Generator V0.2")); } uint32_t Frequency=0; void SetFrequency(SCPI_C commands, SCPI_P parameters, Stream& interface) { String str=parameters[0]; Frequency=str.toInt(); Serial.print("set it: "); Serial.println(Frequency); tone(FREQENCY_PIN,Frequency); } void GetFrequency(SCPI_C commands, SCPI_P parameters, Stream& interface) { Serial.print("get it: "); Serial.println(Frequency); } /* import tkinter as tk from tkinter import ttk import serial import time arduino = serial.Serial(port='/dev/ttyACM0', baudrate=115200, timeout=.1) LARGE_FONT= ("Verdana", 12) Hauptfenster = tk.Tk() Hauptfenster.geometry("1024x640") Hauptfenster.title('frequency controller') Anzeiger_comRead=ttk.Label(Hauptfenster, text="empfangener text: ") Anzeiger_comRead.place(x=100, y=400) def sendValue(txt): txtLf=txt+"\r\n" arduino.write(bytes(txtLf, 'utf-8')) data = arduino.readline() print(data) Anzeiger_comRead.configure(text=data) #===================== Button ====================================== button1 = ttk.Button(Hauptfenster, text="100Hz", command=lambda: sendValue('GENE:FREQ 100')) button1.place(x=100, y=100) button2 = ttk.Button(Hauptfenster, text="1000Hz", command=lambda: sendValue('GENE:FREQ 1000')) button2.place(x=100, y=150) button_getFrequency = ttk.Button(Hauptfenster, text="get frequency", command=lambda: sendValue('GENE:FREQ?')) button_getFrequency.place(x=100, y=200) button_getIdentifier = ttk.Button(Hauptfenster, text="SCPI device info", command=lambda: sendValue('*IDN? ')) button_getIdentifier.place(x=100, y=300) time.sleep(1) arduino.flush() Hauptfenster.mainloop() */