-1

I'm doing a school project where I need to pass notes (char, ascii) using a C# code in VS to an Altera card using a RS232 cable. when I'm sending a single note('A') the data goes through just fine but when I'm trying to send two notes at the same time ("AB") the data isn't receive well enough and I'm getting multiple problems like the wrong Leds turning on and off.

My VHDL code

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity UART_RX is
-- clockls per bit = 50Mhz/9600baudrate
generic(
        gClocks_per_Bit : integer := 5208
        
       );
        
port(
      iClk       : in std_logic;
      iReset     : in std_logic;
      iRX        : in std_logic:='0';
        led1,led2  : out std_logic:='0';
      oTX        : out std_logic_vector (7 downto 0)
      
    );
end UART_RX;

architecture Behavioral of UART_RX  is 

type uart_rx_type is (idle,start,data,stop);
signal sNext_state : uart_rx_type  := idle;

signal sClock_counter : integer range 0 to gClocks_per_Bit := 0;
signal sIndex : integer range 0 to 15 := 0;
signal sTemp : std_logic_vector (15 downto 0);


signal receiver1, receiver2 : std_logic_vector (7 downto 0);
signal oTX_1 : std_logic_vector (7 downto 0);
begin
    process(iClk,iReset)
        begin 
            if(iReset = '0') then
                sIndex <= 0;
                sClock_counter <= 0;        
            elsif(rising_edge(iClk)) then  
                case sNext_state is
                    when idle =>                    
                        sClock_counter <= 0;
                        sIndex <= 0;
                        if(iRX = '0') then
                            sNext_state <= start;
                        elsif(iRX = '1') then
                            sNext_state <= idle;
                        end if;
                    
                    when start => 
                        if(sClock_counter < gClocks_per_Bit) then
                            sClock_counter <= sClock_counter  + 1;
                                if(sClock_counter = gClocks_per_Bit/2 AND iRX ='0') then
                                    sNext_state <= start;
                                elsif(sClock_counter = gClocks_per_Bit/2 AND iRX ='1') then
                                    sClock_counter <= 0;
                                    sNext_state <= idle;
                                end if;
                            sNext_state <= start;
                        else
                            sClock_counter <= 0;
                            sNext_state <= data;
                        end if;
                    
                    when data => 
                        if(sClock_counter < gClocks_per_Bit) then
                            sClock_counter <= sClock_counter  + 1;
                            sNext_state <= data;
                            if(sClock_counter = gClocks_per_Bit/2) then
                                sTemp(sindex)<= iRX;
                            end if;
                        else
                            sClock_counter <= 0;
                                if(sIndex < 15) then
                                    sIndex <= sIndex + 1;
                                    sNext_state <= data;
                                else
                                    sIndex <= 0;
                                    sNext_state <= stop;
                                end if;
                        end if;
                    
                    when stop =>
                        if(sClock_counter = gClocks_per_Bit / 2) then
                            sClock_counter <= 0;
                            sNext_state <= idle;
                        elsif(sClock_counter  < gClocks_per_Bit) then
                            sClock_counter <= sClock_counter + 1;
                        end if;
                          when others => null;
                end case;
                end if;
            end process;
            receiver1<=sTemp(7 downto 0);
            receiver2<=sTemp(15 downto 8);
--          oTX <= sTemp;
process(receiver1,receiver2,iReset)
begin
    if iReset = '0' then
        led2<='0';
        led1<='0';
            
    elsif receiver1 = "01000001" then -- A (led 1 on)
        led2 <= '0';
        led1 <= '1';
        
        
    elsif receiver1 = "01000010" then -- B
        led2 <= '1';
        led1 <= '0';
    elsif receiver1 = "01000011" then -- C
        led2 <= '0';
        led1 <= '0';
    elsif sTemp = "0100000101000010" then -- AB
        led2 <= '1';
        led1 <= '1';
    
    else -- What Ever You Write Turns Off/On Leds.
    null;
    
    end if;
end process;
oTX <= sTemp(7 downto 0);
end Behavioral ;

My C# code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;

namespace COMPORT
{
    public partial class Form1 : Form
    {
        string dataOUT;
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.PortName = cBoxCOMPORT.Text;
                serialPort1.BaudRate = Convert.ToInt32(CBoxBaudRate.Text);
                serialPort1.DataBits = Convert.ToInt32(cBoxDataBits.Text);
                serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cBoxStopBits.Text);
                serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), cBoxParityBits.Text);

                serialPort1.Open();
                progressBar1.Value = 100;
            }
            
            catch (Exception err)
            {
                MessageBox.Show(err.Message,"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if(serialPort1.IsOpen)
            {
                serialPort1.Close();
                progressBar1.Value = 0;
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            cBoxCOMPORT.Items.AddRange(ports);

        }

        private void btnSendData_Click(object sender, EventArgs e)
        {
            if(serialPort1.IsOpen)
            {
                dataOUT = tBoxDataOut.Text;
                //dataOUT = ConvertToHex(dataOUT);
                //dataOUT = hex2binary(dataOUT);
                serialPort1.Write(dataOUT);
                Thread.Sleep((int)TimeSpan.FromSeconds(2).TotalMilliseconds);
                MessageBox.Show(dataOUT);

            }

        }

        private void tBoxDataOut_TextChanged(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Text = DateTime.Now.ToString();
        }

        //public static string ConvertToHex(string asciiString)
        //{
        //    string bin = "";
        //    foreach (char c in asciiString)
        //    {
        //        int tmp = c;
        //        bin += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
        //    }
        //    return bin;
        //}

        //private string hex2binary(string hexvalue)
        //{
        //    string binaryval = "";
        //    binaryval = Convert.ToString(Convert.ToInt32(hexvalue, 16), 2);
        //    return binaryval;
        //}
    }
}

thanks in advance

Tried looking online but what I look for is really specific.

Omer DT8
  • 1
  • 1

1 Answers1

0

Your receiver process does not assign a value to led1, led2 in all cases. That means in hardware you will get latches for these 2 signals (look into the synthesize messages). The enable of each latch is controlled by a big combinatorial logic, which looks at reveiver1 and sTemp (and iReset). While these multi-bit signals change their values you will have glitches at the latch enable signal at any time which will store unexpected values in the latches. So assign a value to led1/2 in all cases.