Solved

Serial port Comms

Posted on 1999-01-02
5
179 Views
Last Modified: 2010-04-16
How do I get COM 1 to Tx/Rx 8 bits?
WinProcs only do 7Bits since the buffer is PChar
ok so Ord(Chr(255))=255 but WriteCom from one PC and ReadCom on another 255 comes out as 127
even with BulidComDCB with mode COM1:9600,n,8,1,p

Ta Pete
0
Comment
Question by:PeteTheHat
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:scrapdog
ID: 1216799
I found this in SWAG...maybe it will help you.

---BEGIN PASTE---

>What I need to do is write an arbitrary number of bytes to a serial
>port.  Period.  No modem commands, no acknowledgement from the
>distant end of the serial line, no reading of the port (at least, not
>yet), no nothing except FOR i := 1 TO X DO Send(AByte);

Well, that's an easy task in D2.  Here is how you open the port:

  Var Port:STRING;  Handle:INTEGER;

  Port:='COM2';  // or whatever COM port
  Handle:=CreateFile(PChar(Port),GENERIC_READ+GENERIC_WRITE,
                               0,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
  If Handle=INVALID_HANDLE_VALUE then exit; // handle the error if it didn't open

  SetupComm(8192,8192);  // works best if you set the buffer size high

Now you write the characters to the port using one of two ways...

Using WriteFile:

  Var DataToSend:STRING;  Written:DWORD;

   WriteFile(Handle,DataToSend[1],Length(String),Written,Nil);

The above will use the buffer to send the data, so it will return before
the actual data has been sent to the port, so be sure to delay a bit
before closing the port so you don't truncate the outgoing data.  The
parameters are Modem handle, Where the data is, The number of
bytes to send, Returns the number of bytes actually sent, and the last
parameter is if you want to do overlapped writes (sounds to me like
you don't need to in your situation).

The other way to send data to the port is

  Var K:CHAR;
  While not TransmitCommChar(Handle,K) do Application.ProcessMessages;

This sends one character at a time.  TransmitCommChar will return
FALSE if the last character sent hasn't actually gone out the port yet,
so you have to loop like above to send every character. It also returns
FALSE if the port hasn't been opened properly, so make sure it was
opened before you get stuck in a loop somewhere./

When you are done with the port, use

   CloseHandle(Handle);

To close the port and turn off the modem.   Make sure you do this
in your error handlers because if the application terminates without
doing this you'll have to reboot your system to get access to the
COM port a second time.

I hope this is what you were looking for.

----END PASTE---
0
 

Author Comment

by:PeteTheHat
ID: 1216800
Cheeers Scrapdog ill give it a whirl soon ive been down for a while back soon again ta
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1216801
You there?
0
 

Author Comment

by:PeteTheHat
ID: 1216802
Ill buy that for a dollar ta Scrapdog give me an answer u got the points
0
 
LVL 5

Accepted Solution

by:
scrapdog earned 100 total points
ID: 1216803
Thanks for the points!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Extract text from webbrowser 5 1,385
Enable/Disable the Windows 7 'START' button 9 1,455
TMemoryStream -> PNGImage -> TIcon convertion 22 1,226
Delphi XE 5 - windows 3 864
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now