Autor Wątek:  Maszyna with Arduino  (Przeczytany 5332 razy)

0 użytkowników i 1 Gość przegląda ten wątek.

Offline Touny

  • Wiadomości: 5
    • Zobacz profil
  • Otrzymane polubienia: 1
Maszyna with Arduino
« dnia: 09 Lipca 2020, 20:35:22 »
Greetings everyone. I'm missing english version of topics "Nastawnik/Pulpit do komputera" and "MaSzyna z portem COM" so i decided to create this topic.
---------------------

My question:

I've managed to get communication to work thanks to this: https://eu07.pl/forum/index.php/topic,13529.msg529872.html#msg529872
My RX and TX LEDs are blinking, output from Maszyna - CA/SHP LED works fine. However, I can't get any input to Maszyna. For input I tried this: https://eu07.pl/forum/index.php/topic,13529.msg524782.html#msg524782 so then the whole code in Arduino looks like this:

volatile uint8_t zPC[52] = {0}; // standardowe bajty przekazywane do kodu Arduino
volatile uint8_t doPC[20] = {0xEF, 0xEF, 0xEF, 0xEF, 0}; // standardowe bajty przyjmowane z kodu Arduino

void setup()
{
pinMode(48, OUTPUT); //ustawienie pinu 48 (wbudowana w Arduino dioda LED) jako wyjście cyfrowe
digitalWrite(48, LOW); //ustawienie stanu niskiego na pinie 48

pinMode(33, INPUT_PULLUP);

Serial.begin(115200); //nawiązanie komunikacji z prędkością 115200b/s
Serial.setTimeout(10);
while(!Serial){}; //czekanie do nawiązania komunikacji z komputerem
}

void loop()
{

digitalWrite(48, (bitRead(zPC[8], 7))); // lampka CA/SHP
bitWrite(doPC[0], 7, !digitalRead(33)); // przycisk CA/SHP

while (!Serial.available()) {};
Serial.readBytes((char*)zPC, 52);
Serial.write((char*)doPC, 20);

}

Problem 1: When I press the button (33), RX and TX LEDs stop blinking and Arduino does not respond until simulator restart. (CA/SHP LED keeps turned on, etc.)

Problem 2: Also I'm very confused by the bytes and bits layouts. Understanding output layout was easy with this (UART xml): https://eu07.pl/forum/index.php/topic,28460.msg430131.html#msg430131 but I cant understand the input - combination of switch group, bytes and bits exceeding number 7 in input-uart.ini. Solving problem 1 will surely help solving problem 2 by trial and error.

Offline tmj

  • Zasłużony dla Symulatora
  • Wiadomości: 3808
    • Zobacz profil
  • Otrzymane polubienia: 2351
Odp: Maszyna with Arduino
« Odpowiedź #1 dnia: 09 Lipca 2020, 20:51:50 »
Problem 2: Also I'm very confused by the bytes and bits layouts. Understanding output layout was easy with this (UART xml): https://eu07.pl/forum/index.php/topic,28460.msg430131.html#msg430131 but I cant understand the input - combination of switch group, bytes and bits exceeding number 7 in input-uart.ini. Solving problem 1 will surely help solving problem 2 by trial and error.
eu07_input-uart.ini specifies layout of input from arduino controller in the following manner:

- each input bit is described in one line of the ini file
- tokens in each line specify:
-- bit of the received data package
-- control type, which can be toggle (for on-off switches which depending on their state can send one of two associated commands) or impulse which sends associated command when pressed
-- associated command(s) which are generated depending on the control type. available command names can be found in file eu07_input-keyboard.ini

e.g. 10 toggle compressorenable compressordisable means

10th bit of the data package carries state of toggle type switch, which when put in on position (value 1) generates command compressorenable and when put in off position (value 0) generates command compressordisable
« Ostatnia zmiana: 10 Lipca 2020, 00:54:59 wysłana przez tmj »

Offline Touny

  • Wiadomości: 5
    • Zobacz profil
  • Otrzymane polubienia: 1
Odp: Maszyna with Arduino
« Odpowiedź #2 dnia: 09 Lipca 2020, 21:59:44 »
I knew Problem 1 will take longer! :D
I'm trying other switches (byte, bit combination) still same result.
Although thanks for that! I'm getting into that slowly but surely.

Cytuj
10th bit of the data package
Since byte has 8 bits (0-7). Am I right then, that to send 10th bit, do I need to write it like this?
bitWrite(doPC[1], 2, !digitalRead(33));
Does that mean, that switch group equals byte in brackets? [ ]
If yes, then in Maszyna UART xml doc "Numer bajtu struktury" should work for inputs. But for outputs, column "Nr bajtu transmisji danych" works just fine. (atleast for me) Is this right? I find it rather confusing (maybe because i dont understand Polish much). I was looking into column "Nr B danych" and was confused why 0 was used here. bitWrite(doPC[0], 7, !digitalRead(41));         // przycisk CA/SHP

Offline tmj

  • Zasłużony dla Symulatora
  • Wiadomości: 3808
    • Zobacz profil
  • Otrzymane polubienia: 2351
Odp: Maszyna with Arduino
« Odpowiedź #3 dnia: 09 Lipca 2020, 22:46:55 »
Since byte has 8 bits (0-7). Am I right then, that to send 10th bit, do I need to write it like this?
bitWrite(doPC[1], 2, !digitalRead(33));
Does that mean, that switch group equals byte in brackets? [ ]
While i think this is generally correct, there's a bit more to it, see below:

Cytuj
If yes, then in Maszyna UART xml doc "Numer bajtu struktury" should work for inputs. But for outputs, column "Nr bajtu transmisji danych" works just fine. (atleast for me) Is this right? I find it rather confusing (maybe because i dont understand Polish much). I was looking into column "Nr B danych" and was confused why 0 was used here. bitWrite(doPC[0], 7, !digitalRead(41));         // przycisk CA/SHP

"Numer bajtu danych" indicates the byte of the data package sent from the simulator to the external device. The first four bytes of this package hold fixed header, EFEFEFEF, which is why "numer bajtu struktury" i.e. the byte id for each piece of actual data is shifted by 4 (the 1st byte of data is the 5th byte of the package, and so on)
The xls table describes (fixed) layout of data which the simulator sends to the external device. What you send back to the simulator is left largely up to you, as long as you inform the simulator how to interpret the data, using the eu07_input-uart.ini but there's one caveat -- the first four bytes in the package you sent also need to contain EFEFEFEF

This is probably the source of your problem #1, in your code you set the content of first byte to a different value by setting one of its bits. As a result, the header is no longer EFEFEFEF and the simulator doesn't accept your data. So i'm guessing the data which in the .ini file is described as "bit 1" should be recorded as doPC[4], 0 and so on. However this is just a guess, as i have no personal experience with Arduino.

EDIT: for a better idea of what's going on, you can add string
uartdebug yes
to the file eu07.ini
« Ostatnia zmiana: 09 Lipca 2020, 22:55:42 wysłana przez tmj »

Offline Touny

  • Wiadomości: 5
    • Zobacz profil
  • Otrzymane polubienia: 1
Odp: Maszyna with Arduino
« Odpowiedź #4 dnia: 10 Lipca 2020, 00:21:18 »
Problems solved. Thanks a lot!

From this (https://eu07.pl/forum/index.php/topic,13529.msg524782.html#msg524782) there was bit shift already included, I suppose.
bitWrite(doPC[0], 7, !digitalRead(41));         // przycisk CA/SHPto this
bitWrite(doPC[4], 7, digitalRead(41));         // przycisk CA/SHP
And it works!

Offline tmj

  • Zasłużony dla Symulatora
  • Wiadomości: 3808
    • Zobacz profil
  • Otrzymane polubienia: 2351
Odp: Maszyna with Arduino
« Odpowiedź #5 dnia: 10 Lipca 2020, 00:36:48 »
That code without the shift was likely for the earlier version of the protocol, which had no header and so the data started right from the beginning :>  In any case glad it worked out.