Re: How to Read and Write Serial Port Data in Raspberry Pi

Nov 14, 2023 Last reply: 2 years ago 2 Replies


Hi,


> I want Raspberry Pi to run a piece of code independently, read and write data from the serial port separately, and the other recipient is my PC. Can I directly deploy this code into it?I followed the Mathworks tutorial, but I don't know where the problem occurred. My current steps are as follows
> Firstly, I created a connection with Raspberry Pi in MATLAB and sent a matrix > Theme
> Copy
> r = raspi;
> pc = serialdev(r,'/dev/serial0');
> dataToSend = [22;4;5;6;7];
> byteStream = typecast(dataToSend, 'double');
> Then,I will deploy the following code to Raspberry Pi to read data from the serial port and send some data to the serial port
> Theme
> Copy
> function tablelook()%#codegen
> r = raspi;
> rsp = serialdev(r,'/dev/serial0');
> output = read(rsp,5,'double');
> ...%my system code
> write(rsp,1,'double')
> (I obtain my final 0/1 through the five input parameters and output it to the serial port)
> Finally,I read the data in MALTAB
> Theme
> Copy
> read(pc,1,'double')
> But I get wrong data during transmission,all of them is zeros.
>
> Can you give me some advice?
>
> Many thanks,
> Bang
>

"Serial" can mean several things these days - from RS-232 and friends to SPI and I2C


Sounds like you want RS-232



I'd suggest a practical tutorial like :



formatting link
DO remember that PIs operate at 3.3 volts and PC serial comms are usually 5 volts - and the REAL standard is like up to 15 volts, including NEGATIVE volts. Ergo level-shifting hardware is required. It's cheap and not hard to use, try AdaFruit or Seeed or SparkFun, but you HAVE to use it.



RS-232 can be done with as few as three wires - transmit, receive and ground. Usually, these days, you don't need the hardware "flow control" wires. 232 CAN do a LOT of things, but 99.9% of people rarely go beyond the simple.



Somewhere I saw an OPTICAL FIBER serial link which gets past those voltage issues. Alas a quick look reveals nobody selling such things of late. It would be good for medium-length runs though ... no noise issues. If you are fair at electronics though you could MAKE one.



A very simple pgm example here :

formatting link
More complicated examples here :

formatting link
Bi-directional async comms are more involved, you'd start by putting the send and receive examples above into separate threads and 'pipe' info to the mother program. You'd also need a pair of serial lines ... it'd be like a "chat" server where either party can transmit even as the other is also doing so. The good old Send/Receive-Then-Wait model is easier.

Depend

formatting link
"You can check if the serial port works by following our Serial Port Loopback Test <=== tutorial steps."

Tying TX to RX and "debugging your software path", is a time honored tradition. Home experimenters have to be a lot more inventive, when they have no test equipment. Before you even hook up any level shifters, and start connecting to a second device, you want to prove the local device is working properly.

For example, if you typed "U" and a "U" did not come back, then there would be no point hooking up any interconnect cables to a second device, until that issue was corrected. Once your device can talk to itself, then you can work on the harder problem of making it talk to a peer.

Paul

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required