Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,689 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,294 people online right now. Registration is fast and FREE... Join Now!




How to communicate with the SFP devices (I2C EEPROM) using serial port

 
Reply to this topicStart new topic

How to communicate with the SFP devices (I2C EEPROM) using serial port

dennisxu
post 2 Jun, 2008 - 09:51 AM
Post #1


New D.I.C Head

*
Joined: 21 May, 2008
Posts: 3

I wrote the following C# codes to read from my SFP device:

csharp

Console.Write("\n Enter the address to be read: ");
var ReadAddrText = Console.ReadLine();

byte[] SerBuf = new byte[30];
byte[] SerBufa = new byte[30];
byte Addr;

Addr = Convert.ToByte(ReadAddrText, 16);
SerBuf[0] = 0x56; // Command for Devices with 1 address byte
SerBuf[1] = 0xA1; // 24C32 I2C address + low bit set to indicate its a read operation
SerBuf[2] = 0xA2; // 24C32 high byte of internal address to read from
SerBuf[3] = 0xF0; // 24C32 low byte of internal address to read from
SerBuf[4] = 8; // number of bytes to read
comport.Write(SerBuf, 0, 5); // send the read command to the serial port
while (comport.BytesToRead < 16) ; // wait for data to arrive from the EEPROM
int bytes = comport.BytesToRead;
Console.WriteLine(" Bytes to be read: {0}", bytes);
comport.Read(SerBufa, 0, Addr); // get the 8 data bytes
Console.WriteLine("\n Origial received(fir): {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15}", SerBufa[0], SerBufa[1], SerBufa[2], SerBufa[3], SerBufa[4], SerBufa[5], SerBufa[6], SerBufa[7], SerBufa[8], SerBufa[9], SerBufa[10], SerBufa[11], SerBufa[12], SerBufa[13], SerBufa[14], SerBufa[15]);


The problem is that I got the same results whatever I changed the internal reading addresses.

running results: 57 57 13 10 57 57 13 10 57 57 13 10 57 57 13 10

Does anyone know what's wrong with it?

Thanks,
Dennis

EDIT: Code blocks added => code.gif

This post has been edited by PsychoCoder: 2 Jun, 2008 - 11:32 AM
User is offlineProfile CardPM

Go to the top of the page

zakary
post 2 Jun, 2008 - 10:39 AM
Post #2


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 401



Thanked 6 times

Dream Kudos: 175
My Contributions


first of all the var keyword is valid in c# but can be problematic, I would use
csharp
string readAddrText = Console.ReadLine();

next is the result always the last values, I would thinks so, SerBufa is being set to new ever time some input is received, you need to make that a global variable.

This post has been edited by zakary: 2 Jun, 2008 - 10:41 AM
User is offlineProfile CardPM

Go to the top of the page

djkitt
post 2 Jun, 2008 - 12:24 PM
Post #3


D.I.C Head

**
Joined: 22 May, 2008
Posts: 128



Thanked 13 times
My Contributions


Hey there Dennis,

If comport is a SerialPort object then I think maybe you are trying to specify an address in comport.Read where number of bytes to read is actually specified.

-> comport.Read(Buffer , Offset, Count)

User is offlineProfile CardPM

Go to the top of the page

dennisxu
post 2 Jun, 2008 - 12:32 PM
Post #4


New D.I.C Head

*
Joined: 21 May, 2008
Posts: 3

QUOTE(djkitt @ 2 Jun, 2008 - 01:24 PM) *

Hey there Dennis,

If comport is a SerialPort object then I think maybe you are trying to specify an address in comport.Read where number of bytes to read is actually specified.

-> comport.Read(Buffer , Offset, Count)

User is offlineProfile CardPM

Go to the top of the page

dennisxu
post 2 Jun, 2008 - 12:41 PM
Post #5


New D.I.C Head

*
Joined: 21 May, 2008
Posts: 3

Yes, zakary and djkitt, you're right.

my purpose is to read from a specific address in the target.
however, I always get the same results(99) when I varied the address I want to read. This's not what I expected.

Thanks,
Dennis
User is offlineProfile CardPM

Go to the top of the page

djkitt
post 2 Jun, 2008 - 12:49 PM
Post #6


D.I.C Head

**
Joined: 22 May, 2008
Posts: 128



Thanked 13 times
My Contributions


QUOTE(dennisxu @ 2 Jun, 2008 - 03:41 PM) *

Yes, zakary and djkitt, you're right.

my purpose is to read from a specific address in the target.
however, I always get the same results(99) when I varied the address I want to read. This's not what I expected.

Thanks,
Dennis


OK.

I have a few questions:

What are you using the variable Addr for?

What type of device are you reading?

What results are you expecting?




User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:32AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month