FTDI RS232 EEPROM Problem writing new Data - c#

I'm trying to edit the EEPROM in the User Area of my FTDI Cable.
Int16 Tokens = BitConverter.ToInt16(EEBuf, 0);
if (TokenToEdit != 0)
{
Tokens += TokenToEdit;
var tokenArray = BitConverter.GetBytes(Tokens);
Array.Copy(tokenArray, EEBuf, 2);
TokenToEdit = 0;
ftStat = myFTDIobject.EEWriteUserArea(EEBuf);
TokenToExpect = Tokens;
System.Threading.Thread.Sleep(5000);
checkCableIdCorrect(myFTDIobject);
}
if (TokenToExpect != Tokens && TokenToExpect != -1)
{
var tokenArray = BitConverter.GetBytes(TokenToExpect);
Array.Copy(tokenArray, EEBuf, 2);
ftStat = myFTDIobject.EEWriteUserArea(EEBuf);
System.Threading.Thread.Sleep(5000);
checkCableIdCorrect(myFTDIobject);
}
else
{
TokenToExpect = -1;
}
It reads out the right data and ftStat indicates that the writing of new data worked. But when reading the EEPROM again, the data is not modified. The code below works fine, but in the same function the code above doesn't work.
eeData.Manufacturer = Manufacturer;
eeData.Description = description;
ftStat = myFTDIobject.WriteFT232REEPROM(eeData);
byte x = 2; //4 Tokens kostenlos wenn Kabel noch nicht für dieses System freigeschalten ist
byte[] EEBuf2 = new byte[EEBytesRead];
var tokenArray = BitConverter.GetBytes(x);
Array.Copy(tokenArray, EEBuf2, 2);
DateTime dt = DateTime.Now;
byte[] MyDateArray = new byte[4];
MyDateArray[0] = (byte)dt.Day;
MyDateArray[1] = (byte)dt.Month;
var yearByte1 = BitConverter.GetBytes(dt.Year + 1);
Array.Copy(yearByte1, 0, MyDateArray, 2, 2); //[0] = Tag; [1] = Monat; [2] = Byte1 von Jahr; [3] = Byte2 von Jahr;
Array.Copy(MyDateArray, 0, EEBuf2 ,2, 4);
ftStat = myFTDIobject.EEWriteUserArea(EEBuf2);
I tried to use the function recursive, so it can update the data until it finally works, but this results in an endless loop.

Related

performing DFU OTA using mcumgr in xamarin forms

I am very new to firmware updates and the whole zephyr rtos. I am trying to perform a firmware update using the mcumgr. I ran into some problems. I read the documentation, but it's not very well written and even the autor phrased that it isn't even complete. So the problem is that i split up my bin file into packets and add the header (i think this is working), but when the transfer completes, the firmware isn't uploaded to any slot on the device. I am learning and implementing this code from boogie https://github.com/boogie/mcumgr-web
This is my code. The DfuSend method runs on a button click.
public async void DfuSend()
{
if (SelectedFirmwareFile == null)
{
await Application.Current.MainPage.DisplayAlert("Error", "No file selected", "OK");
return;
}
var service = await Globals.ConnectedDevice.GetServiceAsync(GattIdentifiers.UartGattSMPServiceId);
if (service != null)
{
try
{
sendCharacteristic = await service.GetCharacteristicAsync(GattIdentifiers.UartGattSMPCharacteristicsId);
receiveCharacteristic = await service.GetCharacteristicAsync(GattIdentifiers.UartGattSMPCharacteristicsId);
uploadIsInProgress = true;
sendCharacteristic.WriteType = CharacteristicWriteType.WithoutResponse;
FirmwareFileBytes = File.ReadAllBytes(SelectedFirmwareFile.FullName);
while (uploadIsInProgress == true)
{
await _uploadNext();
HandleUploadProgress(_uploadOffset);
}
sendCharacteristic.WriteType = CharacteristicWriteType.WithResponse;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
else
{
await Application.Current.MainPage.DisplayAlert("Error", "No service found", "OK");
}
The header bytes definition:
byte MGMT_OP_READ = 0;
byte MGMT_OP_READ_RSP = 1;
byte MGMT_OP_WRITE = 2;
byte MGMT_OP_WRITE_RSP = 3;
// Groups
byte MGMT_GROUP_ID_OS = 0;
byte MGMT_GROUP_ID_IMAGE = 1;
byte MGMT_GROUP_ID_STAT = 2;
byte MGMT_GROUP_ID_CONFIG = 3;
byte MGMT_GROUP_ID_LOG = 4;
byte MGMT_GROUP_ID_CRASH = 5;
byte MGMT_GROUP_ID_SPLIT = 6;
byte MGMT_GROUP_ID_RUN = 7;
byte MGMT_GROUP_ID_FS = 8;
byte MGMT_GROUP_ID_SHELL = 9;
// OS group
byte OS_MGMT_ID_ECHO = 0;
byte OS_MGMT_ID_CONS_ECHO_CTRL = 1;
byte OS_MGMT_ID_TASKSTAT = 2;
byte OS_MGMT_ID_MPSTAT = 3;
byte OS_MGMT_ID_DATETIME_STR = 4;
byte OS_MGMT_ID_RESET = 5;
// Image group
byte IMG_MGMT_ID_STATE = 0;
byte IMG_MGMT_ID_UPLOAD = 1;
byte IMG_MGMT_ID_FILE = 2;
byte IMG_MGMT_ID_CORELIST = 3;
byte IMG_MGMT_ID_CORELOAD = 4;
byte IMG_MGMT_ID_ERASE = 5;
private int _seq = 0;
and the uploadnext method:
private async Task _uploadNext()
{
if (_uploadOffset >= SelectedFirmwareFile.Length)
{
uploadIsInProgress = false;
HandleUploadFinished();
return;
}
if (uploadIsInProgress == false)
return;
const int nmpOverhead = 8;
var message = new ImageData();
if (_uploadOffset == 0)
{
message.len = Convert.ToInt32(SelectedFirmwareFile.Length);
using (var sha = SHA256.Create())
{
message.sha = sha.ComputeHash(await File.ReadAllBytesAsync(SelectedFirmwareFile.FullName));
}
}
//the encode function that i am trying to implement
var length = _mtu - Encode(message).Length - nmpOverhead;
message.data = new byte[SelectedFirmwareFile.Length - _uploadOffset > length ? length : SelectedFirmwareFile.Length - _uploadOffset];
Array.Copy(await File.ReadAllBytesAsync(SelectedFirmwareFile.FullName), _uploadOffset, message.data, 0, message.data.Length);
_uploadOffset += message.data.Length;
await SendMessage(MGMT_OP_WRITE, MGMT_GROUP_ID_IMAGE, IMG_MGMT_ID_UPLOAD, message);
}
And then i need to send the packet and encode it using CBOR. There is my code for the SendMessage method (i am really sure that it doesn't work)
async Task SendMessage(byte op, byte group, byte id, ImageData data)
{
var cborMessage = CBORObject.FromObject(new { op, group, id, seq = _seq++, data });
await sendCharacteristic.WriteAsync(cborMessage.EncodeToBytes());
}
BTW the characteristics use the SMP id.
I'll appreciate every comment or an idea. Thank you and have a nice day

Modbus TCP Communication C#

I am looking for a little help.
I have a program to communicate with a controller through Modbus TCP.
The only problem is I cannot extend the Nop from 125 to 400 because I got Illegal Data Address error message.
Could you please help me with this?
try
{
byte slaveid = 1;
byte function = 4;
ushort id = function;
ushort startAddress = 0;
uint NoP = 125;
byte[] frame = ReadInputRegistersMsg(id, slaveid, startAddress, function, NoP);
this.Write(frame); //data send to controller
Thread.Sleep(100);
byte[] buffReceiver = this.Read(); //data recieving from controller
int SizeByte = buffReceiver[8]; // Data what I got from the controller
UInt16[] temp = null;
if (function != buffReceiver[7])
{
byte[] byteMsg = new byte[9];
Array.Copy(buffReceiver, 0, byteMsg, 0, byteMsg.Length);
byte[] data = new byte[SizeByte];
textBox2.Text = Display(byteMsg);
byte[] errorbytes = new byte[3];
Array.Copy(buffReceiver, 6, errorbytes, 0, errorbytes.Length);
this.CheckValidate(errorbytes); // check the answer -> error message
}
else
{
byte[] byteMsg = new byte[9 + SizeByte];
Array.Copy(buffReceiver, 0, byteMsg, 0, byteMsg.Length);
byte[] data = new byte[SizeByte];
textBox2.Text = Display(byteMsg); // Show received messages in windows form app
Array.Copy(buffReceiver, 9, data, 0, data.Length);
temp = Word.ConvertByteArrayToWordArray(data); // Convert Byte[]-> Word[]
}
// Result
if (temp == null) return;
string result = string.Empty;
//foreach (var item in temp) // show all the data
for(int i=0;i<100;i++) // show the first 100 data
{
//result += string.Format("{0} ", item);
result += temp[i];
}
textBox3.Text = result; // insert the result into the textbox (windows form app)
}
catch
{
}
The ReadInputRegister Message is the following:
private byte[] ReadInputRegistersMsg(ushort id, byte slaveAddress, ushort startAddress, byte function, uint NoP)
{
byte[] frame = new byte[12];
frame[0] = (byte)(id >> 8); // Transaction Identifier High
frame[1] = (byte)id; // Transaction Identifier Low
frame[2] = 0; // Protocol Identifier High
frame[3] = 0; // Protocol Identifier Low
frame[4] = 0; // Message Length High
frame[5] = 6; // Message Length Low(6 bytes to follow)
frame[6] = slaveAddress; // Slave address(Unit Identifier)
frame[7] = function; // Function
frame[8] = (byte)(startAddress >> 8); // Starting Address High
frame[9] = (byte)startAddress; // Starting Address Low
frame[10] = (byte)(NoP >> 8); // Quantity of Registers High
frame[11] = (byte)NoP; // Quantity of Registers Low
return frame;
}
The hard limit for modbus is 125 NoP

Read mobile phone book using obex in c#

i have a OBEXConnect and OBEXRequest custom functions, i am not using library for it
OBEXConnect function is as following
private bool OBEXConnect()
{
//send client request
byte[] ConnectPacket = new byte[7];
ConnectPacket[0] = 0x80; // Connect
ConnectPacket[1] = 0x00; // Packetlength Hi Byte
ConnectPacket[2] = 0x07; // Packetlength Lo Byte
ConnectPacket[3] = 0x10; // Obex v1
ConnectPacket[4] = 0x00; // no flags
ConnectPacket[5] = 0x20; // 8k max packet size Hi Byte
ConnectPacket[6] = 0x00; // 8k max packet size Lo Byte
stream.Write(ConnectPacket,0,ConnectPacket.Length);
//listen for server response
byte[] ReceiveBufferA = new byte[3];
stream.Read(ReceiveBufferA,0,3);
if (ReceiveBufferA[0] == 160) // 0xa0
{
//success, decode rest of packet
int plength = (0xff * ReceiveBufferA[1]) + ReceiveBufferA[2]; //length of packet is...
//listen for rest of packet
byte[] ReceiveBufferB = new byte[plength-3];
stream.Read(ReceiveBufferB,0,plength-3);
int obver = ReceiveBufferB[0]; //server obex version (16 = v1.0)
int cflags = ReceiveBufferB[1]; //connect flags
int maxpack = (0xff * ReceiveBufferB[2]) + ReceiveBufferB[3]; //max packet size
return true;
}
else
{
return false;
}
}
and here is OBEXRequest function
private int OBEXRequest(string tReqType, string tName, string tType, string tFileContent)
{
//send client request
int i;
int offset;
int packetsize;
byte reqtype = 0x82;
int tTypeLen = 0x03;
int typeheadsize;
int typesizeHi = 0x00;
int typesizeLo = 0x03;
//tName = "contact.vcf";
//tType = "text/x-vCard";
//tFileContent = "BEGIN:VCARD\r\nVERSION:2.1\r\nN:;aardvark\r\nFN:aardvark\r\nEND:VCARD\r\n";
if (tReqType == "GET")
{
reqtype = 0x83; // 131 GET-Final
}
if (tReqType == "PUT")
{
reqtype = 0x82; // 130 PUT-Final
}
packetsize = 3;
//Name Header
int tNameLength = tName.Length;
int nameheadsize = (3 + (tNameLength*2) + 2);
int namesizeHi = (nameheadsize & 0xff00)/0xff;
int namesizeLo = nameheadsize & 0x00ff;
packetsize = packetsize + nameheadsize;
if (tType != "")
{
//Type Header
tTypeLen = tType.Length;
typeheadsize = 3 + tTypeLen + 1;
typesizeHi = (typeheadsize & 0xff00)/0xff;
typesizeLo = typeheadsize & 0x00ff;
packetsize = packetsize + typeheadsize;
}
//Body
int fileLen = tFileContent.Length;
int fileheadsize = 3 + fileLen ;
int filesizeHi = (fileheadsize & 0xff00)/0xff;;
int filesizeLo = fileheadsize & 0x00ff;;
packetsize = packetsize + fileheadsize;
int packetsizeHi = (packetsize & 0xff00)/0xff;
int packetsizeLo = packetsize & 0x00ff;
byte[] tSendByte = new byte[packetsize];
//PUT-final Header
tSendByte[0] = reqtype; // Request type e.g. PUT-final 130
tSendByte[1] = Convert.ToByte(packetsizeHi); // Packetlength Hi
tSendByte[2] = Convert.ToByte(packetsizeLo); // Packetlength Lo
offset = 2;
//Name Header
tSendByte[offset+1] = 0x01; // HI for Name header
tSendByte[offset+2] = Convert.ToByte(namesizeHi); // Length of Name header (2 bytes per char)
tSendByte[offset+3] = Convert.ToByte(namesizeLo); // Length of Name header (2 bytes per char)
// Name+\n\n in unicode
byte[] tNameU = System.Text.Encoding.BigEndianUnicode.GetBytes(tName);
tNameU.CopyTo(tSendByte,offset+4);
offset = offset + 3 + (tNameLength*2);
tSendByte[offset+1] = 0x00; // null term
tSendByte[offset+2] = 0x00; // null term
offset = offset + 2;
if (tType != "")
{
//Type Header
tSendByte[offset+1] = 0x42; // HI for Type Header 66
tSendByte[offset+2] = Convert.ToByte(typesizeHi); // Length of Type Header
tSendByte[offset+3] = Convert.ToByte(typesizeLo); // Length of Type Header
for (i=0;i<=(tTypeLen-1);i++)
{
tSendByte[offset+4+i] = Convert.ToByte(Convert.ToChar(tType.Substring(i,1)));
}
tSendByte[offset+3+tTypeLen+1] = 0x00; // null terminator
offset = offset+3+tTypeLen+1;
}
//Body
tSendByte[offset+1] = 0x49; //HI End of Body 73
tSendByte[offset+2] = Convert.ToByte(filesizeHi); //
tSendByte[offset+3] = Convert.ToByte(filesizeLo); //1k payload + 3 for HI header
for (i=0;i<=(fileLen-1);i++)
{
tSendByte[offset+4+i] = Convert.ToByte(Convert.ToChar(tFileContent.Substring(i,1)));
}
//tSendByte[offset+4+fileLen] = 0x00; // null terminator
offset = offset+3+fileLen;
stream.Write(tSendByte,0,tSendByte.Length );
//listen for server response
//TODO: can hang here forever waiting response...
bool x = stream.DataAvailable; // changed bluetoothclient - public NetworkStream GetStream()
byte[] tArray4 = new byte[3];
stream.Read(tArray4,0,3);
x = stream.DataAvailable;
if (tArray4[0] == 160) // 0xa0
{
int plength = (tArray4[1] * 256) + tArray4[2] - 3;
byte[] tArray5 = new byte[plength];
if (plength >0)
{
stream.Read(tArray5,0,plength);
//TODO: data in returned packet to deal with
}
return 160;
}
if (tArray4[0] == 197) // 0xc5 Method not allowed
{
return 197;
}
if (tArray4[0] == 192) // 0xc0 Bad Request
{
return 192;
}
return 0;
}
i want to read the phone book of mobile but i not succeeded yet
i try on one samsung mobile it gives error bad request i follow the solutions above mentioned but when name = null then obexrequest function gives error
when i try it on huawei mobile it gives unknown error
i try this code as well it throw error that this method is not implemented
Read contacts using obex in c#
i call these functions like this
string tName = "";
string tType = "text/x-vCard";
string tFileContent = "";
int result = OBEXRequest("GET",tName,tType,tFileContent);
switch (result)
{
case 160: // 0xa0
addtolog("OK");
break;
case 197: // 0xc5
addtolog("Method not allowed");
break;
case 192: // 0xc0
addtolog("Bad Request");
break;
default:
addtolog("Other Error");
break;
}
can any body point out my mistake

playing sound with SlimDX and DirectSound (C#)

(apologies if this is a duplicate ... i posted but saw no evidence that it actually made it to the forum)
I've been trying to get SlimDX DirectSound working. Here's the code I have. It fills the secondary buffer from a wav file and then, in a thread loop, alternately fills the lower or upper halves of the buffer.
It plays the first load of the buffer fine. The AutoResetEvents fire when they should and the lower half then upper half of the buffer are populated (verified with Debug statements). But playing does not continue after the first load of the buffer. So somehow the repopulation of the buffer doesn't work as it should.
Ideas?
(I'm using DirectSound because it's the only way I've found to set the guid of the audio device that I want to use. Am open to other .NET-friendly approaches.)
private void PlaySound(Guid soundCardGuid, string audioFile) {
DirectSound ds = new DirectSound(soundCardGuid);
ds.SetCooperativeLevel(this.Handle, CooperativeLevel.Priority);
WaveFormat format = new WaveFormat();
format.BitsPerSample = 16;
format.BlockAlignment = 4;
format.Channels = 2;
format.FormatTag = WaveFormatTag.Pcm;
format.SamplesPerSecond = 44100;
format.AverageBytesPerSecond = format.SamplesPerSecond * format.BlockAlignment;
SoundBufferDescription desc = new SoundBufferDescription();
desc.Format = format;
desc.Flags = BufferFlags.GlobalFocus;
desc.SizeInBytes = 8 * format.AverageBytesPerSecond;
PrimarySoundBuffer pBuffer = new PrimarySoundBuffer(ds, desc);
SoundBufferDescription desc2 = new SoundBufferDescription();
desc2.Format = format;
desc2.Flags = BufferFlags.GlobalFocus | BufferFlags.ControlPositionNotify | BufferFlags.GetCurrentPosition2;
desc2.SizeInBytes = 8 * format.AverageBytesPerSecond;
SecondarySoundBuffer sBuffer1 = new SecondarySoundBuffer(ds, desc2);
NotificationPosition[] notifications = new NotificationPosition[2];
notifications[0].Offset = desc2.SizeInBytes / 2 + 1;
notifications[1].Offset = desc2.SizeInBytes - 1; ;
notifications[0].Event = new AutoResetEvent(false);
notifications[1].Event = new AutoResetEvent(false);
sBuffer1.SetNotificationPositions(notifications);
byte[] bytes1 = new byte[desc2.SizeInBytes / 2];
byte[] bytes2 = new byte[desc2.SizeInBytes];
Stream stream = File.Open(audioFile, FileMode.Open);
Thread fillBuffer = new Thread(() => {
int readNumber = 1;
int bytesRead;
bytesRead = stream.Read(bytes2, 0, desc2.SizeInBytes);
sBuffer1.Write<byte>(bytes2, 0, LockFlags.None);
sBuffer1.Play(0, PlayFlags.None);
while (true) {
if (bytesRead == 0) { break; }
notifications[0].Event.WaitOne();
bytesRead = stream.Read(bytes1, 0, bytes1.Length);
sBuffer1.Write<byte>(bytes1, 0, LockFlags.None);
if (bytesRead == 0) { break; }
notifications[1].Event.WaitOne();
bytesRead = stream.Read(bytes1, 0, bytes1.Length);
sBuffer1.Write<byte>(bytes1, desc2.SizeInBytes / 2, LockFlags.None);
}
stream.Close();
stream.Dispose();
});
fillBuffer.Start();
}
}
You haven't set it to loop on the play buffer. Change your code to:
sBuffer1.Play(0, PlayFlags.Looping);

Modbus and C# -- problem in reading response

I am a newbie to modbus and need some help. I am trying to connect using modbus and serial communication. so far i managed to send data but i am unable to get any. the following is my code.
Building packet
private byte[] BuildPacket(int meter_address,int function,int table_name,int table_offset,int high_byte, int low_byte)
{
try
{
byte[] packet = new byte[6];
packet[0] = Convert.ToByte(meter_address);
packet[1] = Convert.ToByte(function);
packet[2] = Convert.ToByte(table_name);
packet[3] = Convert.ToByte(table_offset);
packet[4] = Convert.ToByte(high_byte);
packet[5] = Convert.ToByte(low_byte);
byte[] checksum = DoCheckSum(packet);
byte[] sendPacket = new byte[8];
sendPacket[0] = packet[0];
sendPacket[1] = packet[1];
sendPacket[2] = packet[2];
sendPacket[3] = packet[3];
sendPacket[4] = packet[4];
sendPacket[5] = packet[5];
sendPacket[6] = checksum[0];
sendPacket[7] = checksum[1];
return sendPacket;
}
catch (Exception)
{
throw;
}
}
Checksum for modbus
try
{
ushort CRCFull = 0xFFFF;
byte CRCHigh = 0xFF, CRCLow = 0xFF;
char CRCLSB;
for (int i = 0; i < (packet.Length); i++)
{
CRCFull = (ushort)(CRCFull ^ packet[i]);
for (int j = 0; j < 8; j++)
{
CRCLSB = (char)(CRCFull & 0x0001);
CRCFull = (ushort)((CRCFull >> 1) & 0x7FFF);
if (CRCLSB == 1)
CRCFull = (ushort)(CRCFull ^ 0xA001);
}
}
byte[] crcByte = new byte[2];
crcByte[1] = CRCHigh = (byte)((CRCFull >> 8) & 0xFF);
crcByte[0] = CRCLow = (byte)(CRCFull & 0xFF);
return crcByte;
}
catch (Exception ex)
{
throw ex;
}
}
Connection through serial and modbus
public void ConnectSerialModBus(string COM, int baud)
{
SerialPort port = new SerialPort(COM, baud, Parity.None, 8, StopBits.One);
if (!(port.IsOpen))
{
byte[] sendPacket = BuildPacket(3, 4, 11, 0, 1, 200);
port.Open();
port.RtsEnable = false;
port.Handshake = Handshake.None;
//SEND PACKET TO DEVICE
port.Write(sendPacket, 0, sendPacket.Length);
#region RECEIVE DATA FROM SERIAL
//MAKE PROCESS STOP FOR 5sec
Thread.Sleep(3000);
port.DiscardOutBuffer();
port.DiscardInBuffer();
port.RtsEnable = true;
int size = port.ReadBufferSize;
byte[] readingbyte = new byte[size];
port.Read(readingbyte, 0, readingbyte.Length);
string reading = Encoding.GetEncoding("Windows-1252").GetString(readingbyte);
port.Close();
port.Dispose();
#endregion
}
}
The problem is when it comes to reading the response, the program gets stuck. if possible please help me out figure what is wrong with it.
found a solution to the problem, the problem was with the thread.sleep. was giving it 3secs which is too much for the rtf to receive the packet. changed to 10ms and worked fine.

Categories

Resources