Send TTL signal through serial port in Matlab - matlab

I am trying to send a TTL signal through a serial port using Matlab. I just need to send 1 value to the device so it should be a really simple procedure. My problem is that I don't know if I am not doing this correctly or if the device is not processing the signal. My code is this:
mysignal = serial('com1');
fopen(mysignal);
fwrite(mysignal,1);

I don't think you can output TTL using the actual serial port, as that is RS-232, not TTL logic levels. If you're using the serial port, you'll need a converter, like this. If you're using an FTDI driver/USB cable, that already outputs TTL logic levels, and the code looks good to me, which means the bug is probably on the device end.

Related

how to make event marker for Emotiv EPOC (portable EEG)?

I am thinking to measure ERP from Emotiv EPOC (EEG), but in analysis, data should be divided based on timing of onset and offset of stimulus. So, during recording, timing of event, such as onset or offset, needs to be marked. Emotiv PRO offers function to record event marker via serial port. However, I have little knowledge about serial port, and I couldn't marked event.
Could you tell me basic usage for Emotiv PRO about event marker via serial port ?
Below is what I tried.
First, I tried from MATLAB (2018a),
s=serial("/dev/cu.Bluetooth-Incoming-Port");
fopen(s);
then, from Emotiv PRO, I started "set up serial point markers" but response was
Resource Busy.
Second, I tried in different order and from Emotiv PRO, started "set up serial point markers", and then from MATLAB, ran same code, but response from MATLAB was
Cannot connect to the /dev/cu.Bluetooth-Incoming-Port port. Possible reasons are
another
application is connected to the port or the port does not exist.
Third, I tried new MATLAB (2020a),
s=serialport("/dev/cu.Bluetooth-Incoming-Port",9600)
and I started "set up serial point markers" from Emotiv PRO (error didn't appeared),
and, again from MATLAB (2020a)
write(s,1,"uint8")
but, marker didn't appeared in Emotiv PRO.
I confirmed that Emotiv PRO appropriately record marker from key pressing, but I couldn't from serial port.
Oy, this isn't easy. Assuming you have no actual serial port, you need some sort of virtual port. One option is to use a software to create it. then your matlab will send a message to its port and Emptiv will receive the message with another port, both virtual. You will need to make sure the format is set the same way for both sender and receiver, such as BAUD = 115200 bps, Data size = 8 bits etc. Alternatively you can use a null modem cable, to connect a USB wire and convince both sender and receiver that these are two sides of a serial cable. I used it successfully using two computers, one to send the signal and one to receive.

Simulating multiple modbus slave devices using node red

I've managed to simulate a single slave device on my raspberry pi using node-red using functions to send data random values to the Modbus flex server. However, now I want to be able to simulate multiple Modbus slave devices on the port number and I'm unsure how to do this.
I've tried creating another Modbus flex server with the same port number, but this causes the whole node-red application to crash when it's deployed. Secondly, I've tried using different Modbus flex-write nodes to simulate different slave devices, but I'm unsure whether this is correct and if so, how I'd configure them to appear as different slave devices. This is because so far, my raspberry pi appears as slave 1, but I'm unsure where this comes from. I'm guessing it's to do with the unit-id of the Modbus flex-server but when I change the unit-id to a different number and type that number as the address in the master, it says no connection.
In conclusion, is it possible to use a single raspberry pi to simulate multiple slave devices on node-red using node-red-contrib-modbus and if so how do you do it?
The concept of Slaves in Modbus TCP differ somewhat from RTP as set out in the Modbus TCP Spec:
The MODBUS ‘slave address’ field usually used on MODBUS Serial Line is
replaced by a single byte ‘Unit Identifier’ within the MBAP Header.
The ‘Unit Identifier’ is used to communicate via devices such as
bridges, routers and gateways that use a single IP address to support
multiple independent MODBUS end units.
So there is a difference in termanalogy between Modbus RTP and TCP as well as a difference in the intended use of this field. The solution suggested by the spec would be to setup multiple servers on different ports (you cannot run multiple servers on a single port).
Having said that some TCP->RTP gateways (and other devices) use the unitid as the slave ID so I'm assuming you are trying to simulate something like this?
The first issue is that there appears to be a bug in Modbus Flex Server (reported) in that when you change the unit-id it is being stored as a string rather than a number. If you export the flow you will see something like "unitId": "3",; changing this to "unitId": 3, (no quotes around the 3) and importing fixes the issue (so that probably explains why you could not get this working).
Having said that changing the unit-id like this does not help you because it only supports one ID. However if you set the unit-id to 255 then it will listen on all unit-ids (this is a feature of the modbus-serial module used internally). Remember that you will currently need to manually fix the config to get this to work due to the bug.
Having done that you can do something like the following to respond to requests to different unit ids (the example will return the unit id (1 or 2) for all addresses so is not useful but shows the concept):

Usage of serial receive block of simulink to read from COM port of PC giving erroneous value any solution?

I was trying to use serial receive block of simulink to read data from COM port. The data is received from an mbed board to COM port and from COM port I want to read it using simulink. When I read it, Im getting some values not related to what I send. Some values like 1.38*e^-38 is showing as output of serial receive block when i try to read '10'. Is this any conversion issue?
I have given baud rates correctly in both ends.

Handling concurrent UDP DatagramReceivedFcn executions in Matlab

I'm attempting to read ocean depth values at multiple frequencies which are being broadcast via UDP packets. What I'm doing is telling the logging program to return the depth values to a specific UDP port, then use the DatagramReceivedFcn to run a function when data is received and essentially save that depth.
u1 = udp(remoteip,dataport18,'ByteOrder','littleEndian','LocalPort',dataport18,'DatagramTerminateMode','off');
set(u1,'InputBufferSize',6000);
u1.DatagramReceivedFcn = {#receivedata18};
fopen(u1);
Thus, when data is received on the port specified in 'dataport18', it will run the function receivedata18(). However, I'm trying to read depth data for multiple frequencies, so I create additional UDP objects:
u2 = udp(remoteip,dataport38,'ByteOrder','littleEndian','LocalPort',dataport38,'DatagramTerminateMode','off');
set(u2,'InputBufferSize',6000);
u2.DatagramReceivedFcn = {#receivedata38};
fopen(u2);
What I'm finding though is that only data for u1 (18 kHz) is being saved. My guess is that since both frequencies ping at the same time, they both send their UDP packets and try to evaluate their respective functions at the same time, which Matlab is not capable of doing.
Is this indeed what is going on? If so, is there any way around this issue so that I can concurrently read depth data that is being sent at the same time from two separate UDP packets?
Thanks!
Update
I'm wondering if I would need the Parallel Computing Toolbox in order to perform this. I have a similar program in Python that is performed in essentially the same way, however it has no issues. I'm assuming that it must be that Matlab can't run simultaneous functions without the Parallel Computing Toolbox
Thought I should update this in case anyone cares. It's not really an answer to my question, but what I'm currently doing that's working.
Instead of having the data sent to different UDP port, I simply have them sent to the same port and then read them sequentially. Thus I'm not reading them synchronously, although that doesn't really slow down the operation much at all.

What is the difference between UART port and serial port in a computer?

What is the difference, if any, between a UART port and a serial port in a computer?
UART is the abbreviation of Universal Asynchronous Receiver Transmitter, the name of the chip that enables the computer to communicate via a serial line (eg. RS-232, RS-485, RS-422).
The serial port is the RS-232 interface (internally connected to the UART) of the computer.
As #SurDin explained some more points I want to add
1.UART takes data parallel and the help of shift register transfer it bit by bit or takes data bit by bit and reform those into parallel form via communication channel like RS-232,RS-485,RS-422.
2.UART has a baud rate generator, transmeter and a receiver.
3.Its baud rate generator gets clocked either by internal clock or by external source.
communication media linked to serial port which internally connected to UART controller.