• How to Read and Write Serial Port Data in Raspberry Pi

    From Jackson Zhang@3:770/3 to All on Monday, November 13, 2023 02:03:40
    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

    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From 56d.1152@3:770/3 to Jackson Zhang on Monday, November 13, 2023 23:26:38
    On 11/13/23 5:03 AM, Jackson Zhang wrote:
    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 :

    https://roboticsbackend.com/raspberry-pi-arduino-serial-communication/

    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.

    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From Jackson Zhang@3:770/3 to All on Monday, November 13, 2023 23:14:05
    On Tuesday, November 14, 2023 at 12:26:53 PM UTC+8, 56d.1152 wrote:
    On 11/13/23 5:03 AM, Jackson Zhang wrote:
    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 :

    https://roboticsbackend.com/raspberry-pi-arduino-serial-communication/

    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.

    Yes you are right,I use RS-485 to connact raspberry pi and PC.And I already connect them with RXD、TXD、GND,I can detect the data of RS-485 use serial assist,but I dont know how to judge whether the raspberry pi receive the data too?

    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From Jackson Zhang@3:770/3 to All on Monday, November 13, 2023 23:16:25
    On Tuesday, November 14, 2023 at 12:26:53 PM UTC+8, 56d.1152 wrote:
    On 11/13/23 5:03 AM, Jackson Zhang wrote:
    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 :

    https://roboticsbackend.com/raspberry-pi-arduino-serial-communication/

    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.
    I just want the raspberry pi run some code independently,and can send and receive data to my PC with serial ports.

    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From 56d.1152@3:770/3 to Jackson Zhang on Tuesday, November 14, 2023 23:01:12
    On 11/14/23 2:16 AM, Jackson Zhang wrote:
    On Tuesday, November 14, 2023 at 12:26:53 PM UTC+8, 56d.1152 wrote:
    On 11/13/23 5:03 AM, Jackson Zhang wrote:
    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 :

    https://roboticsbackend.com/raspberry-pi-arduino-serial-communication/

    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.
    I just want the raspberry pi run some code independently,and can send and receive data to my PC with serial ports.


    A very simple pgm example here :

    https://stackoverflow.com/questions/17539185/serial-communication-with-raspberry-pi

    More complicated examples here :

    https://pimylifeup.com/raspberry-pi-serial/

    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.

    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From Paul@3:770/3 to All on Saturday, November 25, 2023 10:44:22
    On 11/14/2023 11:01 PM, 56d.1152 wrote:
    On 11/14/23 2:16 AM, Jackson Zhang wrote:
    On Tuesday, November 14, 2023 at 12:26:53 PM UTC+8, 56d.1152 wrote:
    On 11/13/23 5:03 AM, Jackson Zhang wrote:
    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 :

    https://roboticsbackend.com/raspberry-pi-arduino-serial-communication/

    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.
       I just want the raspberry pi run some code independently,and can send and receive data to my PC with serial ports.


    A very simple pgm example here :

    https://stackoverflow.com/questions/17539185/serial-communication-with-raspberry-pi

    More complicated examples here :

    https://pimylifeup.com/raspberry-pi-serial/

    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.

    Depending on what kind of serial link you are debugging, this is sage advice.
    A comment down near the end of the article says...

    https://www.abelectronics.co.uk/kb/article/1035/serial-port-setup-in-raspberry-pi-os

    "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

    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)