Bartłomiej Drozd

Forum Replies Created

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • in reply to: I2C communication PCF8574AP #4889
    Author Image
    Bartłomiej Drozd
    Participant

    ok, i just tested it with LejOS and now i know, how to use it 😉 i must write to register 0x16 byte array 😉 and here is my test code

    public static void Main (string[] args)
    		{
    			EventWaitHandle stopped = new ManualResetEvent (false);
    			ButtonEvents btn = new ButtonEvents ();
    			byte address = 0x70;
    			byte[] data1 = new byte[1] { 0x05 };
    			byte[] data0 = new byte[1] { 0x0A };
    			LcdConsole.WriteLine ("hello world!");
    			pcf pcf = new pcf (SensorPort.In1, address);
    			btn.EscapePressed += () => {
    				LcdConsole.WriteLine("stopped");
    				stopped.Set ();
    			};
    			btn.LeftPressed += () => {
    				pcf.WriteReg(0x16, data1);
    				LcdConsole.WriteLine("write data1");
    			};
    			btn.RightPressed += () => {
    				pcf.WriteReg(0x16, data0);
    				LcdConsole.WriteLine("write data0");
    			};
    			btn.EnterPressed += () => {
    				LcdConsole.WriteLine(BitConverter.ToString(pcf.ReadReg(16, 1)));
    			};
    			stopped.WaitOne ();
    		}

    thanks for help 😉

    in reply to: I2C communication PCF8574AP #4888
    Author Image
    Bartłomiej Drozd
    Participant

    i will try with LejOS, but i don’t like Java… And still waiting for help with C# 😉

    in reply to: I2C communication PCF8574AP #4885
    Author Image
    Bartłomiej Drozd
    Participant

    yes, PCF need from 2.5V to 6V, so i have correct mode. I really don’t know, what i should do… Maybe is there any option, where i will be able to construct my own byte array to send? Like in NXC?

    in reply to: I2C communication PCF8574AP #4883
    Author Image
    Bartłomiej Drozd
    Participant

    unfortunetly, that is not working… it display only “System.Byte[]”. When i use my version, it dislpay hex byte. When i change to pcf.ReadReg(16, 1) it is reading all 8 bits

    in reply to: I2C communication PCF8574AP #4880
    Author Image
    Bartłomiej Drozd
    Participant

    ok, i did that:
    LcdConsole.WriteLine(BitConverter.ToString(pcf.ReadReg(2, 1)));
    and i got “03” on lcd when i connect P0 to + and “02” when i connect P0 to -. So, register is just a number of port to read/write?

    in reply to: I2C communication PCF8574AP #4878
    Author Image
    Bartłomiej Drozd
    Participant

    in datasheet for PCF8574A address is
    0 1 1 1 A2 A1 A0 0
    and my A2, A1, A0 is 0, so b01110000 = 0x70. i should read bytes, but i need only write bits… i don’t understand too much…

    in reply to: I2C communication PCF8574AP #4875
    Author Image
    Bartłomiej Drozd
    Participant

    unfortunetly, it is not workking… i found source of sending function

                   /// <summary>
    		/// Write and read an array of bytes to the sensor
    		/// </summary>
    		/// <returns>The bytes that was read</returns>
    		/// <param name="register">Register to write to.</param>
    		/// <param name="data">Byte array to write</param>
    		/// <param name="rxLength">Length of the expected reply</param>
            protected byte[] WriteAndRead (byte register, byte[] data, int rxLength)
    		{
    			if (rxLength > BufferSize)
    				throw new ArgumentOutOfRangeException("I2C Receive Buffer only holds " + BufferSize + " bytes");
    			if (data.Length > BufferSize) {
    				throw new ArgumentOutOfRangeException("I2C Write Buffer only holds " + BufferSize + " bytes");
    			}
    			bool dataReady = false;
    			int replyIndex = 0;
    			byte[] writeData = new byte[BufferSize];//30
    			Array.Copy (data, 0, writeData, 0, data.Length);
    			ByteArrayCreator command = new ByteArrayCreator ();
    			command.Append ((int)-1);
    			command.Append ((byte)this.port);
    			command.Append ((byte)1);//repeat
    			command.Append ((short)0);//time
    			command.Append ((byte)(data.Length + 2));//length of write data
    			command.Append ((byte)((byte)I2CAddress >> 1));
    			command.Append (register);
    			command.Append(writeData);
    			command.Append ((byte)-rxLength);
    			replyIndex = command.Data.Length;
    			command.Append (new byte[BufferSize]);//make room for reply
    			byte[] i2cData = command.Data;
    			while (!dataReady) {
    				unchecked {
    					I2CDevice.IoCtl ((Int32)I2CIOSetup, i2cData);
    				}
    				int status = BitConverter.ToInt32 (i2cData, 0);
    				if (status < 0) {
    					throw new Exception ("I2C I/O error");
    				}
    				if (status == 0) {
    					byte[] reply = new byte[rxLength];
    					if (rxLength > 0) {
    						Array.Copy(i2cData,replyIndex, reply,0, rxLength);
    					}
    					return reply;
    				}
    			}
            	throw new TimeoutException("I2C timeout");
        	}

    maybe this should help…

    in reply to: I2C communication PCF8574AP #4873
    Author Image
    Bartłomiej Drozd
    Participant

    sorry, i don’t know how to read a register… in NXC i did it that:

    void writeReg(byte reg)
    {
     byte buff[] = {0x70, 0x00};
     buff[1] = reg;
     int nbytes;
     I2CWrite(I2C, 0, buff);
     while(I2CStatus(I2C, nbytes) == STAT_COMM_PENDING);
     Wait(100);
    }

    because i need only outputs and that is working fine, but i need it in C# on EV3. Maybe i’m doing something wrong? Here is my code…
    Main.cs

    using System;
    using System.Threading;
    using MonoBrickFirmware;
    using MonoBrickFirmware.Display.Dialogs;
    using MonoBrickFirmware.Display;
    using MonoBrickFirmware.Sensors;
    using MonoBrickFirmware.UserInput;
    
    namespace test
    {
    	class MainClass
    	{
    		public static void Main (string[] args)
    		{
    			EventWaitHandle stopped = new ManualResetEvent (false);
    			ButtonEvents btn = new ButtonEvents ();
    			byte address = 0x70;
    			byte[] data1 = new byte[8] {1, 1, 1, 1, 1, 1, 1, 1};
    			byte[] data0 = new byte[8] {0, 0, 0, 0, 0, 0, 0, 0};
    			LcdConsole.WriteLine ("hello world!");
    			pcf pcf = new pcf (SensorPort.In1, address);
    			btn.EscapePressed += () => {
    				LcdConsole.WriteLine("stopped");
    				stopped.Set ();
    			};
    			btn.LeftPressed += () => {
    				pcf.WriteReg(0, data1);
    				LcdConsole.WriteLine("write data1");
    			};
    			btn.RightPressed += () => {
    				pcf.WriteReg(0, data0);
    				LcdConsole.WriteLine("write data0");
    			};
    			stopped.WaitOne ();
    		}
    	}
    }

    pcf.cs

    using System;
    using MonoBrickFirmware;
    using MonoBrickFirmware.Sensors;
    
    namespace test
    {
    	public class pcf : I2CSensor
    	{
    		public pcf (SensorPort Port, byte adress) : base (Port, adress, I2CMode.LowSpeed)
    		{
    			base.Initialise();
    		}
    
    		public byte[] ReadReg(byte Register, byte Length)
    		{
    			return base.ReadRegister(Register, Length);
    		}
    
    		public void WriteReg(byte Register, byte[] Data)
    		{
    			base.WriteRegister(Register, Data);
    		}
    
    		public byte[] ReadAndWriteReg(byte Register, byte[] Data, int rxLength)
    		{
    			return base.WriteAndRead(Register, Data, rxLength);
    		}
    
    		public override string ReadAsString()
    		{
    			throw new NotImplementedException();
    		}
    
    		public override void SelectNextMode()
    		{
    			throw new NotImplementedException ();
    		}
    
    		public override string GetSensorName()
    		{
    			return "PCF8574AP";
    		}
    
    		public override void SelectPreviousMode()
    		{
    			throw new NotImplementedException ();
    		}
    
    		public override int NumberOfModes()
    		{
    			return 0;
    		}
    
    		public override string SelectedMode()
    		{
    			return "0";
    		}
    	}
    }
    in reply to: I2C communication PCF8574AP #4869
    Author Image
    Bartłomiej Drozd
    Participant

    why i don’t need the resistors? In datasheet PCF8574AP doesn’t have any registers

    in reply to: pc sender nxt with nxc receiver #3958
    Author Image
    Bartłomiej Drozd
    Participant

    ok, i solved this problem. I noticed, i must to send 5 times the same string to nxt, then nxt receive it correctly.
    In c# it’s look that:

    bool f = true;
    Console.WriteLine("Y: ");
    data = Int32.Parse(Console.ReadLine());
    command = data.ToString();
    if (f == true) //because first time send correctly
    {
      brick.Mailbox.Send(command, Box.Box1, false);
      f = false;
    }
    brick.Mailbox.Send(command, Box.Box1, false);
    brick.Mailbox.Send(command, Box.Box1, false);
    brick.Mailbox.Send(command, Box.Box1, false);
    brick.Mailbox.Send(command, Box.Box1, false);
    brick.Mailbox.Send(command, Box.Box1, false);
    brick.Mailbox.Send(command, Box.Box1, false);
    Console.WriteLine("Y pos: " + command);
    in reply to: pc sender nxt with nxc receiver #3957
    Author Image
    Bartłomiej Drozd
    Participant

    Or maybe You could help mi with c++ library? I need to make one-way communication pc -> nxt with camera vision and i have finished program to camera vision in c++, without communication between pc and nxt. I need to send some string messages to difefrent mailbox. I don’t know how to use Your c++ library to send bt or usb messages to default mailbox

    in reply to: pc sender nxt with nxc receiver #3956
    Author Image
    Bartłomiej Drozd
    Participant

    but when i do second sender program in second brick work fine at the same mailbox.

    ReceiveRemoteString(queue, remove, out strval)
    This method is used on a master brick to receive a string value from a slave device
    communicating via a specific mailbox or message queue. Optionally remove the last
    read message from the message queue depending on the value of the boolean remove
    parameter.

    in reply to: pc sender nxt with nxc receiver #3954
    Author Image
    Bartłomiej Drozd
    Participant

    Unfortunately, that not solved my problem. I send from pc string to mailbox 0 and in brick i receive string from mailbox 0. First time that work great, but when i try to send next string to the same mailbox that didn’t work. When i close program in brick and run it again, then i can again send one string and it’s a loop… when i do program in nxt-g i send from pc string to mailbox 0 and receive in brick from mailbox 1 and that work fine. I don’t know what is wrong

Viewing 13 posts - 1 through 13 (of 13 total)
Posted in

Make a donation