このページはEtoJ逐語翻訳フィルタによって翻訳生成されました。

翻訳前ページへ


Delphi: Serial Port: Data from one PC to another HOME - - - - - - - TUTORIALS INDEX - - - - - - - - - - - - Other 構成要素 for programmers
Delicious  Bookmark this on Delicious    Recommend to StumbleUpon

Delphi: How to connect 経由で the serial port. How to use API calls

This tutorial is a little rough around the 辛勝する/優位s, but it is far enough along that I thought you'd want what's here.

にもかかわらず its sins, it has been 実験(する)d... 早期に in April 2010, using Windows XP and Delphi 4, and everything was 罰金 負かす/撃墜する to "...WriteString is, of course 有能な of sending more...". (It is probably 罰金 past there, too... but my energy for 実験(する)ing ran out at that point!)


I revisited the program in the course of working with serial comms with the marvelous Arduino microprocessor. There are a number of articles about serial comms with Arduinos at my Arduino "How To" 場所/位置... probably still in the "Level 4" section, but I make no 約束s! The best is probably the Delphi tutorial about bi-directional serial comms. That is built upon the work done in the tutorial you are 現在/一般に reading. Unless you have some 専門的知識, you might want to finish this one before 試みる/企てるing the more 前進するd tutorial. Things you can learn here will make it easier for you to understand it.

Maybe you, like me, 手配中の,お尋ね者 to hook a microprocessor up to your "big" computer, for instance an Arduino?

While I was doing the April 2010 実験(する)ing, I created a zip とじ込み/提出する you can download with the sourcecode and even the 収集するd .exe which you are welcome to use.

It shows you how to send messages from one PC to another 経由で the serial port. It does not 要求する anything more than Delphi 2 and Win98. I think it will work with higher 見解/翻訳/版s of Delphi and Windows.

For a different approach to 接近ing not only the COM ports, but other ports too, see my tutorial about the 解放する/自由な DLL InpOut. For more about using the serial port, see my serial port tutorial. I also 申し込む/申し出 a page about using a COM port to communicate between microprocessors and Windows PCs. Although it is couched in Arduino 条件, much of the (警察などへの)密告,告訴(状) is applicable to other 状況s. Don't forget: There are 安価な USB 装置s which 供給する a Windows PC with a 事実上の COM port. (Sadly, the USB to 平行の port printer 装置s seem いっそう少なく 井戸/弁護士席 ふさわしい to connecting things other than printers.)
At the moment, this tutorial tells you about sending data only one way, from PC "A", to PC "B". For bi-directional serial communications, you have to work harder. I would 示唆する that you finish the page you are reading first, as everything here is part of the bidirectional answer. I have done a その上の tutorial on how to 遂行する bi-directional serial comms with Windows, when you are ready for it.

This will not give you an all singing, all dancing 使用/適用!

What it will do, I hope, is give you a starting point, useful in its own 限られた/立憲的な way, and perhaps also useful if you want to go その上の.

Along the way, the tutorial illustrates the general 技術 of using Windows API calls 直接/まっすぐに, instead of with the help of Delphi wrappers. The latter is より望ましい... where possible... but the latter is an 代案/選択肢 you should know about.

Before you try to do anything yourself with the serial port, make sure that your 金物類/武器類 is working 適切に.

I make many 言及/関連s to Hyperterminal in what follows. That used to come with Windows, but doesn't now, and isn't always 任命する/導入するd on older machines. You can use any simple serial 終点 ソフトウェア. I've been using PuTTY, but 港/避難所't used it much yet. It is 解放する/自由な, and runs on Windows an Linux. For what we're doing here, you only need to download putty.exe, and that's the program... not a 体制/機構 一括.

Connect two PCs with a null modem cable. Start Hyperterminal on each of them. 勝利,勝つ 98: It is in Programs|従犯者s|Communications.... if it has been 任命する/導入するd. It is one of the 解放する/自由な things which come with Windows, but it isn't always 任命する/導入するd. To 任命する/導入する: Settings | 支配(する)/統制する パネル盤 | 追加する/除去する Programs, The Windows 体制/機構 tab.

Once your computers have Hyperterminal, start it up... Your start menu will probably have been given a link to the Hyperterminal folder. You need to 二塁打-click on "Hypertrm.exe". (The 入ること/参加(者)s with .ht 拡張s can be 削除するd).

When you first start Hyperterminal, you'll probably get the New 関係 Wizard. If you want your machines to be 一貫した with what I say in this tutorial, create a 関係 called DD55HTC (Delphi デモ 55, HyperTerminal 関係). It doesn't 事柄 what you put in the phone number fields. Just "Connect Using..." one of your Com ports. Of course it has to be the 権利 Com port. Oh goodie... the fun has started already. (I really hate serial comms).

A detour: On one of my machines, a laptop, the port has a little sticker I put on it 説 it is Com5, but Hyperterminal didn't 申し込む/申し出 a Com5. Com1 gave rise to "Could not open."... and the wizard の近くにd. とじ込み/提出する | 所有物/資産/財産s is what you need to try again. The Configure button "worked" for Com3, but that didn't seem 権利 either. Next I 権利 clicked "My Computer", clicked "所有物/資産/財産s", went into 装置 経営者/支配人, Infra Red 装置s. I said "無能にする in this profile". Rebooted... At last I COULD "talk" from one computer to the other over the null modem, using Com4. Bah.

I then went to the second machine, started Hyperterminal, started a new 関係. Called it DD55HTC, too. (No need for different 指名するs... but you could have them, if you wished.)

Thence to the Configure dialog for each machine's Com port. Most of the settings don't 事柄 much in this 使用/適用... but they must be the same in both machines. Try:

Bits per second: 9600
Data bits:8
Parity: 非,不,無
Stop bits: 1
Flow: 金物類/武器類 or 非,不,無.

One more thing to do... Call | Call 経由で the menus, or just click on the "Call" button (a little telephone). You should see "Connnected" in the status 妨げる/法廷,弁護士業, window's lower left. Now, all 存在 井戸/弁護士席 (it is やめる possible that it won't be!), when you type on one of the computers, what you typed will appear on the other. And 副/悪徳行為 versa. Without "地元の echo", you won't see what you type on the machine you are on. Get that much working!!! Good luck. Serial comms... yuck. (Did I say that?)

If you want, you can turn on a 地元の echo... not very important to what is to follow, but if you want it: とじ込み/提出する | 所有物/資産/財産s | Settings... and there you'll find a box to tick.

When you leave Hyperterminal, you are asked "Do you want to save 開会/開廷/会期?" 説 yes 保存するs any settings you may have made.


Shut 負かす/撃墜する Hyperterminal on the machine you have Delphi on.

Start a new Delphi 使用/適用. Put five buttons on it. 指名する them....

buOpenConnection
buSendX
buSendY
buCloseConnection
buQuit

... and make the labels the obvious things.

指名する the main form DD55f1. Save the 事業/計画(する) in a folder called DD55. Call the 部隊 DD55u1, the 事業/計画(する) DD55. Make buQuit's OnClick
使用/適用.終結させる;
Before I go any その上の... my thanks to the good people at Borland (Inprise) for the good (警察などへの)密告,告訴(状) that used to be at http://community.borland.com/article/0,1410,16400,00.html, from which the heart of all this (機の)カム.

Use the 反対する 視察官 to 始める,決める to 誤った the "enabled" 所有物/資産/財産 of buSendX, buSendY and buCloseConnection.

宣言する a 全世界の variable...
    hCommFile : THandle;
... by putting the above just after the comment "{私的な 宣言s}" in the TDD55F1 class 宣言. Then, just after...
    hCommFile : THandle;    
... 挿入する the に引き続いて 今後 宣言....
    手続き WriteString(sToSend:string);
Then, just before the 部隊's 終点 "end.", 挿入する....
手続き TDD55F1.WriteString(sToSend:string);
var NumberWritten : dWord;
  {N.B.: I saw the に引き続いて in a newsgroup 地位,任命する:
  "警告: There is an error in the FAQ - NumberWritten must be
  dword not LongInt"}
begin
if WriteFile(hCommFile,
               PChar(sToSend)^,
               Length(sToSend),
               NumberWritten,
               nil)=誤った then
      showmessage('Unable to send');
end;
(We'll see what this is good for in a moment!)

Make buOpenConnection's OnClick....
手続き TDD55F1.buOpenConnectionClick(Sender: TObject);
var
  CommPort : string;
begin
  CommPort := 'COM1';
  hCommFile := CreateFile(PChar(CommPort),
                          GENERIC_WRITE,
                          0,
                          nil,
                          OPEN_EXISTING,
                          FILE_ATTRIBUTE_NORMAL,
                          0);
  if hCommFile=INVALID_HANDLE_VALUE then begin
      ShowMessage('Unable to open '+ CommPort);
      end // no ; here
      else begin
      buSendX.enabled:=true;
      buSendY.enabled:=true;
      buCloseConnection.enabled:=true;
      buOpenConnection.enabled:=誤った;
      end; //else
end;
公式文書,認める: If when you connected 経由で Hyperterminal, you were using a Com port other than 1, then you will need to 修正する the COM1 above accordingly.

Now make buSendX's OnClick....
手続き TDD55F1.buSendXClick(Sender: TObject);
begin
WriteString('X');
end;
Run the program. Click on the "Open 関係" button. Click the "Send X" button. You should see an "X" appear on the other computer! You should click on the "の近くに 関係" button before 出口ing the program, though Windows may clean up after you if you fail to do so. A "proper" 使用/適用 would need to 監視する whether the 関係 was open or の近くにd, の近くに it when the program is の近くにd, if not の近くにd.

追加する the に引き続いて for buSendY's OnClick....
手続き TDD55F1.buSendYClick(Sender: TObject);
begin
WriteString('Y');
end;
Now you can send "X" or "Y" to the other computer (Useful, when you've sent a bunch of Xs, and it is a little hard to see if another is arriving!

The 手続き WriteString is, of course 有能な of sending more than a 選び出す/独身 byte. 始める,決める up a button called buHelloWorld, make it's OnClick...
手続き TDD55F1.buHelloWorldClick(Sender: TObject);
begin
WriteString('Hello World ');
end;
... and then you can send "Hello World" to the 目的地 computer. Ta! Da!


You can send numbers to the other computer... but the receiving program will have to be 推定する/予想するing them. If Hyperterminal receives a 65, it will 陳列する,発揮する the character "A". There are numbers Hyperterminal will 答える/応じる 不正に to.

If you want "65" (i.e. the two digits 6 and 5) to appear on the 目的地 computer, use WriteString('65'). If you want the number 65 sent to the other computer, change the line of code in buSendX's OnClick to....
WriteString(chr(65));
SendX will now send 65 to the other computer. If it is running Hyperterminal, it will 陳列する,発揮する an "A". There is nothing to stop you from creating a system which will dispense 65 marbles if it is sent 65... but that's a story for another time! (Or visit my 場所/位置 about sensing and 支配(する)/統制する.)

I have done a その上の tutorial on how to 遂行する bi-directional serial comms with Windows, when you are ready for it.

Lastly, if you want to work through an earlier tutorial that was a "行う/開催する/段階 along the way" to what you saw above, my "Delphi Serial Comms" tutorial is still online. Looking at that might help you with something that wasn't (疑いを)晴らす above, or 単に be useful to 強固にする/合併する/制圧する what you learned above.
            力/強力にするd by FreeFind
  場所/位置 search Web search
場所/位置 地図/計画する    What's New    Search This search 単に looks for the words you enter. It won't answer "Where can I download InpOut32?"

Click here if you're feeling 肉親,親類d! (促進するs my 場所/位置 経由で "Top100Borland")


If you visit 1&1's 場所/位置 from here, it helps me. They host my website, and I wouldn't put this link up for them if I wasn't happy with their service. They 申し込む/申し出 things for the beginner and the 会社/団体.www.1and1.com icon

広告 from page's editor: Yes.. I do enjoy 収集するing these things for you... hope they are helpful. However.. this doesn't 支払う/賃金 my 法案s!!! If you find this stuff useful, (and you run an MS-DOS or Windows pc) please visit my freeware and shareware page, download something, and 循環させる it for me? Links on your page to this page would also be 高く評価する/(相場などが)上がるd!
Click here to visit editor's freeware, shareware page.

Link to Tutorials main page
How to email or 令状 this page's editor, Tom Boyd


Valid HTML 4.01 Transitional Page 実験(する)d for 同意/服従 with INDUSTRY (not MS-only) 基準s, using the 解放する/自由な, 公然と accessible validator at validator.w3.org


If this page 原因(となる)s a script to run, why? Because of things like Google パネル盤s, and the code for the search button. Why do I について言及する scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....