| |

VerySource

 Forgot password?
 Register
Search
Author: zfight

How to solve the problem of high CPU usage during serial communication

[Copy link]

0

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-7-27 13:30:01
| Show all posts
It is estimated that there is a problem with your code, please post it and have a look
Reply

Use magic Report

1

Threads

19

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

 Author| Post time: 2020-7-28 08:45:01
| Show all posts
Private Sub Form_Load()
    MSComm1.SThreshold = 0
    MSComm1.RThreshold = 1
    MSComm1.Settings = "19200,e,7,1"
    MSComm1.CommPort = 1
    MSComm1.PortOpen = True
    Timer1.Interval = 5000
    Timer1.Enabled = True

End Sub
Private Sub Timer1_Timer()
    Timer1.Enabled = False
    str = "04" + "51" + Hex(&H1C00 + 510 * 2) + "0A" + Chr(3)
    ChkSum = 0
    For i = 1 To Len(str)
        ChkSum = ChkSum + Asc(Mid$(str, i, 1))
    Next i
    str = Chr(2) + str + Right(Hex(ChkSum), 2)
    MSComm1.Output = str
end sub
The above is to organize the data according to the sending format,
Private Sub MSComm1_OnComm()
    If MSComm1.CommEvent = comEvReceive Then
       Timer1.Interval = 5000
       Timer1.Enabled = True
       Text2.Text = 0
       Text2.Text = MSC1.Input
    end if
End Sub
I have tried, if the received data length is less than 45 bytes when RThreshold=1, the results displayed in Text2.Text are all correct, and when it is greater than 45, it is wrong.
Reply

Use magic Report

0

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-7-28 23:45:01
| Show all posts
1 If you want to send data every 5 seconds, restarting the timer in a communication event is not a good way. Your current practice causes the data to be retransmitted 5 seconds after receiving the first byte (don't understand your Intention). In addition, the sentence Text2.Text = MSC1.Input does not match the control name (MSComm1). You can use the following methods to try to receive all data normally:

Private Sub MSComm1_OnComm()
If MSComm1.CommEvent = comEvReceive Then
       Text2.Text = Text2.Text&MSComm1.Input
end if
End Sub

2 Also note that the feedback data format is text or binary data, which should match the InputMode property of MSComm1. When binary data is included, the text control may not be displayed normally.
3 Set the InBufferSize and InputLen properties appropriately, or use the default values.
Reply

Use magic Report

1

Threads

19

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

 Author| Post time: 2020-7-29 12:15:02
| Show all posts
I made a mistake in MSC1.Input. The received data is hexadecimal data.

What I mean is that the data reception is normal, and I have also used msgbox to monitor the received data.Although the RThreshold is set to 1, in fact, the OnComm event is triggered after receiving ten data, and only once. This is what I don't understand.It should be triggered every time a byte is received.

Although it is a bit unreasonable to set RThreshold=1 and the program is a bit unreasonable, and I will not use it like this in the future. But I want to know why the above situation occurs.
Reply

Use magic Report

1

Threads

19

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

 Author| Post time: 2020-7-30 12:15:01
| Show all posts
Also, if the device is disconnected, only the send event can be triggered, and other ONCOMM events, including error events, cannot be triggered.

What should I do if I disconnect and want to trigger ONCOMM.

Thank you
Reply

Use magic Report

1

Threads

19

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

 Author| Post time: 2020-8-3 17:00:01
| Show all posts
Disconnection, can the ONCOMM event be triggered, and if it can correspond to what?
Reply

Use magic Report

0

Threads

18

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-8-3 18:15:01
| Show all posts
Is the communication mode string or binary?
Reply

Use magic Report

0

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-8-3 22:15:01
| Show all posts
ONCOMM events also include some error events, but the disconnection may not necessarily trigger the ONCOMM event. Usually the method of detecting disconnection is to send a simple command to the device, and if it returns in time, the line is normal.
Reply

Use magic Report

1

Threads

19

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

 Author| Post time: 2020-8-4 08:00:02
| Show all posts
TO:fan3786
The communication mode used is a hexadecimal string

TO:mayhem
I am using the method you mentioned, but it is a bit troublesome to process, because I need to use a serial port to achieve multiple reading and writing. During these processes, the device may be disconnected. If the disconnection cannot trigger the ONCOMM event, I also need to use a timer and a BOOLEN variable. After some time, I will see if the device has a return value to trigger the ONCOMM event, and also set the processing method for different reading and writing. If the disconnection can trigger the ONCOMM event, this Much simpler
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-8-4 19:15:02
| Show all posts
Do
DoEvents
Loop Until (Timer> Wait)' takes a lot of CPU
It can be better with the timer control
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list