A
A
AJ2012-08-12 01:17:40
Delphi
AJ, 2012-08-12 01:17:40

What, in fact, did the Arduino miracle speak Chinese?

Good.
The problem is this. For some time I struggled with the communication protocol between Delphi and Arduino. Tyrnet helped with WinAPI, forums, helpdesk and other delights of finding solutions. In general, sending / receiving seems to have adjusted. Arduino stubbornly refused to read what I sent it. And I decided to check what comes to her. Sent Low - Chinese character arrived. Hmm… I thought that I might be mistaken in sending and wrote the word string in the sketch for sending . Came a triad of Chinese characters.
Google refused to help. There is nothing substantive. Maybe you comrades know something about this problem?

Sketch

String inString;
int inChar;

void setup() {
    Serial.begin(9600); // устанавливаем последовательное соединение
    pinMode(12, OUTPUT);
    pinMode(11, OUTPUT);
    digitalWrite(12, HIGH);
    digitalWrite(11, LOW);    
} 
 
void loop() {

    while (Serial.available() > 0) {
      int inChar = Serial.read();
        inString += (char)inChar; 
        delay(2);
    }
    if (inString != "") {
        Serial.print("String");
        inString = "";
    }
/*  
    if (inString == "L") {
        digitalWrite(12, LOW);
        inString = "";
       }  

    if (inString == "H") {
        digitalWrite(12, HIGH);
        inString = "";
       } 
*/

    }



delphi reading

var
  Form2: TForm2;
  BoardPortHandle: THandle;         //Хендл порта, который открываем для работы
  BoardConnectedStatus: boolean;    //Состояние соединения с портом
  BoardPortNumber: PWideChar;       //Номер порта в который отправляется команда

implementation

{$R *.dfm}

procedure TReadPortThread.Execute;
var
 ComStat: TComStat;
 dwMask, dwError: DWORD;
 OverRead: TOverlapped;
 Buf: array[0..10] of Char;
 dwRead: DWORD;
begin
  OverRead.hEvent := CreateEvent(nil, True, False, nil);
 if OverRead.hEvent = Null then
  raise Exception.Create('Error creating read event');

 FreeOnTerminate := True;

 while not Terminated do
 begin
  if not WaitCommEvent(BoardPortHandle, dwMask, @OverRead) then
  begin
   if GetLastError = ERROR_IO_PENDING then
    WaitForSingleObject(OverRead.hEvent, INFINITE)
   else
    Form2.RichEdit1.Lines.Add('Ошибка создания события чтения');
  end;

  if not ClearCommError(BoardPortHandle, dwError, @ComStat) then
   Form2.RichEdit1.Lines.Add('Ошибка очистки порта');

  dwRead:= ComStat.cbInQue;

  if dwRead > 0 then
  begin
   if not ReadFile(BoardPortHandle, Buf, dwRead, dwRead, @OverRead) then
    Form2.RichEdit1.Lines.Add('Ошибка чтения порта');
//Обработка полученных данных
   ReadenChars:=Buf;
   Synchronize(GetChars);
  end;
 end;
end;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Altf1, 2012-08-12
@Altf1

Is the data exchange rate the same on both sides?

A
AJ, 2012-08-12
@2ball

Well, in Delphi the setting is carried out:

//Настройка соединения с портом
if not GetCommState(BoardPortHandle, Dcb) then
 Form2.RichEdit1.Lines.Add('Ошибка получения настроек порта');

    Dcb.BaudRate := CBR_9600;
    Dcb.Parity := NOPARITY;
    Dcb.ByteSize := 8;
    Dcb.StopBits := ONESTOPBIT;

if not SetCommState(BoardPortHandle, Dcb) then
 Form2.RichEdit1.Lines.Add('Ошибка настройки порта')
 else Form2.RichEdit1.Lines.Add('Порт настроен');

And in Arduino, except for the speed settings, I did not find. They don't seem to exist, according to the Arduino site .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question