Results 1 to 12 of 12

Thread: Serial data outputs nonsense

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Serial data outputs nonsense

    Hi guys,

    I'm attempting at just displaying the raw values from a COM serial port from a sensor, which gives around 4000 rows of data per cycle.

    Here's the code:

    Code:
    Imports System
    Imports System.IO.Ports
    Imports System.Threading
    
    Public Class Form1
    
        Dim readThread As Thread = New Thread(AddressOf ReadFromCom)
        Dim abortThread As Boolean
    
        Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SerialPort2.Open()
            readThread.Start()
    
        End Sub
    
        Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With SerialPort2
                .PortName = "COM9"
                .BaudRate = 2000000
                .Parity = Parity.None
                .DataBits = 8
                .StopBits = 1
                .ReadTimeout = Nothing
                .DtrEnable = True
                .RtsEnable = True
            End With
        End Sub
    
        Public Sub ReadFromCom()
    
            While abortThread = False
                Try
                    Dim message As String = SerialPort2.ReadExisting
                    updateStatus("Received: " & message)
                Catch ex As TimeoutException
                    updateStatus(ex.ToString)
                End Try
            End While
        End Sub
    
        Public Delegate Sub updateStatusDelegate(ByVal newStatus As String)
    
        Public Sub updateStatus(ByVal newStatus As String)
            If Me.InvokeRequired Then
                Dim upbd As New updateStatusDelegate(AddressOf updateStatus)
                Me.Invoke(upbd, New Object() {newStatus})
            Else
                TextBox1.Text = newStatus & vbCrLf & vbCrLf & TextBox1.Text
            End If
        End Sub
    End Class
    The output textbox gives out a string of ????????? most of the time, sometimes numerical. Sometimes it's nothing.

    Anything I should read more into? It seems not many thread discusses this at length so I'm tweaking about other people's code hoping for the best.

    Anyhow an independent Python code with MatPlotLib, with the same settings for the serial port outputs the data quite nicely.

    Thanks.
    Vizier87

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Serial data outputs nonsense

    Quote Originally Posted by Vizier87 View Post
    Hi guys,

    I'm attempting at just displaying the raw values from a COM serial port from a sensor, which gives around 4000 rows of data per cycle.

    Here's the code:

    Code:
    Imports System
    Imports System.IO.Ports
    Imports System.Threading
    
    Public Class Form1
    
        Dim readThread As Thread = New Thread(AddressOf ReadFromCom)
        Dim abortThread As Boolean
    
        Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SerialPort2.Open()
            readThread.Start()
    
        End Sub
    
        Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With SerialPort2
                .PortName = "COM9"
                .BaudRate = 2000000
                .Parity = Parity.None
                .DataBits = 8
                .StopBits = 1
                .ReadTimeout = Nothing
                .DtrEnable = True
                .RtsEnable = True
            End With
        End Sub
    
        Public Sub ReadFromCom()
    
            While abortThread = False
                Try
                    Dim message As String = SerialPort2.ReadExisting
                    updateStatus("Received: " & message)
                Catch ex As TimeoutException
                    updateStatus(ex.ToString)
                End Try
            End While
        End Sub
    
        Public Delegate Sub updateStatusDelegate(ByVal newStatus As String)
    
        Public Sub updateStatus(ByVal newStatus As String)
            If Me.InvokeRequired Then
                Dim upbd As New updateStatusDelegate(AddressOf updateStatus)
                Me.Invoke(upbd, New Object() {newStatus})
            Else
                TextBox1.Text = newStatus & vbCrLf & vbCrLf & TextBox1.Text
            End If
        End Sub
    End Class
    The output textbox gives out a string of ????????? most of the time, sometimes numerical. Sometimes it's nothing.

    Anything I should read more into? It seems not many thread discusses this at length so I'm tweaking about other people's code hoping for the best.

    Anyhow an independent Python code with MatPlotLib, with the same settings for the serial port outputs the data quite nicely.

    Thanks.
    Vizier87
    Hello Vizier87,

    What kind of data are you expecting from that sensor? If it is plain text, your problem might be an encoding issue. If it isn't plain text you probably shouldn't be reading it straight into a string but rather a byte array and display it's contents as a series of hexadecimal values. Try this page: https://social.msdn.microsoft.com/Fo...orum=vbgeneral

    yours,
    Peter Swinkels

    EDIT:
    Perhaps you could upload that Python code? It would make understanding the issue easier.
    Last edited by Peter Swinkels; Jul 16th, 2020 at 09:06 AM.

  3. #3
    New Member
    Join Date
    Aug 2017
    Posts
    7

    Re: Serial data outputs nonsense

    Quote Originally Posted by Vizier87 View Post
    Hi guys,

    I'm attempting at just displaying the raw values from a COM serial port from a sensor, which gives around 4000 rows of data per cycle.

    Here's the code:

    Code:
    Imports System
    Imports System.IO.Ports
    Imports System.Threading
    
    Public Class Form1
    
        Dim readThread As Thread = New Thread(AddressOf ReadFromCom)
        Dim abortThread As Boolean
    
        Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SerialPort2.Open()
            readThread.Start()
    
        End Sub
    
        Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With SerialPort2
                .PortName = "COM9"
                .BaudRate = 2000000
                .Parity = Parity.None
                .DataBits = 8
                .StopBits = 1
                .ReadTimeout = Nothing
                .DtrEnable = True
                .RtsEnable = True
            End With
        End Sub
    
        Public Sub ReadFromCom()
    
            While abortThread = False
                Try
                    Dim message As String = SerialPort2.ReadExisting
                    updateStatus("Received: " & message)
                Catch ex As TimeoutException
                    updateStatus(ex.ToString)
                End Try
            End While
        End Sub
    
        Public Delegate Sub updateStatusDelegate(ByVal newStatus As String)
    
        Public Sub updateStatus(ByVal newStatus As String)
            If Me.InvokeRequired Then
                Dim upbd As New updateStatusDelegate(AddressOf updateStatus)
                Me.Invoke(upbd, New Object() {newStatus})
            Else
                TextBox1.Text = newStatus & vbCrLf & vbCrLf & TextBox1.Text
            End If
        End Sub
    End Class
    The output textbox gives out a string of ????????? most of the time, sometimes numerical. Sometimes it's nothing.

    Anything I should read more into? It seems not many thread discusses this at length so I'm tweaking about other people's code hoping for the best.

    Anyhow an independent Python code with MatPlotLib, with the same settings for the serial port outputs the data quite nicely.

    Thanks.
    Vizier87
    I would double check the Baud Rate that the sensor uses. 2,000,000 seems a bit far fetched. I think the highest truly supported by serial ports is 256,000 and even so most devices only use 9,200 or 19,200. If you look into the details for your sensor they should mention the baud rate somewhere. (Also if the baud rate is wrong that causes exactly what you are describing)

    Hope that helps!

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Serial data outputs nonsense

    I agree with Catch22. Perhaps with Python, and the library used, it caps the speed at a maximum value, so even though you specify 2000000 perhaps it caps it at 115200, which happened to match the sensor speed, but with .Net it caps at a higher speed, or actually tries to use the 2000000 value.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Serial data outputs nonsense

    I agree with both points. My first thought was that this was an encoding issue. If anything is coming through, it is bytes, and those bytes aren't being turned into recognizable characters. However, that baud is too hot for serial. Something is coming through, but it may be both corrupted and incorrectly interpreted.

    I'd fix the baud issue, because that's clearly wrong, and if that isn't sufficient, then I'd look at the return as an array of bytes.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: Serial data outputs nonsense

    Hi everyone. Thanks for your replies!

    The data comes from a CCD sensor (attached the datasheet). It has around 4000 values which need to be read close to real-time.

    And is interfaced to the PC through a USB-to-Serial FTDI chip on a development board, which shows LED response when data is received.

    So, I've tested with several values of the baud rate in the code (No means no LED response, Yes means the LED blinks rapidly):

    100k: No, no data received at all
    200k: Yes, data, short strings of "????"
    300k: Yes, mixed short and long strings of "????"
    400k: Yes, short strings of "????" at first, then long.
    500k: Yes, long strings of "????"
    600k: Yes, long strings of "????"
    700k: Yes, long strings of "????"
    1m: Yes, long strings of "????"
    3m: No, no data received at all

    Perhaps the actual baud rate sits somewhere in 200k to 400k?

    I'm currently researching on how to accurately figure out the baud rate, but any pointers here would be greatly appreciated.

    Edit: I'm currently working on converting the byte values. I'll update here soon if I get anything new.

    Thanks guys.

    Vizier87
    Attached Images Attached Images

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: Serial data outputs nonsense

    Quote Originally Posted by Peter Swinkels View Post
    Hello Vizier87,

    What kind of data are you expecting from that sensor? If it is plain text, your problem might be an encoding issue. If it isn't plain text you probably shouldn't be reading it straight into a string but rather a byte array and display it's contents as a series of hexadecimal values. Try this page: https://social.msdn.microsoft.com/Fo...orum=vbgeneral

    yours,
    Peter Swinkels

    EDIT:
    Perhaps you could upload that Python code? It would make understanding the issue easier.



    Hi Peter, many thanks for your kind reply.

    Here's the code:
    python Code:
    1. import serial
    2. import time
    3. import csv
    4. import matplotlib
    5. matplotlib.use("tkAgg")
    6. import matplotlib.pyplot as plt
    7. import matplotlib.animation as animation
    8. import numpy as np
    9.  
    10. def animate(i):
    11.     buffer = []
    12.     i = 0
    13.    
    14.     bufferx = ser.read(3648)
    15.     buffer = [x for x in bufferx]  
    16.  
    17.     buffer_arr = np.asarray(buffer,dtype=np.float64)
    18.     buffer_arr = (buffer_arr*5)/255
    19.  
    20.     x = np.array(range(0,len(buffer_arr)))
    21.     ax1.clear()
    22.     ax1.plot(x,buffer_arr)
    23.     plt.xlabel('CCD Column')
    24.     plt.ylabel('Voltage, V')
    25.     plt.ylim((1,3.2))
    26.    
    27. fig = plt.figure()
    28. ax1 = fig.add_subplot(1,1,1)
    29.  
    30.  
    31. ser = serial.Serial(
    32.     port='COM9',\
    33.     baudrate=2000000,\
    34.     parity=serial.PARITY_NONE,\
    35.     stopbits=serial.STOPBITS_ONE,\
    36.     bytesize=serial.EIGHTBITS,\
    37.         timeout=None)
    38.  
    39.  
    40.  
    41. print("connected to: " + ser.portstr)
    42. count=1
    43.  
    44.  
    45.  
    46. time.sleep(1)
    47.  
    48.  
    49. ani = animation.FuncAnimation(fig, animate, interval=500)
    50. plt.show()

    Edit: I forgot to mention, the Python code is from a friend. I'm not well versed in the code, I have to admit. Since I'm more familiar with VB, I'd like to make my own version in VB.NET.
    Last edited by Vizier87; Jul 17th, 2020 at 03:45 AM.

  8. #8
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Serial data outputs nonsense

    Quote Originally Posted by Vizier87 View Post
    Hi Peter, many thanks for your kind reply.

    Here's the code:
    python Code:
    1. import serial
    2. import time
    3. import csv
    4. import matplotlib
    5. matplotlib.use("tkAgg")
    6. import matplotlib.pyplot as plt
    7. import matplotlib.animation as animation
    8. import numpy as np
    9.  
    10. def animate(i):
    11.     buffer = []
    12.     i = 0
    13.    
    14.     bufferx = ser.read(3648)
    15.     buffer = [x for x in bufferx]  
    16.  
    17.     buffer_arr = np.asarray(buffer,dtype=np.float64)
    18.     buffer_arr = (buffer_arr*5)/255
    19.  
    20.     x = np.array(range(0,len(buffer_arr)))
    21.     ax1.clear()
    22.     ax1.plot(x,buffer_arr)
    23.     plt.xlabel('CCD Column')
    24.     plt.ylabel('Voltage, V')
    25.     plt.ylim((1,3.2))
    26.    
    27. fig = plt.figure()
    28. ax1 = fig.add_subplot(1,1,1)
    29.  
    30.  
    31. ser = serial.Serial(
    32.     port='COM9',\
    33.     baudrate=2000000,\
    34.     parity=serial.PARITY_NONE,\
    35.     stopbits=serial.STOPBITS_ONE,\
    36.     bytesize=serial.EIGHTBITS,\
    37.         timeout=None)
    38.  
    39.  
    40.  
    41. print("connected to: " + ser.portstr)
    42. count=1
    43.  
    44.  
    45.  
    46. time.sleep(1)
    47.  
    48.  
    49. ani = animation.FuncAnimation(fig, animate, interval=500)
    50. plt.show()

    Edit: I forgot to mention, the Python code is from a friend. I'm not well versed in the code, I have to admit. Since I'm more familiar with VB, I'd like to make my own version in VB.NET.
    Hello Vizier87, you're welcome. To be honest I am not that good with Python, but, I have a few questions (perhaps you should show these to your friend who gave you that code):

    1. Is that code reading an array of 64 bit values?
    2. Does that Python code procedure a graph?
    3. Do you have a screenshot of that Python code's output?

    yours,
    Peter Swinkels

    ps:
    I also checked the manual you provided, but beyond that you're dealing with some kind of optical sensor I can't really understand it. You might want to do a Google search for "tcd1304ap" .
    Last edited by Peter Swinkels; Jul 17th, 2020 at 04:01 AM.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: Serial data outputs nonsense

    Quote Originally Posted by Peter Swinkels View Post
    Hello Vizier87, you're welcome. To be honest I am not that good with Python, but, I have a few questions (perhaps you should show these to your friend who gave you that code):

    1. Is that code reading an array of 64 bit values?
    2. Does that Python code procedure a graph?
    3. Do you have a screenshot of that Python code's output?

    yours,
    Peter Swinkels

    ps:
    I also checked the manual you provided, but beyond that you're dealing with some kind of optical sensor I can't really understand it. You might want to do a Google search for "tcd1304ap" .
    I'll reply the first question later, but for 2: Yes, 3: Here you go Attachment 177957

    Edit: My friend just replied. Yes, it is an array of 64 bit. When it was appended, it was 1 byte.

    Thanks

    Vziier87
    Last edited by Vizier87; Jul 17th, 2020 at 04:14 AM.

  10. #10
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Serial data outputs nonsense

    Quote Originally Posted by Vizier87 View Post
    I'll reply the first question later, but for 2: Yes, 3: Here you go Attachment 177957

    Edit: My friend just replied. Yes, it is an array of 64 bit. When it was appended, it was 1 byte.

    Thanks

    Vziier87
    Vziier87, your attachment won't open and I am afraid I am out of ideas. All I can suggest is doing a Google search: https://www.google.com/search?q=capt...hrome&ie=UTF-8

    yours,
    Peter Swinkels

  11. #11
    New Member
    Join Date
    Aug 2017
    Posts
    7

    Re: Serial data outputs nonsense

    I just did some more Google-Fu on your behalf and read up on what you have already tried. I think there are a few things wrong here.

    What are you using to interface with the chip/sensor? Microcontroller? Arduino?
    The interface device is important because that will be setting your baud rate for communication.

    If you are using a microcontroller did you write the code? Or do you have a copy of the code we could see?
    This would further help identify the correct baud rate.

    Next you seem to be just pulling wild baud rates to try. That will almost never work unless you understand the serial standard. Baud rates are not usually just arbitrary numbers like 1000 or 2000000. There are standard rates that have been used since serial was created. A quick read through wikipedia about serial shows, "Bit rates commonly supported include 75, 110, 300, 1200, 2400, 4800, 9600, 19200, 38400, 57600 and 115200 bit/s.".

    With all that said, I did some Google-Fu and found that the Python library tops out at 115200 bits/s and that a project similar sounding to yours with the same chip used a baud rate of 115200 bits/s, so I would give that baud rate a shot.

    Let us know how it goes!

    -Catch

  12. #12
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Serial data outputs nonsense

    Been a while since I worked with anything using serial com and a pc but I used to do a lot of it and never even heard of anything going above 115200. I always assumed that was the upper limit for serial com and most device use a lower value than that. 9600 and 19200 were the most common values I saw with the various devices I worked with.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width