Home > Software engineering >  Urgent urgent, the great god help me to solve VS2010 serialport send and receive Chinese question!
Urgent urgent, the great god help me to solve VS2010 serialport send and receive Chinese question!

Time:10-15

Private Sub SendButton_Click (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles SendButton. Click 'send
Dim As a Integer
Try
If Me. RadioButton3. Checked=True Then
Me. SerialPort1. Write Me. TextBox1. (Text)
A=Me. TextBox1. TextLength
SCount. Text=(a + CULng (SCount. Text))
The Else
Dim SendConvert As String
SendConvert=ConvertHexChr (TextBox1. Text) 'to space
Dim BytSendDec As String
Dim Temp As Byte (0)
Dim As Integer I
Dim longth As Integer
The longth=Len (SendConvert)
A=longth/2
SCount. Text=(a + CULng (SCount. Text))
For I=1 To longth Step 2
BytSendDec=Mid (SendConvert, I, 2)
Temp (0)="& amp; H "& amp; BytSendDec
SerialPort1. Write (Temp, 0, 1)
Next
End the If
If SCount. Text="2147483647" Then
SCount. Text=0
End the If
Catch the ex As Exception
MsgBox (" failure ")
End the Try
End Sub

Public Sub SerialPort1_DataReceived (ByVal sender As Object, ByVal e the As System. IO, Ports, SerialDataReceivedEventArgs) Handles SerialPort1. Method DataReceived receive
'Dim a As an Integer, I, As an Integer
A=SerialPort1. BytesToRead 'all bytes in the buffer
ReDim ss (a - 1)
SerialPort1. Read (ss, 0, a)
SerialPort1. DiscardInBuffer ()
If RadioButton1. Checked=True Then 'character display
For I=0 To a - 1
TextBox2. AppendText (CRH (ss) (I))
Next
TextBox2. AppendText (CRH (32))
The Else
For I=0 To a - 1
If Len (Hex (ss) (I))=1 Then
TextBox2. AppendText (" 0 "& amp; ) (I) (ss 'hexadecimal display
The Else
TextBox2. AppendText (Hex (ss) (I))
End the If
TextBox2. AppendText (CRH (32))
Next
End the If
'count
RCount. Text=(CStr (a + CULng (RCount. Text)))
If RCount. Text="2147483647" Then
RCount. Text=0
End the If
End Sub

CodePudding user response:

Send the Chinese characters are all "?"

CodePudding user response:

For Chinese characters, the need to transform, using Binary way communication, you received the data in an array of Byte type, and then use StrConv transformation can,
Such as the following:
 
Option Explicit
Dim bytHanZi () As Byte
Dim intP As Integer
Private Sub Command1_Click ()
BytHanZi=StrConv (Text1. Text, vbFromUnicode)
Text2. Text=""
For intP=LBound (bytHanZi) To UBound (bytHanZi)
Text2. Text=Text2. Text & amp; Right (" 00 "& amp; Hex (bytHanZi (intP)), 2)
Next intP
End Sub

Private Sub Command2_Click ()
Text1. Text=StrConv (bytHanZi vbUnicode)
End Sub

Private Sub Form_Load ()
Text1. Text="shame"
Text2. Text=""
End Sub


Command2_Click the code in which will be the contents of the byte array can be converted into Chinese characters,

CodePudding user response:

Already have, I design the master change, cattle

CodePudding user response:

Have a code? A serial port receives the data, I have been couldn't display the Chinese characters

CodePudding user response:

With q, I received the Chinese character is also a question mark, think this is how change

CodePudding user response:

Receive also received Binary array, again StrConv into string,
  • Related