[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

COM port

Posted on 2005-04-20
1
Medium Priority
?
207 Views
Last Modified: 2013-11-15
I would like to send some binary bytes ('FFEEDDCCBBAA99887766554433221100' in HEX) to COM1 to control another device.
However, the following code cannot work properly. So, what is the problem here ?


  hndFile:= CreateFile(PChar('COM1'),
                       GENERIC_WRITE,
                       0,
                       nil,
                       OPEN_EXISTING,
                       FILE_ATTRIBUTE_NORMAL,
                       0);

  strSource:= 'FFEEDDCCBBAA99887766554433221100';
  intLength:= Length(strSource);
  pchSource:= StrAlloc(intLength);
  StrPCopy(pchSource, strSource);
  intLength:= Round(StrLen(pchSource) / 2);
  pchTarget:= StrAlloc(intLength);
  HexToBin(pchSource, pchTarget, intLength);
  WriteFile(hndFile,
            pchTarget,
            intLength,
            dwdNumberWritten,
            nil);
  StrDispose(pchSource);
  StrDispose(pchTarget);

  CloseHandle(hndFile);
0
Comment
Question by:andrewyu
1 Comment
 
LVL 17

Accepted Solution

by:
mokule earned 120 total points
ID: 13829101
You've missed setting Com state. Something like

    if  hndFile <> INVALID_HANDLE_VALUE then
      begin
      if GetCommState( hndFile, dcbPort) then
        begin
        dcbPort.BaudRate := 9600;
        dcbPort.ByteSize := 8;
        dcbPort.Parity := 0;
        dcbPort.StopBits := 0;
        dcbPort.Flags := 0;
        SetCommState( hndFile, dcbPort);
        end;
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If something goes wrong with Exchange, your IT resources are in trouble.All Exchange server migration processes are not designed to be identical and though migrating email from on-premises Exchange mailbox to Cloud’s Office 365 is relatively simple…
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Using Adobe Premiere Pro, the viewer will learn how to set up a sequence with proper settings, importing pictures, rendering, and exporting the finished product.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question