Windows CE Programming Serial Port - Getting Garbled Output

Posted by user576639 on Stack Overflow See other posts from Stack Overflow or by user576639
Published on 2012-09-27T03:35:44Z Indexed on 2012/09/27 3:37 UTC
Read the original article Hit count: 677

Filed under:
|
|

I am programming a Windows CE 6 device (Motorola MC3100 scanner Terminal). Using Lazarus FPC to compile it.

After 3 weeks work I reluctantly post here in the hope someone can suggest why I am getting garbled output from the serial port.

The code I am using is posted below. This is the standard code I have found from several places.

The OpenPort works OK.

When I send the string using SendString('ABCDEF') I get garbled input to the PC Serial port such as:

4[#131][#26][#0][#0][#0][#0] (the bracketed data indicates that it is a non-printable character ASCII Code)

Obviously it is connecting to the port OK AND it is sending the correct no of characters (7).

I have tried all combinations of Baud Rate, Data Bits, Parity and Stop Bits without any joy. Also tried changing cable, on a different PC etc.

Could it be I need to set something else in the DCB?

Any help or suggestions would be GREATLY appreciated.

unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls, 
Windows, LResources;

type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
function OpenPort(ComPort:String;BaudRate,ByteSize,Parity,StopBits:integer):String;
procedure SendString(str:String);

private
{ private declarations }
public
{ public declarations }
end;

var
Form1: TForm1;

cc:TCOMMCONFIG;
Connected:Boolean;

implementation
{$R *.lfm}
var F: TextFile;
var hComm: THandle;
str: String;
lrc: LongWord;

{ TForm1 }  
function  
OpenPort(ComPort:String;BaudRate,ByteSize,Parity,StopBits:integer):String;

var
cc:TCOMMCONFIG;
SWide:WideString;
Port:LPCWSTR;

begin
SWide:=ComPort;
Port:=PWideChar(SWide);
result:='';
if (1=1) then begin
Connected:=False;
hComm:=CreateFile(Port, GENERIC_READ or GENERIC_WRITE,0, nil,OPEN_EXISTING,0,0);
if (hComm = INVALID_HANDLE_VALUE) then begin
ShowMessage('Fail to Open');
exit;

end;
GetCommState(hComm,cc.dcb);
cc.dcb.BaudRate:=BaudRate;
cc.dcb.ByteSize:=ByteSize;
cc.dcb.Parity:=Parity;
cc.dcb.StopBits:=StopBits;

if not SetCommState(hComm, cc.dcb) then begin
result:='SetCommState Error!';
CloseHandle(hComm);
exit;
end;
Connected:=True;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
OpenPort('COM1:',9600,8,0,0);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
SendString('ABCDEFG');
end;

procedure TForm1.SendString(str:String);
var
lrc:LongWord;
begin
if (hComm=0) then exit;
try
if not PurgeComm(hComm, PURGE_TXABORT or PURGE_TXCLEAR) then
raise Exception.Create('Unable to purge com: ');
except
Exit;
end;
WriteFile(hComm,str,Length(str), lrc, nil);
end;

end.

© Stack Overflow or respective owner

Related posts about serial

Related posts about lazarus