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.
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.
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)
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
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.
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.
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:
import serial
import time
import csv
import matplotlib
matplotlib.use("tkAgg")
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
def animate(i):
buffer = []
i = 0
bufferx = ser.read(3648)
buffer = [x for x in bufferx]
buffer_arr = np.asarray(buffer,dtype=np.float64)
buffer_arr = (buffer_arr*5)/255
x = np.array(range(0,len(buffer_arr)))
ax1.clear()
ax1.plot(x,buffer_arr)
plt.xlabel('CCD Column')
plt.ylabel('Voltage, V')
plt.ylim((1,3.2))
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
ser = serial.Serial(
port='COM9',\
baudrate=2000000,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=None)
print("connected to: " + ser.portstr)
count=1
time.sleep(1)
ani = animation.FuncAnimation(fig, animate, interval=500)
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.
ani = animation.FuncAnimation(fig, animate, interval=500)
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.
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.
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.
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.