another RS232 Encoding problem ...

nescartel

Member
Joined
Jun 10, 2007
Messages
22
Programming Experience
3-5
hello,

i have this simple application that echoes serial port communication:
2003796598727757721_rs.jpg


"Tx" is the HEX value (from 00 to FF) which i will send,
and "Rx" should echo directly what is written in "Tx".

if i use ASCII Encoding, i cannot transmit values bigger than 80, as it will be replaced with 3F (or "?"). and from recommendations found on this forum, it is better if i use UTF8 Encoding, which is what i am using now.

However, problem still arise on values larger than 80 hexadecimal. and this time, it is not replaced with 3F, but rather different values, for example:

if i Tx : 15 07 72 10 00 80 F0
the Rx will be: 15 07 72 10 00 C2 80 C3 B0

if i Tx : 15 07 72 10 00 90 E0
the Rx will be: 15 07 72 10 00 C2 90 C3 A0

why am i not getting the EXACT value that is transmitted for values larger than 80 hexadecimals? :confused:
i have read in other threads how this problem is solved by changing the encoding method to UTF8, so why is it not solving my problem?
any help is very much appreciated... danke! :(



this is my Transmit subroutine:
VB.NET:
    Sub TransmitData()
        Dim StartTx As Integer
        RxDump.Text = Nothing

        TxBufferSize = UBound(TxBuffer)

        For StartTx = 0 To TxBufferSize
            TxBuffer(StartTx) = Chr("&H" & TxBuffer(StartTx))  
            SerialPort1.Write(TxBuffer(StartTx))
        Next StartTx

        Array.Clear(TxBuffer, 0, TxBufferSize + 1)
    End Sub
and this is my Receive subroutine:
VB.NET:
    Private Sub ReceiverData(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim StartByteRx As Integer

        RxBufferSize = SerialPort1.BytesToRead          'number of bytes to receive

        For StartByteRx = 0 To RxBufferSize - 1
            RxBuffer = SerialPort1.ReadByte.ToString()

            If InValue = TxBufferSize + 2 Then
                RxLength = RxBuffer                     'amik nilai Rx byte 2 (Rx length)
            End If

            RxData(InValue) = RxBuffer                  'all Rx message stored in RxData

            If DisplayFlag = True Then
                
                RxDump.Invoke(New StringSubPointerReceived(AddressOf DisplayReceived), RxBuffer)
               
            End If
            InValue = InValue + 1
        Next StartByteRx

    End Sub

    Private Sub DisplayReceived(ByVal RBuffer As String) 'display received message from COM1
        RxDump.Text = LTrim(RxDump.Text & " " & Hex(RBuffer))
       
    End Sub
 
hi guys,

found the solution,
i just use the encoding as below:
VB.NET:
 Serial.Encoding = System.Text.Encoding.GetEncoding(28591)
and it solves everything! :D

another obstacle solved! hehehe...
 
Back
Top