Home > Software engineering >  VS2010 serial communication
VS2010 serial communication

Time:10-31


You big, the younger brother, recently started to get VS2010 for PC, suffer from on the Internet also didn't find related routines to learn, now can only resort to everybody here,
Now I put my PC serial port communication requirements described below, hope expert help,
1, up and down a machine to a serial port communication, data types for hexadecimal data;
2, when PC receives the next bit machine data, then to a serial port interrupt receiving function, at the same time, number of bytes read communication within the buffer to the buffer and the data read out and saved to the specified data,
Will now my code post, share, hope to be able to show my mistakes,
Because students in the home, now I am not in the laboratory, PC program, so I do hope you can help me to test or check, thank you,
'define the RS232 serial port to send and receive array
Dim txbuf (7) As Byte
Dim rxbuf (7) As Byte

Private Sub Form1_Load (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles MyBase. Load
'==========================================================================
'=========================================================================
'a serial port initialization configuration 1 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
RS232. The Close ()
RS232. PortName="COM1"
RS232. The BaudRate=9600 baud rate
'RS232. DataBits=8 'length of data bits
RS232, Parity=IO Ports. Parity. None 'no check
RS232, StopBits=IO Ports. StopBits. One 'One stop bit
RS232. The Open ()
Txbuf (0)=& amp; H1
Txbuf (1)=& amp; The H2
Txbuf (2)=& amp; H3
Txbuf (3)=& amp; H4
Txbuf (4)=& amp; H5
Txbuf (5)=& amp; H6
Txbuf (6)=& amp; HBA
Txbuf (7)=& amp; HDE
CRC16 (txbuf)
'the Timer1. Enabled=True
End Sub

This part was received my serial code, I receive the hexadecimal data
'the RS232 communication - a serial port interrupt receiving data
Private Sub RS232_DataReceived (ByVal sender As Object, ByVal e the As System. IO, Ports, SerialDataReceivedEventArgs) Handles RS232. The method DataReceived

RS232. Read (rxbuf, 0, 8)
RS232. DiscardInBuffer () 'discard a serial port receives the data in the buffer
If (CRC16 (rxbuf)=0) Then

End the If
End Sub

CRC check code
'the CRC check function
The Function CRC16 (ByVal data_in () As Byte) As an Integer
Dim CRC16Lo As Byte, CRC16Hi As Byte CRC register
'Dim CL As Byte, CH As Byte 'polynomial code & amp; HA001
Dim SaveHi As Byte, SaveLo As Byte
Dim As Integer I
Dim Flag As Integer
CRC16Lo=& amp; HFF
CRC16Hi=& amp; HFF
CL=& amp; H1
CH=& amp; HA0
For I=0 To UBound (data_in)
CRC16Lo=CRC16Lo Xor data_in (I) 'every data with CRC register for exclusive or
For Flag=0 To 7
SaveHi=CRC16Hi
SaveLo=CRC16Lo
CRC16Hi=CRC16Hi \ 2 'high moves to the right a
CRC16Lo=CRC16Lo \ 2 'low moves to the right a
If ((SaveHi And & amp; H1)=& amp; H1) Then "if high byte last 1
CRC16Lo=CRC16Lo Or & amp; H80 'are front moves to the right after the low byte complement 1
End the If 'or automatic filling 0
If ((SaveLo And & amp; H1)=& amp; H1) Then 'if the LSB is 1 with polynomial code for exclusive or
CRC16Hi=CRC16Hi Xor CH
CRC16Lo=CRC16Lo Xor CL
End the If
Next Flag
Next I
Dim ReturnData (1) the As Byte
ReturnData CRC high (0)=CRC16Hi '
ReturnData (1)=CRC16Lo CRC low
'* 256 Or ReturnData CRC16=ReturnData (0) (1)
End the Function

So that's my charge code serial port to send and receive under a machine to send data, receive hexadecimal data using a serial port interrupt mode, plus receive CRC check, please feel free to comment,

CodePudding user response:

http://download.csdn.net/detail/veron_04/4037248

CodePudding user response:

VB.NET 2008 serial port engineering

CodePudding user response:

Boon boon, thank you, by the way, do you have to use VB PC driver printer need to print the data?

CodePudding user response:

The
reference 3 floor u011465497 response:
boon boon, thank you. By the way, do you have to use VB PC driver printers print to print data?


No,
  • Related