So the thing i want to do is send an image as of right now a jpg to through pipeline that uses spi aswell, in the console it says that everything is connected but when i look at the windows form i see no image yet it says that everything is connected. I feel like a big problem is on the program.cs file (the last part), there is more code so pls inform me if anything feels unclear.
public class PipeServer
{
public static async Task RunServerAsync(string imagePath)
{
try
{
// The server is created to send data, hence PipeDirection.
using (var server = new NamedPipeServerStream("datapipe", PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
{
//string imagepath2 = @"C:Usersu096849Desktopbildermc1.jpg";
//imagePath = imagepath2;
Console.WriteLine("Server waiting for connection...");
await server.WaitForConnectionAsync();
Console.WriteLine("Server connected to client.");
if (File.Exists(imagePath))
{
using (Bitmap image = new Bitmap(imagePath))
{
Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
BitmapData bmpData = image.LockBits(rect,ImageLockMode.ReadOnly, image.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = Math.Abs(bmpData.Stride) * image.Height;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
image.UnlockBits(bmpData);
int scaledWidth2 = (int)(image.Width * 1.0f);
int scaledHeight2 = (int)(image.Height * 1.0f);
if (scaledWidth2 <= 0) scaledWidth2 = 1;
if (scaledHeight2 <= 0) scaledHeight2 = 1;
Bitmap scaledImage = new Bitmap(scaledWidth2, scaledHeight2);
using (Graphics gfx = Graphics.FromImage(scaledImage))
{
gfx.DrawImage(image, new Rectangle(0, 0, scaledWidth2, scaledHeight2));
}
await server.WriteAsync(rgbValues, 0, rgbValues.Length);
Console.WriteLine("Image sent successfully");
}
}
else
{
Console.WriteLine("Error: Specified image path does not exist");
}
}
}
catch(Exception ex)
{
Console.WriteLine($"An error occurred in the client: {ex.Message}");
}
}
}
public class MockSpiDevice : ISpiDevice
{
private NamedPipeClientStream clientStream;
public void Open()
{
if (clientStream == null || !clientStream.IsConnected)
{
clientStream = new NamedPipeClientStream(".", "datapipe", PipeDirection.In);
clientStream.Connect();
Console.WriteLine("SPI connected");
}
else
{
Console.WriteLine("SPI already connected.");
}
}
public void Close()
{
if (clientStream != null)
{
clientStream.Close();
clientStream = null;
Console.WriteLine("SPI disconnected");
}
}
public int Read(byte[] buffer, int offset, int count)
{
if (clientStream != null && clientStream.CanRead)
{
return clientStream.Read(buffer, offset, count);
}
else
{
throw new InvalidOperationException("Attempt to read from an unconnected or closed stream.");
}
}
public int BytesToRead
{
get
{
return (clientStream != null && clientStream.IsConnected) ? clientStream.InBufferSize : 0;
}
}
}
public partial class DisplayForm : Form
{
bool bigDisplay = false;
private float scaleFactor = 1.0f;
private Bitmap originalImage;
//private SerialPort spiPort = new SerialPort("");
private ISpiDevice spiDevice;
private PictureBox pictureBox;
private Timer updateTimer;
public PixelFormat Desktop { get; private set; }
private enum Buttons
{
A = 0b01000001,
B = 0b01000100,
Q = 0b01010001
}
public DisplayForm(ISpiDevice spiDevice)
{
InitializeComponent();
this.Text = "Display Emulator : " + Application.ProductVersion;
//spiDevice = device;
pictureBox = new PictureBox()
{
Dock = DockStyle.Fill,
BorderStyle = BorderStyle.Fixed3D
};
this.Controls.Add(pictureBox);
updateTimer = new Timer()
{
Interval = 1000
};
updateTimer.Tick += UpdateTimer_Tick;
updateTimer.Start();
//this.window.Paint += new PaintEventHandler(Canvas_Paint);
/*
spiPort.BaudRate = 115200;
spiPort.DataBits = 8;
spiPort.Parity = Parity.None;
spiPort.StopBits = StopBits.One;
*/
this.spiDevice = spiDevice;
SetupFromComponets();
//spiPort.Open();
zoomBar.Visible = false;
}
private async Task<Bitmap> ReadImageFromSPIAsync(ISpiDevice spiDevice)
{
try
{
if (spiDevice.BytesToRead > 0)
{
byte[] buffer = new byte[spiDevice.BytesToRead];
int bytesRead = spiDevice.Read(buffer, 0, buffer.Length);
using (MemoryStream ms = new MemoryStream(buffer, 0, bytesRead))
{
return new Bitmap(ms);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error reading from SPI: " + ex.Message);
}
return null;
}
private void SpiPort_DataRecevide(object sender, DataReceivedEventArgs e)
{
// Handle incoming SPI data here
// For example, read the incoming data and convert it to an image
// Note: This will likely involve more complex handling including knowing the image format
// Be sure to invoke this action on the UI thread
this.Invoke((MethodInvoker)delegate
{
UpdateImageFromSPI();
});
}
private void UpdateImageFromSPI()
{
byte[] imageData = ReadDataFromSPI();
originalImage = CovertToBitmap(imageData);
window.Invalidate();
}
private byte[] ReadDataFromSPI()
{
byte[] buffer = new byte[spiDevice.BytesToRead];
spiDevice.Read(buffer, 0, buffer.Length);
return buffer;
}
private Bitmap CovertToBitmap(byte[] imageData)
{
using (MemoryStream ms = new MemoryStream(imageData))
{
return new Bitmap(ms);
}
}
private async void UpdateTimer_Tick(object sender, EventArgs e)
{
try
{
if (spiDevice != null && spiDevice.BytesToRead > 0)
{
var buffer = new byte[spiDevice.BytesToRead];
int bytesRead = spiDevice.Read(buffer, 0, buffer.Length);
if (bytesRead > 0)
{
using (MemoryStream ms = new MemoryStream(buffer))
{
Bitmap newImage = new Bitmap(ms);
pictureBox.Image?.Dispose();
pictureBox.Image = newImage;
pictureBox.Refresh();
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Failed to update image from SPI: " + ex.Message);
}
}
}
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ISpiDevice spiDevice = new MockSpiDevice();
string imagePath = (@"C:Usersu096849Desktopbildermc1.jpg");
var serverTask = PipeServer.RunServerAsync(imagePath);
spiDevice.Open();
Application.Run(new DisplayForm(spiDevice));
}
static async Task PipeSetup(ISpiDevice spiDevice)
{
//MockSpiDevice mockSpiDevice = new MockSpiDevice();
//mockSpiDevice.Open();
//await Task.WhenAll(serverTask);
}
}