Serial port Communication - Send Numeric Values

10 visualizaciones (últimos 30 días)
Mikael Jonathan
Mikael Jonathan el 19 de Mzo. de 2024
Comentada: Mikael Jonathan el 29 de Mzo. de 2024
Hi,
I'm trying to control the electric stimulator Digitimer DS5 with Matlab through the serial port. I got a cable that goes from serial port to BNC. Serial port on my computer, BNC on the electrical stimulator. For now I just try to look on an oscilloscope (TekTronix TDS 320) that the signal I'm sending is correct.
I'm writing a simple array or a sine in double, and write in the serial port, and observing in the oscilloscope. I keep have an intermitting signal, instead of something continuous. I followed two ways (Version 1 and 2 below), one just by reading the explication in the help for the serialport function, and the second one following another Matlab tutorial, but I didn't understood what all the strings that I had to write meant.
Basically my goal is to define an array of double (sine wave, square wave) and to send it throught the serial port.
I looking for help, thank you in advance !
Exemple of what happen when I use write(device,WaveForm, "double") from the version 1
% Version 1 based on the elements from the function https://uk.mathworks.com/help/matlab/ref/serialport.html#mw_8342f8eb-6599-4493-9013-3e7bd707194c
WaveForm = ones(1,2000)*10;
freeports = serialportlist("available");
device = serialport(freeports(1),9600);
device.Parity = 'none';
device.StopBits = 1;
device.DataBits = 8;
device.FlowControl ="none";
device.ByteOrder = 'little-endian';
write(device,WaveForm, "double")
flush(device);
clear device
% Version 2 based on the tutorial:
% https://uk.mathworks.com/help/instrument/write-and-read-serial-port-data.html
s = serialport("COM1",9600);
write(s,"Data:Destination RefB","string");
write(s,"Data:Encdg SRPbinary","string");
write(s,"Data:Width 2","string");
write(s,"Data:Start 1","string");
t = (0:2999) .* 8 * pi / 500;
data = round(sin(t) * 90 + 127);
write(s,"CURVE #3500","string");
write(s,data,"int16")
clear s

Respuestas (1)

Ayush
Ayush el 28 de Mzo. de 2024
Editada: Ayush el 28 de Mzo. de 2024
Hi Mikael,
From what I could gather from your problem is that you want to define an array of type “double” and send it through the serial port to the connected electric stimulator Digimeter DS5. I went over the two approaches you have taken and found some discrepancies in the underlying concept.
  • Firstly, sending data over a serial port depends on a lot about the format accepted by the receiving device i.e. Digitimer DS5 and its interpretation to generate the desired waveforms.
  • Secondly, to get a continuous representation of data is dependent on the way the serial communication is formatted as it is inherently packet based communication which leads to you observing intermittent signals.
  • Lastly, the device i.e. Digitimer DS5 that is to be controlled and communicated via serial ports usually have some specific protocols and commands to establish the communication format and set configurations.
Additionally, I found that the device i.e. Digitimer DS5 that you are trying to communicate through a serial port is not supported by MATLAB as of now. You can check the same from the following link:
Furthermore, there are some controls and settings that you can still configure from the instrument company’s own GUI based software called the “DS5 Control Software”. You can learn more about it from the FAQ answered in the product page link:
You can also refer to the below example to know more about how to read a waveform from the scope on your oscilloscope (TekTronix TDS 320):
Hope this helps!
  1 comentario
Mikael Jonathan
Mikael Jonathan el 29 de Mzo. de 2024
Hi Ayush,
Thank you for your answer, I'll explore your suggestions.
With further testing, I understood few more things that I will write here in case that's usefull for someone else.
The serial data format includes one start bit, between five and eight data bits, and one stop bit. A parity bit (not mandatory in my case I set it to 'none') and an additional stop bit might be included in the format as well.
So I always wrote 1 start bit, 8 data bits and 1 stop bit.
The start bit is transmitted with a value of 0.
The stop bit is transmitted with a value of 1.
I tested sending a single value 0 and 255.
So 0 is 00000000. With the start and stop bits it become: 0000000001.
And 255 is 11111111. With the start and stop bits it become: 0111111111.
The DS5 read each bits, so in this case, it read 10 bits. Read the 0 results of triggering while reading the 1 results in no triggering. So in the case of writing a 0 I have 9 triggers and in the case of the 255 only one trigger.
So if I want to send a train of trigger at a specific frequency, I cannot write a string containing all my elements to code for example 1 triger every seconds, since even for 255, there a bit equal to 0 resulting of a trigger. The only solution I found for now is to rely on the write function everytime I want to trigger, so if I want 1 trigger everyseconds, I write 255 every seconds.
Can a converter could work, like something from USB to serial ? and instead of using the serial port, i write something in the USB port ?
Thank you in advance !

Iniciar sesión para comentar.

Productos


Versión

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by