Communication between. NET and RS232 Devices

Source: Internet
Author: User

I wanted to sort out some things. Later I found that there was nothing special to explain.ArticleIt is very clear.
The only thing that needs to be explained is how to use this class.Source codeWe can also find two good implementations.

    1. Commp ingpong
    2. Commline

It may be worth mentioning that lineterm. VB, which is used as an example in the article, is a very simple and clear implementation:

1 Imports Forbidden. commbase
2
3 Public   Class lineterm Class Lineterm
4 Inherits Commline
5
6 Public   Sub sendcommand () SubSendcommand (ByvalSAs String)
7Send (s)
8End sub
9
10 Public   Sub transactcommand () Sub Transactcommand ( Byval S As   String )
11 Dim R As   String
12 R = TransAct (s)
13 Console. Writeline ( " Response: "   + R)
14 Prompt ()
15 End sub
16
17 Public   Sub prompt () SubPrompt ()
18Console.Writeline("Type string to send and press Enter. Empty string to close COM port.")
19End sub
20
21 Protected   Overrides   Function commsettings () Function Commsettings () As Commbasesettings
22 Dim CS As   New Commlinesettings ()
23 CS. setstandard ( " COM1: " , 19200 , Handshake. None)
24 CS. rxfilter =   New ASCII () {ASCII. Lf, ASCII. Soh}
25 CS. rxterminator = ASCII. cr
26 CS. txterminator =   New ASCII () {ASCII. Cr}
27 Setup (CS)
28 Return CS
29 End Function
30
31 Protected   Overrides   Sub onrxline () Sub Onrxline ( Byval S As   String )
32 Console. Writeline ( " Received: "   + S)
33 Prompt ()
34 End sub
35
36 Protected   Overrides   Sub ontxdone () SubOntxdone ()
37Console.Writeline("Transmission complete")
38Prompt ()
39End sub
40
41 End Class
42 Module module1 Module Module1
43
44 Sub main () Sub Main ()
45 Dim T As   New Lineterm ()
46 Dim C As   String
47 Console. Writeline ( " Press enter to open COM port " )
48 Console. Readline ()
49 If T. open () Then
50 Console. Writeline ( " COM port open " )
51 T. Prompt ()
52 While   True
53 C = Console. Readline (). Trim
54 If C =   ""   Then   Exit   While
55 T. sendcommand (c)
56 ' T. transactcommand (c)
57 End   While
58 T. Close ()
59 End   If
60 Console. Writeline ( " COM port closed " )
61 Console. Writeline ( " Press enter to close program. " )
62 Console. Readline ()
63 End sub
64
65 End Module
66
67

Among them, send and transact are the differences between synchronous and asynchronous.

If transactcommand is used, the system sends the command and waits for a response. The received data is returned as the return value of the function.

If sendcommand is used, the onrxline function is triggered after the message is sent. (The onrxchar event is triggered every time a character is received to determine whether the received data is correct, or is there a correct end? Once the data reception is determined to be complete, the onrxline function is triggered, which is implemented in commline), indicating the received data display or prompt.

Oh, it may be worth noting that this control is different from other class libraries that use Windows API. NET and communicate with RS232 devices:

    1. Using inheritance, there is also the idea of OO, unlike other can only be counted as a tool class.
    2. The thread is used for receiving, and the inherited classes are automatically received by the thread.
    3. One character is received at a time. Others receive a string at a time. The advantage is that, unlike others, sometimes the results are returned if the receiving is not completed. The disadvantage may be that the speed is a little slow. For some RS232 devices, the speed requirement in milliseconds, coupled with the speed of. net, may cause some problems.
    4. You can write a judgment function in onrxchar, such as checking whether there are start and end characters or whether the verification bit is correct to determine whether the receiving is complete (if the judgment of this function is complicated, it may affect the transmission speed. This event will be triggered every time a character is received ......).
    5. You can easily encapsulate RS232 devices into objects. Or use the singleton mode to ensure resource conflicts between RS232 devices (not tested ).
    6. Others.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.