Forum > Embedded - AVR

One-way serial communication example using Synaser & Arduino Nano

<< < (6/6)

ccrause:
Below a plain program that uses the serial unit for serial data transmission on the PC side.  Tested with the previous AVR firmware code.

A note about the Arduino board reset from DTR signal: The serial port should first be opened to obtain a handle before a call to SerSetDTR can be made.  The OS/USB driver will however assert the DTR (and other signal lines) when the serial port is opened, therefore the initial behaviour is determined be the previous settings of that port.  Also, the DTR output of the Arduino Uno/Nano USB-serial converters are inverted, so setting DTR to true/high results in a low signal to the reset circuitry which then causes a reset.  Thus to prevent  a board reset when opening the serial port on the PC, set DTR to true (3rd command line argument set to 1).


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program serialtransmitter; uses  serial, sysutils; procedure help;begin  WriteLn('Usage:');  WriteLn('  test <portname> <baud> {DTR}');  WriteLn('    <port> - required: full name of serial port e.g. /dev/ttyUSB0 or com3');  WriteLn('    <baud> - required: baud rate of connection e.g. 9600');  WriteLn('    {DTR}  - optional: assert logical state of DTR line, 0 or 1');  WriteLn;end; var  ser: THandle;  s: string;  baud, i: integer;  doSetDTR, DTRsetting: boolean; begin  if ParamCount < 2 then  begin    help;    Exit;  end;   s := ParamStr(2);  val(s, baud, i);  if i <> 0 then  begin    WriteLn('Invalid argument for baud. Value must be a valid integer.');    help;    Exit;  end;   if ParamCount > 2 then  begin    if (ParamStr(3) = '0') or (ParamStr(3) = '1') then    begin      doSetDTR := true;      DTRsetting := ParamStr(3) = '1';    end    else    begin      WriteLn('Invalid argument for DTR. Value must be 0 or 1');      help;      Exit;    end;  end  else    doSetDTR := false;   s := ParamStr(1);  ser := SerOpen(s);  if ser > -1 then  begin    WriteLn('Usage: type command followed by enter key.');    WriteLn('Type exit to exit');    WriteLn;     SerSetParams(ser, baud, 8, NoneParity, 1, []);     // Set DTR to false    if doSetDTR then      SerSetDTR(ser, DTRsetting);     repeat      // Wait for data to be entered      ReadLn(s);       if CompareText(s, 'exit') = 0 then        Break;       if SerWrite(ser, s[1], Length(s)) <> Length(s) then        WriteLn('*Error transmitting data: ', GetLastOSError);    until false;    SerClose(ser);  end  else    WriteLn('Error ', GetLastOSError, ' opening serial port ', s);end.

Navigation

[0] Message Index

[*] Previous page

Go to full version