Как поочередно отправлять данные, используя 2 трекбара для одного последовательного порта в VB.Net 2012 - PullRequest
1 голос
/ 15 мая 2019

Я пытаюсь контролировать напряжение и частоту на выходе моего проекта инвертора. Но я сталкиваюсь с проблемами с дизайном VB.Net. Но я не могу получить это работает. Вы можете видеть на изображении, я использую 2 переключателя. Моя цель - когда флажок radiobutton1 проверен, затем trackbar1 отправляет данные в arduino, то же самое для radiobutton2 и trackbar2 (но когда radiobutton2 отправляет данные, trackbar1 завершается). А также, я не знаю, как связать мой код контроля напряжения и частоты с VB.Net. Может ли кто-нибудь помочь мне, пожалуйста?

Вот мой текущий код Arduino

int i=0;
int j=0;
int k=0;

int x=0;
int y=0;
int z=0;

int OK=0;
int OK1=0;
int OK2=0;
int OK3=0;
int OK4=0;

int sinPWM[]={0,1,2,3,4,6,7,8,9,11,12,13,14,16,17,18,19,21,22,23,24,26,27,28,29,31,32,33,34,36,37,38,39,40,42,43,44,45,47,48,49,50,52,53,54,55,56,
              58,59,60,61,62,64,65,66,67,68,70,71,72,73,74,76,77,78,79,80,82,83,84,85,86,87,89,90,91,92,93,94,96,97,98,99,100,101,102,104,105,106,
              107,108,109,110,111,112,114,115,116,117,118,119,120,121,122,123,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,
              142,143,144,145,147,148,149,150,151,152,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,171,172,173,
              174,175,176,177,178,179,179,180,181,182,183,184,185,185,186,187,188,189,189,190,191,192,193,193,194,195,196,197,197,198,199,200,200,
              201,202,203,203,204,205,205,206,207,208,208,209,210,210,211,212,212,213,214,214,215,215,216,217,217,218,219,219,220,220,221,221,222,
              223,223,224,224,225,225,226,226,227,227,228,228,229,229,230,230,231,231,232,232,233,233,234,234,234,235,235,236,236,237,237,237,238,
              238,238,239,239,240,240,240,241,241,241,241,242,242,242,243,243,243,244,244,244,244,245,245,245,245,246,246,246,246,246,247,247,247,
              247,247,247,248,248,248,248,248,248,248,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,249,
              249,249,249,249,249,249,249,249,249,249,249,249,249,248,248,248,248,248,248,248,247,247,247,247,247,247,246,246,246,246,246,245,245,
              245,245,244,244,244,244,243,243,243,242,242,242,241,241,241,241,240,240,240,239,239,238,238,238,237,237,237,236,236,235,235,234,234,
              234,233,233,232,232,231,231,230,230,229,229,228,228,227,227,226,226,225,225,224,224,223,223,222,221,221,220,220,219,219,218,217,217,
              216,215,215,214,214,213,212,212,211,210,210,209,208,208,207,206,205,205,204,203,203,202,201,200,200,199,198,197,197,196,195,194,193,
              193,192,191,190,189,189,188,187,186,185,185,184,183,182,181,180,179,179,178,177,176,175,174,173,172,171,171,170,169,168,167,166,165,
              164,163,162,161,160,159,158,157,156,155,154,153,152,152,151,150,149,148,147,145,144,143,142,141,140,139,138,137,136,135,134,133,132,
              131,130,129,128,127,126,125,123,122,121,120,119,118,117,116,115,114,112,111,110,109,108,107,106,105,104,102,101,100,99,98,97,96,94,
              93,92,91,90,89,87,86,85,84,83,82,80,79,78,77,76,74,73,72,71,70,68,67,66,65,64,62,61,60,59,58,56,55,54,53,52,50,49,48,47,45,44,43,42,
              40,39,38,37,36,34,33,32,31,29,28,27,26,24,23,22,21,19,18,17,16,14,13,12,11,9,8,7,6,4,3,2,1,0};

#define toggle 7
int sw;
int AnalogDataVolt;
int nilaiVB;

int potvolt = A1; //Pengatur Tegangan
int potfreq = A2; //Pengatur Frequency

float potvoltval;
float potfreqval;

float A;
float F;

String inputString = "";         // a String to hold incoming data
boolean stringComplete = false;  // whether the string is complete
int val = 0;

int Volt = 0;
int Freq = 0;

void setup() {
Serial.begin(9600);
inputString.reserve(200);

pinMode(4, OUTPUT);
pinMode(13,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);

cli();// stop interrupts
TCCR0A=0;//reset nilai
TCCR0B=0;//reset nilai
TCNT0=0;//reset nilai
//0b : menulis bits dalam binary
TCCR0A=0b10100001;//phase correct pwm mode
TCCR0B=0b00000001; //tidak ada prescaler

TCCR2A=0;//reset nilai
TCCR2B=0;//reset nilai
TCNT2=0;//reset nilai
//0b : menulis bits dalam binary
TCCR2A=0b10100001;//phase correct pwm mode
TCCR2B=0b00000001; //tidak ada prescaler

TCCR3A=0;//reset nilai
TCCR3B=0;//reset nilai
TCNT3=0;//reset nilai
//0b  : menulis bits dalam binary
TCCR3A=0b10100001;//phase correct pwm mode
TCCR3B=0b00000001; //tidak ada prescaler

TCCR1A=0;//reset nilai
TCCR1B=0;//reset nilai
TCNT1=0;//reset nilai
OCR1A=509;// compare match value
TCCR1B=0b00001001; //WGM12 bit is 1 for CTC mode and no prescaler

TIMSK1 |=(1 << OCIE1A);// memulai interrupts

sei();//stop interrupts
}
ISR(TIMER1_COMPA_vect){// interrupt mulai ketika timer 1 cocok dengan OCR1A 
if(i>629 && OK==0){// nilai akhir dari sample untuk pin 13
i=0;// mulai dari nilai awal sample
OK=1;//mengaktifkan pin 4
}
if(i>629 && OK==1){// nilai akhir dari sample untuk pin 4
i=0;//mulai dari nilai awal sample
OK=0;//mengaktifkan pin 13
}
x=round(A*sinPWM[i]);// nilai x berasal dari sample dan i berindex 0
i=i+F;// ke posisi selanjutnya dari sample
if(OK==0){
OCR0B=0;//pin 4 0
OCR0A=x;//pin 13 memulai pembacaan sample
}
if(OK==1){
OCR0A=0;//pin 13 0
OCR0B=x;//pin 4 memulai pembacaan sample
}

if ((i>=418) || OK1==1){//jika nilai i mencapai 209, maka phase kedua akan dimulai
OK1=1;//mempertahankan if function
if(j>629 && OK2==0){// nilai akhir dari sample untuk pin 10
j=0;// mulai dari nilai awal sample
OK2=1;//mengaktifkan pin 9
}
if(j>629 && OK2==1){// nilai akhir dari sample untuk pin 9
j=0;//mulai dari nilai awal sample
OK2=0;//mengaktifkan pin 10
}
y=round(A*sinPWM[j]);// nilai y berasal dari sample dan i berindex 0
j=j+F;// ke posisi selanjutnya dari sample
if(OK2==0){
OCR2B=0;//pin 9 0
OCR2A=y;//pin 10 memulai pembacaan sample
}
if(OK2==1){
OCR2A=0;//pin 10 0
OCR2B=y;//pin 9 memulai pembacaan sample
}
}
if ((j>=418) || OK3==1){//jika nilai j mencapai 209, maka phase ketiga akan dimulai
OK3=1;//mempertahankan if function
if(k>629 && OK4==0){// nilai akhir dari sample untuk pin 5
k=0;// mulai dari nilai awal sample
OK4=1;//mengaktifkan pin 2
}
if(k>629 && OK4==1){// nilai akhir dari sample untuk pin 2
k=0;//mulai dari nilai awal sample
OK4=0;//mengaktifkan pin 5
}
z=round(A*sinPWM[k]);// nilai z berasal dari sample dan i berindex 0
k=k+F;// ke posisi selanjutnya dari sample
if(OK4==0){
OCR3B=0;//pin 2 0
OCR3A=z;//pin 5 memulai pembacaan sample
}
if(OK4==1){
OCR3A=0;//pin 5 0
OCR3B=z;//pin 2 memulai pembacaan sample
}
}
}
void loop() {  
sw = digitalRead(toggle);

    potvoltval= analogRead(AnalogDataVolt);
    A= potvoltval/1023;

    potfreqval= analogRead(potfreq);
    F= map(potfreqval,1023,0,0,50);

    if (sw == 1){ //    vb
    if (stringComplete) {
    val = inputString.toInt();
    AnalogDataVolt = nilaiVB;
    nilaiVB = map(val, 0, 255, 0, 1023);
    Serial.println(val);
    inputString = "";
    stringComplete = false;}
    }
    else
    AnalogDataVolt = analogRead(potvolt); //    pot
  }
}

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

А вот мой код VB.Net

    Public Class Form1
    Sub Serial()
        Me.CenterToParent()
        SerialPort1.PortName = "COM4"
        SerialPort1.BaudRate = 9600
        SerialPort1.Open()
        Timer1.Start()
        SerialPort1.Write(TrackBar2.Value & Chr(10))
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.CenterToParent()
        SerialPort1.PortName = "COM4"
        SerialPort1.BaudRate = 9600
        SerialPort1.Open()
        Timer1.Start()
        SerialPort1.Write(TrackBar1.Value & Chr(10))
    End Sub

    Public Sub send_data(ByVal data_kirim As String)
        If SerialPort1.IsOpen() Then
            SerialPort1.Write(data_kirim + Environment.NewLine())
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TrackBar1.Value = 255
        TrackBar2.Value = 255
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        TrackBar1.Value = 0
        TrackBar2.Value = 0
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Try
            Dim i As Single = SerialPort1.ReadExisting
            lbl1.Text = "PWM Value : " & i.ToString
        Catch ex As Exception

        End Try
    End Sub

    Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
        If RadioButton1.Checked = True Then
            Call Serial()
        End If
    End Sub
End Class
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...